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 | 23 |
25 import 'package:dev_compiler/src/options.dart'; | 24 import 'package:dev_compiler/src/options.dart'; |
26 import 'package:dev_compiler/src/report.dart'; | |
27 import 'package:dev_compiler/src/utils.dart'; | 25 import 'package:dev_compiler/src/utils.dart'; |
28 import 'dart_sdk.dart'; | 26 import 'dart_sdk.dart'; |
29 import 'multi_package_resolver.dart'; | 27 import 'multi_package_resolver.dart'; |
30 | 28 |
31 final _log = new logger.Logger('dev_compiler.src.resolver'); | 29 final _log = new logger.Logger('dev_compiler.src.resolver'); |
32 | 30 |
31 // TODO(jmesserly): this class can be removed, and converted to some top-level | |
32 // methods that create the AnalysisContext. | |
33 /// Encapsulates a resolver from the analyzer package. | 33 /// Encapsulates a resolver from the analyzer package. |
34 class TypeResolver { | 34 class TypeResolver { |
Jennifer Messerly
2015/05/13 23:21:16
happy to remove this type in follow up CL. It does
vsm
2015/05/14 22:02:08
sgtm
| |
35 final InternalAnalysisContext context; | 35 final InternalAnalysisContext context; |
36 | 36 |
37 final Map<Uri, Source> _sources = <Uri, Source>{}; | |
38 | |
39 TypeResolver(DartUriResolver sdkResolver, ResolverOptions options, | 37 TypeResolver(DartUriResolver sdkResolver, ResolverOptions options, |
40 {List otherResolvers}) | 38 {List otherResolvers}) |
41 : context = _initContext(options) { | 39 : context = _initContext(options) { |
42 var resolvers = [sdkResolver]; | 40 var resolvers = [sdkResolver]; |
43 if (otherResolvers == null) { | 41 if (otherResolvers == null) { |
44 resolvers.add(new FileUriResolver()); | 42 resolvers.add(new FileUriResolver()); |
45 resolvers.add(options.useMultiPackage | 43 resolvers.add(options.useMultiPackage |
46 ? new MultiPackageResolver(options.packagePaths) | 44 ? new MultiPackageResolver(options.packagePaths) |
47 : new PackageUriResolver([new JavaFile(options.packageRoot)])); | 45 : new PackageUriResolver([new JavaFile(options.packageRoot)])); |
48 } else { | 46 } else { |
49 resolvers.addAll(otherResolvers); | 47 resolvers.addAll(otherResolvers); |
50 } | 48 } |
51 context.sourceFactory = new SourceFactory(resolvers); | 49 context.sourceFactory = new SourceFactory(resolvers); |
52 } | 50 } |
53 | 51 |
54 /// Creates a [TypeResolver] that uses a mock 'dart:' library contents. | 52 /// Creates a [TypeResolver] that uses a mock 'dart:' library contents. |
55 TypeResolver.fromMock( | 53 TypeResolver.fromMock( |
56 Map<String, String> mockSources, ResolverOptions options, | 54 Map<String, String> mockSources, ResolverOptions options, |
57 {List otherResolvers}) | 55 {List otherResolvers}) |
58 : this( | 56 : this( |
59 new MockDartSdk(mockSources, reportMissing: true).resolver, options, | 57 new MockDartSdk(mockSources, reportMissing: true).resolver, options, |
60 otherResolvers: otherResolvers); | 58 otherResolvers: otherResolvers); |
61 | 59 |
62 /// Creates a [TypeResolver] that uses the SDK at the given [sdkPath]. | 60 /// Creates a [TypeResolver] that uses the SDK at the given [sdkPath]. |
63 TypeResolver.fromDir(String sdkPath, ResolverOptions options, | 61 TypeResolver.fromDir(String sdkPath, ResolverOptions options, |
64 {List otherResolvers}) | 62 {List otherResolvers}) |
65 : this( | 63 : this( |
66 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))), | 64 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))), |
67 options, otherResolvers: otherResolvers); | 65 options, otherResolvers: otherResolvers); |
68 | |
69 /// Find the corresponding [Source] for [uri]. | |
70 Source findSource(Uri uri) { | |
71 var source = _sources[uri]; | |
72 if (source != null) return source; | |
73 return _sources[uri] = context.sourceFactory.forUri('$uri'); | |
74 } | |
75 | |
76 /// Log any errors encountered when resolving [source] and return whether any | |
77 /// errors were found. | |
78 bool logErrors(Source source, CheckerReporter reporter) { | |
79 List<analyzer.AnalysisError> errors = context.getErrors(source).errors; | |
80 bool failure = false; | |
81 if (errors.isNotEmpty) { | |
82 for (var error in errors) { | |
83 var message = new AnalyzerError.from(error); | |
84 if (message.level == logger.Level.SEVERE) failure = true; | |
85 reporter.log(message); | |
86 } | |
87 } | |
88 return failure; | |
89 } | |
90 } | |
91 | |
92 class AnalyzerError extends Message { | |
93 factory AnalyzerError.from(analyzer.AnalysisError error) { | |
94 var severity = error.errorCode.type.severity; | |
95 var isError = severity == analyzer.ErrorSeverity.ERROR; | |
96 var level = isError ? logger.Level.SEVERE : logger.Level.WARNING; | |
97 int begin = error.offset; | |
98 int end = begin + error.length; | |
99 return new AnalyzerError(error.message, level, begin, end); | |
100 } | |
101 | |
102 const AnalyzerError(String message, logger.Level level, int begin, int end) | |
103 : super('[from analyzer]: $message', level, begin, end); | |
104 } | 66 } |
105 | 67 |
106 /// Creates an analysis context that contains our restricted typing rules. | 68 /// Creates an analysis context that contains our restricted typing rules. |
107 InternalAnalysisContext _initContext(ResolverOptions options) { | 69 InternalAnalysisContext _initContext(ResolverOptions options) { |
108 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; | 70 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; |
109 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); | 71 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); |
110 res.analysisOptions = analysisOptions; | 72 res.analysisOptions = analysisOptions; |
111 res.libraryResolverFactory = | 73 res.libraryResolverFactory = |
112 (context) => new LibraryResolverWithInference(context, options); | 74 (context) => new LibraryResolverWithInference(context, options); |
113 return res; | 75 return res; |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
769 } | 731 } |
770 } | 732 } |
771 | 733 |
772 // Review note: no longer need to override visitFunctionExpression, this is | 734 // Review note: no longer need to override visitFunctionExpression, this is |
773 // handled by the analyzer internally. | 735 // handled by the analyzer internally. |
774 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? | 736 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? |
775 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression | 737 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression |
776 // type in a (...) => expr or just the written type? | 738 // type in a (...) => expr or just the written type? |
777 | 739 |
778 } | 740 } |
OLD | NEW |