Skip to content

Commit c8c6a53

Browse files
committed
d964888 fix(metadata): Do not attach module names to metadata.
1 parent 62edc4a commit c8c6a53

File tree

3 files changed

+82
-93
lines changed

3 files changed

+82
-93
lines changed

BUILD_INFO

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Thu Apr 28 01:31:27 UTC 2016
2-
35cd0ded226cde87599080cc329d8d85e33c8c35
1+
Thu Apr 28 02:13:18 UTC 2016
2+
d9648887b8b50e54083f47704b5b01323f73ec8b

lib/src/compiler/static_reflector.dart

+17-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
library angular2.src.compiler.static_reflector;
22

3-
import "package:angular2/src/facade/collection.dart"
4-
show ListWrapper, StringMapWrapper;
3+
import "package:angular2/src/facade/collection.dart" show StringMapWrapper;
54
import "package:angular2/src/facade/lang.dart"
6-
show isArray, isBlank, isNumber, isPresent, isPrimitive, isString, Type;
5+
show isArray, isPresent, isPrimitive;
76
import "package:angular2/src/core/metadata.dart"
87
show
98
AttributeMetadata,
@@ -32,13 +31,18 @@ import "package:angular2/src/core/reflection/reflector_reader.dart"
3231
*/
3332
abstract class StaticReflectorHost {
3433
/**
35-
* Return a ModuleMetadata for the give module.
34+
* Return a ModuleMetadata for the given module.
3635
*
3736
*
38-
* module import of an import statement.
3937
*
4038
*/
4139
Map<String, dynamic> getMetadataFor(String moduleId);
40+
/**
41+
* Resolve a module from an import statement form to an absolute path.
42+
*
43+
*
44+
*/
45+
String resolveModule(String moduleName, [String containingFile]);
4246
}
4347

