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 library dev_compiler.src.analysis_context; | 5 library dev_compiler.src.analysis_context; |
6 | 6 |
7 import 'package:analyzer/file_system/file_system.dart'; | |
8 import 'package:analyzer/file_system/memory_file_system.dart'; | |
9 import 'package:analyzer/src/generated/engine.dart'; | 7 import 'package:analyzer/src/generated/engine.dart'; |
10 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 8 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
11 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 9 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
12 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | 10 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; |
13 import 'package:analyzer/src/generated/source_io.dart'; | 11 import 'package:analyzer/src/generated/source_io.dart'; |
14 import 'package:path/path.dart' as path; | |
15 | 12 |
16 import 'package:dev_compiler/strong_mode.dart' show StrongModeOptions; | 13 import 'package:dev_compiler/strong_mode.dart' show StrongModeOptions; |
17 | 14 |
18 import 'checker/resolver.dart'; | 15 import 'checker/resolver.dart'; |
19 import 'dart_sdk.dart'; | 16 import 'dart_sdk.dart'; |
20 import 'multi_package_resolver.dart'; | 17 import 'multi_package_resolver.dart'; |
21 import 'options.dart'; | 18 import 'options.dart'; |
22 | 19 |
23 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, | 20 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, |
24 /// using [createSourceFactory] to set up its [SourceFactory]. | 21 /// using [createSourceFactory] to set up its [SourceFactory]. |
25 AnalysisContext createAnalysisContextWithSources( | 22 AnalysisContext createAnalysisContextWithSources( |
26 StrongModeOptions strongOptions, SourceResolverOptions srcOptions, | 23 StrongModeOptions strongOptions, SourceResolverOptions srcOptions, |
27 {DartUriResolver sdkResolver, List fileResolvers}) { | 24 {DartUriResolver sdkResolver, List fileResolvers}) { |
28 var srcFactory = createSourceFactory(srcOptions, | 25 var srcFactory = createSourceFactory(srcOptions, |
29 sdkResolver: sdkResolver, fileResolvers: fileResolvers); | 26 sdkResolver: sdkResolver, fileResolvers: fileResolvers); |
30 return createAnalysisContext(strongOptions)..sourceFactory = srcFactory; | 27 return createAnalysisContext(strongOptions)..sourceFactory = srcFactory; |
31 } | 28 } |
32 | 29 |
33 /// Creates an analysis context that contains our restricted typing rules. | 30 /// Creates an analysis context that contains our restricted typing rules. |
34 AnalysisContext createAnalysisContext(StrongModeOptions options) { | 31 AnalysisContext createAnalysisContext(StrongModeOptions options) { |
35 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); | 32 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); |
36 res.libraryResolverFactory = | 33 enableDevCompilerInference(res, options); |
37 (context) => new LibraryResolverWithInference(context, options); | |
38 return res; | 34 return res; |
39 } | 35 } |
40 | 36 |
| 37 /// Enables dev_compiler inference rules. |
| 38 // TODO(jmesserly): is there a cleaner way to plug this in? |
| 39 void enableDevCompilerInference( |
| 40 AnalysisContextImpl context, StrongModeOptions options) { |
| 41 context.libraryResolverFactory = |
| 42 (c) => new LibraryResolverWithInference(c, options); |
| 43 } |
| 44 |
41 /// Creates a SourceFactory configured by the [options]. | 45 /// Creates a SourceFactory configured by the [options]. |
42 /// | 46 /// |
43 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] | 47 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] |
44 /// to entirely override the DartUriResolver. | 48 /// to entirely override the DartUriResolver. |
45 /// | 49 /// |
46 /// If supplied, [fileResolvers] will override the default `file:` and | 50 /// If supplied, [fileResolvers] will override the default `file:` and |
47 /// `package:` URI resolvers. | 51 /// `package:` URI resolvers. |
48 SourceFactory createSourceFactory(SourceResolverOptions options, | 52 SourceFactory createSourceFactory(SourceResolverOptions options, |
49 {DartUriResolver sdkResolver, List fileResolvers}) { | 53 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { |
50 var sdkResolver = options.useMockSdk | 54 var sdkResolver = options.useMockSdk |
51 ? createMockSdkResolver(mockSdkSources) | 55 ? createMockSdkResolver(mockSdkSources) |
52 : createSdkPathResolver(options.dartSdkPath); | 56 : createSdkPathResolver(options.dartSdkPath); |
53 | 57 |
54 var resolvers = []; | 58 var resolvers = []; |
55 if (options.customUrlMappings.isNotEmpty) { | 59 if (options.customUrlMappings.isNotEmpty) { |
56 resolvers.add(new CustomUriResolver(options.customUrlMappings)); | 60 resolvers.add(new CustomUriResolver(options.customUrlMappings)); |
57 } | 61 } |
58 resolvers.add(sdkResolver); | 62 resolvers.add(sdkResolver); |
59 if (options.useImplicitHtml) { | 63 if (fileResolvers == null) fileResolvers = createFileResolvers(options); |
60 resolvers.add(_createImplicitEntryResolver(options)); | |
61 } | |
62 if (fileResolvers == null) { | |
63 fileResolvers = [new FileUriResolver()]; | |
64 fileResolvers.add(options.useMultiPackage | |
65 ? new MultiPackageResolver(options.packagePaths) | |
66 : new PackageUriResolver([new JavaFile(options.packageRoot)])); | |
67 } | |
68 resolvers.addAll(fileResolvers); | 64 resolvers.addAll(fileResolvers); |
69 return new SourceFactory(resolvers); | 65 return new SourceFactory(resolvers); |
70 } | 66 } |
71 | 67 |
| 68 List<UriResolver> createFileResolvers(SourceResolverOptions options) { |
| 69 return [ |
| 70 new FileUriResolver(), |
| 71 options.useMultiPackage |
| 72 ? new MultiPackageResolver(options.packagePaths) |
| 73 : new PackageUriResolver([new JavaFile(options.packageRoot)]) |
| 74 ]; |
| 75 } |
| 76 |
72 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. | 77 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. |
73 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => | 78 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => |
74 new MockDartSdk(mockSources, reportMissing: true).resolver; | 79 new MockDartSdk(mockSources, reportMissing: true).resolver; |
75 | 80 |
76 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. | 81 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. |
77 DartUriResolver createSdkPathResolver(String sdkPath) => new DartUriResolver( | 82 DartUriResolver createSdkPathResolver(String sdkPath) => new DartUriResolver( |
78 new DirectoryBasedDartSdk( | 83 new DirectoryBasedDartSdk( |
79 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true)); | 84 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true)); |
80 | |
81 UriResolver _createImplicitEntryResolver(SourceResolverOptions options) { | |
82 var entry = path.absolute(SourceResolverOptions.implicitHtmlFile); | |
83 var src = path.absolute(options.entryPointFile); | |
84 var provider = new MemoryResourceProvider(); | |
85 provider.newFile( | |
86 entry, '<body><script type="application/dart" src="$src"></script>'); | |
87 return new ExistingSourceUriResolver(new ResourceUriResolver(provider)); | |
88 } | |
89 | |
90 /// A UriResolver that continues to the next one if it fails to find an existing | |
91 /// source file. This is unlike normal URI resolvers, that always return | |
92 /// something, even if it is a non-existing file. | |
93 class ExistingSourceUriResolver implements UriResolver { | |
94 final UriResolver resolver; | |
95 ExistingSourceUriResolver(this.resolver); | |
96 | |
97 Source resolveAbsolute(Uri uri) { | |
98 var src = resolver.resolveAbsolute(uri); | |
99 return src.exists() ? src : null; | |
100 } | |
101 Uri restoreAbsolute(Source source) => resolver.restoreAbsolute(source); | |
102 } | |
OLD | NEW |