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 library code_transformer.src.resolver_impl; | 5 library code_transformer.src.resolver_impl; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'package:analyzer/analyzer.dart' show parseCompilationUnit; | 8 import 'package:analyzer/analyzer.dart' show parseCompilationUnit; |
9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, |
| 11 EvaluationResult; |
10 import 'package:analyzer/src/generated/element.dart'; | 12 import 'package:analyzer/src/generated/element.dart'; |
11 import 'package:analyzer/src/generated/engine.dart'; | 13 import 'package:analyzer/src/generated/engine.dart'; |
12 import 'package:analyzer/src/generated/error.dart'; | 14 import 'package:analyzer/src/generated/error.dart'; |
13 import 'package:analyzer/src/generated/java_io.dart'; | 15 import 'package:analyzer/src/generated/java_io.dart'; |
14 import 'package:analyzer/src/generated/parser.dart'; | 16 import 'package:analyzer/src/generated/parser.dart'; |
15 import 'package:analyzer/src/generated/scanner.dart'; | 17 import 'package:analyzer/src/generated/scanner.dart'; |
16 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; | 18 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; |
17 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 19 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
18 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
19 import 'package:barback/barback.dart'; | 21 import 'package:barback/barback.dart'; |
20 import 'package:code_transformers/assets.dart'; | 22 import 'package:code_transformers/assets.dart'; |
21 import 'package:path/path.dart' as native_path; | 23 import 'package:path/path.dart' as native_path; |
22 import 'package:source_maps/refactor.dart'; | 24 import 'package:source_maps/refactor.dart'; |
23 import 'package:source_maps/span.dart' show SourceFile, Span; | 25 import 'package:source_maps/span.dart' show SourceFile, Span; |
24 | 26 |
25 import 'resolver.dart'; | 27 import 'resolver.dart'; |
26 | 28 |
27 // We should always be using url paths here since it's always Dart/pub code. | 29 // We should always be using url paths here since it's always Dart/pub code. |
28 final path = native_path.url; | 30 final path = native_path.url; |
29 | 31 |
30 /// Resolves and updates an AST based on Barback-based assets. | 32 /// Resolves and updates an AST based on Barback-based assets. |
31 /// | 33 /// |
32 /// This also provides a handful of useful APIs for traversing and working | 34 /// This also provides a handful of useful APIs for traversing and working |
33 /// with the resolved AST. | 35 /// with the resolved AST. |
34 class ResolverImpl implements Resolver { | 36 class ResolverImpl implements Resolver { |
35 /// Cache of all asset sources currently referenced. | 37 /// Cache of all asset sources currently referenced. |
36 final Map<AssetId, _AssetBasedSource> sources = | 38 final Map<AssetId, _AssetBasedSource> sources = |
37 <AssetId, _AssetBasedSource>{}; | 39 <AssetId, _AssetBasedSource>{}; |
38 | 40 |
39 final AnalysisContext _context = | 41 final InternalAnalysisContext _context = |
40 AnalysisEngine.instance.createAnalysisContext(); | 42 AnalysisEngine.instance.createAnalysisContext(); |
41 | 43 |
42 /// Transform for which this is currently updating, or null when not updating. | 44 /// Transform for which this is currently updating, or null when not updating. |
43 Transform _currentTransform; | 45 Transform _currentTransform; |
44 | 46 |
45 /// The currently resolved entry libraries, or null if nothing is resolved. | 47 /// The currently resolved entry libraries, or null if nothing is resolved. |
46 List<LibraryElement> _entryLibraries; | 48 List<LibraryElement> _entryLibraries; |
47 Set<LibraryElement> _libraries; | 49 Set<LibraryElement> _libraries; |
48 | 50 |
49 /// Future indicating when this resolver is done in the current phase. | 51 /// Future indicating when this resolver is done in the current phase. |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 var name = dotIndex == -1 ? | 218 var name = dotIndex == -1 ? |
217 fnName : fnName.substring(dotIndex + 1); | 219 fnName : fnName.substring(dotIndex + 1); |
218 | 220 |
219 return libraries.where((lib) => lib.name == libraryName) | 221 return libraries.where((lib) => lib.name == libraryName) |
220 .expand((lib) => lib.units) | 222 .expand((lib) => lib.units) |
221 .expand((unit) => unit.functions) | 223 .expand((unit) => unit.functions) |
222 .firstWhere((fn) => fn.name == name, | 224 .firstWhere((fn) => fn.name == name, |
223 orElse: () => null); | 225 orElse: () => null); |
224 } | 226 } |
225 | 227 |
| 228 EvaluationResult evaluateConstant( |
| 229 LibraryElement library, Expression expression) { |
| 230 return new ConstantEvaluator(library.source, _context.typeProvider) |
| 231 .evaluate(expression); |
| 232 } |
| 233 |
226 Uri getImportUri(LibraryElement lib, {AssetId from}) => | 234 Uri getImportUri(LibraryElement lib, {AssetId from}) => |
227 _getSourceUri(lib, from: from); | 235 _getSourceUri(lib, from: from); |
228 | 236 |
229 | 237 |
230 /// Similar to getImportUri but will get the part URI for parts rather than | 238 /// Similar to getImportUri but will get the part URI for parts rather than |
231 /// the library URI. | 239 /// the library URI. |
232 Uri _getSourceUri(Element element, {AssetId from}) { | 240 Uri _getSourceUri(Element element, {AssetId from}) { |
233 var source = element.source; | 241 var source = element.source; |
234 if (source is _AssetBasedSource) { | 242 if (source is _AssetBasedSource) { |
235 return source.getSourceUri(from); | 243 return source.getSourceUri(from); |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 | 610 |
603 void apply(ChangeSet changeSet) { | 611 void apply(ChangeSet changeSet) { |
604 if (!source.updateContents(content)) return; | 612 if (!source.updateContents(content)) return; |
605 if (source._revision == 1 && source._contents != null) { | 613 if (source._revision == 1 && source._contents != null) { |
606 changeSet.addedSource(source); | 614 changeSet.addedSource(source); |
607 } else { | 615 } else { |
608 changeSet.changedSource(source); | 616 changeSet.changedSource(source); |
609 } | 617 } |
610 } | 618 } |
611 } | 619 } |
OLD | NEW |