Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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/analysis/session.dart'; | 7 import 'package:analyzer/dart/analysis/session.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/ast/visitor.dart'; | |
| 9 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/visitor.dart'; | 11 import 'package:analyzer/dart/element/visitor.dart'; |
| 11 import 'package:analyzer/exception/exception.dart'; | 12 import 'package:analyzer/exception/exception.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 13 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/file_system/memory_file_system.dart'; | 14 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 14 import 'package:analyzer/source/package_map_resolver.dart'; | 15 import 'package:analyzer/source/package_map_resolver.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/driver.dart'; | 16 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 16 import 'package:analyzer/src/dart/analysis/file_state.dart'; | 17 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart' as engine; | 19 import 'package:analyzer/src/generated/engine.dart' as engine; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 34 } | 35 } |
| 35 if (kind != null && element.kind != kind) { | 36 if (kind != null && element.kind != kind) { |
| 36 return; | 37 return; |
| 37 } | 38 } |
| 38 result = element; | 39 result = element; |
| 39 })); | 40 })); |
| 40 return result; | 41 return result; |
| 41 } | 42 } |
| 42 | 43 |
| 43 /** | 44 /** |
| 45 * Search the [unit] for the [Element]s with the given [name]. | |
| 46 */ | |
| 47 List<Element> findElementsByName(CompilationUnit unit, String name) { | |
|
Brian Wilkerson
2017/07/06 18:19:18
Can we use the copy in pkg/analyzer/test/utils.dar
| |
| 48 var finder = new _ElementsByNameFinder(name); | |
| 49 unit.accept(finder); | |
| 50 return finder.elements; | |
| 51 } | |
| 52 | |
| 53 /** | |
| 44 * A function to be called for every [Element]. | 54 * A function to be called for every [Element]. |
| 45 */ | 55 */ |
| 46 typedef void _ElementVisitorFunction(Element element); | 56 typedef void _ElementVisitorFunction(Element element); |
| 47 | 57 |
| 48 class AbstractContextTest { | 58 class AbstractContextTest { |
| 49 MemoryResourceProvider provider; | 59 MemoryResourceProvider provider; |
| 50 DartSdk sdk; | 60 DartSdk sdk; |
| 51 Map<String, List<Folder>> packageMap; | 61 Map<String, List<Folder>> packageMap; |
| 52 UriResolver resourceResolver; | 62 UriResolver resourceResolver; |
| 53 | 63 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 path = provider.convertPath(path); | 102 path = provider.convertPath(path); |
| 93 } | 103 } |
| 94 driver.addFile(path); | 104 driver.addFile(path); |
| 95 driver.changeFile(path); | 105 driver.changeFile(path); |
| 96 _fileContentOverlay[path] = content; | 106 _fileContentOverlay[path] = content; |
| 97 return provider.getFile(path).createSource(); | 107 return provider.getFile(path).createSource(); |
| 98 } | 108 } |
| 99 | 109 |
| 100 Element findElementInUnit(CompilationUnit unit, String name, | 110 Element findElementInUnit(CompilationUnit unit, String name, |
| 101 [ElementKind kind]) { | 111 [ElementKind kind]) { |
| 102 return findChildElement(unit.element, name, kind); | 112 return findElementsByName(unit, name) |
| 113 .where((e) => kind == null || e.kind == kind) | |
| 114 .single; | |
| 103 } | 115 } |
| 104 | 116 |
| 105 File newFile(String path, [String content]) => | 117 File newFile(String path, [String content]) => |
| 106 provider.newFile(provider.convertPath(path), content ?? ''); | 118 provider.newFile(provider.convertPath(path), content ?? ''); |
| 107 | 119 |
| 108 Folder newFolder(String path) => | 120 Folder newFolder(String path) => |
| 109 provider.newFolder(provider.convertPath(path)); | 121 provider.newFolder(provider.convertPath(path)); |
| 110 | 122 |
| 111 void processRequiredPlugins() { | 123 void processRequiredPlugins() { |
| 112 AnalysisEngine.instance.processRequiredPlugins(); | 124 AnalysisEngine.instance.processRequiredPlugins(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 | 182 |
| 171 @override | 183 @override |
| 172 void logInformation(String message, [CaughtException exception]) { | 184 void logInformation(String message, [CaughtException exception]) { |
| 173 print(message); | 185 print(message); |
| 174 if (exception != null) { | 186 if (exception != null) { |
| 175 print(exception); | 187 print(exception); |
| 176 } | 188 } |
| 177 } | 189 } |
| 178 } | 190 } |
| 179 | 191 |
| 192 class _ElementsByNameFinder extends RecursiveAstVisitor<Null> { | |
| 193 final String name; | |
| 194 final List<Element> elements = []; | |
| 195 | |
| 196 _ElementsByNameFinder(this.name); | |
| 197 | |
| 198 @override | |
| 199 visitSimpleIdentifier(SimpleIdentifier node) { | |
| 200 if (node.name == name && node.inDeclarationContext()) { | |
| 201 elements.add(node.staticElement); | |
| 202 } | |
| 203 } | |
| 204 } | |
| 205 | |
| 180 /** | 206 /** |
| 181 * Wraps the given [_ElementVisitorFunction] into an instance of | 207 * Wraps the given [_ElementVisitorFunction] into an instance of |
| 182 * [engine.GeneralizingElementVisitor]. | 208 * [engine.GeneralizingElementVisitor]. |
| 183 */ | 209 */ |
| 184 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { | 210 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { |
| 185 final _ElementVisitorFunction function; | 211 final _ElementVisitorFunction function; |
| 186 | 212 |
| 187 _ElementVisitorFunctionWrapper(this.function); | 213 _ElementVisitorFunctionWrapper(this.function); |
| 188 | 214 |
| 189 @override | 215 @override |
| 190 visitElement(Element element) { | 216 visitElement(Element element) { |
| 191 function(element); | 217 function(element); |
| 192 super.visitElement(element); | 218 super.visitElement(element); |
| 193 } | 219 } |
| 194 } | 220 } |
| OLD | NEW |