| 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 library mock_compiler; | 5 library mock_compiler; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:compiler/compiler.dart' as api; | 10 import 'package:compiler/compiler_new.dart' as api; |
| 11 import 'package:compiler/src/common/names.dart' show Uris; | 11 import 'package:compiler/src/common/names.dart' show Uris; |
| 12 import 'package:compiler/src/constants/expressions.dart'; | 12 import 'package:compiler/src/constants/expressions.dart'; |
| 13 import 'package:compiler/src/elements/resolution_types.dart' | 13 import 'package:compiler/src/elements/resolution_types.dart' |
| 14 show ResolutionDartType; | 14 show ResolutionDartType; |
| 15 import 'package:compiler/src/diagnostics/diagnostic_listener.dart'; | 15 import 'package:compiler/src/diagnostics/diagnostic_listener.dart'; |
| 16 import 'package:compiler/src/diagnostics/source_span.dart'; | 16 import 'package:compiler/src/diagnostics/source_span.dart'; |
| 17 import 'package:compiler/src/diagnostics/spannable.dart'; | 17 import 'package:compiler/src/diagnostics/spannable.dart'; |
| 18 import 'package:compiler/src/elements/elements.dart'; | 18 import 'package:compiler/src/elements/elements.dart'; |
| 19 import 'package:compiler/src/elements/visitor.dart'; | 19 import 'package:compiler/src/elements/visitor.dart'; |
| 20 import 'package:compiler/src/js_backend/backend_helpers.dart' | 20 import 'package:compiler/src/js_backend/backend_helpers.dart' |
| 21 show BackendHelpers; | 21 show BackendHelpers; |
| 22 import 'package:compiler/src/js_backend/lookup_map_analysis.dart' | 22 import 'package:compiler/src/js_backend/lookup_map_analysis.dart' |
| 23 show LookupMapAnalysis; | 23 show LookupMapAnalysis; |
| 24 import 'package:compiler/src/io/source_file.dart'; | 24 import 'package:compiler/src/io/source_file.dart'; |
| 25 import 'package:compiler/src/options.dart' show CompilerOptions; | 25 import 'package:compiler/src/options.dart' show CompilerOptions; |
| 26 import 'package:compiler/src/resolution/members.dart'; | 26 import 'package:compiler/src/resolution/members.dart'; |
| 27 import 'package:compiler/src/resolution/registry.dart'; | 27 import 'package:compiler/src/resolution/registry.dart'; |
| 28 import 'package:compiler/src/resolution/scope.dart'; | 28 import 'package:compiler/src/resolution/scope.dart'; |
| 29 import 'package:compiler/src/resolution/tree_elements.dart'; | 29 import 'package:compiler/src/resolution/tree_elements.dart'; |
| 30 import 'package:compiler/src/resolved_uri_translator.dart'; | 30 import 'package:compiler/src/resolved_uri_translator.dart'; |
| 31 import 'package:compiler/src/script.dart'; | 31 import 'package:compiler/src/script.dart'; |
| 32 import 'package:compiler/src/tree/tree.dart'; | 32 import 'package:compiler/src/tree/tree.dart'; |
| 33 import 'package:compiler/src/old_to_new_api.dart'; | 33 import 'package:compiler/src/old_to_new_api.dart'; |
| 34 import 'parser_helper.dart'; | 34 import 'parser_helper.dart'; |
| 35 | 35 |
| 36 import 'package:compiler/src/elements/modelx.dart' | 36 import 'package:compiler/src/elements/modelx.dart' |
| 37 show ElementX, LibraryElementX, ErroneousElementX, FunctionElementX; | 37 show ErroneousElementX, FunctionElementX; |
| 38 | 38 |
| 39 import 'package:compiler/src/compiler.dart'; | 39 import 'package:compiler/src/compiler.dart'; |
| 40 import 'package:compiler/src/common/tasks.dart' show Measurer; | 40 import 'package:compiler/src/common/tasks.dart' show Measurer; |
| 41 | 41 |
| 42 import 'package:compiler/src/deferred_load.dart' | 42 import 'package:compiler/src/deferred_load.dart' |
| 43 show DeferredLoadTask, OutputUnit; | 43 show DeferredLoadTask, OutputUnit; |
| 44 | 44 |
| 45 import 'mock_libraries.dart'; | 45 import 'mock_libraries.dart'; |
| 46 import 'diagnostic_helper.dart'; | 46 import 'diagnostic_helper.dart'; |
| 47 | 47 |
| 48 export 'diagnostic_helper.dart'; | 48 export 'diagnostic_helper.dart'; |
| 49 | 49 |
| 50 final Uri PATCH_CORE = new Uri(scheme: 'patch', path: 'core'); | 50 final Uri PATCH_CORE = new Uri(scheme: 'patch', path: 'core'); |
| 51 | 51 |
| 52 typedef String LibrarySourceProvider(Uri uri); | 52 typedef String LibrarySourceProvider(Uri uri); |
| 53 | 53 |
| 54 class MockCompiler extends Compiler { | 54 class MockCompiler extends Compiler { |
| 55 api.DiagnosticHandler diagnosticHandler; | 55 api.CompilerDiagnostics diagnosticHandler; |
| 56 | 56 |
| 57 /// Expected number of warnings. If `null`, the number of warnings is | 57 /// Expected number of warnings. If `null`, the number of warnings is |
| 58 /// not checked. | 58 /// not checked. |
| 59 final int expectedWarnings; | 59 final int expectedWarnings; |
| 60 | 60 |
| 61 /// Expected number of errors. If `null`, the number of errors is not checked. | 61 /// Expected number of errors. If `null`, the number of errors is not checked. |
| 62 final int expectedErrors; | 62 final int expectedErrors; |
| 63 final Map<String, SourceFile> sourceFiles; | 63 final Map<String, SourceFile> sourceFiles; |
| 64 Node parsedTree; | 64 Node parsedTree; |
| 65 final String testedPatchVersion; | 65 final String testedPatchVersion; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 79 bool analyzeOnly: false, | 79 bool analyzeOnly: false, |
| 80 bool preserveComments: false, | 80 bool preserveComments: false, |
| 81 // Our unit tests check code generation output that is | 81 // Our unit tests check code generation output that is |
| 82 // affected by inlining support. | 82 // affected by inlining support. |
| 83 bool disableInlining: true, | 83 bool disableInlining: true, |
| 84 bool trustTypeAnnotations: false, | 84 bool trustTypeAnnotations: false, |
| 85 bool trustJSInteropTypeAnnotations: false, | 85 bool trustJSInteropTypeAnnotations: false, |
| 86 bool enableAsyncAwait: false, | 86 bool enableAsyncAwait: false, |
| 87 int this.expectedWarnings, | 87 int this.expectedWarnings, |
| 88 int this.expectedErrors, | 88 int this.expectedErrors, |
| 89 api.CompilerOutputProvider outputProvider, | 89 api.CompilerOutput outputProvider, |
| 90 String patchVersion, | 90 String patchVersion, |
| 91 LibrarySourceProvider this.librariesOverride}) | 91 LibrarySourceProvider this.librariesOverride}) |
| 92 : sourceFiles = new Map<String, SourceFile>(), | 92 : sourceFiles = new Map<String, SourceFile>(), |
| 93 testedPatchVersion = patchVersion, | 93 testedPatchVersion = patchVersion, |
| 94 super( | 94 super( |
| 95 options: new CompilerOptions( | 95 options: new CompilerOptions( |
| 96 entryPoint: new Uri(scheme: 'mock'), | 96 entryPoint: new Uri(scheme: 'mock'), |
| 97 libraryRoot: Uri.parse('placeholder_library_root_for_mock/'), | 97 libraryRoot: Uri.parse('placeholder_library_root_for_mock/'), |
| 98 enableTypeAssertions: enableTypeAssertions, | 98 enableTypeAssertions: enableTypeAssertions, |
| 99 enableUserAssertions: enableUserAssertions, | 99 enableUserAssertions: enableUserAssertions, |
| 100 disableInlining: disableInlining, | 100 disableInlining: disableInlining, |
| 101 enableAssertMessage: true, | 101 enableAssertMessage: true, |
| 102 enableMinification: enableMinification, | 102 enableMinification: enableMinification, |
| 103 disableTypeInference: disableTypeInference, | 103 disableTypeInference: disableTypeInference, |
| 104 analyzeAll: analyzeAll, | 104 analyzeAll: analyzeAll, |
| 105 analyzeOnly: analyzeOnly, | 105 analyzeOnly: analyzeOnly, |
| 106 preserveComments: preserveComments, | 106 preserveComments: preserveComments, |
| 107 trustTypeAnnotations: trustTypeAnnotations, | 107 trustTypeAnnotations: trustTypeAnnotations, |
| 108 trustJSInteropTypeAnnotations: trustJSInteropTypeAnnotations, | 108 trustJSInteropTypeAnnotations: trustJSInteropTypeAnnotations, |
| 109 shownPackageWarnings: const []), | 109 shownPackageWarnings: const []), |
| 110 outputProvider: new LegacyCompilerOutput(outputProvider)) { | 110 outputProvider: outputProvider) { |
| 111 deferredLoadTask = new MockDeferredLoadTask(this); | 111 deferredLoadTask = new MockDeferredLoadTask(this); |
| 112 | 112 |
| 113 registerSource( | 113 registerSource( |
| 114 Uris.dart_core, buildLibrarySource(DEFAULT_CORE_LIBRARY, coreSource)); | 114 Uris.dart_core, buildLibrarySource(DEFAULT_CORE_LIBRARY, coreSource)); |
| 115 registerSource(PATCH_CORE, DEFAULT_PATCH_CORE_SOURCE); | 115 registerSource(PATCH_CORE, DEFAULT_PATCH_CORE_SOURCE); |
| 116 | 116 |
| 117 registerSource(BackendHelpers.DART_JS_HELPER, | 117 registerSource(BackendHelpers.DART_JS_HELPER, |
| 118 buildLibrarySource(DEFAULT_JS_HELPER_LIBRARY)); | 118 buildLibrarySource(DEFAULT_JS_HELPER_LIBRARY)); |
| 119 registerSource(BackendHelpers.DART_FOREIGN_HELPER, | 119 registerSource(BackendHelpers.DART_FOREIGN_HELPER, |
| 120 buildLibrarySource(DEFAULT_FOREIGN_HELPER_LIBRARY)); | 120 buildLibrarySource(DEFAULT_FOREIGN_HELPER_LIBRARY)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 int begin; | 195 int begin; |
| 196 int end; | 196 int end; |
| 197 String text = '${message.message}'; | 197 String text = '${message.message}'; |
| 198 if (span != null) { | 198 if (span != null) { |
| 199 uri = span.uri; | 199 uri = span.uri; |
| 200 begin = span.begin; | 200 begin = span.begin; |
| 201 end = span.end; | 201 end = span.end; |
| 202 } | 202 } |
| 203 diagnosticCollector.report(message.message, uri, begin, end, text, kind); | 203 diagnosticCollector.report(message.message, uri, begin, end, text, kind); |
| 204 if (diagnosticHandler != null) { | 204 if (diagnosticHandler != null) { |
| 205 diagnosticHandler(uri, begin, end, text, kind); | 205 diagnosticHandler.report(message.message, uri, begin, end, text, kind); |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 processMessage(message, kind); | 209 processMessage(message, kind); |
| 210 infoMessages.forEach((i) => processMessage(i, api.Diagnostic.INFO)); | 210 infoMessages.forEach((i) => processMessage(i, api.Diagnostic.INFO)); |
| 211 } | 211 } |
| 212 | 212 |
| 213 CollectingTreeElements resolveStatement(String text) { | 213 CollectingTreeElements resolveStatement(String text) { |
| 214 parsedTree = parseStatement(text); | 214 parsedTree = parseStatement(text); |
| 215 return resolveNodeStatement(parsedTree, new MockElement(mainApp)); | 215 return resolveNodeStatement(parsedTree, new MockElement(mainApp)); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 330 |
| 331 // The mock compiler does not split the program in output units. | 331 // The mock compiler does not split the program in output units. |
| 332 class MockDeferredLoadTask extends DeferredLoadTask { | 332 class MockDeferredLoadTask extends DeferredLoadTask { |
| 333 MockDeferredLoadTask(Compiler compiler) : super(compiler); | 333 MockDeferredLoadTask(Compiler compiler) : super(compiler); |
| 334 | 334 |
| 335 OutputUnit getElementOutputUnit(dynamic dependency) { | 335 OutputUnit getElementOutputUnit(dynamic dependency) { |
| 336 return mainOutputUnit; | 336 return mainOutputUnit; |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 api.DiagnosticHandler createHandler(MockCompiler compiler, String text, | 340 api.CompilerDiagnostics createHandler(MockCompiler compiler, String text, |
| 341 {bool verbose: false}) { | 341 {bool verbose: false}) { |
| 342 return (uri, int begin, int end, String message, kind) { | 342 return new LegacyCompilerDiagnostics( |
| 343 (uri, int begin, int end, String message, kind) { |
| 343 if (kind == api.Diagnostic.VERBOSE_INFO && !verbose) return; | 344 if (kind == api.Diagnostic.VERBOSE_INFO && !verbose) return; |
| 344 SourceFile sourceFile; | 345 SourceFile sourceFile; |
| 345 if (uri == null) { | 346 if (uri == null) { |
| 346 sourceFile = new StringSourceFile.fromName('analysis', text); | 347 sourceFile = new StringSourceFile.fromName('analysis', text); |
| 347 } else { | 348 } else { |
| 348 sourceFile = compiler.sourceFiles[uri.toString()]; | 349 sourceFile = compiler.sourceFiles[uri.toString()]; |
| 349 } | 350 } |
| 350 if (sourceFile != null && begin != null && end != null) { | 351 if (sourceFile != null && begin != null && end != null) { |
| 351 print('${kind}: ${sourceFile.getLocationMessage(message, begin, end)}'); | 352 print('${kind}: ${sourceFile.getLocationMessage(message, begin, end)}'); |
| 352 } else { | 353 } else { |
| 353 print('${kind}: $message'); | 354 print('${kind}: $message'); |
| 354 } | 355 } |
| 355 }; | 356 }); |
| 356 } | 357 } |
| 357 | 358 |
| 358 class MockElement extends FunctionElementX { | 359 class MockElement extends FunctionElementX { |
| 359 MockElement(Element enclosingElement) | 360 MockElement(Element enclosingElement) |
| 360 : super('', ElementKind.FUNCTION, Modifiers.EMPTY, enclosingElement); | 361 : super('', ElementKind.FUNCTION, Modifiers.EMPTY, enclosingElement); |
| 361 | 362 |
| 362 get node => null; | 363 get node => null; |
| 363 | 364 |
| 364 parseNode(_) => null; | 365 parseNode(_) => null; |
| 365 | 366 |
| 366 bool get hasNode => false; | 367 bool get hasNode => false; |
| 367 | 368 |
| 368 accept(ElementVisitor visitor, arg) { | 369 accept(ElementVisitor visitor, arg) { |
| 369 return visitor.visitMethodElement(this, arg); | 370 return visitor.visitMethodElement(this, arg); |
| 370 } | 371 } |
| 371 } | 372 } |
| 372 | 373 |
| 373 // TODO(herhut): Disallow warnings and errors during compilation by default. | 374 // TODO(herhut): Disallow warnings and errors during compilation by default. |
| 374 MockCompiler compilerFor(String code, Uri uri, | 375 MockCompiler compilerFor(String code, Uri uri, |
| 375 {bool analyzeAll: false, | 376 {bool analyzeAll: false, |
| 376 bool analyzeOnly: false, | 377 bool analyzeOnly: false, |
| 377 Map<String, String> coreSource, | 378 Map<String, String> coreSource, |
| 378 bool disableInlining: true, | 379 bool disableInlining: true, |
| 379 bool minify: false, | 380 bool minify: false, |
| 380 bool trustTypeAnnotations: false, | 381 bool trustTypeAnnotations: false, |
| 381 bool enableTypeAssertions: false, | 382 bool enableTypeAssertions: false, |
| 382 bool enableUserAssertions: false, | 383 bool enableUserAssertions: false, |
| 383 int expectedErrors, | 384 int expectedErrors, |
| 384 int expectedWarnings, | 385 int expectedWarnings, |
| 385 api.CompilerOutputProvider outputProvider}) { | 386 api.CompilerOutput outputProvider}) { |
| 386 MockCompiler compiler = new MockCompiler.internal( | 387 MockCompiler compiler = new MockCompiler.internal( |
| 387 analyzeAll: analyzeAll, | 388 analyzeAll: analyzeAll, |
| 388 analyzeOnly: analyzeOnly, | 389 analyzeOnly: analyzeOnly, |
| 389 coreSource: coreSource, | 390 coreSource: coreSource, |
| 390 disableInlining: disableInlining, | 391 disableInlining: disableInlining, |
| 391 enableMinification: minify, | 392 enableMinification: minify, |
| 392 trustTypeAnnotations: trustTypeAnnotations, | 393 trustTypeAnnotations: trustTypeAnnotations, |
| 393 enableTypeAssertions: enableTypeAssertions, | 394 enableTypeAssertions: enableTypeAssertions, |
| 394 enableUserAssertions: enableUserAssertions, | 395 enableUserAssertions: enableUserAssertions, |
| 395 expectedErrors: expectedErrors, | 396 expectedErrors: expectedErrors, |
| 396 expectedWarnings: expectedWarnings, | 397 expectedWarnings: expectedWarnings, |
| 397 outputProvider: outputProvider); | 398 outputProvider: outputProvider); |
| 398 compiler.registerSource(uri, code); | 399 compiler.registerSource(uri, code); |
| 399 compiler.diagnosticHandler = createHandler(compiler, code); | 400 compiler.diagnosticHandler = createHandler(compiler, code); |
| 400 return compiler; | 401 return compiler; |
| 401 } | 402 } |
| OLD | NEW |