From 811d4c03bd2e6fcade8dc5c1ef6024e0956187e8 Mon Sep 17 00:00:00 2001 From: Tim Blasi Date: Wed, 7 Oct 2015 16:18:28 -0700 Subject: [PATCH] fix(dart/transform): Run DeferredRewriter in the correct phase `DeferredRewriter` depends on the presence of `.ng_deps.dart` files, which do not yet exist in the phase where it was previously run. Update the transformer phases to fix this and add an integration test to prevent regression. --- .../lib/src/transform/transformer.dart | 3 +- .../test/transform/integration/all_tests.dart | 61 +++++++++++++------ .../integration/deferred_files/bar.dart | 17 ++++++ .../deferred_files/expected/bar.dart | 17 ++++++ .../deferred_files/expected/bar.ng_deps.dart | 24 ++++++++ .../deferred_files/expected/output.dart | 18 ++++++ .../integration/deferred_files/index.dart | 9 +++ 7 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 modules_dart/transform/test/transform/integration/deferred_files/bar.dart create mode 100644 modules_dart/transform/test/transform/integration/deferred_files/expected/bar.dart create mode 100644 modules_dart/transform/test/transform/integration/deferred_files/expected/bar.ng_deps.dart create mode 100644 modules_dart/transform/test/transform/integration/deferred_files/expected/output.dart create mode 100644 modules_dart/transform/test/transform/integration/deferred_files/index.dart diff --git a/modules_dart/transform/lib/src/transform/transformer.dart b/modules_dart/transform/lib/src/transform/transformer.dart index 975274b364..66695c6b24 100644 --- a/modules_dart/transform/lib/src/transform/transformer.dart +++ b/modules_dart/transform/lib/src/transform/transformer.dart @@ -30,10 +30,11 @@ class AngularTransformerGroup extends TransformerGroup { [new DirectiveProcessor(options)] ]; phases.addAll([ - [new DeferredRewriter(options), new DirectiveMetadataLinker()], + [new DirectiveMetadataLinker()], [new BindGenerator(options)], [new TemplateCompiler(options)], [new StylesheetCompiler()], + [new DeferredRewriter(options)] ]); return new AngularTransformerGroup._(phases, formatCode: options.formatCode); diff --git a/modules_dart/transform/test/transform/integration/all_tests.dart b/modules_dart/transform/test/transform/integration/all_tests.dart index 3970815697..d8d3e279d5 100644 --- a/modules_dart/transform/test/transform/integration/all_tests.dart +++ b/modules_dart/transform/test/transform/integration/all_tests.dart @@ -6,6 +6,7 @@ import 'package:code_transformers/tests.dart'; import 'package:dart_style/dart_style.dart'; import '../common/read_file.dart'; +import 'deferred_files/expected/output.dart' as deferredOuts; main() { allTests(); @@ -15,6 +16,21 @@ var formatter = new DartFormatter(); var transform = new AngularTransformerGroup( new TransformerOptions(['web/index.dart'], formatCode: true)); +// Each test has its own directory for inputs & an `expected` directory for +// expected outputs. +// +// In addition to these declared inputs, we inject a set of common inputs for +// every test. +const commonInputs = const { + 'angular2|lib/src/core/metadata.dart': '../../../lib/src/core/metadata.dart', + 'angular2|lib/src/core/application.dart': '../common/application.dart', + 'angular2|lib/src/core/reflection/reflection_capabilities.dart': + '../common/reflection_capabilities.dart', + 'angular2|lib/core.dart': '../../../lib/core.dart', + 'angular2|lib/src/core/di/decorators.dart': + '../../../lib/src/core/di/decorators.dart', +}; + class IntegrationTestConfig { final String name; final Map assetPathToInputPath; @@ -32,24 +48,6 @@ class IntegrationTestConfig { void allTests() { Html5LibDomAdapter.makeCurrent(); - /* - * Each test has its own directory for inputs & an `expected` directory for - * expected outputs. - * - * In addition to these declared inputs, we inject a set of common inputs for - * every test. - */ - var commonInputs = { - 'angular2|lib/src/core/metadata.dart': - '../../../lib/src/core/metadata.dart', - 'angular2|lib/src/core/application.dart': '../common/application.dart', - 'angular2|lib/src/core/reflection/reflection_capabilities.dart': - '../common/reflection_capabilities.dart', - 'angular2|lib/core.dart': '../../../lib/core.dart', - 'angular2|lib/src/core/di/decorators.dart': - '../../../lib/src/core/di/decorators.dart', - }; - var tests = [ new IntegrationTestConfig( 'should generate proper code for a Component defining only a selector.', @@ -162,10 +160,33 @@ void allTests() { config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []); -//, -// StringFormatter.noNewlinesOrSurroundingWhitespace); } } + + _testDeferredRewriter(); +} + +void _testDeferredRewriter() { + var inputs = { + 'a|web/bar.dart': 'deferred_files/bar.dart', + 'a|web/index.dart': 'deferred_files/index.dart' + }; + inputs.addAll(commonInputs); + inputs.keys.forEach((k) => inputs[k] = _readFile(inputs[k])); + var outputs = { + 'a|web/bar.ng_deps.dart': + _readFile('deferred_files/expected/bar.ng_deps.dart'), + 'a|web/bar.dart': _readFile('deferred_files/expected/bar.dart'), + 'a|web/index.dart': deferredOuts.indexContents + }; + testPhases( + 'should handle deferred imports in input files.', + [ + [transform] + ], + inputs, + outputs, + []); } /// Smooths over differences in CWD between IDEs and running tests in Travis. diff --git a/modules_dart/transform/test/transform/integration/deferred_files/bar.dart b/modules_dart/transform/test/transform/integration/deferred_files/bar.dart new file mode 100644 index 0000000000..b9151d0b2a --- /dev/null +++ b/modules_dart/transform/test/transform/integration/deferred_files/bar.dart @@ -0,0 +1,17 @@ +library bar; + +import 'package:angular2/src/core/metadata.dart'; + +import 'deps/my_dep.dart' deferred as dep; + +@Component(selector: '[soup]') +@View(template: '') +class MyComponent { + void doDeferredThing() { + dep.loadLibrary().then((_) { + dep.doImmediateThing(); + }); + } +} + +void execImmediate() {} diff --git a/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.dart b/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.dart new file mode 100644 index 0000000000..b9151d0b2a --- /dev/null +++ b/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.dart @@ -0,0 +1,17 @@ +library bar; + +import 'package:angular2/src/core/metadata.dart'; + +import 'deps/my_dep.dart' deferred as dep; + +@Component(selector: '[soup]') +@View(template: '') +class MyComponent { + void doDeferredThing() { + dep.loadLibrary().then((_) { + dep.doImmediateThing(); + }); + } +} + +void execImmediate() {} diff --git a/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.ng_deps.dart b/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.ng_deps.dart new file mode 100644 index 0000000000..b207b8263a --- /dev/null +++ b/modules_dart/transform/test/transform/integration/deferred_files/expected/bar.ng_deps.dart @@ -0,0 +1,24 @@ +library bar.ng_deps.dart; + +import 'bar.template.dart' as _templates; + +import 'bar.dart'; +import 'package:angular2/src/core/reflection/reflection.dart' as _ngRef; +import 'package:angular2/src/core/metadata.dart'; +import 'package:angular2/src/core/metadata.ng_deps.dart' as i0; +export 'bar.dart'; + +var _visited = false; +void initReflector() { + if (_visited) return; + _visited = true; + _ngRef.reflector + ..registerType( + MyComponent, + new _ngRef.ReflectionInfo(const [ + const Component(selector: '[soup]'), + const View(template: ''), + _templates.HostMyComponentTemplate + ], const [], () => new MyComponent())); + i0.initReflector(); +} diff --git a/modules_dart/transform/test/transform/integration/deferred_files/expected/output.dart b/modules_dart/transform/test/transform/integration/deferred_files/expected/output.dart new file mode 100644 index 0000000000..f70b733fac --- /dev/null +++ b/modules_dart/transform/test/transform/integration/deferred_files/expected/output.dart @@ -0,0 +1,18 @@ +library angular2.test.transform.integration.deferred; + +// This stored as a constant because we need to be careful to avoid modifying +// source lines for files we rewrite. +// That is, we expect that modifications we make to input files do not change +// line numbers, and storing this expected output as code would allow it to be +// formatted, breaking our tests. +const indexContents = ''' +library web_foo; + +import 'index.ng_deps.dart' as ngStaticInit;import 'bar.ng_deps.dart' deferred as bar; + +void main() { + bar.loadLibrary().then((_) {bar.initReflector();}).then((_) { + bar.execImmediate(); + }); +} +'''; diff --git a/modules_dart/transform/test/transform/integration/deferred_files/index.dart b/modules_dart/transform/test/transform/integration/deferred_files/index.dart new file mode 100644 index 0000000000..1ed56aacd7 --- /dev/null +++ b/modules_dart/transform/test/transform/integration/deferred_files/index.dart @@ -0,0 +1,9 @@ +library web_foo; + +import 'bar.dart' deferred as bar; + +void main() { + bar.loadLibrary().then((_) { + bar.execImmediate(); + }); +}