2016-03-22 20:11:42 -04:00
|
|
|
import * as ts from 'typescript';
|
2016-05-26 13:45:37 -04:00
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
import {Evaluator, ImportMetadata, ImportSpecifierMetadata, isPrimitive} from './evaluator';
|
|
|
|
import {ClassMetadata, ConstructorMetadata, ModuleMetadata, MemberMetadata, MetadataError, MetadataMap, MetadataSymbolicExpression, MetadataSymbolicReferenceExpression, MetadataValue, MethodMetadata, isMetadataError, isMetadataSymbolicReferenceExpression,} from './schema';
|
2016-03-22 20:11:42 -04:00
|
|
|
import {Symbols} from './symbols';
|
2016-05-26 13:45:37 -04:00
|
|
|
|
2016-03-22 20:11:42 -04:00
|
|
|
/**
|
|
|
|
* Collect decorator metadata from a TypeScript module.
|
|
|
|
*/
|
|
|
|
export class MetadataCollector {
|
2016-04-26 00:29:06 -04:00
|
|
|
constructor() {}
|
|
|
|
|
2016-03-22 20:11:42 -04:00
|
|
|
/**
|
|
|
|
* Returns a JSON.stringify friendly form describing the decorators of the exported classes from
|
|
|
|
* the source file that is expected to correspond to a module.
|
|
|
|
*/
|
2016-05-31 14:00:39 -04:00
|
|
|
public getMetadata(sourceFile: ts.SourceFile): ModuleMetadata {
|
|
|
|
const locals = new Symbols(sourceFile);
|
|
|
|
const evaluator = new Evaluator(locals);
|
|
|
|
let metadata: {[name: string]: MetadataValue | ClassMetadata}|undefined;
|
2016-03-22 20:11:42 -04:00
|
|
|
|
|
|
|
function objFromDecorator(decoratorNode: ts.Decorator): MetadataSymbolicExpression {
|
|
|
|
return <MetadataSymbolicExpression>evaluator.evaluateNode(decoratorNode.expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
function classMetadataOf(classDeclaration: ts.ClassDeclaration): ClassMetadata {
|
2016-05-26 13:45:37 -04:00
|
|
|
let result: ClassMetadata = {__symbolic: 'class'};
|
2016-04-19 22:37:57 -04:00
|
|
|
|
|
|
|
function getDecorators(decorators: ts.Decorator[]): MetadataSymbolicExpression[] {
|
|
|
|
if (decorators && decorators.length)
|
|
|
|
return decorators.map(decorator => objFromDecorator(decorator));
|
|
|
|
return undefined;
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
function referenceFrom(node: ts.Node): MetadataSymbolicReferenceExpression|MetadataError {
|
|
|
|
const result = evaluator.evaluateNode(node);
|
|
|
|
if (isMetadataError(result) || isMetadataSymbolicReferenceExpression(result)) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return {__symbolic: 'error', message: 'Symbol reference expected'};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:11:42 -04:00
|
|
|
// Add class decorators
|
|
|
|
if (classDeclaration.decorators) {
|
|
|
|
result.decorators = getDecorators(classDeclaration.decorators);
|
|
|
|
}
|
|
|
|
|
|
|
|
// member decorators
|
|
|
|
let members: MetadataMap = null;
|
|
|
|
function recordMember(name: string, metadata: MemberMetadata) {
|
|
|
|
if (!members) members = {};
|
|
|
|
let data = members.hasOwnProperty(name) ? members[name] : [];
|
|
|
|
data.push(metadata);
|
|
|
|
members[name] = data;
|
|
|
|
}
|
|
|
|
for (const member of classDeclaration.members) {
|
|
|
|
let isConstructor = false;
|
|
|
|
switch (member.kind) {
|
|
|
|
case ts.SyntaxKind.Constructor:
|
|
|
|
isConstructor = true;
|
|
|
|
// fallthrough
|
|
|
|
case ts.SyntaxKind.MethodDeclaration:
|
2016-05-26 13:45:37 -04:00
|
|
|
const method = <ts.MethodDeclaration|ts.ConstructorDeclaration>member;
|
2016-03-22 20:11:42 -04:00
|
|
|
const methodDecorators = getDecorators(method.decorators);
|
|
|
|
const parameters = method.parameters;
|
2016-05-31 14:00:39 -04:00
|
|
|
const parameterDecoratorData: (MetadataSymbolicExpression | MetadataError)[][] = [];
|
|
|
|
const parametersData: (MetadataSymbolicReferenceExpression | MetadataError | null)[] =
|
|
|
|
[];
|
2016-03-22 20:11:42 -04:00
|
|
|
let hasDecoratorData: boolean = false;
|
|
|
|
let hasParameterData: boolean = false;
|
|
|
|
for (const parameter of parameters) {
|
|
|
|
const parameterData = getDecorators(parameter.decorators);
|
|
|
|
parameterDecoratorData.push(parameterData);
|
|
|
|
hasDecoratorData = hasDecoratorData || !!parameterData;
|
|
|
|
if (isConstructor) {
|
2016-05-31 14:00:39 -04:00
|
|
|
if (parameter.type) {
|
|
|
|
parametersData.push(referenceFrom(parameter.type));
|
|
|
|
} else {
|
|
|
|
parametersData.push(null);
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
hasParameterData = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 13:45:37 -04:00
|
|
|
const data: MethodMetadata = {__symbolic: isConstructor ? 'constructor' : 'method'};
|
|
|
|
const name = isConstructor ? '__ctor__' : evaluator.nameOf(member.name);
|
2016-05-04 12:11:04 -04:00
|
|
|
if (methodDecorators) {
|
|
|
|
data.decorators = methodDecorators;
|
|
|
|
}
|
|
|
|
if (hasDecoratorData) {
|
|
|
|
data.parameterDecorators = parameterDecoratorData;
|
|
|
|
}
|
|
|
|
if (hasParameterData) {
|
|
|
|
(<ConstructorMetadata>data).parameters = parametersData;
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
if (!isMetadataError(name)) {
|
|
|
|
recordMember(name, data);
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.PropertyDeclaration:
|
2016-03-25 13:16:06 -04:00
|
|
|
case ts.SyntaxKind.GetAccessor:
|
|
|
|
case ts.SyntaxKind.SetAccessor:
|
2016-03-22 20:11:42 -04:00
|
|
|
const property = <ts.PropertyDeclaration>member;
|
|
|
|
const propertyDecorators = getDecorators(property.decorators);
|
|
|
|
if (propertyDecorators) {
|
2016-05-31 14:00:39 -04:00
|
|
|
let name = evaluator.nameOf(property.name);
|
|
|
|
if (!isMetadataError(name)) {
|
|
|
|
recordMember(name, {__symbolic: 'property', decorators: propertyDecorators});
|
|
|
|
}
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (members) {
|
|
|
|
result.members = members;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.decorators || members ? result : undefined;
|
|
|
|
}
|
|
|
|
|
2016-05-31 14:00:39 -04:00
|
|
|
// Predeclare classes
|
|
|
|
ts.forEachChild(sourceFile, node => {
|
|
|
|
switch (node.kind) {
|
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
|
|
|
const classDeclaration = <ts.ClassDeclaration>node;
|
|
|
|
const className = classDeclaration.name.text;
|
|
|
|
if (node.flags & ts.NodeFlags.Export) {
|
|
|
|
locals.define(className, {__symbolic: 'reference', name: className});
|
|
|
|
} else {
|
|
|
|
locals.define(
|
|
|
|
className,
|
|
|
|
{__symbolic: 'error', message: `Reference to non-exported class ${className}`});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ts.forEachChild(sourceFile, node => {
|
|
|
|
switch (node.kind) {
|
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
|
|
|
const classDeclaration = <ts.ClassDeclaration>node;
|
|
|
|
const className = classDeclaration.name.text;
|
|
|
|
if (node.flags & ts.NodeFlags.Export) {
|
2016-03-22 20:11:42 -04:00
|
|
|
if (classDeclaration.decorators) {
|
|
|
|
if (!metadata) metadata = {};
|
2016-05-31 14:00:39 -04:00
|
|
|
metadata[className] = classMetadataOf(classDeclaration);
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
}
|
|
|
|
// Otherwise don't record metadata for the class.
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.VariableStatement:
|
|
|
|
const variableStatement = <ts.VariableStatement>node;
|
|
|
|
for (let variableDeclaration of variableStatement.declarationList.declarations) {
|
|
|
|
if (variableDeclaration.name.kind == ts.SyntaxKind.Identifier) {
|
|
|
|
let nameNode = <ts.Identifier>variableDeclaration.name;
|
|
|
|
let varValue: MetadataValue;
|
|
|
|
if (variableDeclaration.initializer) {
|
|
|
|
varValue = evaluator.evaluateNode(variableDeclaration.initializer);
|
|
|
|
} else {
|
|
|
|
varValue = {
|
|
|
|
__symbolic: 'error',
|
|
|
|
message: 'Only intialized variables and constants can be referenced statically'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (variableStatement.flags & ts.NodeFlags.Export ||
|
|
|
|
variableDeclaration.flags & ts.NodeFlags.Export) {
|
2016-03-22 20:11:42 -04:00
|
|
|
if (!metadata) metadata = {};
|
2016-05-31 14:00:39 -04:00
|
|
|
metadata[nameNode.text] = varValue;
|
|
|
|
}
|
|
|
|
if (isPrimitive(varValue)) {
|
|
|
|
locals.define(nameNode.text, varValue);
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
} else {
|
|
|
|
// Destructuring (or binding) declarations are not supported,
|
|
|
|
// var {<identifier>[, <identifer>]+} = <expression>;
|
|
|
|
// or
|
|
|
|
// var [<identifier>[, <identifier}+] = <expression>;
|
|
|
|
// are not supported.
|
|
|
|
let varValue = {
|
|
|
|
__symbolc: 'error',
|
|
|
|
message: 'Destructuring declarations cannot be referenced statically'
|
|
|
|
};
|
|
|
|
const report = (nameNode: ts.Node) => {
|
|
|
|
switch (nameNode.kind) {
|
|
|
|
case ts.SyntaxKind.Identifier:
|
|
|
|
const name = <ts.Identifier>nameNode;
|
|
|
|
locals.define(name.text, varValue);
|
|
|
|
if (node.flags & ts.NodeFlags.Export) {
|
|
|
|
if (!metadata) metadata = {};
|
|
|
|
metadata[name.text] = varValue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.BindingElement:
|
|
|
|
const bindingElement = <ts.BindingElement>nameNode;
|
|
|
|
report(bindingElement.name);
|
|
|
|
break;
|
|
|
|
case ts.SyntaxKind.ObjectBindingPattern:
|
|
|
|
case ts.SyntaxKind.ArrayBindingPattern:
|
|
|
|
const bindings = <ts.BindingPattern>nameNode;
|
|
|
|
bindings.elements.forEach(report);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
report(variableDeclaration.name);
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
}
|
|
|
|
break;
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
});
|
2016-04-26 00:29:06 -04:00
|
|
|
|
2016-05-26 13:45:37 -04:00
|
|
|
return metadata && {__symbolic: 'module', metadata};
|
2016-03-22 20:11:42 -04:00
|
|
|
}
|
|
|
|
}
|