feat(tsc-wrapped): allow values to be substituted by collector clients
Also reenabled tests that were unintentionally disabled when they were moved from tools/@angular.
This commit is contained in:

committed by
Alex Rickabaugh

parent
381471d338
commit
67dff7bd5d
@ -9,7 +9,7 @@
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {MetadataCollector} from '../src/collector';
|
||||
import {ClassMetadata, ConstructorMetadata, MetadataEntry, ModuleMetadata, isClassMetadata} from '../src/schema';
|
||||
import {ClassMetadata, ConstructorMetadata, MetadataEntry, ModuleMetadata, isClassMetadata, isMetadataGlobalReferenceExpression} from '../src/schema';
|
||||
|
||||
import {Directory, Host, expectValidSources} from './typescript.mocks';
|
||||
|
||||
@ -939,6 +939,44 @@ describe('Collector', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('substitutions', () => {
|
||||
const lambdaTemp = 'lambdaTemp';
|
||||
|
||||
it('should be able to substitute a lambda', () => {
|
||||
const source = createSource(`
|
||||
const b = 1;
|
||||
export const a = () => b;
|
||||
`);
|
||||
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
|
||||
if (node.kind === ts.SyntaxKind.ArrowFunction) {
|
||||
return {__symbolic: 'reference', name: lambdaTemp};
|
||||
}
|
||||
return value;
|
||||
});
|
||||
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp});
|
||||
});
|
||||
|
||||
it('should compose substitution functions', () => {
|
||||
const collector = new MetadataCollector({
|
||||
substituteExpression: (value, node) => isMetadataGlobalReferenceExpression(value) &&
|
||||
value.name == lambdaTemp ?
|
||||
{__symbolic: 'reference', name: value.name + '2'} :
|
||||
value
|
||||
});
|
||||
const source = createSource(`
|
||||
const b = 1;
|
||||
export const a = () => b;
|
||||
`);
|
||||
const metadata = collector.getMetadata(source, /* strict */ false, (value, node) => {
|
||||
if (node.kind === ts.SyntaxKind.ArrowFunction) {
|
||||
return {__symbolic: 'reference', name: lambdaTemp};
|
||||
}
|
||||
return value;
|
||||
});
|
||||
expect(metadata !.metadata['a']).toEqual({__symbolic: 'reference', name: lambdaTemp + '2'});
|
||||
});
|
||||
});
|
||||
|
||||
function override(fileName: string, content: string) {
|
||||
host.overrideFile(fileName, content);
|
||||
host.addFile(fileName);
|
@ -223,6 +223,45 @@ describe('Evaluator', () => {
|
||||
expect(evaluator.evaluateNode(expr.initializer !))
|
||||
.toEqual({__symbolic: 'new', expression: {__symbolic: 'reference', name: 'f'}});
|
||||
});
|
||||
|
||||
describe('with substitution', () => {
|
||||
let evaluator: Evaluator;
|
||||
const lambdaTemp = 'lambdaTemp';
|
||||
|
||||
beforeEach(() => {
|
||||
evaluator = new Evaluator(symbols, new Map(), {
|
||||
substituteExpression: (value, node) => {
|
||||
if (node.kind == ts.SyntaxKind.ArrowFunction) {
|
||||
return {__symbolic: 'reference', name: lambdaTemp};
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to substitute a lambda with a reference', () => {
|
||||
const source = sourceFileOf(`
|
||||
var b = 1;
|
||||
export var a = () => b;
|
||||
`);
|
||||
const expr = findVar(source, 'a');
|
||||
expect(evaluator.evaluateNode(expr !.initializer !))
|
||||
.toEqual({__symbolic: 'reference', name: lambdaTemp});
|
||||
});
|
||||
|
||||
it('should be able to substitute a lambda in an expression', () => {
|
||||
const source = sourceFileOf(`
|
||||
var b = 1;
|
||||
export var a = [
|
||||
{ provide: 'someValue': useFactory: () => b }
|
||||
];
|
||||
`);
|
||||
const expr = findVar(source, 'a');
|
||||
expect(evaluator.evaluateNode(expr !.initializer !)).toEqual([
|
||||
{provide: 'someValue', useFactory: {__symbolic: 'reference', name: lambdaTemp}}
|
||||
])
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
function sourceFileOf(text: string): ts.SourceFile {
|
Reference in New Issue
Block a user