OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Encapsulates how to invoke the analyzer resolver and overrides how it | 5 /// Encapsulates how to invoke the analyzer resolver and overrides how it |
6 /// computes types on expressions to use our restricted set of types. | 6 /// computes types on expressions to use our restricted set of types. |
7 library dev_compiler.src.checker.resolver; | 7 library dev_compiler.src.checker.resolver; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
11 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
13 import 'package:analyzer/src/generated/error.dart' as analyzer; | |
14 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 13 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
15 import 'package:analyzer/src/generated/resolver.dart'; | 14 import 'package:analyzer/src/generated/resolver.dart'; |
16 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 15 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
17 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | 16 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; |
18 import 'package:analyzer/src/generated/source.dart' show Source; | 17 import 'package:analyzer/src/generated/source.dart' show Source; |
19 import 'package:analyzer/src/generated/source_io.dart'; | 18 import 'package:analyzer/src/generated/source_io.dart'; |
20 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 19 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
21 import 'package:analyzer/src/generated/utilities_collection.dart' | 20 import 'package:analyzer/src/generated/utilities_collection.dart' |
22 show DirectedGraph; | 21 show DirectedGraph; |
23 import 'package:logging/logging.dart' as logger; | 22 import 'package:logging/logging.dart' as logger; |
24 import 'package:path/path.dart' as path; | 23 import 'package:path/path.dart' as path; |
25 | 24 |
26 import 'package:dev_compiler/src/in_memory.dart'; | 25 import 'package:dev_compiler/src/in_memory.dart'; |
27 import 'package:dev_compiler/src/options.dart'; | 26 import 'package:dev_compiler/src/options.dart'; |
28 import 'package:dev_compiler/src/report.dart'; | |
29 import 'package:dev_compiler/src/utils.dart'; | 27 import 'package:dev_compiler/src/utils.dart'; |
30 import 'dart_sdk.dart'; | 28 import 'dart_sdk.dart'; |
31 import 'multi_package_resolver.dart'; | 29 import 'multi_package_resolver.dart'; |
32 | 30 |
33 final _log = new logger.Logger('dev_compiler.src.resolver'); | 31 final _log = new logger.Logger('dev_compiler.src.resolver'); |
34 | 32 |
35 String _implicitEntryHtml(String src) => ''' | 33 String _implicitEntryHtml(String src) => ''' |
36 <html> | 34 <html> |
37 <body> | 35 <body> |
38 <script type="application/dart" src="$src"></script> | 36 <script type="application/dart" src="$src"></script> |
39 </body> | 37 </body> |
40 </html> | 38 </html> |
41 '''; | 39 '''; |
42 | 40 |
| 41 // TODO(jmesserly): this class can be removed, and converted to some top-level |
| 42 // methods that create the AnalysisContext. |
43 /// Encapsulates a resolver from the analyzer package. | 43 /// Encapsulates a resolver from the analyzer package. |
44 class TypeResolver { | 44 class TypeResolver { |
45 final InternalAnalysisContext context; | 45 final InternalAnalysisContext context; |
46 | 46 |
47 final Map<Uri, Source> _sources = <Uri, Source>{}; | |
48 | |
49 TypeResolver(DartUriResolver sdkResolver, ResolverOptions options, | 47 TypeResolver(DartUriResolver sdkResolver, ResolverOptions options, |
50 {List otherResolvers}) | 48 {List otherResolvers}) |
51 : context = _initContext(options) { | 49 : context = _initContext(options) { |
52 var resolvers = options.useImplicitHtml | 50 var resolvers = options.useImplicitHtml |
53 ? [_createImplicitEntryResolver(options), sdkResolver] | 51 ? [_createImplicitEntryResolver(options), sdkResolver] |
54 : [sdkResolver]; | 52 : [sdkResolver]; |
55 if (otherResolvers == null) { | 53 if (otherResolvers == null) { |
56 resolvers.add(new FileUriResolver()); | 54 resolvers.add(new FileUriResolver()); |
57 resolvers.add(options.useMultiPackage | 55 resolvers.add(options.useMultiPackage |
58 ? new MultiPackageResolver(options.packagePaths) | 56 ? new MultiPackageResolver(options.packagePaths) |
(...skipping 11 matching lines...) Expand all Loading... |
70 : this( | 68 : this( |
71 new MockDartSdk(mockSources, reportMissing: true).resolver, options, | 69 new MockDartSdk(mockSources, reportMissing: true).resolver, options, |
72 otherResolvers: otherResolvers); | 70 otherResolvers: otherResolvers); |
73 | 71 |
74 /// Creates a [TypeResolver] that uses the SDK at the given [sdkPath]. | 72 /// Creates a [TypeResolver] that uses the SDK at the given [sdkPath]. |
75 TypeResolver.fromDir(String sdkPath, ResolverOptions options, | 73 TypeResolver.fromDir(String sdkPath, ResolverOptions options, |
76 {UriResolver entryResolver, List otherResolvers}) | 74 {UriResolver entryResolver, List otherResolvers}) |
77 : this( | 75 : this( |
78 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))), | 76 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))), |
79 options, otherResolvers: otherResolvers); | 77 options, otherResolvers: otherResolvers); |
80 | |
81 UriResolver _createImplicitEntryResolver(ResolverOptions options) { | |
82 var entry = path.absolute(ResolverOptions.implicitHtmlFile); | |
83 var src = path.absolute(options.entryPointFile); | |
84 var index = <String, String>{'$entry': _implicitEntryHtml(src)}; | |
85 return new InMemoryUriResolver(index, representNonExistingFiles: false); | |
86 } | |
87 | |
88 /// Find the corresponding [Source] for [uri]. | |
89 Source findSource(Uri uri) { | |
90 var source = _sources[uri]; | |
91 if (source != null) return source; | |
92 return _sources[uri] = context.sourceFactory.forUri('$uri'); | |
93 } | |
94 | |
95 /// Log any errors encountered when resolving [source] and return whether any | |
96 /// errors were found. | |
97 bool logErrors(Source source, CheckerReporter reporter) { | |
98 List<analyzer.AnalysisError> errors = context.getErrors(source).errors; | |
99 bool failure = false; | |
100 if (errors.isNotEmpty) { | |
101 for (var error in errors) { | |
102 var message = new AnalyzerError.from(error); | |
103 if (message.level == logger.Level.SEVERE) failure = true; | |
104 reporter.log(message); | |
105 } | |
106 } | |
107 return failure; | |
108 } | |
109 } | 78 } |
110 | 79 |
111 class AnalyzerError extends Message { | 80 UriResolver _createImplicitEntryResolver(ResolverOptions options) { |
112 factory AnalyzerError.from(analyzer.AnalysisError error) { | 81 var entry = path.absolute(ResolverOptions.implicitHtmlFile); |
113 var severity = error.errorCode.type.severity; | 82 var src = path.absolute(options.entryPointFile); |
114 var isError = severity == analyzer.ErrorSeverity.ERROR; | 83 var index = <String, String>{'$entry': _implicitEntryHtml(src)}; |
115 var level = isError ? logger.Level.SEVERE : logger.Level.WARNING; | 84 return new InMemoryUriResolver(index, representNonExistingFiles: false); |
116 int begin = error.offset; | |
117 int end = begin + error.length; | |
118 return new AnalyzerError(error.message, level, begin, end); | |
119 } | |
120 | |
121 const AnalyzerError(String message, logger.Level level, int begin, int end) | |
122 : super('[from analyzer]: $message', level, begin, end); | |
123 } | 85 } |
124 | 86 |
125 /// Creates an analysis context that contains our restricted typing rules. | 87 /// Creates an analysis context that contains our restricted typing rules. |
126 InternalAnalysisContext _initContext(ResolverOptions options) { | 88 InternalAnalysisContext _initContext(ResolverOptions options) { |
127 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; | 89 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; |
128 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); | 90 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); |
129 res.analysisOptions = analysisOptions; | 91 res.analysisOptions = analysisOptions; |
130 res.libraryResolverFactory = | 92 res.libraryResolverFactory = |
131 (context) => new LibraryResolverWithInference(context, options); | 93 (context) => new LibraryResolverWithInference(context, options); |
132 return res; | 94 return res; |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 } | 750 } |
789 } | 751 } |
790 | 752 |
791 // Review note: no longer need to override visitFunctionExpression, this is | 753 // Review note: no longer need to override visitFunctionExpression, this is |
792 // handled by the analyzer internally. | 754 // handled by the analyzer internally. |
793 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? | 755 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? |
794 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression | 756 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression |
795 // type in a (...) => expr or just the written type? | 757 // type in a (...) => expr or just the written type? |
796 | 758 |
797 } | 759 } |
OLD | NEW |