mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 12:24:24 +00:00
Move findElementsByName() into shared analyzer/element_search.dart file.
R=brianwilkerson@google.com BUG= Review-Url: https://codereview.chromium.org/2965133003 .
This commit is contained in:
parent
ba625804b7
commit
19265a82b4
5 changed files with 36 additions and 76 deletions
|
@ -12,6 +12,7 @@ import 'package:analyzer/src/dart/ast/utilities.dart' as engine;
|
|||
import 'package:analyzer/src/dart/element/element.dart' as engine;
|
||||
import 'package:analyzer/src/error/codes.dart' as engine;
|
||||
import 'package:analyzer/src/generated/source.dart' as engine;
|
||||
import 'package:analyzer/src/generated/testing/search.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
@ -25,16 +26,6 @@ main() {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the [unit] for the [engine.Element]s with the given [name].
|
||||
*/
|
||||
List<engine.Element> findElementsByName(
|
||||
engine.CompilationUnit unit, String name) {
|
||||
var finder = new _ElementsByNameFinder(name);
|
||||
unit.accept(finder);
|
||||
return finder.elements;
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class ElementKindTest {
|
||||
void test_fromEngine() {
|
||||
|
@ -488,17 +479,3 @@ class A {
|
|||
expect(element.flags, 0);
|
||||
}
|
||||
}
|
||||
|
||||
class _ElementsByNameFinder extends engine.RecursiveAstVisitor<Null> {
|
||||
final String name;
|
||||
final List<engine.Element> elements = [];
|
||||
|
||||
_ElementsByNameFinder(this.name);
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(engine.SimpleIdentifier node) {
|
||||
if (node.name == name && node.inDeclarationContext()) {
|
||||
elements.add(node.staticElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
pkg/analyzer/lib/src/generated/testing/element_search.dart
Normal file
30
pkg/analyzer/lib/src/generated/testing/element_search.dart
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/visitor.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
|
||||
/**
|
||||
* Search the [unit] for the [Element]s with the given [name].
|
||||
*/
|
||||
List<Element> findElementsByName(CompilationUnit unit, String name) {
|
||||
var finder = new _ElementsByNameFinder(name);
|
||||
unit.accept(finder);
|
||||
return finder.elements;
|
||||
}
|
||||
|
||||
class _ElementsByNameFinder extends RecursiveAstVisitor<Null> {
|
||||
final String name;
|
||||
final List<Element> elements = [];
|
||||
|
||||
_ElementsByNameFinder(this.name);
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
if (node.name == name && node.inDeclarationContext()) {
|
||||
elements.add(node.staticElement);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,10 +11,10 @@ import 'package:analyzer/src/dart/analysis/driver.dart';
|
|||
import 'package:analyzer/src/dart/analysis/search.dart';
|
||||
import 'package:analyzer/src/dart/ast/utilities.dart';
|
||||
import 'package:analyzer/src/dart/element/member.dart';
|
||||
import 'package:analyzer/src/generated/testing/element_search.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../../../utils.dart';
|
||||
import 'base.dart';
|
||||
|
||||
main() {
|
||||
|
|
|
@ -6,31 +6,21 @@ library analyzer.test.utils;
|
|||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
|
||||
import 'package:analyzer/dart/ast/visitor.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/src/generated/resolver.dart' show TypeProvider;
|
||||
import 'package:analyzer/src/generated/testing/element_search.dart';
|
||||
import 'package:front_end/src/base/source.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
/**
|
||||
* Search the [unit] for the [Element]s with the given [name].
|
||||
*/
|
||||
List<Element> findElementsByName(CompilationUnit unit, String name) {
|
||||
var finder = new _ElementsByNameFinder(name);
|
||||
unit.accept(finder);
|
||||
return finder.elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the [unit] for the [LocalVariableElement] with the given [name].
|
||||
* Fail if there is not exactly one such variable.
|
||||
*/
|
||||
LocalVariableElement findLocalVariable(CompilationUnit unit, String name) {
|
||||
var finder = new _ElementsByNameFinder(name);
|
||||
unit.accept(finder);
|
||||
List<Element> elements = findElementsByName(unit, name);
|
||||
List<Element> localVariables =
|
||||
finder.elements.where((e) => e is LocalVariableElement).toList();
|
||||
elements.where((e) => e is LocalVariableElement).toList();
|
||||
expect(localVariables, hasLength(1));
|
||||
return localVariables[0];
|
||||
}
|
||||
|
@ -333,17 +323,3 @@ class TypeAssertions {
|
|||
expect(t, expected);
|
||||
};
|
||||
}
|
||||
|
||||
class _ElementsByNameFinder extends RecursiveAstVisitor<Null> {
|
||||
final String name;
|
||||
final List<Element> elements = [];
|
||||
|
||||
_ElementsByNameFinder(this.name);
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
if (node.name == name && node.inDeclarationContext()) {
|
||||
elements.add(node.staticElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import 'dart:async';
|
|||
|
||||
import 'package:analyzer/dart/analysis/session.dart';
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/visitor.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/visitor.dart';
|
||||
import 'package:analyzer/exception/exception.dart';
|
||||
|
@ -19,6 +18,7 @@ import 'package:analyzer/src/generated/engine.dart';
|
|||
import 'package:analyzer/src/generated/engine.dart' as engine;
|
||||
import 'package:analyzer/src/generated/sdk.dart';
|
||||
import 'package:analyzer/src/generated/source_io.dart';
|
||||
import 'package:analyzer/src/generated/testing/element_search.dart';
|
||||
import 'package:front_end/src/base/performace_logger.dart';
|
||||
import 'package:front_end/src/incremental/byte_store.dart';
|
||||
|
||||
|
@ -41,15 +41,6 @@ Element findChildElement(Element root, String name, [ElementKind kind]) {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the [unit] for the [Element]s with the given [name].
|
||||
*/
|
||||
List<Element> findElementsByName(CompilationUnit unit, String name) {
|
||||
var finder = new _ElementsByNameFinder(name);
|
||||
unit.accept(finder);
|
||||
return finder.elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* A function to be called for every [Element].
|
||||
*/
|
||||
|
@ -189,20 +180,6 @@ class PrintLogger implements Logger {
|
|||
}
|
||||
}
|
||||
|
||||
class _ElementsByNameFinder extends RecursiveAstVisitor<Null> {
|
||||
final String name;
|
||||
final List<Element> elements = [];
|
||||
|
||||
_ElementsByNameFinder(this.name);
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
if (node.name == name && node.inDeclarationContext()) {
|
||||
elements.add(node.staticElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the given [_ElementVisitorFunction] into an instance of
|
||||
* [engine.GeneralizingElementVisitor].
|
||||
|
|
Loading…
Reference in a new issue