Skip to content

Commit 5583990

Browse files
committed
f114d6c fix(compiler): fix cross view references and providers with useValue.
1 parent 1144eb1 commit 5583990

File tree

15 files changed

+175
-34
lines changed

15 files changed

+175
-34
lines changed

BUILD_INFO

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Sun May 1 02:50:49 UTC 2016
2-
f5f0038e03d4003effb7d0f7b42b6e919cb2f317
1+
Sun May 1 03:07:25 UTC 2016
2+
f114d6c560a00d0d5d8a529c2b0f03a6a8696df4

lib/src/compiler/metadata_resolver.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import "package:angular2/src/core/reflection/reflection.dart" show reflector;
2828
import "package:angular2/src/core/di.dart" show Injectable, Inject, Optional;
2929
import "package:angular2/src/core/platform_directives_and_pipes.dart"
3030
show PLATFORM_DIRECTIVES, PLATFORM_PIPES;
31-
import "util.dart" show MODULE_SUFFIX, sanitizeIdentifier;
31+
import "util.dart"
32+
show MODULE_SUFFIX, sanitizeIdentifier, ValueTransformer, visitValue;
3233
import "assertions.dart" show assertArrayOfStrings;
3334
import "package:angular2/src/compiler/url_resolver.dart" show getUrlScheme;
3435
import "package:angular2/src/core/di/provider.dart" show Provider;
@@ -325,9 +326,7 @@ class CompileMetadataResolver {
325326
? this.getTypeMetadata(
326327
provider.useClass, staticTypeModuleUrl(provider.useClass))
327328
: null,
328-
useValue: isPresent(provider.useValue)
329-
? new cpl.CompileIdentifierMetadata(runtime: provider.useValue)
330-
: null,
329+
useValue: convertToCompileValue(provider.useValue),
331330
useFactory: isPresent(provider.useFactory)
332331
? this.getFactoryMetadata(
333332
provider.useFactory, staticTypeModuleUrl(provider.useFactory))
@@ -432,3 +431,19 @@ String calcTemplateBaseUrl(
432431
}
433432
return reflector.importUri(type);
434433
}
434+
435+
// Only fill CompileIdentifierMetadata.runtime if needed...
436+
dynamic convertToCompileValue(dynamic value) {
437+
return visitValue(value, new _CompileValueConverter(), null);
438+
}
439+
440+
class _CompileValueConverter extends ValueTransformer {
441+
dynamic visitOther(dynamic value, dynamic context) {
442+
if (isStaticType(value)) {
443+
return new cpl.CompileIdentifierMetadata(
444+
name: value["name"], moduleUrl: staticTypeModuleUrl(value));
445+
} else {
446+
return new cpl.CompileIdentifierMetadata(runtime: value);
447+
}
448+
}
449+
}

lib/src/compiler/output/dart_emitter.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ class _DartEmitterVisitor extends AbstractEmitterVisitor
372372