4448
/**
@@ -71,7 +75,7 @@ class StaticReflector implements ReflectorReader {
7175
}
7276

7377
/**
74-
* getStatictype produces a Type whose metadata is known but whose implementation is not loaded.
78+
* getStaticType produces a Type whose metadata is known but whose implementation is not loaded.
7579
* All types passed to the StaticResolver should be pseudo-types returned by this method.
7680
*
7781
*
@@ -147,7 +151,7 @@ class StaticReflector implements ReflectorReader {
147151
var conversionMap = new Map<StaticType,
148152
dynamic /* (moduleContext: string, expression: any) => any */ >();
149153
dynamic initializeConversionMap() {
150-
var core_metadata = "angular2/src/core/metadata";
154+
var core_metadata = this.host.resolveModule("angular2/src/core/metadata");
151155
var conversionMap = this.conversionMap;
152156
conversionMap[this.getStaticType(core_metadata, "Directive")] =
153157
(moduleContext, expression) {
@@ -281,8 +285,7 @@ class StaticReflector implements ReflectorReader {
281285
if (isMetadataSymbolicCallExpression(expression)) {
282286
var target = expression["expression"];
283287
if (isMetadataSymbolicReferenceExpression(target)) {
284-
var moduleId =
285-
this.normalizeModuleName(moduleContext, target["module"]);
288+
var moduleId = this.host.resolveModule(target["module"], moduleContext);
286289
return this.getStaticType(moduleId, target["name"]);
287290
}
288291
}
@@ -438,8 +441,8 @@ class StaticReflector implements ReflectorReader {
438441
return selectTarget[member];
439442
return null;
440443
case "reference":
441-
var referenceModuleName = _this.normalizeModuleName(
442-
moduleContext, expression["module"]);
444+
var referenceModuleName =
445+
_this.host.resolveModule(expression["module"], moduleContext);
443446
var referenceModule =
444447
_this.getModuleMetadata(referenceModuleName);
445448
var referenceValue =
@@ -466,6 +469,9 @@ class StaticReflector implements ReflectorReader {
466469
return simplify(value);
467470
}
468471

472+
/**
473+
*
474+
*/
469475
Map<String, dynamic> getModuleMetadata(String module) {
470476
var moduleMetadata = this.metadataCache[module];
471477
if (!isPresent(moduleMetadata)) {
@@ -490,13 +496,6 @@ class StaticReflector implements ReflectorReader {
490496
}
491497
return result;
492498
}
493-
494-
String normalizeModuleName(String from, String to) {
495-
if (to.startsWith(".")) {
496-
return pathTo(from, to);
497-
}
498-
return to;
499-
}
500499
}
501500

502501
bool isMetadataSymbolicCallExpression(dynamic expression) {
@@ -516,35 +515,3 @@ bool isClassMetadata(dynamic expression) {
516515
!isArray(expression) &&
517516
expression["___symbolic"] == "class";
518517
}
519-
520-
List<String> splitPath(String path) {
521-
return path.split(new RegExp(r'\/|\\'));
522-
}
523-
524-
String resolvePath(List<String> pathParts) {
525-
var result = [];
526-
ListWrapper.forEachWithIndex(pathParts, (part, index) {
527-
switch (part) {
528-
case "":
529-
case ".":
530-
if (index > 0) return;
531-
break;
532-
case "..":
533-
if (index > 0 && result.length != 0) result.removeLast();
534-
return;
535-
}
536-
result.add(part);
537-
});
538-
return result.join("/");
539-
}
540-
541-
String pathTo(String from, String to) {
542-
var result = to;
543-
if (to.startsWith(".")) {
544-
var fromParts = splitPath(from);
545-
fromParts.removeLast();
546-
var toParts = splitPath(to);
547-
result = resolvePath((new List.from(fromParts)..addAll(toParts)));
548-
}
549-
return result;
550-
}

test/compiler/static_reflector_spec.dart

+63-41
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
library angular2.test.compiler.static_reflector_spec;
22

3-
import "package:angular2/testing_internal.dart"
4-
show
5-
ddescribe,
6-
describe,
7-
xdescribe,
8-
it,
9-
iit,
10-
xit,
11-
expect,
12-
beforeEach,
13-
afterEach,
14-
AsyncTestCompleter,
15-
inject,
16-
beforeEachProviders;
3+
import "package:angular2/testing_internal.dart" show describe, it, expect;
4+
import "package:angular2/src/facade/collection.dart" show ListWrapper;
175
import "package:angular2/src/compiler/static_reflector.dart"
186
show StaticReflector, StaticReflectorHost;
197

208
main() {
21-
describe("StaticRefelector", () {
9+
describe("StaticReflector", () {
2210
it("should get annotations for NgFor", () {
2311
var host = new MockReflectorHost();
2412
var reflector = new StaticReflector(host);
2513
var NgFor = reflector.getStaticType(
26-
"angular2/src/common/directives/ng_for", "NgFor");
14+
host.resolveModule("angular2/src/common/directives/ng_for"), "NgFor");
2715
var annotations = reflector.annotations(NgFor);
2816
expect(annotations.length).toEqual(1);
2917
var annotation = annotations[0];
@@ -35,16 +23,20 @@ main() {
3523
var host = new MockReflectorHost();
3624
var reflector = new StaticReflector(host);
3725
var NgFor = reflector.getStaticType(
38-
"angular2/src/common/directives/ng_for", "NgFor");
26+
host.resolveModule("angular2/src/common/directives/ng_for"), "NgFor");
3927
var ViewContainerRef = reflector.getStaticType(
40-
"angular2/src/core/linker/view_container_ref", "ViewContainerRef");
28+
host.resolveModule("angular2/src/core/linker/view_container_ref"),
29+
"ViewContainerRef");
4130
var TemplateRef = reflector.getStaticType(
42-
"angular2/src/core/linker/template_ref", "TemplateRef");
31+
host.resolveModule("angular2/src/core/linker/template_ref"),
32+
"TemplateRef");
4333
var IterableDiffers = reflector.getStaticType(
44-
"angular2/src/core/change_detection/differs/iterable_differs",
34+
host.resolveModule(
35+
"angular2/src/core/change_detection/differs/iterable_differs"),
4536
"IterableDiffers");
4637
var ChangeDetectorRef = reflector.getStaticType(
47-
"angular2/src/core/change_detection/change_detector_ref",
38+
host.resolveModule(
39+
"angular2/src/core/change_detection/change_detector_ref"),
4840
"ChangeDetectorRef");
4941
var parameters = reflector.parameters(NgFor);
5042
expect(parameters).toEqual(
@@ -54,7 +46,7 @@ main() {
5446
var host = new MockReflectorHost();
5547
var reflector = new StaticReflector(host);
5648
var HeroDetailComponent = reflector.getStaticType(
57-
"./app/hero-detail.component", "HeroDetailComponent");
49+
"/src/app/hero-detail.component.ts", "HeroDetailComponent");
5850
var annotations = reflector.annotations(HeroDetailComponent);
5951
expect(annotations.length).toEqual(1);
6052
var annotation = annotations[0];
@@ -64,31 +56,31 @@ main() {
6456
var host = new MockReflectorHost();
6557
var reflector = new StaticReflector(host);
6658
var UnknownClass =
67-
reflector.getStaticType("./app/app.component", "UnknownClass");
59+
reflector.getStaticType("/src/app/app.component.ts", "UnknownClass");
6860
var annotations = reflector.annotations(UnknownClass);
6961
expect(annotations).toEqual([]);
7062
});
7163
it("should get propMetadata for HeroDetailComponent", () {
7264
var host = new MockReflectorHost();
7365
var reflector = new StaticReflector(host);
7466
var HeroDetailComponent = reflector.getStaticType(
75-
"./app/hero-detail.component", "HeroDetailComponent");
67+
"/src/app/hero-detail.component.ts", "HeroDetailComponent");
7668
var props = reflector.propMetadata(HeroDetailComponent);
7769
expect(props["hero"]).toBeTruthy();
7870
});
7971
it("should get an empty object from propMetadata for an unknown class", () {
8072
var host = new MockReflectorHost();
8173
var reflector = new StaticReflector(host);
8274
var UnknownClass =
83-
reflector.getStaticType("./app/app.component", "UnknownClass");
75+
reflector.getStaticType("/src/app/app.component.ts", "UnknownClass");
8476
var properties = reflector.propMetadata(UnknownClass);
8577
expect(properties).toEqual({});
8678
});
8779
it("should get empty parameters list for an unknown class ", () {
8880
var host = new MockReflectorHost();
8981
var reflector = new StaticReflector(host);
9082
var UnknownClass =
91-
reflector.getStaticType("./app/app.component", "UnknownClass");
83+
reflector.getStaticType("/src/app/app.component.ts", "UnknownClass");
9284
var parameters = reflector.parameters(UnknownClass);
9385
expect(parameters).toEqual([]);
9486
});
@@ -583,7 +575,7 @@ main() {
583575
"", ({"___symbolic": "pre", "operator": "!", "operand": false})))
584576
.toBe(!false);
585577
});
586-
it("should simpify an array index", () {
578+
it("should simplify an array index", () {
587579
var host = new MockReflectorHost();
588580
var reflector = new StaticReflector(host);
589581
expect(reflector.simplify(
@@ -609,7 +601,7 @@ main() {
609601
var host = new MockReflectorHost();
610602
var reflector = new StaticReflector(host);
611603
expect(reflector.simplify(
612-
"./cases",
604+
"/src/cases",
613605
({
614606
"___symbolic": "reference",
615607
"module": "./extern",
@@ -621,11 +613,46 @@ main() {
621613
}
622614

623615
class MockReflectorHost implements StaticReflectorHost {
616+
String resolveModule(String moduleName, [String containingFile]) {
617+
List<String> splitPath(String path) {
618+
return path.split(new RegExp(r'\/|\\'));
619+
}
620+
String resolvePath(List<String> pathParts) {
621+
var result = [];
622+
ListWrapper.forEachWithIndex(pathParts, (part, index) {
623+
switch (part) {
624+
case "":
625+
case ".":
626+
if (index > 0) return;
627+
break;
628+
case "..":
629+
if (index > 0 && result.length != 0) result.removeLast();
630+
return;
631+
}
632+
result.add(part);
633+
});
634+
return result.join("/");
635+
}
636+
String pathTo(String from, String to) {
637+
var result = to;
638+
if (to.startsWith(".")) {
639+
var fromParts = splitPath(from);
640+
fromParts.removeLast();
641+
var toParts = splitPath(to);
642+
result = resolvePath((new List.from(fromParts)..addAll(toParts)));
643+
}
644+
return result;
645+
}
646+
if (identical(moduleName.indexOf("."), 0)) {
647+
return pathTo(containingFile, moduleName) + ".d.ts";
648+
}
649+
return "/tmp/" + moduleName + ".d.ts";
650+
}
651+
624652
dynamic getMetadataFor(String moduleId) {
625653
return {
626-
"angular2/src/common/directives/ng_for": {
654+
"/tmp/angular2/src/common/directives/ng_for.d.ts": {
627655
"___symbolic": "module",
628-
"module": "./ng_for",
629656
"metadata": {
630657
"NgFor": {
631658
"___symbolic": "class",
@@ -679,33 +706,29 @@ class MockReflectorHost implements StaticReflectorHost {
679706
}
680707
}
681708
},
682-
"angular2/src/core/linker/view_container_ref": {
683-
"module": "./view_container_ref",
709+
"/tmp/angular2/src/core/linker/view_container_ref.d.ts": {
684710
"metadata": {
685711
"ViewContainerRef": {"___symbolic": "class"}
686712
}
687713
},
688-
"angular2/src/core/linker/template_ref": {
714+
"/tmp/angular2/src/core/linker/template_ref.d.ts": {
689715
"module": "./template_ref",
690716
"metadata": {
691717
"TemplateRef": {"___symbolic": "class"}
692718
}
693719
},
694-
"angular2/src/core/change_detection/differs/iterable_differs": {
695-
"module": "./iterable_differs",
720+
"/tmp/angular2/src/core/change_detection/differs/iterable_differs.d.ts": {
696721
"metadata": {
697722
"IterableDiffers": {"___symbolic": "class"}
698723
}
699724
},
700-
"angular2/src/core/change_detection/change_detector_ref": {
701-
"module": "./change_detector_ref",
725+
"/tmp/angular2/src/core/change_detection/change_detector_ref.d.ts": {
702726
"metadata": {
703727
"ChangeDetectorRef": {"___symbolic": "class"}
704728
}
705729
},
706-
"./app/hero-detail.component": {
730+
"/src/app/hero-detail.component.ts": {
707731
"___symbolic": "module",
708-
"module": "./hero-detail.component",
709732
"metadata": {
710733
"HeroDetailComponent": {
711734
"___symbolic": "class",
@@ -746,9 +769,8 @@ class MockReflectorHost implements StaticReflectorHost {
746769
}
747770
}
748771
},
749-
"./extern": {
772+
"/src/extern.d.ts": {
750773
"___symbolic": "module",
751-
"module": "./extern",
752774
"metadata": {"s": "s"}
753775
}
754776
}[moduleId];

0 commit comments

Comments
 (0)