Skip to content

Commit 811d4c0

Browse files
author
Tim Blasi
committed
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.
1 parent af11190 commit 811d4c0

File tree

7 files changed

+128
-21
lines changed

7 files changed

+128
-21
lines changed

modules_dart/transform/lib/src/transform/transformer.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ class AngularTransformerGroup extends TransformerGroup {
3030
[new DirectiveProcessor(options)]
3131
];
3232
phases.addAll([
33-
[new DeferredRewriter(options), new DirectiveMetadataLinker()],
33+
[new DirectiveMetadataLinker()],
3434
[new BindGenerator(options)],
3535
[new TemplateCompiler(options)],
3636
[new StylesheetCompiler()],
37+
[new DeferredRewriter(options)]
3738
]);
3839
return new AngularTransformerGroup._(phases,
3940
formatCode: options.formatCode);

modules_dart/transform/test/transform/integration/all_tests.dart

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:code_transformers/tests.dart';
66
import 'package:dart_style/dart_style.dart';
77

88
import '../common/read_file.dart';
9+
import 'deferred_files/expected/output.dart' as deferredOuts;
910

1011
main() {
1112
allTests();
@@ -15,6 +16,21 @@ var formatter = new DartFormatter();
1516
var transform = new AngularTransformerGroup(
1617
new TransformerOptions(['web/index.dart'], formatCode: true));
1718

19+
// Each test has its own directory for inputs & an `expected` directory for
20+
// expected outputs.
21+
//
22+
// In addition to these declared inputs, we inject a set of common inputs for
23+
// every test.
24+
const commonInputs = const {
25+
'angular2|lib/src/core/metadata.dart': '../../../lib/src/core/metadata.dart',
26+
'angular2|lib/src/core/application.dart': '../common/application.dart',
27+
'angular2|lib/src/core/reflection/reflection_capabilities.dart':
28+
'../common/reflection_capabilities.dart',
29+
'angular2|lib/core.dart': '../../../lib/core.dart',
30+
'angular2|lib/src/core/di/decorators.dart':
31+
'../../../lib/src/core/di/decorators.dart',
32+
};
33+
1834
class IntegrationTestConfig {
1935
final String name;
2036
final Map<String, String> assetPathToInputPath;
@@ -32,24 +48,6 @@ class IntegrationTestConfig {
3248
void allTests() {
3349
Html5LibDomAdapter.makeCurrent();
3450

35-
/*
36-
* Each test has its own directory for inputs & an `expected` directory for
37-
* expected outputs.
38-
*
39-
* In addition to these declared inputs, we inject a set of common inputs for
40-
* every test.
41-
*/
42-
var commonInputs = {
43-
'angular2|lib/src/core/metadata.dart':
44-
'../../../lib/src/core/metadata.dart',
45-
'angular2|lib/src/core/application.dart': '../common/application.dart',
46-
'angular2|lib/src/core/reflection/reflection_capabilities.dart':
47-
'../common/reflection_capabilities.dart',
48-
'angular2|lib/core.dart': '../../../lib/core.dart',
49-
'angular2|lib/src/core/di/decorators.dart':
50-
'../../../lib/src/core/di/decorators.dart',
51-
};
52-
5351
var tests = [
5452
new IntegrationTestConfig(
5553
'should generate proper code for a Component defining only a selector.',
@@ -162,10 +160,33 @@ void allTests() {
162160
config.assetPathToInputPath,
163161
config.assetPathToExpectedOutputPath,
164162
[]);
165-
//,
166-
// StringFormatter.noNewlinesOrSurroundingWhitespace);
167163
}
168164
}
165+
166+
_testDeferredRewriter();
167+
}
168+
169+
void _testDeferredRewriter() {
170+
var inputs = {
171+
'a|web/bar.dart': 'deferred_files/bar.dart',
172+
'a|web/index.dart': 'deferred_files/index.dart'
173+
};
174+
inputs.addAll(commonInputs);
175+
inputs.keys.forEach((k) => inputs[k] = _readFile(inputs[k]));
176+
var outputs = {
177+
'a|web/bar.ng_deps.dart':
178+
_readFile('deferred_files/expected/bar.ng_deps.dart'),
179+
'a|web/bar.dart': _readFile('deferred_files/expected/bar.dart'),
180+
'a|web/index.dart': deferredOuts.indexContents
181+
};
182+
testPhases(
183+
'should handle deferred imports in input files.',
184+
[
185+
[transform]
186+
],
187+
inputs,
188+
outputs,
189+
[]);
169190
}
170191

171192
/// Smooths over differences in CWD between IDEs and running tests in Travis.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library bar;
2+
3+
import 'package:angular2/src/core/metadata.dart';
4+
5+
import 'deps/my_dep.dart' deferred as dep;
6+
7+
@Component(selector: '[soup]')
8+
@View(template: '')
9+
class MyComponent {
10+
void doDeferredThing() {
11+
dep.loadLibrary().then((_) {
12+
dep.doImmediateThing();
13+
});
14+
}
15+
}
16+
17+
void execImmediate() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library bar;
2+
3+
import 'package:angular2/src/core/metadata.dart';
4+
5+
import 'deps/my_dep.dart' deferred as dep;
6+
7+
@Component(selector: '[soup]')
8+
@View(template: '')
9+
class MyComponent {
10+
void doDeferredThing() {
11+
dep.loadLibrary().then((_) {
12+
dep.doImmediateThing();
13+
});
14+
}
15+
}
16+
17+
void execImmediate() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library bar.ng_deps.dart;
2+
3+
import 'bar.template.dart' as _templates;
4+
5+
import 'bar.dart';
6+
import 'package:angular2/src/core/reflection/reflection.dart' as _ngRef;
7+
import 'package:angular2/src/core/metadata.dart';
8+
import 'package:angular2/src/core/metadata.ng_deps.dart' as i0;
9+
export 'bar.dart';
10+
11+
var _visited = false;
12+
void initReflector() {
13+
if (_visited) return;
14+
_visited = true;
15+
_ngRef.reflector
16+
..registerType(
17+
MyComponent,
18+
new _ngRef.ReflectionInfo(const [
19+
const Component(selector: '[soup]'),
20+
const View(template: ''),
21+
_templates.HostMyComponentTemplate
22+
], const [], () => new MyComponent()));
23+
i0.initReflector();
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library angular2.test.transform.integration.deferred;
2+
3+
// This stored as a constant because we need to be careful to avoid modifying
4+
// source lines for files we rewrite.
5+
// That is, we expect that modifications we make to input files do not change
6+
// line numbers, and storing this expected output as code would allow it to be
7+
// formatted, breaking our tests.
8+
const indexContents = '''
9+
library web_foo;
10+
11+
import 'index.ng_deps.dart' as ngStaticInit;import 'bar.ng_deps.dart' deferred as bar;
12+
13+
void main() {
14+
bar.loadLibrary().then((_) {bar.initReflector();}).then((_) {
15+
bar.execImmediate();
16+
});
17+
}
18+
''';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library web_foo;
2+
3+
import 'bar.dart' deferred as bar;
4+
5+
void main() {
6+
bar.loadLibrary().then((_) {
7+
bar.execImmediate();
8+
});
9+
}

0 commit comments

Comments
 (0)