Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet, Queue; | 5 import 'dart:collection' show HashSet, Queue; |
| 6 import 'dart:convert' show BASE64, JSON, UTF8; | 6 import 'dart:convert' show BASE64, JSON, UTF8; |
| 7 import 'dart:io' show File; | 7 import 'dart:io' show File; |
| 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; | 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; |
| 9 import 'package:analyzer/analyzer.dart' | 9 import 'package:analyzer/analyzer.dart' |
| 10 show AnalysisError, CompilationUnit, ErrorSeverity; | 10 show AnalysisError, CompilationUnit, ErrorSeverity; |
| 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; | 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; |
| 12 import 'package:analyzer/file_system/physical_file_system.dart' | |
| 13 show PhysicalResourceProvider; | |
| 14 import 'package:analyzer/src/context/builder.dart' show ContextBuilder; | |
| 15 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
| 12 import 'package:analyzer/src/generated/engine.dart' | 16 import 'package:analyzer/src/generated/engine.dart' |
| 13 show AnalysisContext, AnalysisEngine; | 17 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; |
| 14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | 18 import 'package:analyzer/src/generated/sdk.dart' show DartSdkManager; |
| 19 import 'package:analyzer/src/generated/source.dart' | |
| 20 show ContentCache, DartUriResolver; | |
| 15 import 'package:analyzer/src/generated/source_io.dart' | 21 import 'package:analyzer/src/generated/source_io.dart' |
| 16 show Source, SourceKind, UriResolver; | 22 show Source, SourceKind, UriResolver; |
| 17 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 23 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; | 24 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; |
| 19 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; | 25 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; |
| 20 import 'package:args/args.dart' show ArgParser, ArgResults; | 26 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 21 import 'package:args/src/usage_exception.dart' show UsageException; | 27 import 'package:args/src/usage_exception.dart' show UsageException; |
| 22 import 'package:func/func.dart' show Func1; | 28 import 'package:func/func.dart' show Func1; |
| 23 import 'package:path/path.dart' as path; | 29 import 'package:path/path.dart' as path; |
| 24 import 'package:source_maps/source_maps.dart'; | 30 import 'package:source_maps/source_maps.dart'; |
| 25 | 31 |
| 26 import '../analyzer/context.dart' | 32 import '../analyzer/context.dart' show AnalyzerOptions, createSourceFactory; |
| 27 show | |
| 28 AnalyzerOptions, | |
| 29 createAnalysisContext, | |
| 30 createSdkPathResolver, | |
| 31 createSourceFactory; | |
| 32 import '../js_ast/js_ast.dart' as JS; | 33 import '../js_ast/js_ast.dart' as JS; |
| 33 import 'code_generator.dart' show CodeGenerator; | 34 import 'code_generator.dart' show CodeGenerator; |
| 34 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors; | 35 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors; |
| 35 import 'extension_types.dart' show ExtensionTypeSet; | 36 import 'extension_types.dart' show ExtensionTypeSet; |
| 36 import 'js_names.dart' as JS; | 37 import 'js_names.dart' as JS; |
| 37 import 'module_builder.dart' show transformModuleFormat, ModuleFormat; | 38 import 'module_builder.dart' show transformModuleFormat, ModuleFormat; |
| 38 import 'source_map_printer.dart' show SourceMapPrintingContext; | 39 import 'source_map_printer.dart' show SourceMapPrintingContext; |
| 39 | 40 |
| 40 /// Compiles a set of Dart files into a single JavaScript module. | 41 /// Compiles a set of Dart files into a single JavaScript module. |
| 41 /// | 42 /// |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 52 /// | 53 /// |
| 53 /// For all other files, it is up to the [AnalysisContext] to decide whether or | 54 /// For all other files, it is up to the [AnalysisContext] to decide whether or |
| 54 /// not any caching is performed. By default an analysis context will assume | 55 /// not any caching is performed. By default an analysis context will assume |
| 55 /// sources are immutable for the life of the context, and cache information | 56 /// sources are immutable for the life of the context, and cache information |
| 56 /// about them. | 57 /// about them. |
| 57 class ModuleCompiler { | 58 class ModuleCompiler { |
| 58 final AnalysisContext context; | 59 final AnalysisContext context; |
| 59 final SummaryDataStore summaryData; | 60 final SummaryDataStore summaryData; |
| 60 final ExtensionTypeSet _extensionTypes; | 61 final ExtensionTypeSet _extensionTypes; |
| 61 | 62 |
| 62 ModuleCompiler.withContext(AnalysisContext context, this.summaryData) | 63 ModuleCompiler._(AnalysisContext context, this.summaryData) |
| 63 : context = context, | 64 : context = context, |
| 64 _extensionTypes = new ExtensionTypeSet(context) { | 65 _extensionTypes = new ExtensionTypeSet(context); |
| 65 if (!context.analysisOptions.strongMode) { | |
| 66 throw new ArgumentError('AnalysisContext must be strong mode'); | |
| 67 } | |
| 68 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) { | |
| 69 throw new ArgumentError('AnalysisContext must have strong mode SDK'); | |
| 70 } | |
| 71 } | |
| 72 | 66 |
| 73 factory ModuleCompiler(AnalyzerOptions options, | 67 factory ModuleCompiler(String projRoot, AnalyzerOptions options, |
|
vsm
2017/01/04 20:20:39
Perhaps rename projRoot to analysisOptionsPath or
danrubel
2017/01/04 20:41:20
The analyzer-cli looks in the current working dire
vsm
2017/01/04 20:46:20
Yes, I think it would make sense to act the same a
danrubel
2017/01/04 21:37:42
Done.
| |
| 74 {DartUriResolver sdkResolver, | 68 {ResourceProvider resourceProvider, List<UriResolver> fileResolvers}) { |
| 75 ResourceProvider resourceProvider, | |
| 76 List<UriResolver> fileResolvers}) { | |
| 77 AnalysisEngine.instance.processRequiredPlugins(); | 69 AnalysisEngine.instance.processRequiredPlugins(); |
|
vsm
2017/01/04 20:20:39
Would it make sense to push much of the logic here
danrubel
2017/01/04 20:41:19
Yes, that's my long term goal, but I'm working tow
vsm
2017/01/04 20:46:20
SGTM :-) Do you mind adding a comment / todo here
danrubel
2017/01/04 21:37:42
Good point. Done.
| |
| 78 | 70 |
| 79 sdkResolver ??= | 71 resourceProvider ??= PhysicalResourceProvider.INSTANCE; |
| 80 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); | 72 |
| 73 var contextBuilder = new ContextBuilder(resourceProvider, | |
| 74 new DartSdkManager(options.dartSdkPath, true), new ContentCache(), | |
| 75 options: options.contextBuilderOptions); | |
| 76 | |
| 77 var analysisOptions = contextBuilder.getAnalysisOptions(projRoot); | |
| 78 var sdk = contextBuilder.findSdk(null, analysisOptions); | |
| 79 | |
| 80 var sdkResolver = new DartUriResolver(sdk); | |
| 81 | 81 |
| 82 // Read the summaries. | 82 // Read the summaries. |
| 83 var summaryData = | 83 var summaryData = |
| 84 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true); | 84 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true); |
| 85 | 85 |
| 86 var srcFactory = createSourceFactory(options, | 86 var srcFactory = createSourceFactory(options, |
| 87 sdkResolver: sdkResolver, | 87 sdkResolver: sdkResolver, |
| 88 fileResolvers: fileResolvers, | 88 fileResolvers: fileResolvers, |
| 89 summaryData: summaryData, | 89 summaryData: summaryData, |
| 90 resourceProvider: resourceProvider); | 90 resourceProvider: resourceProvider); |
| 91 | 91 |
| 92 var context = createAnalysisContext(); | 92 var context = |
| 93 AnalysisEngine.instance.createAnalysisContext() as AnalysisContextImpl; | |
| 94 context.analysisOptions = analysisOptions; | |
| 93 context.sourceFactory = srcFactory; | 95 context.sourceFactory = srcFactory; |
| 94 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; | 96 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; |
| 95 context.resultProvider = | 97 context.resultProvider = |
| 96 new InputPackagesResultProvider(context, summaryData); | 98 new InputPackagesResultProvider(context, summaryData); |
| 97 options.declaredVariables.forEach(context.declaredVariables.define); | 99 options.declaredVariables.forEach(context.declaredVariables.define); |
| 98 context.declaredVariables.define('dart.isVM', 'false'); | 100 context.declaredVariables.define('dart.isVM', 'false'); |
| 99 | 101 |
| 100 // TODO(vsm): Should this be hardcoded? | 102 // TODO(vsm): Should this be hardcoded? |
| 101 context.declaredVariables.define('dart.library.html', 'true'); | 103 context.declaredVariables.define('dart.library.html', 'true'); |
| 102 context.declaredVariables.define('dart.library.io', 'false'); | 104 context.declaredVariables.define('dart.library.io', 'false'); |
| 103 | 105 |
| 104 return new ModuleCompiler.withContext(context, summaryData); | 106 if (!context.analysisOptions.strongMode) { |
| 107 throw new ArgumentError('AnalysisContext must be strong mode'); | |
| 108 } | |
| 109 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) { | |
| 110 throw new ArgumentError('AnalysisContext must have strong mode SDK'); | |
| 111 } | |
| 112 | |
| 113 return new ModuleCompiler._(context, summaryData); | |
| 105 } | 114 } |
| 106 | 115 |
| 107 bool _isFatalError(AnalysisError e, CompilerOptions options) { | 116 bool _isFatalError(AnalysisError e, CompilerOptions options) { |
| 108 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false; | 117 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false; |
| 109 | 118 |
| 110 // These errors are not fatal in the REPL compile mode as we | 119 // These errors are not fatal in the REPL compile mode as we |
| 111 // allow access to private members across library boundaries | 120 // allow access to private members across library boundaries |
| 112 // and those accesses will show up as undefined members unless | 121 // and those accesses will show up as undefined members unless |
| 113 // additional analyzer changes are made to support them. | 122 // additional analyzer changes are made to support them. |
| 114 // TODO(jacobr): consider checking that the identifier name | 123 // TODO(jacobr): consider checking that the identifier name |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 558 // Fall back to a relative path. | 567 // Fall back to a relative path. |
| 559 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); | 568 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); |
| 560 } | 569 } |
| 561 | 570 |
| 562 for (int i = 0; i < list.length; i++) { | 571 for (int i = 0; i < list.length; i++) { |
| 563 list[i] = transformUri(list[i]); | 572 list[i] = transformUri(list[i]); |
| 564 } | 573 } |
| 565 map['file'] = transformUri(map['file']); | 574 map['file'] = transformUri(map['file']); |
| 566 return map; | 575 return map; |
| 567 } | 576 } |
| OLD | NEW |