feat(compiler): Support default parameters in static reflector (#10370)
Closes: #10369
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {Evaluator, errorSymbol, isPrimitive} from './evaluator';
|
||||
import {ClassMetadata, ConstructorMetadata, MemberMetadata, MetadataError, MetadataMap, MetadataObject, MetadataSymbolicExpression, MetadataSymbolicReferenceExpression, MetadataSymbolicSelectExpression, MetadataValue, MethodMetadata, ModuleMetadata, VERSION, isMetadataError, isMetadataSymbolicReferenceExpression, isMetadataSymbolicSelectExpression} from './schema';
|
||||
import {ClassMetadata, ConstructorMetadata, FunctionMetadata, MemberMetadata, MetadataError, MetadataMap, MetadataObject, MetadataSymbolicExpression, MetadataSymbolicReferenceExpression, MetadataSymbolicSelectExpression, MetadataValue, MethodMetadata, ModuleMetadata, VERSION, isMetadataError, isMetadataSymbolicReferenceExpression, isMetadataSymbolicSelectExpression} from './schema';
|
||||
import {Symbols} from './symbols';
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ export class MetadataCollector {
|
||||
public getMetadata(sourceFile: ts.SourceFile): ModuleMetadata {
|
||||
const locals = new Symbols(sourceFile);
|
||||
const evaluator = new Evaluator(locals);
|
||||
let metadata: {[name: string]: MetadataValue | ClassMetadata}|undefined;
|
||||
let metadata: {[name: string]: MetadataValue | ClassMetadata | FunctionMetadata}|undefined;
|
||||
|
||||
function objFromDecorator(decoratorNode: ts.Decorator): MetadataSymbolicExpression {
|
||||
return <MetadataSymbolicExpression>evaluator.evaluateNode(decoratorNode.expression);
|
||||
@ -32,7 +32,7 @@ export class MetadataCollector {
|
||||
|
||||
function maybeGetSimpleFunction(
|
||||
functionDeclaration: ts.FunctionDeclaration |
|
||||
ts.MethodDeclaration): {func: MetadataValue, name: string}|undefined {
|
||||
ts.MethodDeclaration): {func: FunctionMetadata, name: string}|undefined {
|
||||
if (functionDeclaration.name.kind == ts.SyntaxKind.Identifier) {
|
||||
const nameNode = <ts.Identifier>functionDeclaration.name;
|
||||
const functionName = nameNode.text;
|
||||
@ -42,13 +42,17 @@ export class MetadataCollector {
|
||||
if (statement.kind === ts.SyntaxKind.ReturnStatement) {
|
||||
const returnStatement = <ts.ReturnStatement>statement;
|
||||
if (returnStatement.expression) {
|
||||
return {
|
||||
name: functionName, func: {
|
||||
__symbolic: 'function',
|
||||
parameters: namesOf(functionDeclaration.parameters),
|
||||
value: evaluator.evaluateNode(returnStatement.expression)
|
||||
}
|
||||
const func: FunctionMetadata = {
|
||||
__symbolic: 'function',
|
||||
parameters: namesOf(functionDeclaration.parameters),
|
||||
value: evaluator.evaluateNode(returnStatement.expression)
|
||||
};
|
||||
if (functionDeclaration.parameters.some(p => p.initializer != null)) {
|
||||
const defaults: MetadataValue[] = [];
|
||||
func.defaults = functionDeclaration.parameters.map(
|
||||
p => p.initializer && evaluator.evaluateNode(p.initializer));
|
||||
}
|
||||
return { func, name: functionName }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,8 +94,8 @@ export class MetadataCollector {
|
||||
}
|
||||
|
||||
// static member
|
||||
let statics: MetadataObject = null;
|
||||
function recordStaticMember(name: string, value: MetadataValue) {
|
||||
let statics: {[name: string]: MetadataValue | FunctionMetadata} = null;
|
||||
function recordStaticMember(name: string, value: MetadataValue | FunctionMetadata) {
|
||||
if (!statics) statics = {};
|
||||
statics[name] = value;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ export const VERSION = 1;
|
||||
export interface ModuleMetadata {
|
||||
__symbolic: 'module';
|
||||
version: number;
|
||||
metadata: {[name: string]: (ClassMetadata | MetadataValue)};
|
||||
metadata: {[name: string]: (ClassMetadata | FunctionMetadata | MetadataValue)};
|
||||
}
|
||||
export function isModuleMetadata(value: any): value is ModuleMetadata {
|
||||
return value && value.__symbolic === 'module';
|
||||
@ -22,7 +22,7 @@ export interface ClassMetadata {
|
||||
__symbolic: 'class';
|
||||
decorators?: (MetadataSymbolicExpression|MetadataError)[];
|
||||
members?: MetadataMap;
|
||||
statics?: MetadataObject;
|
||||
statics?: {[name: string]: MetadataValue | FunctionMetadata};
|
||||
}
|
||||
export function isClassMetadata(value: any): value is ClassMetadata {
|
||||
return value && value.__symbolic === 'class';
|
||||
@ -65,7 +65,8 @@ export function isConstructorMetadata(value: any): value is ConstructorMetadata
|
||||
export interface FunctionMetadata {
|
||||
__symbolic: 'function';
|
||||
parameters: string[];
|
||||
result: MetadataValue;
|
||||
defaults?: MetadataValue[];
|
||||
value: MetadataValue;
|
||||
}
|
||||
export function isFunctionMetadata(value: any): value is FunctionMetadata {
|
||||
return value && value.__symbolic === 'function';
|
||||
|
@ -28,6 +28,7 @@ describe('Collector', () => {
|
||||
'static-method.ts',
|
||||
'static-method-call.ts',
|
||||
'static-method-with-if.ts',
|
||||
'static-method-with-default.ts',
|
||||
]);
|
||||
service = ts.createLanguageService(host, documentRegistry);
|
||||
program = service.getProgram();
|
||||
@ -445,6 +446,35 @@ describe('Collector', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to collect a method with a default parameter', () => {
|
||||
let source = program.getSourceFile('/static-method-with-default.ts');
|
||||
let metadata = collector.getMetadata(source);
|
||||
expect(metadata).toBeDefined();
|
||||
let classData = <ClassMetadata>metadata.metadata['MyModule'];
|
||||
expect(classData).toBeDefined();
|
||||
expect(classData.statics).toEqual({
|
||||
with: {
|
||||
__symbolic: 'function',
|
||||
parameters: ['comp', 'foo', 'bar'],
|
||||
defaults: [undefined, true, false],
|
||||
value: [
|
||||
{__symbolic: 'reference', name: 'MyModule'}, {
|
||||
__symbolic: 'if',
|
||||
condition: {__symbolic: 'reference', name: 'foo'},
|
||||
thenExpression: {provider: 'a', useValue: {__symbolic: 'reference', name: 'comp'}},
|
||||
elseExpression: {provider: 'b', useValue: {__symbolic: 'reference', name: 'comp'}}
|
||||
},
|
||||
{
|
||||
__symbolic: 'if',
|
||||
condition: {__symbolic: 'reference', name: 'bar'},
|
||||
thenExpression: {provider: 'c', useValue: {__symbolic: 'reference', name: 'comp'}},
|
||||
elseExpression: {provider: 'd', useValue: {__symbolic: 'reference', name: 'comp'}}
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// TODO: Do not use \` in a template literal as it confuses clang-format
|
||||
@ -700,6 +730,20 @@ const FILES: Directory = {
|
||||
}
|
||||
}
|
||||
`,
|
||||
'static-method-with-default.ts': `
|
||||
import {Injectable} from 'angular2/core';
|
||||
|
||||
@Injectable()
|
||||
export class MyModule {
|
||||
static with(comp: any, foo: boolean = true, bar: boolean = false): any[] {
|
||||
return [
|
||||
MyModule,
|
||||
foo ? { provider: 'a', useValue: comp } : {provider: 'b', useValue: comp},
|
||||
bar ? { provider: 'c', useValue: comp } : {provider: 'd', useValue: comp}
|
||||
];
|
||||
}
|
||||
}
|
||||
`,
|
||||
'static-method-call.ts': `
|
||||
import {Component} from 'angular2/core';
|
||||
import {MyModule} from './static-method.ts';
|
||||
|
Reference in New Issue
Block a user