refactor(compiler): support hash syntax for providers.

This commit is contained in:
Martin Probst 2016-04-29 14:34:01 -07:00
parent 365be6a309
commit 176e55927c
2 changed files with 121 additions and 112 deletions

View File

@ -7,6 +7,7 @@ import {
isStringMap, isStringMap,
FunctionWrapper FunctionWrapper
} from 'angular2/src/facade/lang'; } from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions';
import { import {
AttributeMetadata, AttributeMetadata,
DirectiveMetadata, DirectiveMetadata,
@ -32,8 +33,9 @@ import {
InjectableMetadata, InjectableMetadata,
SelfMetadata, SelfMetadata,
SkipSelfMetadata, SkipSelfMetadata,
InjectMetadata InjectMetadata,
} from "angular2/src/core/di/metadata"; } from "angular2/src/core/di/metadata";
import {OpaqueToken} from 'angular2/src/core/di/opaque_token';
export class ModuleContext { export class ModuleContext {
constructor(public moduleId: string, public filePath: string) {} constructor(public moduleId: string, public filePath: string) {}
@ -94,7 +96,7 @@ export class StaticReflector implements ReflectorReader {
if (!isPresent(annotations)) { if (!isPresent(annotations)) {
let classMetadata = this.getTypeMetadata(type); let classMetadata = this.getTypeMetadata(type);
if (isPresent(classMetadata['decorators'])) { if (isPresent(classMetadata['decorators'])) {
annotations = this.simplify(type, classMetadata['decorators'], false); annotations = this.simplify(type, classMetadata['decorators']);
} else { } else {
annotations = []; annotations = [];
} }
@ -111,7 +113,7 @@ export class StaticReflector implements ReflectorReader {
propMetadata = mapStringMap(members, (propData, propName) => { propMetadata = mapStringMap(members, (propData, propName) => {
let prop = (<any[]>propData).find(a => a['__symbolic'] == 'property'); let prop = (<any[]>propData).find(a => a['__symbolic'] == 'property');
if (isPresent(prop) && isPresent(prop['decorators'])) { if (isPresent(prop) && isPresent(prop['decorators'])) {
return this.simplify(type, prop['decorators'], false); return this.simplify(type, prop['decorators']);
} else { } else {
return []; return [];
} }
@ -122,39 +124,43 @@ export class StaticReflector implements ReflectorReader {
} }
public parameters(type: StaticSymbol): any[] { public parameters(type: StaticSymbol): any[] {
let parameters = this.parameterCache.get(type); try {
if (!isPresent(parameters)) { let parameters = this.parameterCache.get(type);
let classMetadata = this.getTypeMetadata(type);
let members = isPresent(classMetadata) ? classMetadata['members'] : null;
let ctorData = isPresent(members) ? members['__ctor__'] : null;
if (isPresent(ctorData)) {
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
let parameterTypes = <any[]>this.simplify(type, ctor['parameters'], false);
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators'], false);
parameters = [];
ListWrapper.forEachWithIndex(parameterTypes, (paramType, index) => {
let nestedResult = [];
if (isPresent(paramType)) {
nestedResult.push(paramType);
}
let decorators = isPresent(parameterDecorators) ? parameterDecorators[index] : null;
if (isPresent(decorators)) {
ListWrapper.addAll(nestedResult, decorators);
}
parameters.push(nestedResult);
});
}
if (!isPresent(parameters)) { if (!isPresent(parameters)) {
parameters = []; let classMetadata = this.getTypeMetadata(type);
let members = isPresent(classMetadata) ? classMetadata['members'] : null;
let ctorData = isPresent(members) ? members['__ctor__'] : null;
if (isPresent(ctorData)) {
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
let parameterTypes = <any[]>this.simplify(type, ctor['parameters']);
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators']);
parameters = [];
ListWrapper.forEachWithIndex(parameterTypes, (paramType, index) => {
let nestedResult = [];
if (isPresent(paramType)) {
nestedResult.push(paramType);
}
let decorators = isPresent(parameterDecorators) ? parameterDecorators[index] : null;
if (isPresent(decorators)) {
ListWrapper.addAll(nestedResult, decorators);
}
parameters.push(nestedResult);
});
}
if (!isPresent(parameters)) {
parameters = [];
}
this.parameterCache.set(type, parameters);
} }
this.parameterCache.set(type, parameters); return parameters;
} catch (e) {
console.error('Failed on type', type, 'with error', e);
throw e;
} }
return parameters;
} }
private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any, private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any): void {
crossModuleProps: any[] = []): void {
this.conversionMap.set(type, (moduleContext: ModuleContext, args: any[]) => { this.conversionMap.set(type, (moduleContext: ModuleContext, args: any[]) => {
let argValues = []; let argValues = [];
ListWrapper.forEachWithIndex(args, (arg, index) => { ListWrapper.forEachWithIndex(args, (arg, index) => {
@ -162,9 +168,9 @@ export class StaticReflector implements ReflectorReader {
if (isStringMap(arg) && isBlank(arg['__symbolic'])) { if (isStringMap(arg) && isBlank(arg['__symbolic'])) {
argValue = argValue =
mapStringMap(arg, (value, key) => this.simplify( mapStringMap(arg, (value, key) => this.simplify(
moduleContext, value, crossModuleProps.indexOf(key) !== -1)); moduleContext, value) );
} else { } else {
argValue = this.simplify(moduleContext, arg, crossModuleProps.indexOf(index) !== -1); argValue = this.simplify(moduleContext, arg);
} }
argValues.push(argValue); argValues.push(argValue);
}); });
@ -177,6 +183,7 @@ export class StaticReflector implements ReflectorReader {
let diDecorators = 'angular2/src/core/di/decorators'; let diDecorators = 'angular2/src/core/di/decorators';
let diMetadata = 'angular2/src/core/di/metadata'; let diMetadata = 'angular2/src/core/di/metadata';
let provider = 'angular2/src/core/di/provider'; let provider = 'angular2/src/core/di/provider';
let opaqueToken = 'angular2/src/core/di/opaque_token';
this.registerDecoratorOrConstructor(this.host.findDeclaration(provider, 'Provider'), Provider); this.registerDecoratorOrConstructor(this.host.findDeclaration(provider, 'Provider'), Provider);
this.registerDecoratorOrConstructor(this.host.findDeclaration(diDecorators, 'Host'), this.registerDecoratorOrConstructor(this.host.findDeclaration(diDecorators, 'Host'),
@ -216,10 +223,9 @@ export class StaticReflector implements ReflectorReader {
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'HostListener'), this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'HostListener'),
HostListenerMetadata); HostListenerMetadata);
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Directive'), this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Directive'),
DirectiveMetadata, ['bindings', 'providers']); DirectiveMetadata);
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Component'), this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Component'),
ComponentMetadata, ComponentMetadata);
['bindings', 'providers', 'directives', 'pipes']);
// Note: Some metadata classes can be used directly with Provider.deps. // Note: Some metadata classes can be used directly with Provider.deps.
this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'HostMetadata'), this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'HostMetadata'),
@ -230,10 +236,12 @@ export class StaticReflector implements ReflectorReader {
SkipSelfMetadata); SkipSelfMetadata);
this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'OptionalMetadata'), this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'OptionalMetadata'),
OptionalMetadata); OptionalMetadata);
this.registerDecoratorOrConstructor(this.host.findDeclaration(opaqueToken, 'OpaqueToken'),
OpaqueToken);
} }
/** @internal */ /** @internal */
public simplify(moduleContext: ModuleContext, value: any, crossModules: boolean): any { public simplify(moduleContext: ModuleContext, value: any): any {
let _this = this; let _this = this;
function simplify(expression: any): any { function simplify(expression: any): any {
@ -328,19 +336,17 @@ export class StaticReflector implements ReflectorReader {
staticSymbol = _this.host.getStaticSymbol( staticSymbol = _this.host.getStaticSymbol(
moduleContext.moduleId, moduleContext.filePath, expression['name']); moduleContext.moduleId, moduleContext.filePath, expression['name']);
} }
let result; let result = staticSymbol;
if (crossModules || isBlank(expression['module'])) { let moduleMetadata = _this.getModuleMetadata(staticSymbol.filePath);
let moduleMetadata = _this.getModuleMetadata(staticSymbol.filePath); let declarationValue = isPresent(moduleMetadata) ? moduleMetadata['metadata'][staticSymbol.name] : null;
let declarationValue = moduleMetadata['metadata'][staticSymbol.name]; if (isPresent(declarationValue)) {
if (isClassMetadata(declarationValue)) { if (isClassMetadata(declarationValue)) {
result = staticSymbol; result = staticSymbol;
} else { } else {
let newModuleContext = let newModuleContext =
new ModuleContext(staticSymbol.moduleId, staticSymbol.filePath); new ModuleContext(staticSymbol.moduleId, staticSymbol.filePath);
result = _this.simplify(newModuleContext, declarationValue, crossModules); result = _this.simplify(newModuleContext, declarationValue);
} }
} else {
result = staticSymbol;
} }
return result; return result;
case "new": case "new":
@ -349,11 +355,18 @@ export class StaticReflector implements ReflectorReader {
staticSymbol = _this.host.findDeclaration(target['module'], target['name'], staticSymbol = _this.host.findDeclaration(target['module'], target['name'],
moduleContext.filePath); moduleContext.filePath);
let converter = _this.conversionMap.get(staticSymbol); let converter = _this.conversionMap.get(staticSymbol);
let args = expression['arguments']; if (isBlank(converter)) {
if (isBlank(args)) { throw new BaseException(`Cannot convert call/new expression for ${target['name']} in ${moduleContext.filePath}`)
args = []; }
if (isPresent(converter)) {
let args = expression['arguments'];
if (isBlank(args)) {
args = [];
}
return converter(moduleContext, args);
} else {
return staticSymbol;
} }
return isPresent(converter) ? converter(moduleContext, args) : null;
} }
return null; return null;
} }

