Skip to content

Commit 2909576

Browse files
committed
fix(bootstrap): fix expressions containing bootstrap (fixes #3309)
1 parent eee2146 commit 2909576

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

modules/angular2/src/core/application_static.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import 'application_common.dart';
77
///
88
/// See [commonBootstrap] for detailed documentation.
99
Future<ApplicationRef> bootstrapStatic(Type appComponentType,
10-
[List componentInjectableBindings]) {
10+
[List componentInjectableBindings, void initReflector()]) {
11+
if (initReflector != null) {
12+
initReflector();
13+
}
1114
return commonBootstrap(appComponentType, componentInjectableBindings);
1215
}

modules/angular2/src/transform/reflection_remover/rewriter.dart

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,38 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
124124
_rewriteBootstrapCallToStatic(MethodInvocation node) {
125125
if (_rewriter._writeStaticInit) {
126126
buf.write(_rewriter._code.substring(_currentIndex, node.offset));
127-
_writeStaticReflectorInitOnce();
127+
128+
var args = node.argumentList.arguments;
129+
int numArgs = node.argumentList.arguments.length;
130+
if (numArgs < 1 || numArgs > 2) {
131+
logger.warning('`bootstrap` does not support $numArgs arguments. Found bootstrap${node.argumentList}. Transform may not succeed.');
132+
}
133+
134+
var reflectorInit = _setupAdded
135+
? ''
136+
: ', () { ${_getStaticReflectorInitBlock()} }';
137+
128138
// rewrite `bootstrap(...)` to `bootstrapStatic(...)`
129-
buf.write('bootstrapStatic${node.argumentList}');
139+
buf.write('bootstrapStatic(');
140+
buf.write(args[0]);
141+
if (numArgs == 1) {
142+
if (reflectorInit.isNotEmpty) {
143+
buf.write(', null');
144+
}
145+
} else {
146+
buf.write(', ${args[1]}');
147+
}
148+
buf.write(reflectorInit);
149+
buf.write(')');
130150
} else {
131151
// leave it as is
132152
buf.write(_rewriter._code.substring(_currentIndex, node.end));
133153
}
134154
_currentIndex = node.end;
135155
}
136156

137-
_writeStaticReflectorInitOnce() {
138-
if (!_setupAdded) {
139-
buf.write(_rewriter._codegen.codegenSetupReflectionCall());
140-
_setupAdded = true;
141-
}
157+
String _getStaticReflectorInitBlock() {
158+
return _rewriter._codegen.codegenSetupReflectionCall();
142159
}
143160

144161
_rewriteReflectionCapabilitiesImport(ImportDirective node) {
@@ -162,8 +179,9 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
162179
node = node.parent;
163180
}
164181
buf.write(_rewriter._code.substring(_currentIndex, node.offset));
165-
if (_rewriter._writeStaticInit) {
166-
_writeStaticReflectorInitOnce();
182+
if (_rewriter._writeStaticInit && !_setupAdded) {
183+
buf.write(_getStaticReflectorInitBlock());
184+
_setupAdded = true;
167185
}
168186
switch (_rewriter._mirrorMode) {
169187
case MirrorMode.debug:

modules/angular2/test/transform/reflection_remover/all_tests.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import 'reflection_remover_files/expected/index.dart' as expected;
1010
import 'debug_mirrors_files/expected/index.dart' as debug_mirrors;
1111
import 'log_mirrors_files/expected/index.dart' as log_mirrors;
1212
import 'verbose_files/expected/index.dart' as verbose_mirrors;
13+
import 'bootstrap_files/expected/index.dart' as bootstrap_expected;
1314
import '../common/read_file.dart';
1415

1516
main() => allTests();
1617

1718
void allTests() {
1819
var codegen = new Codegen('web/index.dart', ['web/index.ng_deps.dart']);
1920
var code = readFile('reflection_remover/index.dart').replaceAll('\r\n', '\n');
21+
var bootstrapCode = readFile('reflection_remover/bootstrap_files/index.dart').replaceAll('\r\n', '\n');
2022

2123
it('should remove uses of mirrors & '
2224
'insert calls to generated code by default.', () {
@@ -45,4 +47,10 @@ void allTests() {
4547
.rewrite(parseCompilationUnit(code));
4648
expect(output).toEqual(log_mirrors.code);
4749
});
50+
51+
it('should rewrite bootstrap.', () {
52+
var output = new Rewriter(bootstrapCode, codegen, writeStaticInit: true)
53+
.rewrite(parseCompilationUnit(bootstrapCode));
54+
expect(output).toEqual(bootstrap_expected.code);
55+
});
4856
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Tests that the reflection removal step:
2+
1. Comments out reflective `bootstrap.dart` import
3+
1. Adds `bootstrap_static.dart` import
4+
1. Adds the call to `initReflector`
5+
1. Handles bootstrap return values properly
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library angular2.test.transform.reflection_remover.reflection_remover_files;
2+
3+
// This file is intentionally formatted as a string to avoid having the
4+
// automatic transformer prettify it.
5+
//
6+
// This file represents transformed user code. Because this code will be
7+
// linked to output by a source map, we cannot change line numbers from the
8+
// original code and we therefore add our generated code on the same line as
9+
// those we are removing.
10+
11+
var code = """
12+
library web_foo;
13+
14+
import 'package:angular2/bootstrap_static.dart';import 'index.ng_deps.dart' as ngStaticInit0;
15+
16+
void main() async {
17+
var appRef = await bootstrapStatic(MyComponent, null, () { ngStaticInit0.initReflector(); });
18+
}
19+
""";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
library web_foo;
2+
3+
import 'package:angular2/bootstrap.dart';
4+
5+
void main() async {
6+
var appRef = await bootstrap(MyComponent);
7+
}

0 commit comments

Comments
 (0)