OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; |
8 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
9 import 'package:analyzer/error/error.dart'; | 10 import 'package:analyzer/error/error.dart'; |
10 import 'package:analyzer/src/dart/analysis/driver.dart'; | 11 import 'package:analyzer/src/dart/analysis/driver.dart'; |
11 import 'package:analyzer/src/dart/ast/utilities.dart'; | 12 import 'package:analyzer/src/dart/ast/utilities.dart'; |
12 import 'package:analyzer/src/dart/error/hint_codes.dart'; | 13 import 'package:analyzer/src/dart/error/hint_codes.dart'; |
13 import 'package:analyzer/src/generated/java_engine.dart'; | 14 import 'package:analyzer/src/generated/java_engine.dart'; |
14 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
15 import 'package:test/test.dart'; | 16 import 'package:test/test.dart'; |
16 | 17 |
17 import 'abstract_context.dart'; | 18 import 'abstract_context.dart'; |
(...skipping 21 matching lines...) Expand all Loading... |
39 return findOffset(search) + search.length; | 40 return findOffset(search) + search.length; |
40 } | 41 } |
41 | 42 |
42 /** | 43 /** |
43 * Returns the [SimpleIdentifier] at the given search pattern. | 44 * Returns the [SimpleIdentifier] at the given search pattern. |
44 */ | 45 */ |
45 SimpleIdentifier findIdentifier(String search) { | 46 SimpleIdentifier findIdentifier(String search) { |
46 return findNodeAtString(search, (node) => node is SimpleIdentifier); | 47 return findNodeAtString(search, (node) => node is SimpleIdentifier); |
47 } | 48 } |
48 | 49 |
| 50 /** |
| 51 * Search the [testUnit] for the [LocalVariableElement] with the given [name]. |
| 52 * Fail if there is not exactly one such variable. |
| 53 */ |
| 54 LocalVariableElement findLocalVariable(String name) { |
| 55 var finder = new _ElementsByNameFinder(name); |
| 56 testUnit.accept(finder); |
| 57 List<Element> localVariables = |
| 58 finder.elements.where((e) => e is LocalVariableElement).toList(); |
| 59 expect(localVariables, hasLength(1)); |
| 60 return localVariables[0]; |
| 61 } |
| 62 |
49 AstNode findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { | 63 AstNode findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { |
50 AstNode result = new NodeLocator(offset).searchWithin(testUnit); | 64 AstNode result = new NodeLocator(offset).searchWithin(testUnit); |
51 if (result != null && predicate != null) { | 65 if (result != null && predicate != null) { |
52 result = result.getAncestor(predicate); | 66 result = result.getAncestor(predicate); |
53 } | 67 } |
54 return result; | 68 return result; |
55 } | 69 } |
56 | 70 |
57 AstNode findNodeAtString(String search, [Predicate<AstNode> predicate]) { | 71 AstNode findNodeAtString(String search, [Predicate<AstNode> predicate]) { |
58 int offset = findOffset(search); | 72 int offset = findOffset(search); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 error.errorCode != HintCode.UNUSED_ELEMENT && | 121 error.errorCode != HintCode.UNUSED_ELEMENT && |
108 error.errorCode != HintCode.UNUSED_FIELD && | 122 error.errorCode != HintCode.UNUSED_FIELD && |
109 error.errorCode != HintCode.UNUSED_IMPORT && | 123 error.errorCode != HintCode.UNUSED_IMPORT && |
110 error.errorCode != HintCode.UNUSED_LOCAL_VARIABLE; | 124 error.errorCode != HintCode.UNUSED_LOCAL_VARIABLE; |
111 }), isEmpty); | 125 }), isEmpty); |
112 } | 126 } |
113 testUnitElement = testUnit.element; | 127 testUnitElement = testUnit.element; |
114 testLibraryElement = testUnitElement.library; | 128 testLibraryElement = testUnitElement.library; |
115 } | 129 } |
116 } | 130 } |
| 131 |
| 132 class _ElementsByNameFinder extends RecursiveAstVisitor<Null> { |
| 133 final String name; |
| 134 final List<Element> elements = []; |
| 135 |
| 136 _ElementsByNameFinder(this.name); |
| 137 |
| 138 @override |
| 139 visitSimpleIdentifier(SimpleIdentifier node) { |
| 140 if (node.name == name && node.inDeclarationContext()) { |
| 141 elements.add(node.staticElement); |
| 142 } |
| 143 } |
| 144 } |
OLD | NEW |