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