Skip to content

Commit

Permalink
fix(bootstrap): fix expressions containing bootstrap (fixes #3309)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjbanov committed Jul 30, 2015
1 parent eee2146 commit 2909576
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
5 changes: 4 additions & 1 deletion modules/angular2/src/core/application_static.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import 'application_common.dart';
///
/// See [commonBootstrap] for detailed documentation.
Future<ApplicationRef> bootstrapStatic(Type appComponentType,
[List componentInjectableBindings]) {
[List componentInjectableBindings, void initReflector()]) {
if (initReflector != null) {
initReflector();
}
return commonBootstrap(appComponentType, componentInjectableBindings);
}
36 changes: 27 additions & 9 deletions modules/angular2/src/transform/reflection_remover/rewriter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,38 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
_rewriteBootstrapCallToStatic(MethodInvocation node) {
if (_rewriter._writeStaticInit) {
buf.write(_rewriter._code.substring(_currentIndex, node.offset));
_writeStaticReflectorInitOnce();

var args = node.argumentList.arguments;
int numArgs = node.argumentList.arguments.length;
if (numArgs < 1 || numArgs > 2) {
logger.warning('`bootstrap` does not support $numArgs arguments. Found bootstrap${node.argumentList}. Transform may not succeed.');
}

var reflectorInit = _setupAdded
? ''
: ', () { ${_getStaticReflectorInitBlock()} }';

// rewrite `bootstrap(...)` to `bootstrapStatic(...)`
buf.write('bootstrapStatic${node.argumentList}');
buf.write('bootstrapStatic(');
buf.write(args[0]);
if (numArgs == 1) {
if (reflectorInit.isNotEmpty) {
buf.write(', null');
}
} else {
buf.write(', ${args[1]}');
}
buf.write(reflectorInit);
buf.write(')');
} else {
// leave it as is
buf.write(_rewriter._code.substring(_currentIndex, node.end));
}
_currentIndex = node.end;
}

_writeStaticReflectorInitOnce() {
if (!_setupAdded) {
buf.write(_rewriter._codegen.codegenSetupReflectionCall());
_setupAdded = true;
}
String _getStaticReflectorInitBlock() {
return _rewriter._codegen.codegenSetupReflectionCall();
}

_rewriteReflectionCapabilitiesImport(ImportDirective node) {
Expand All @@ -162,8 +179,9 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
node = node.parent;
}
buf.write(_rewriter._code.substring(_currentIndex, node.offset));
if (_rewriter._writeStaticInit) {
_writeStaticReflectorInitOnce();
if (_rewriter._writeStaticInit && !_setupAdded) {
buf.write(_getStaticReflectorInitBlock());
_setupAdded = true;
}
switch (_rewriter._mirrorMode) {
case MirrorMode.debug:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import 'reflection_remover_files/expected/index.dart' as expected;
import 'debug_mirrors_files/expected/index.dart' as debug_mirrors;
import 'log_mirrors_files/expected/index.dart' as log_mirrors;
import 'verbose_files/expected/index.dart' as verbose_mirrors;
import 'bootstrap_files/expected/index.dart' as bootstrap_expected;
import '../common/read_file.dart';

main() => allTests();

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

it('should remove uses of mirrors & '
'insert calls to generated code by default.', () {
Expand Down Expand Up @@ -45,4 +47,10 @@ void allTests() {
.rewrite(parseCompilationUnit(code));
expect(output).toEqual(log_mirrors.code);
});

it('should rewrite bootstrap.', () {
var output = new Rewriter(bootstrapCode, codegen, writeStaticInit: true)
.rewrite(parseCompilationUnit(bootstrapCode));
expect(output).toEqual(bootstrap_expected.code);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Tests that the reflection removal step:
1. Comments out reflective `bootstrap.dart` import
1. Adds `bootstrap_static.dart` import
1. Adds the call to `initReflector`
1. Handles bootstrap return values properly
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library angular2.test.transform.reflection_remover.reflection_remover_files;

// This file is intentionally formatted as a string to avoid having the
// automatic transformer prettify it.
//
// This file represents transformed user code. Because this code will be
// linked to output by a source map, we cannot change line numbers from the
// original code and we therefore add our generated code on the same line as
// those we are removing.

var code = """
library web_foo;
import 'package:angular2/bootstrap_static.dart';import 'index.ng_deps.dart' as ngStaticInit0;
void main() async {
var appRef = await bootstrapStatic(MyComponent, null, () { ngStaticInit0.initReflector(); });
}
""";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library web_foo;

import 'package:angular2/bootstrap.dart';

void main() async {
var appRef = await bootstrap(MyComponent);
}

0 comments on commit 2909576

Please sign in to comment.