Skip to content

Commit 15f6b27

Browse files
tboschmprobst
authored andcommitted
refactor(compiler): support referenced OpaqueTokens correctly in offline compiler.
1 parent 176e559 commit 15f6b27

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

modules/angular2/src/compiler/static_reflector.ts

+19-33
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
isStringMap,
88
FunctionWrapper
99
} from 'angular2/src/facade/lang';
10-
import {BaseException} from 'angular2/src/facade/exceptions';
1110
import {
1211
AttributeMetadata,
1312
DirectiveMetadata,
@@ -35,11 +34,6 @@ import {
3534
SkipSelfMetadata,
3635
InjectMetadata,
3736
} from "angular2/src/core/di/metadata";
38-
import {OpaqueToken} from 'angular2/src/core/di/opaque_token';
39-
40-
export class ModuleContext {
41-
constructor(public moduleId: string, public filePath: string) {}
42-
}
4337

4438
/**
4539
* The host of the static resolver is expected to be able to provide module metadata in the form of
@@ -71,7 +65,7 @@ export interface StaticReflectorHost {
7165
*
7266
* This token is unique for a moduleId and name and can be used as a hash table key.
7367
*/
74-
export class StaticSymbol implements ModuleContext {
68+
export class StaticSymbol {
7569
constructor(public moduleId: string, public filePath: string, public name: string) {}
7670
}
7771

@@ -84,8 +78,7 @@ export class StaticReflector implements ReflectorReader {
8478
private propertyCache = new Map<StaticSymbol, {[key: string]: any}>();
8579
private parameterCache = new Map<StaticSymbol, any[]>();
8680
private metadataCache = new Map<string, {[key: string]: any}>();
87-
private conversionMap =
88-
new Map<StaticSymbol, (moduleContext: ModuleContext, args: any[]) => any>();
81+
private conversionMap = new Map<StaticSymbol, (context: StaticSymbol, args: any[]) => any>();
8982

9083
constructor(private host: StaticReflectorHost) { this.initializeConversionMap(); }
9184

@@ -155,22 +148,20 @@ export class StaticReflector implements ReflectorReader {
155148
}
156149
return parameters;
157150
} catch (e) {
158-
console.error('Failed on type', type, 'with error', e);
151+
console.log(`Failed on type ${type} with error ${e}`);
159152
throw e;
160153
}
161154
}
162155

163156
private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any): void {
164-
this.conversionMap.set(type, (moduleContext: ModuleContext, args: any[]) => {
157+
this.conversionMap.set(type, (context: StaticSymbol, args: any[]) => {
165158
let argValues = [];
166159
ListWrapper.forEachWithIndex(args, (arg, index) => {
167160
let argValue;
168161
if (isStringMap(arg) && isBlank(arg['__symbolic'])) {
169-
argValue =
170-
mapStringMap(arg, (value, key) => this.simplify(
171-
moduleContext, value) );
162+
argValue = mapStringMap(arg, (value, key) => this.simplify(context, value));
172163
} else {
173-
argValue = this.simplify(moduleContext, arg);
164+
argValue = this.simplify(context, arg);
174165
}
175166
argValues.push(argValue);
176167
});
@@ -183,7 +174,6 @@ export class StaticReflector implements ReflectorReader {
183174
let diDecorators = 'angular2/src/core/di/decorators';
184175
let diMetadata = 'angular2/src/core/di/metadata';
185176
let provider = 'angular2/src/core/di/provider';
186-
let opaqueToken = 'angular2/src/core/di/opaque_token';
187177
this.registerDecoratorOrConstructor(this.host.findDeclaration(provider, 'Provider'), Provider);
188178

189179
this.registerDecoratorOrConstructor(this.host.findDeclaration(diDecorators, 'Host'),
@@ -236,12 +226,10 @@ export class StaticReflector implements ReflectorReader {
236226
SkipSelfMetadata);
237227
this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'OptionalMetadata'),
238228
OptionalMetadata);
239-
this.registerDecoratorOrConstructor(this.host.findDeclaration(opaqueToken, 'OpaqueToken'),
240-
OpaqueToken);
241229
}
242230

243231
/** @internal */
244-
public simplify(moduleContext: ModuleContext, value: any): any {
232+
public simplify(context: StaticSymbol, value: any): any {
245233
let _this = this;
246234

247235
function simplify(expression: any): any {
@@ -331,41 +319,39 @@ export class StaticReflector implements ReflectorReader {
331319
case "reference":
332320
if (isPresent(expression['module'])) {
333321
staticSymbol = _this.host.findDeclaration(expression['module'], expression['name'],
334-
moduleContext.filePath);
322+
context.filePath);
335323
} else {
336-
staticSymbol = _this.host.getStaticSymbol(
337-
moduleContext.moduleId, moduleContext.filePath, expression['name']);
324+
staticSymbol = _this.host.getStaticSymbol(context.moduleId, context.filePath,
325+
expression['name']);
338326
}
339327
let result = staticSymbol;
340328
let moduleMetadata = _this.getModuleMetadata(staticSymbol.filePath);
341-
let declarationValue = isPresent(moduleMetadata) ? moduleMetadata['metadata'][staticSymbol.name] : null;
329+
let declarationValue =
330+
isPresent(moduleMetadata) ? moduleMetadata['metadata'][staticSymbol.name] : null;
342331
if (isPresent(declarationValue)) {
343332
if (isClassMetadata(declarationValue)) {
344333
result = staticSymbol;
345334
} else {
346-
let newModuleContext =
347-
new ModuleContext(staticSymbol.moduleId, staticSymbol.filePath);
348-
result = _this.simplify(newModuleContext, declarationValue);
335+
result = _this.simplify(staticSymbol, declarationValue);
349336
}
350337
}
351338
return result;
339+
case "class":
340+
return context;
352341
case "new":
353342
case "call":
354343
let target = expression['expression'];
355-
staticSymbol = _this.host.findDeclaration(target['module'], target['name'],
356-
moduleContext.filePath);
344+
staticSymbol =
345+
_this.host.findDeclaration(target['module'], target['name'], context.filePath);
357346
let converter = _this.conversionMap.get(staticSymbol);
358-
if (isBlank(converter)) {
359-
throw new BaseException(`Cannot convert call/new expression for ${target['name']} in ${moduleContext.filePath}`)
360-
}
361347
if (isPresent(converter)) {
362348
let args = expression['arguments'];
363349
if (isBlank(args)) {
364350
args = [];
365351
}
366-
return converter(moduleContext, args);
352+
return converter(context, args);
367353
} else {
368-
return staticSymbol;
354+
return context;
369355
}
370356
}
371357
return null;

modules/angular2/test/compiler/static_reflector_spec.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import {ListWrapper} from 'angular2/src/facade/collection';
55
import {
66
StaticReflector,
77
StaticReflectorHost,
8-
StaticSymbol,
9-
ModuleContext
8+
StaticSymbol
109
} from 'angular2/src/compiler/static_reflector';
1110

1211
export function main() {
1312
// Static reflector is not supported in Dart
1413
// as we use reflection to create objects.
1514
if (IS_DART) return;
16-
let noContext = new ModuleContext('', '');
15+
let noContext = new StaticSymbol('', '', '');
1716

1817
describe('StaticReflector', () => {
1918
let host: StaticReflectorHost;
@@ -24,8 +23,8 @@ export function main() {
2423
reflector = new StaticReflector(host);
2524
});
2625

27-
function simplify(moduleContext: ModuleContext, value: any) {
28-
return reflector.simplify(moduleContext, value);
26+
function simplify(context: StaticSymbol, value: any) {
27+
return reflector.simplify(context, value);
2928
}
3029

3130
it('should get annotations for NgFor', () => {
@@ -219,9 +218,7 @@ export function main() {
219218
});
220219

221220
it('should simplify an array index', () => {
222-
expect(
223-
simplify(noContext, ({__symbolic: "index", expression: [1, 2, 3], index: 2})))
224-
.toBe(3);
221+
expect(simplify(noContext, ({__symbolic: "index", expression: [1, 2, 3], index: 2}))).toBe(3);
225222
});
226223

227224
it('should simplify an object index', () => {
@@ -230,14 +227,14 @@ export function main() {
230227
});
231228

232229
it('should simplify a module reference', () => {
233-
expect(simplify(new ModuleContext('', '/src/cases'),
234-
({__symbolic: "reference", module: "./extern", name: "s"})))
230+
expect(simplify(new StaticSymbol('', '/src/cases', ''),
231+
({__symbolic: "reference", module: "./extern", name: "s"})))
235232
.toEqual("s");
236233
});
237234

238235
it('should simplify a non existing reference as a static symbol', () => {
239-
expect(simplify(new ModuleContext('', '/src/cases'),
240-
({__symbolic: "reference", module: "./extern", name: "nonExisting"})))
236+
expect(simplify(new StaticSymbol('', '/src/cases', ''),
237+
({__symbolic: "reference", module: "./extern", name: "nonExisting"})))
241238
.toEqual(host.getStaticSymbol('', '/src/extern.d.ts', 'nonExisting'));
242239
});
243240
});

0 commit comments

Comments
 (0)