View File

@ -24,12 +24,8 @@ export function main() {
reflector = new StaticReflector(host); reflector = new StaticReflector(host);
}); });
function singleModuleSimplify(moduleContext: ModuleContext, value: any) { function simplify(moduleContext: ModuleContext, value: any) {
return reflector.simplify(moduleContext, value, false); return reflector.simplify(moduleContext, value);
}
function crossModuleSimplify(moduleContext: ModuleContext, value: any) {
return reflector.simplify(moduleContext, value, true);
} }
it('should get annotations for NgFor', () => { it('should get annotations for NgFor', () => {
@ -95,154 +91,154 @@ export function main() {
}); });
it('should simplify primitive into itself', () => { it('should simplify primitive into itself', () => {
expect(singleModuleSimplify(noContext, 1)).toBe(1); expect(simplify(noContext, 1)).toBe(1);
expect(singleModuleSimplify(noContext, true)).toBe(true); expect(simplify(noContext, true)).toBe(true);
expect(singleModuleSimplify(noContext, "some value")).toBe("some value"); expect(simplify(noContext, "some value")).toBe("some value");
}); });
it('should simplify an array into a copy of the array', it('should simplify an array into a copy of the array',
() => { expect(singleModuleSimplify(noContext, [1, 2, 3])).toEqual([1, 2, 3]); }); () => { expect(simplify(noContext, [1, 2, 3])).toEqual([1, 2, 3]); });
it('should simplify an object to a copy of the object', () => { it('should simplify an object to a copy of the object', () => {
let expr = {a: 1, b: 2, c: 3}; let expr = {a: 1, b: 2, c: 3};
expect(singleModuleSimplify(noContext, expr)).toEqual(expr); expect(simplify(noContext, expr)).toEqual(expr);
}); });
it('should simplify &&', () => { it('should simplify &&', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: true, right: true}))).toBe(true); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: true, right: true}))).toBe(true);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: true, right: false}))).toBe(false); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: true, right: false}))).toBe(false);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: false, right: true}))).toBe(false); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: false, right: true}))).toBe(false);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: false, right: false}))).toBe(false); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&&', left: false, right: false}))).toBe(false);
}); });
it('should simplify ||', () => { it('should simplify ||', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '||', left: true, right: true}))).toBe(true); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '||', left: true, right: true}))).toBe(true);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '||', left: true, right: false}))).toBe(true); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '||', left: true, right: false}))).toBe(true);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '||', left: false, right: true}))).toBe(true); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '||', left: false, right: true}))).toBe(true);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '||', left: false, right: false}))).toBe(false); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '||', left: false, right: false}))).toBe(false);
}); });
it('should simplify &', () => { it('should simplify &', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&', left: 0x22, right: 0x0F}))).toBe(0x22 & 0x0F); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&', left: 0x22, right: 0x0F}))).toBe(0x22 & 0x0F);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '&', left: 0x22, right: 0xF0}))).toBe(0x22 & 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '&', left: 0x22, right: 0xF0}))).toBe(0x22 & 0xF0);
}); });
it('should simplify |', () => { it('should simplify |', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0x0F}))).toBe(0x22 | 0x0F); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0x0F}))).toBe(0x22 | 0x0F);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0xF0}))).toBe(0x22 | 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0xF0}))).toBe(0x22 | 0xF0);
}); });
it('should simplify ^', () => { it('should simplify ^', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0x0F}))).toBe(0x22 | 0x0F); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0x0F}))).toBe(0x22 | 0x0F);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0xF0}))).toBe(0x22 | 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '|', left: 0x22, right: 0xF0}))).toBe(0x22 | 0xF0);
}); });
it('should simplify ==', () => { it('should simplify ==', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '==', left: 0x22, right: 0x22}))).toBe(0x22 == 0x22); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '==', left: 0x22, right: 0x22}))).toBe(0x22 == 0x22);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '==', left: 0x22, right: 0xF0}))).toBe(0x22 == 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '==', left: 0x22, right: 0xF0}))).toBe(0x22 == 0xF0);
}); });
it('should simplify !=', () => { it('should simplify !=', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '!=', left: 0x22, right: 0x22}))).toBe(0x22 != 0x22); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '!=', left: 0x22, right: 0x22}))).toBe(0x22 != 0x22);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '!=', left: 0x22, right: 0xF0}))).toBe(0x22 != 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '!=', left: 0x22, right: 0xF0}))).toBe(0x22 != 0xF0);
}); });
it('should simplify ===', () => { it('should simplify ===', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '===', left: 0x22, right: 0x22}))).toBe(0x22 === 0x22); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '===', left: 0x22, right: 0x22}))).toBe(0x22 === 0x22);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '===', left: 0x22, right: 0xF0}))).toBe(0x22 === 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '===', left: 0x22, right: 0xF0}))).toBe(0x22 === 0xF0);
}); });
it('should simplify !==', () => { it('should simplify !==', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '!==', left: 0x22, right: 0x22}))).toBe(0x22 !== 0x22); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '!==', left: 0x22, right: 0x22}))).toBe(0x22 !== 0x22);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '!==', left: 0x22, right: 0xF0}))).toBe(0x22 !== 0xF0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '!==', left: 0x22, right: 0xF0}))).toBe(0x22 !== 0xF0);
}); });
it('should simplify >', () => { it('should simplify >', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 1, right: 1}))).toBe(1 > 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 1, right: 1}))).toBe(1 > 1);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 1, right: 0}))).toBe(1 > 0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 1, right: 0}))).toBe(1 > 0);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 0, right: 1}))).toBe(0 > 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>', left: 0, right: 1}))).toBe(0 > 1);
}); });
it('should simplify >=', () => { it('should simplify >=', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 1, right: 1}))).toBe(1 >= 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 1, right: 1}))).toBe(1 >= 1);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 1, right: 0}))).toBe(1 >= 0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 1, right: 0}))).toBe(1 >= 0);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 0, right: 1}))).toBe(0 >= 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>=', left: 0, right: 1}))).toBe(0 >= 1);
}); });
it('should simplify <=', () => { it('should simplify <=', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 1, right: 1}))).toBe(1 <= 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 1, right: 1}))).toBe(1 <= 1);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 1, right: 0}))).toBe(1 <= 0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 1, right: 0}))).toBe(1 <= 0);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 0, right: 1}))).toBe(0 <= 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<=', left: 0, right: 1}))).toBe(0 <= 1);
}); });
it('should simplify <', () => { it('should simplify <', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 1, right: 1}))).toBe(1 < 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 1, right: 1}))).toBe(1 < 1);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 1, right: 0}))).toBe(1 < 0); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 1, right: 0}))).toBe(1 < 0);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 0, right: 1}))).toBe(0 < 1); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<', left: 0, right: 1}))).toBe(0 < 1);
}); });
it('should simplify <<', () => { it('should simplify <<', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '<<', left: 0x55, right: 2}))).toBe(0x55 << 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '<<', left: 0x55, right: 2}))).toBe(0x55 << 2);
}); });
it('should simplify >>', () => { it('should simplify >>', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '>>', left: 0x55, right: 2}))).toBe(0x55 >> 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '>>', left: 0x55, right: 2}))).toBe(0x55 >> 2);
}); });
it('should simplify +', () => { it('should simplify +', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '+', left: 0x55, right: 2}))).toBe(0x55 + 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '+', left: 0x55, right: 2}))).toBe(0x55 + 2);
}); });
it('should simplify -', () => { it('should simplify -', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '-', left: 0x55, right: 2}))).toBe(0x55 - 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '-', left: 0x55, right: 2}))).toBe(0x55 - 2);
}); });
it('should simplify *', () => { it('should simplify *', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '*', left: 0x55, right: 2}))).toBe(0x55 * 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '*', left: 0x55, right: 2}))).toBe(0x55 * 2);
}); });
it('should simplify /', () => { it('should simplify /', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '/', left: 0x55, right: 2}))).toBe(0x55 / 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '/', left: 0x55, right: 2}))).toBe(0x55 / 2);
}); });
it('should simplify %', () => { it('should simplify %', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'binop', operator: '%', left: 0x55, right: 2}))).toBe(0x55 % 2); expect(simplify(noContext, ({ __symbolic: 'binop', operator: '%', left: 0x55, right: 2}))).toBe(0x55 % 2);
}); });
it('should simplify prefix -', () => { it('should simplify prefix -', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'pre', operator: '-', operand: 2}))).toBe(-2); expect(simplify(noContext, ({ __symbolic: 'pre', operator: '-', operand: 2}))).toBe(-2);
}); });
it('should simplify prefix ~', () => { it('should simplify prefix ~', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'pre', operator: '~', operand: 2}))).toBe(~2); expect(simplify(noContext, ({ __symbolic: 'pre', operator: '~', operand: 2}))).toBe(~2);
}); });
it('should simplify prefix !', () => { it('should simplify prefix !', () => {
expect(singleModuleSimplify(noContext, ({ __symbolic: 'pre', operator: '!', operand: true}))).toBe(!true); expect(simplify(noContext, ({ __symbolic: 'pre', operator: '!', operand: true}))).toBe(!true);
expect(singleModuleSimplify(noContext, ({ __symbolic: 'pre', operator: '!', operand: false}))).toBe(!false); expect(simplify(noContext, ({ __symbolic: 'pre', operator: '!', operand: false}))).toBe(!false);
}); });
it('should simplify an array index', () => { it('should simplify an array index', () => {
expect( expect(
singleModuleSimplify(noContext, ({__symbolic: "index", expression: [1, 2, 3], index: 2}))) simplify(noContext, ({__symbolic: "index", expression: [1, 2, 3], index: 2})))
.toBe(3); .toBe(3);
}); });
it('should simplify an object index', () => { it('should simplify an object index', () => {
let expr = {__symbolic: "select", expression: {a: 1, b: 2, c: 3}, member: "b"}; let expr = {__symbolic: "select", expression: {a: 1, b: 2, c: 3}, member: "b"};
expect(singleModuleSimplify(noContext, expr)).toBe(2); expect(simplify(noContext, expr)).toBe(2);
}); });
it('should simplify a module reference across modules', () => { it('should simplify a module reference', () => {
expect(crossModuleSimplify(new ModuleContext('', '/src/cases'), expect(simplify(new ModuleContext('', '/src/cases'),
({__symbolic: "reference", module: "./extern", name: "s"}))) ({__symbolic: "reference", module: "./extern", name: "s"})))
.toEqual("s"); .toEqual("s");
}); });
it('should simplify a module reference without crossing modules', () => { it('should simplify a non existing reference as a static symbol', () => {
expect(singleModuleSimplify(new ModuleContext('', '/src/cases'), expect(simplify(new ModuleContext('', '/src/cases'),
({__symbolic: "reference", module: "./extern", name: "s"}))) ({__symbolic: "reference", module: "./extern", name: "nonExisting"})))
.toEqual(host.getStaticSymbol('', '/src/extern.d.ts', 's')); .toEqual(host.getStaticSymbol('', '/src/extern.d.ts', 'nonExisting'));
}); });
}); });
} }