373373
void _visitIdentifier(CompileIdentifierMetadata value,
374374
List<o.Type> typeParams, EmitterVisitorContext ctx) {
375+
if (isBlank(value.name)) {
376+
throw new BaseException(
377+
'''Internal error: unknown identifier ${ value}''');
378+
}
375379
if (isPresent(value.moduleUrl) && value.moduleUrl != this._moduleUrl) {
376380
var prefix = this.importsWithPrefixes[value.moduleUrl];
377381
if (isBlank(prefix)) {

lib/src/compiler/output/js_emitter.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import "package:angular2/src/facade/lang.dart"
99
evalExpression,
1010
RegExpWrapper,
1111
StringWrapper;
12+
import "package:angular2/src/facade/exceptions.dart" show BaseException;
1213
import "abstract_emitter.dart" show OutputEmitter, EmitterVisitorContext;
1314
import "abstract_js_emitter.dart" show AbstractJsEmitterVisitor;
1415
import "path_util.dart" show getImportModulePath, ImportEnv;
@@ -38,6 +39,10 @@ class JsEmitterVisitor extends AbstractJsEmitterVisitor {
3839
/* super call moved to initializer */;
3940
}
4041
dynamic visitExternalExpr(o.ExternalExpr ast, EmitterVisitorContext ctx) {
42+
if (isBlank(ast.value.name)) {
43+
throw new BaseException(
44+
'''Internal error: unknown identifier ${ ast . value}''');
45+
}
4146
if (isPresent(ast.value.moduleUrl) &&
4247
ast.value.moduleUrl != this._moduleUrl) {
4348
var prefix = this.importsWithPrefixes[ast.value.moduleUrl];

lib/src/compiler/output/ts_emitter.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor
330330

331331
void _visitIdentifier(CompileIdentifierMetadata value,
332332
List<o.Type> typeParams, EmitterVisitorContext ctx) {
333+
if (isBlank(value.name)) {
334+
throw new BaseException(
335+
'''Internal error: unknown identifier ${ value}''');
336+
}
333337
if (isPresent(value.moduleUrl) && value.moduleUrl != this._moduleUrl) {
334338
var prefix = this.importsWithPrefixes[value.moduleUrl];
335339
if (isBlank(prefix)) {

lib/src/compiler/util.dart

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
library angular2.src.compiler.util;
22

33
import "package:angular2/src/facade/lang.dart"
4-
show IS_DART, StringWrapper, Math, isBlank;
4+
show
5+
IS_DART,
6+
StringWrapper,
7+
Math,
8+
isBlank,
9+
isArray,
10+
isStrictStringMap,
11+
isPrimitive;
12+
import "package:angular2/src/facade/collection.dart" show StringMapWrapper;
513

614
var MODULE_SUFFIX = IS_DART ? ".dart" : "";
715
var CAMEL_CASE_REGEXP = new RegExp(r'([A-Z])');
@@ -30,3 +38,44 @@ List<String> splitAtColon(String input, List<String> defaultValues) {
3038
String sanitizeIdentifier(String name) {
3139
return StringWrapper.replaceAll(name, new RegExp(r'\W'), "_");
3240
}
41+
42+
dynamic visitValue(dynamic value, ValueVisitor visitor, dynamic context) {
43+
if (isArray(value)) {
44+
return visitor.visitArray((value as List<dynamic>), context);
45+
} else if (isStrictStringMap(value)) {
46+
return visitor.visitStringMap((value as Map<String, dynamic>), context);
47+
} else if (isBlank(value) || isPrimitive(value)) {
48+
return visitor.visitPrimitive(value, context);
49+
} else {
50+
return visitor.visitOther(value, context);
51+
}
52+
}
53+
54+
abstract class ValueVisitor {
55+
dynamic visitArray(List<dynamic> arr, dynamic context);
56+
dynamic visitStringMap(Map<String, dynamic> map, dynamic context);
57+
dynamic visitPrimitive(dynamic value, dynamic context);
58+
dynamic visitOther(dynamic value, dynamic context);
59+
}
60+
61+
class ValueTransformer implements ValueVisitor {
62+
dynamic visitArray(List<dynamic> arr, dynamic context) {
63+
return arr.map((value) => visitValue(value, this, context)).toList();
64+
}
65+
66+
dynamic visitStringMap(Map<String, dynamic> map, dynamic context) {
67+
var result = {};
68+
StringMapWrapper.forEach(map, (value, key) {
69+
result[key] = visitValue(value, this, context);
70+
});
71+
return result;
72+
}
73+
74+
dynamic visitPrimitive(dynamic value, dynamic context) {
75+
return value;
76+
}
77+
78+
dynamic visitOther(dynamic value, dynamic context) {
79+
return value;
80+
}
81+
}

lib/src/compiler/view_compiler/compile_element.dart

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library angular2.src.compiler.view_compiler.compile_element;
22

3+
import "package:angular2/src/facade/exceptions.dart" show BaseException;
34
import "../output/output_ast.dart" as o;
45
import "../identifiers.dart" show Identifiers, identifierToken;
56
import "constants.dart" show InjectMethodVars;
@@ -27,6 +28,7 @@ import "util.dart"
2728
import "compile_query.dart"
2829
show CompileQuery, createQueryList, addQueryToTokenMap;
2930
import "compile_method.dart" show CompileMethod;
31+
import "../util.dart" show ValueTransformer, visitValue;
3032

3133
class CompileNode {
3234
CompileElement parent;
@@ -104,6 +106,7 @@ class CompileElement extends CompileNode {
104106
_createAppElement() {
105107
var fieldName = '''_appEl_${ this . nodeIndex}''';
106108
var parentNodeIndex = this.isRootElement() ? null : this.parent.nodeIndex;
109+
// private is fine here as no child view will reference an AppElement
107110
this.view.fields.add(new o.ClassField(fieldName,
108111
o.importType(Identifiers.AppElement), [o.StmtModifier.Private]));
109112
var statement = o.THIS_EXPR
@@ -187,13 +190,7 @@ class CompileElement extends CompileNode {
187190
.importExpr(provider.useClass)
188191
.instantiate(depsExpr, o.importType(provider.useClass));
189192
} else {
190-
if (provider.useValue is CompileIdentifierMetadata) {
191-
return o.importExpr(provider.useValue);
192-
} else if (provider.useValue is o.Expression) {
193-
return provider.useValue;
194-
} else {
195-
return o.literal(provider.useValue);
196-
}
193+
return _convertValueToOutputAst(provider.useValue);
197194
}
198195
}).toList();
199196
var propName =
@@ -468,13 +465,12 @@ o.Expression createProviderProperty(
468465
type = o.DYNAMIC_TYPE;
469466
}
470467
if (isEager) {
471-
view.fields.add(new o.ClassField(propName, type, [o.StmtModifier.Private]));
468+
view.fields.add(new o.ClassField(propName, type));
472469
view.createMethod.addStmt(
473470
o.THIS_EXPR.prop(propName).set(resolvedProviderValueExpr).toStmt());
474471
} else {
475472
var internalField = '''_${ propName}''';
476-
view.fields
477-
.add(new o.ClassField(internalField, type, [o.StmtModifier.Private]));
473+
view.fields.add(new o.ClassField(internalField, type));
478474
var getter = new CompileMethod(view);
479475
getter.resetDebugInfo(compileElement.nodeIndex, compileElement.sourceAst);
480476
// Note: Equals is important for JS so that it also checks the undefined case!
@@ -494,3 +490,37 @@ class _QueryWithRead {
494490
this.read = isPresent(query.meta.read) ? query.meta.read : match;
495491
}
496492
}
493+
494+
o.Expression _convertValueToOutputAst(dynamic value) {
495+
return visitValue(value, new _ValueOutputAstTransformer(), null);
496+
}
497+
498+
class _ValueOutputAstTransformer extends ValueTransformer {
499+
o.Expression visitArray(List<dynamic> arr, dynamic context) {
500+
return o.literalArr(
501+
arr.map((value) => visitValue(value, this, context)).toList());
502+
}
503+
504+
o.Expression visitStringMap(Map<String, dynamic> map, dynamic context) {
505+
var entries = [];
506+
StringMapWrapper.forEach(map, (value, key) {
507+
entries.add([key, visitValue(value, this, context)]);
508+
});
509+
return o.literalMap(entries);
510+
}
511+
512+
o.Expression visitPrimitive(dynamic value, dynamic context) {
513+
return o.literal(value);
514+
}
515+
516+
o.Expression visitOther(dynamic value, dynamic context) {
517+
if (value is CompileIdentifierMetadata) {
518+
return o.importExpr(value);
519+
} else if (value is o.Expression) {
520+
return value;
521+
} else {
522+
throw new BaseException(
523+
'''Illegal state: Don\'t now how to compile value ${ value}''');
524+
}
525+
}
526+
}

lib/src/compiler/view_compiler/compile_pipe.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class CompilePipe {
3838
}
3939
return injectFromViewParentInjector(diDep.token, false);
4040
}).toList();
41-
this.view.fields.add(new o.ClassField(this.instance.name,
42-
o.importType(this.meta.type), [o.StmtModifier.Private]));
41+
this.view.fields.add(
42+
new o.ClassField(this.instance.name, o.importType(this.meta.type)));
4343
this.view.createMethod.resetDebugInfo(null, null);
4444
this.view.createMethod.addStmt(o.THIS_EXPR
4545
.prop(this.instance.name)

lib/src/compiler/view_compiler/compile_query.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ o.Expression createQueryList(
109109
o.Expression directiveInstance,
110110
String propertyName,
111111
CompileView compileView) {
112-
compileView.fields.add(new o.ClassField(propertyName,
113-
o.importType(Identifiers.QueryList), [o.StmtModifier.Private]));
112+
compileView.fields
113+
.add(new o.ClassField(propertyName, o.importType(Identifiers.QueryList)));
114114
var expr = o.THIS_EXPR.prop(propertyName);
115115
compileView.createMethod.addStmt(o.THIS_EXPR
116116
.prop(propertyName)

lib/src/compiler/view_compiler/event_binder.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class CompileEventListener {
9292
markPathToRootStart.callMethod("markPathToRootAsCheckOnce", []).toStmt()
9393
]))..addAll(this._method.finish())))
9494
..addAll([new o.ReturnStatement(resultExpr)]));
95+
// private is fine here as no child view will reference the event handler...
9596
this.compileElement.view.eventHandlerMethods.add(new o.ClassMethod(
9697
this._methodName,
9798
[this._eventParam],
@@ -123,6 +124,7 @@ class CompileEventListener {
123124
var disposable = o.variable(
124125
'''disposable_${ this . compileElement . view . disposables . length}''');
125126
this.compileElement.view.disposables.add(disposable);
127+
// private is fine here as no child view will reference the event handler...
126128
this.compileElement.view.createMethod.addStmt(disposable
127129
.set(listenExpr)
128130
.toDeclStmt(o.FUNCTION_TYPE, [o.StmtModifier.Private]));

0 commit comments

Comments
 (0)