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; | |
| 5 import 'package:args/args.dart' show ArgParser, ArgResults; | 6 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 6 import 'package:analyzer/analyzer.dart' | 7 import 'package:analyzer/analyzer.dart' |
| 7 show AnalysisError, CompilationUnit, ErrorSeverity; | 8 show AnalysisError, CompilationUnit, CompileTimeErrorCode, ErrorSeverity; |
| 8 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | 9 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; |
| 9 import 'package:analyzer/src/generated/java_engine.dart' show AnalysisException; | 10 import 'package:analyzer/src/generated/java_engine.dart' show AnalysisException; |
| 10 import 'package:analyzer/src/generated/source_io.dart' show Source, SourceKind; | 11 import 'package:analyzer/src/generated/source_io.dart' show Source, SourceKind; |
| 11 import 'package:func/func.dart' show Func1; | 12 import 'package:func/func.dart' show Func1; |
| 12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 13 | 14 |
| 14 import '../analyzer/context.dart' | 15 import '../analyzer/context.dart' |
| 15 show AnalyzerOptions, createAnalysisContextWithSources; | 16 show AnalyzerOptions, createAnalysisContextWithSources; |
| 16 import 'extension_types.dart' show ExtensionTypeSet; | 17 import 'extension_types.dart' show ExtensionTypeSet; |
| 17 import 'code_generator.dart' show CodeGenerator; | 18 import 'code_generator.dart' show CodeGenerator; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 45 | 46 |
| 46 /// Compiles a single Dart build unit into a JavaScript module. | 47 /// Compiles a single Dart build unit into a JavaScript module. |
| 47 /// | 48 /// |
| 48 /// *Warning* - this may require resolving the entire world. | 49 /// *Warning* - this may require resolving the entire world. |
| 49 /// If that is not desired, the analysis context must be pre-configured using | 50 /// If that is not desired, the analysis context must be pre-configured using |
| 50 /// summaries before calling this method. | 51 /// summaries before calling this method. |
| 51 JSModuleFile compile(BuildUnit unit, CompilerOptions options) { | 52 JSModuleFile compile(BuildUnit unit, CompilerOptions options) { |
| 52 var trees = <CompilationUnit>[]; | 53 var trees = <CompilationUnit>[]; |
| 53 var errors = <AnalysisError>[]; | 54 var errors = <AnalysisError>[]; |
| 54 | 55 |
| 56 // Validate that all parts were explicitly passed in. | |
| 57 // If not, it's an error. | |
| 58 var explicitParts = new HashSet<Source>(); | |
| 59 var implicitParts = new HashSet<Source>(); | |
|
Paul Berry
2016/04/28 20:12:33
Nit: this is a confusing name. It seems to imply
Jennifer Messerly
2016/04/28 20:18:23
good idea, done
| |
| 55 for (var sourcePath in unit.sources) { | 60 for (var sourcePath in unit.sources) { |
| 56 var sourceUri = Uri.parse(sourcePath); | 61 var sourceUri = Uri.parse(sourcePath); |
| 57 if (sourceUri.scheme == '') { | 62 if (sourceUri.scheme == '') { |
| 58 sourceUri = path.toUri(path.absolute(sourcePath)); | 63 sourceUri = path.toUri(path.absolute(sourcePath)); |
| 59 } | 64 } |
| 60 Source source = context.sourceFactory.forUri(sourceUri.toString()); | 65 Source source = context.sourceFactory.forUri(sourceUri.toString()); |
| 61 if (source == null) { | 66 if (source == null) { |
| 62 throw new AnalysisException('could not create a source for $sourcePath.' | 67 throw new AnalysisException('could not create a source for $sourcePath.' |
| 63 ' The file name is in the wrong format or was not found.'); | 68 ' The file name is in the wrong format or was not found.'); |
| 64 } | 69 } |
| 65 | 70 |
| 66 // Ignore parts. They need to be handled in the context of their library. | 71 // Ignore parts. They need to be handled in the context of their library. |
| 67 if (context.getKindOf(source) == SourceKind.PART) { | 72 if (context.computeKindOf(source) == SourceKind.PART) { |
| 73 explicitParts.add(source); | |
| 68 continue; | 74 continue; |
| 69 } | 75 } |
| 70 | 76 |
| 71 var resolvedTree = context.resolveCompilationUnit2(source, source); | 77 var resolvedTree = context.resolveCompilationUnit2(source, source); |
| 72 trees.add(resolvedTree); | 78 trees.add(resolvedTree); |
| 73 errors.addAll(context.computeErrors(source)); | 79 errors.addAll(context.computeErrors(source)); |
| 74 | 80 |
| 75 var library = resolvedTree.element.library; | 81 var library = resolvedTree.element.library; |
| 76 for (var part in library.parts) { | 82 for (var part in library.parts) { |
| 83 implicitParts.add(part.source); | |
| 77 trees.add(context.resolveCompilationUnit(part.source, library)); | 84 trees.add(context.resolveCompilationUnit(part.source, library)); |
| 78 errors.addAll(context.computeErrors(part.source)); | 85 errors.addAll(context.computeErrors(part.source)); |
| 79 } | 86 } |
| 80 } | 87 } |
| 81 | 88 |
| 89 // Check if all parts were explicitly passed in. | |
| 90 // Also verify all explicitly parts were used. | |
| 91 var missingParts = implicitParts.difference(explicitParts); | |
| 92 var unusedParts = explicitParts.difference(implicitParts); | |
| 93 errors.addAll(missingParts | |
| 94 .map((s) => new AnalysisError(s, 0, 0, missingPartErrorCode))); | |
| 95 errors.addAll(unusedParts | |
| 96 .map((s) => new AnalysisError(s, 0, 0, unusedPartErrorCode))); | |
| 97 | |
| 82 sortErrors(context, errors); | 98 sortErrors(context, errors); |
| 83 var messages = <String>[]; | 99 var messages = <String>[]; |
| 84 for (var e in errors) { | 100 for (var e in errors) { |
| 85 var m = formatError(context, e); | 101 var m = formatError(context, e); |
| 86 if (m != null) messages.add(m); | 102 if (m != null) messages.add(m); |
| 87 } | 103 } |
| 88 | 104 |
| 89 if (!options.unsafeForceCompile && | 105 if (!options.unsafeForceCompile && |
| 90 errors.any((e) => errorSeverity(context, e) == ErrorSeverity.ERROR)) { | 106 errors.any((e) => errorSeverity(context, e) == ErrorSeverity.ERROR)) { |
| 91 return new JSModuleFile.invalid(unit.name, messages); | 107 return new JSModuleFile.invalid(unit.name, messages); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 | 278 |
| 263 var map = new Map.from(this.sourceMap); | 279 var map = new Map.from(this.sourceMap); |
| 264 List list = new List.from(map['sources']); | 280 List list = new List.from(map['sources']); |
| 265 map['sources'] = list; | 281 map['sources'] = list; |
| 266 for (int i = 0; i < list.length; i++) { | 282 for (int i = 0; i < list.length; i++) { |
| 267 list[i] = path.relative(list[i], from: dir); | 283 list[i] = path.relative(list[i], from: dir); |
| 268 } | 284 } |
| 269 return map; | 285 return map; |
| 270 } | 286 } |
| 271 } | 287 } |
| 288 | |
| 289 /// (Public for tests) the error code used when a part is missing. | |
| 290 final missingPartErrorCode = const CompileTimeErrorCode( | |
| 291 'MISSING_PART', 'The part was not supplied as an input to the compiler.'); | |
| 292 | |
| 293 /// (Public for tests) the error code used when a part is unused. | |
| 294 final unusedPartErrorCode = const CompileTimeErrorCode( | |
|
Paul Berry
2016/04/28 20:12:33
Should this really be an error? It's harmless, an
Jennifer Messerly
2016/04/28 20:18:23
Yeah, that's a good question. I'm fine taking it o
| |
| 295 'UNUSED_PART', 'The part was not used by any libraries being compiled.'); | |
| OLD | NEW |