feat(transformers): makes the map of resolved identifiers configurable

Closes #7359
This commit is contained in:
vsavkin 2016-03-01 09:43:56 -08:00 committed by Victor Savkin
parent 59629a0801
commit 0bb10d6bb6
6 changed files with 60 additions and 34 deletions

View File

@ -11,6 +11,7 @@ const FORMAT_CODE_PARAM = 'format_code';
const REFLECT_PROPERTIES_AS_ATTRIBUTES = 'reflect_properties_as_attributes';
const PLATFORM_DIRECTIVES = 'platform_directives';
const PLATFORM_PIPES = 'platform_pipes';
const RESOLVED_IDENTIFIERS = 'resolved_identifiers';
const INIT_REFLECTOR_PARAM = 'init_reflector';
const INLINE_VIEWS_PARAM = 'inline_views';
const MIRROR_MODE_PARAM = 'mirror_mode';
@ -54,6 +55,9 @@ class TransformerOptions {
/// angular2/lib/src/common/pipes.dart#COMMON_PIPES
final List<String> platformPipes;
/// A map of identifier/asset pairs used when resolving identifiers.
final Map<String, String> resolvedIdentifiers;
/// Whether to format generated code.
/// Code that is only modified will never be formatted because doing so may
/// invalidate the source maps generated by `dart2js` and/or other tools.
@ -98,6 +102,7 @@ class TransformerOptions {
this.lazyTransformers,
this.platformDirectives,
this.platformPipes,
this.resolvedIdentifiers,
this.reflectPropertiesAsAttributes});
factory TransformerOptions(List<String> entryPoints,
@ -111,6 +116,7 @@ class TransformerOptions {
bool reflectPropertiesAsAttributes: false,
List<String> platformDirectives,
List<String> platformPipes,
Map<String, String> resolvedIdentifiers,
bool lazyTransformers: false,
bool formatCode: false}) {
var annotationMatcher = new AnnotationMatcher()
@ -125,6 +131,7 @@ class TransformerOptions {
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
resolvedIdentifiers: resolvedIdentifiers,
inlineViews: inlineViews,
lazyTransformers: lazyTransformers,
formatCode: formatCode);

View File

@ -14,6 +14,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
_readBool(config, REFLECT_PROPERTIES_AS_ATTRIBUTES, defaultValue: false);
var platformDirectives = _readStringList(config, PLATFORM_DIRECTIVES);
var platformPipes = _readStringList(config, PLATFORM_PIPES);
var resolvedIdentifiers = config[RESOLVED_IDENTIFIERS];
var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false);
String mirrorModeVal =
config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : '';
@ -38,6 +39,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
resolvedIdentifiers: resolvedIdentifiers,
inlineViews: _readBool(config, INLINE_VIEWS_PARAM, defaultValue: false),
lazyTransformers:
_readBool(config, LAZY_TRANSFORMERS, defaultValue: false),

View File

