fix(bootstrap): fix expressions containing bootstrap (fixes #3309)

This commit is contained in:
Yegor Jbanov 2015-07-30 08:47:23 -07:00 committed by yjbanov
parent eee2146735
commit 29095766e6
6 changed files with 70 additions and 10 deletions

View File

@ -7,6 +7,9 @@ import 'application_common.dart';
/// ///
/// See [commonBootstrap] for detailed documentation. /// See [commonBootstrap] for detailed documentation.
Future<ApplicationRef> bootstrapStatic(Type appComponentType, Future<ApplicationRef> bootstrapStatic(Type appComponentType,
[List componentInjectableBindings]) { [List componentInjectableBindings, void initReflector()]) {
if (initReflector != null) {
initReflector();
}
return commonBootstrap(appComponentType, componentInjectableBindings); return commonBootstrap(appComponentType, componentInjectableBindings);
} }

View File

@ -124,9 +124,29 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
_rewriteBootstrapCallToStatic(MethodInvocation node) { _rewriteBootstrapCallToStatic(MethodInvocation node) {
if (_rewriter._writeStaticInit) { if (_rewriter._writeStaticInit) {
buf.write(_rewriter._code.substring(_currentIndex, node.offset)); 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(...)` // 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 { } else {
// leave it as is // leave it as is
buf.write(_rewriter._code.substring(_currentIndex, node.end)); buf.write(_rewriter._code.substring(_currentIndex, node.end));
@ -134,11 +154,8 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
_currentIndex = node.end; _currentIndex = node.end;
} }
_writeStaticReflectorInitOnce() { String _getStaticReflectorInitBlock() {
if (!_setupAdded) { return _rewriter._codegen.codegenSetupReflectionCall();
buf.write(_rewriter._codegen.codegenSetupReflectionCall());
_setupAdded = true;
}
} }
_rewriteReflectionCapabilitiesImport(ImportDirective node) { _rewriteReflectionCapabilitiesImport(ImportDirective node) {
@ -162,8 +179,9 @@ class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
node = node.parent; node = node.parent;
} }
buf.write(_rewriter._code.substring(_currentIndex, node.offset)); buf.write(_rewriter._code.substring(_currentIndex, node.offset));
if (_rewriter._writeStaticInit) { if (_rewriter._writeStaticInit && !_setupAdded) {
_writeStaticReflectorInitOnce(); buf.write(_getStaticReflectorInitBlock());
_setupAdded = true;
} }
switch (_rewriter._mirrorMode) { switch (_rewriter._mirrorMode) {
case MirrorMode.debug: case MirrorMode.debug:

View File

@ -10,6 +10,7 @@ import 'reflection_remover_files/expected/index.dart' as expected;
import 'debug_mirrors_files/expected/index.dart' as debug_mirrors; import 'debug_mirrors_files/expected/index.dart' as debug_mirrors;
import 'log_mirrors_files/expected/index.dart' as log_mirrors; import 'log_mirrors_files/expected/index.dart' as log_mirrors;
import 'verbose_files/expected/index.dart' as verbose_mirrors; import 'verbose_files/expected/index.dart' as verbose_mirrors;
import 'bootstrap_files/expected/index.dart' as bootstrap_expected;
import '../common/read_file.dart'; import '../common/read_file.dart';
main() => allTests(); main() => allTests();
@ -17,6 +18,7 @@ main() => allTests();
void allTests() { void allTests() {
var codegen = new Codegen('web/index.dart', ['web/index.ng_deps.dart']); var codegen = new Codegen('web/index.dart', ['web/index.ng_deps.dart']);
var code = readFile('reflection_remover/index.dart').replaceAll('\r\n', '\n'); 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 & ' it('should remove uses of mirrors & '
'insert calls to generated code by default.', () { 'insert calls to generated code by default.', () {
@ -45,4 +47,10 @@ void allTests() {
.rewrite(parseCompilationUnit(code)); .rewrite(parseCompilationUnit(code));
expect(output).toEqual(log_mirrors.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);
});
} }

View File

@ -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

View File

@ -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(); });
}
""";

View File

@ -0,0 +1,7 @@
library web_foo;
import 'package:angular2/bootstrap.dart';
void main() async {
var appRef = await bootstrap(MyComponent);
}