feat(dart/transform): Add debug transform parameters

Add two transform parameters to aid in debugging the transformer
- `mirror_mode`, with values {`debug`, `none`, and `verbose`}
- `init_reflector`, with values {`true`, `false`}

`mirror_mode`:
- `debug`: Allow reflective access, but log a message if it is used
- `none`: Remove reflective access, `throw` if it is used. Default value
- `verbose`: Allow reflective access, log a stack trace if it is used

`init_reflector`: Whether to generate calls to our generated
`initReflector` code.

These will be useful to reveal areas where the transformer is not generating
appropriate code and to quickly see where reflective accesses occur.

When the pub mode is `transform_dynamic`, we run in MirrorMode.debug
with `init_reflector = false`. This is used for testing purposes.
This commit is contained in:
Tim Blasi
2015-04-17 11:12:35 -07:00
parent 5b4eb0c6d7
commit 77b31ab42f
22 changed files with 343 additions and 22 deletions

View File

@ -1,21 +1,46 @@
library angular2.test.transform.reflection_remover;
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/mirror_mode.dart';
import 'package:angular2/src/transform/reflection_remover/codegen.dart';
import 'package:angular2/src/transform/reflection_remover/rewriter.dart';
import 'package:guinness/guinness.dart';
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 '../common/read_file.dart';
void allTests() {
var codegen = new Codegen('web/index.dart', ['web/index.ng_deps.dart']);
var code = readFile('reflection_remover/index.dart');
it('should remove uses of mirrors & insert calls to generated code.', () {
var code =
readFile('reflection_remover/reflection_remover_files/index.dart');
it('should remove uses of mirrors & '
'insert calls to generated code by default.', () {
var output =
new Rewriter(code, codegen).rewrite(parseCompilationUnit(code));
expect(output).toEqual(expected.code);
});
it('should replace uses of mirrors with the debug implementation & '
'insert calls to generated code in `MirrorMode.debug`.', () {
var output = new Rewriter(code, codegen, mirrorMode: MirrorMode.debug)
.rewrite(parseCompilationUnit(code));
expect(output).toEqual(debug_mirrors.code);
});
it('should replace uses of mirrors with the verbose implementation '
'in `MirrorMode.verbose`.', () {
var output = new Rewriter(code, codegen, mirrorMode: MirrorMode.verbose)
.rewrite(parseCompilationUnit(code));
expect(output).toEqual(verbose_mirrors.code);
});
it('should not initialize the reflector when `writeStaticInit` is `false`.',
() {
var output = new Rewriter(code, codegen, writeStaticInit: false)
.rewrite(parseCompilationUnit(code));
expect(output).toEqual(log_mirrors.code);
});
}

View File

@ -0,0 +1,7 @@
Tests that the reflection removal step:
1. Comments out the import of reflection_capabilities.dart
2. Comments out the instantiation of `ReflectionCapabilities`
3. Adds the appropriate import.
4. Adds the call to `initReflector`
5. Does not change line numbers in the source.
6. Makes minimal changes to source offsets.

View File

@ -0,0 +1,22 @@
library angular2.test.transform.debug_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/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/debug_reflection_capabilities.dart';import 'index.ng_deps.dart' as ngStaticInit0;
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();ngStaticInit0.initReflector(reflector);
bootstrap(MyComponent);
}
""";

View File

@ -0,0 +1,10 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -0,0 +1,7 @@
Tests that the reflection removal step:
1. Comments out the import of reflection_capabilities.dart
2. Comments out the instantiation of `ReflectionCapabilities`
3. Adds the appropriate import.
4. Adds the call to `initReflector`
5. Does not change line numbers in the source.
6. Makes minimal changes to source offsets.

View File

@ -0,0 +1,22 @@
library angular2.test.transform.debug_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/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/debug_reflection_capabilities.dart';import 'index.ng_deps.dart' as ngStaticInit0;
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();ngStaticInit0.initReflector(reflector);
bootstrap(MyComponent);
}
""";

View File

@ -0,0 +1,10 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -0,0 +1,10 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -0,0 +1,7 @@
Tests that the reflection removal step:
1. Comments out the import of reflection_capabilities.dart
2. Comments out the instantiation of `ReflectionCapabilities`
3. Adds the appropriate import.
4. Adds the call to `initReflector`
5. Does not change line numbers in the source.
6. Makes minimal changes to source offsets.

View File

@ -0,0 +1,22 @@
library angular2.test.transform.log_mirrors_files.expected;
// 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/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
/*import 'package:angular2/src/reflection/reflection_capabilities.dart';*/
void main() {
/*reflector.reflectionCapabilities = new ReflectionCapabilities();*/
bootstrap(MyComponent);
}
""";

View File

@ -0,0 +1,10 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -0,0 +1,22 @@
library angular2.test.transform.debug_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/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/debug_reflection_capabilities.dart';import 'index.ng_deps.dart' as ngStaticInit0;
void main() {
reflector.reflectionCapabilities = new ReflectionCapabilities(verbose: true);ngStaticInit0.initReflector(reflector);
bootstrap(MyComponent);
}
""";