@ -29,10 +29,12 @@ Future<CompileDataResults> createCompileData(
AssetReader reader,
AssetId assetId,
List<String> platformDirectives,
List<String> platformPipes) async {
List<String> platformPipes,
Map<String, String> resolvedIdentifiers
) async {
return logElapsedAsync(() async {
final creator = await _CompileDataCreator.create(
reader, assetId, platformDirectives, platformPipes);
reader, assetId, platformDirectives, platformPipes, resolvedIdentifiers);
return creator != null ? creator.createCompileData() : null;
}, operationName: 'createCompileData', assetId: assetId);
}
@ -53,19 +55,20 @@ class _CompileDataCreator {
final NgMeta ngMeta;
final List<String> platformDirectives;
final List<String> platformPipes;
final Map<String, String> resolvedIdentifiers;
_CompileDataCreator(this.reader, this.entryPoint, this.ngMeta,
this.platformDirectives, this.platformPipes);
this.platformDirectives, this.platformPipes, this.resolvedIdentifiers);
static Future<_CompileDataCreator> create(AssetReader reader, AssetId assetId,
List<String> platformDirectives, List<String> platformPipes) async {
List<String> platformDirectives, List<String> platformPipes, Map<String, String> resolvedIdentifiers) async {
if (!(await reader.hasInput(assetId))) return null;
final json = await reader.readAsString(assetId);
if (json == null || json.isEmpty) return null;
final ngMeta = new NgMeta.fromJson(JSON.decode(json));
return new _CompileDataCreator(
reader, assetId, ngMeta, platformDirectives, platformPipes);
reader, assetId, ngMeta, platformDirectives, platformPipes, resolvedIdentifiers);
}
NgDepsModel get ngDeps => ngMeta.ngDeps;
@ -215,24 +218,8 @@ class _CompileDataCreator {
} else if (_isPrimitive(id.name)) {
return id;
// TODO: move the following if statements into transformer configuration
} else if (id.name == "Window") {
return new CompileIdentifierMetadata(name: "Window", moduleUrl: 'dart:html');
} else if (id.name == "Clock") {
return new CompileIdentifierMetadata(name: "Clock", moduleUrl: 'asset:quiver/time/clock.dart');
} else if (id.name == "Profiler") {
return new CompileIdentifierMetadata(name: "Profiler", moduleUrl: 'asset:perf_api/perf_api.dart');
} else if (id.name == "Campaign") {
return new CompileIdentifierMetadata(name: "Campaign", moduleUrl: 'unspecified');
} else if (id.name == "PreloadData") {
return new CompileIdentifierMetadata(name: "PreloadData", moduleUrl: 'unspecified');
} else if (id.name == "FiberMarket") {
return new CompileIdentifierMetadata(name: "FiberMarket", moduleUrl: 'unspecified');
} else if (resolvedIdentifiers != null && resolvedIdentifiers.containsKey(id.name)) {
return new CompileIdentifierMetadata(name: id.name, moduleUrl: resolvedIdentifiers[id.name]);
} else {
log.warning(

View File

@ -38,9 +38,11 @@ Future<Outputs> processTemplates(AssetReader reader, AssetId assetId,
{bool genChangeDetectionDebugInfo: false,
bool reflectPropertiesAsAttributes: false,
List<String> platformDirectives,
List<String> platformPipes}) async {
List<String> platformPipes,
Map<String, String> resolvedIdentifiers
}) async {
var viewDefResults = await createCompileData(
reader, assetId, platformDirectives, platformPipes);
reader, assetId, platformDirectives, platformPipes, resolvedIdentifiers);
if (viewDefResults == null) return null;
final compileTypeMetadatas = viewDefResults.ngMeta.identifiers.values;
if (compileTypeMetadatas.isNotEmpty) {

View File

@ -47,7 +47,9 @@ class TemplateCompiler extends Transformer implements LazyTransformer {
genChangeDetectionDebugInfo: options.genChangeDetectionDebugInfo,
reflectPropertiesAsAttributes: options.reflectPropertiesAsAttributes,
platformDirectives: options.platformDirectives,
platformPipes: options.platformPipes);
platformPipes: options.platformPipes,
resolvedIdentifiers: options.resolvedIdentifiers
);
var ngDepsCode = _emptyNgDepsContents;
if (outputs != null) {
if (outputs.ngDeps != null) {

View File

@ -83,12 +83,13 @@ void allTests() {
});
Future<String> process(AssetId assetId,
{List<String> platformDirectives, List<String> platformPipes}) {
{List<String> platformDirectives, List<String> platformPipes, Map<String,String> resolvedIdentifiers}) {
logger = new RecordingLogger();
return zone.exec(
() => processTemplates(reader, assetId,
platformDirectives: platformDirectives,
platformPipes: platformPipes),
platformPipes: platformPipes,
resolvedIdentifiers: resolvedIdentifiers),
log: logger);
}
@ -142,7 +143,7 @@ void allTests() {
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], []);
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
@ -165,7 +166,7 @@ void allTests() {
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], []);
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
@ -193,7 +194,7 @@ void allTests() {
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], []);
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
@ -219,7 +220,7 @@ void allTests() {
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], []);
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
@ -248,7 +249,7 @@ void allTests() {
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], []);
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
@ -587,6 +588,31 @@ void allTests() {
..toContain("import 'bar.dart'")
..toContain(barPipeMeta.name);
});
it('should fallback to the list of resolved identifiers.', () async {
barNgMeta.identifiers['Service2'] = new CompileTypeMetadata(name: 'Service2', moduleUrl: 'moduleUrl');
fooComponentMeta.template = new CompileTemplateMetadata(template: "import 'bar.dart';");
fooComponentMeta.providers = [new CompileProviderMetadata(token: new CompileIdentifierMetadata(name: 'Service1'), useClass:
new CompileTypeMetadata(name: 'Service2'))];
final viewAnnotation = new AnnotationModel()..name = 'View'..isView = true;
final reflectable = fooNgMeta.ngDeps.reflectables.first;
reflectable.annotations.add(viewAnnotation);
fooNgMeta.ngDeps.imports.add(new ImportModel()..uri = 'package:a/bar.dart');
updateReader();
final viewDefResults = await createCompileData(reader, fooAssetId, [], [], {"Service1": "someModuleUrl", "Service2": "someModuleUrl"});
final cmp = viewDefResults.viewDefinitions.values.first.component;
expect(cmp.providers.length).toEqual(1);
expect(cmp.providers[0].token.name).toEqual("Service1");
expect(cmp.providers[0].token.moduleUrl).toEqual("someModuleUrl");
expect(cmp.providers[0].useClass.name).toEqual("Service2");
expect(cmp.providers[0].useClass.moduleUrl).toEqual("moduleUrl");
});
}
String _generatedCode(Outputs outputs) {