Skip to content

Commit

Permalink
fix(tsc-wrapped): generate metadata for exports without module specifier
Browse files Browse the repository at this point in the history
fixes #13327
  • Loading branch information
vicb committed Dec 15, 2016
1 parent f6ef7d6 commit cd03c77
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
5 changes: 5 additions & 0 deletions modules/@angular/compiler-cli/test/aot_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('CompilerHost', () => {
foo: {__symbolic: 'class'},
Bar: {__symbolic: 'class', members: {ngOnInit: [{__symbolic: 'method'}]}},
BarChild: {__symbolic: 'class', extends: {__symbolic: 'reference', name: 'Bar'}},
ReExport: {__symbolic: 'reference', module: './lib/utils2', name: 'ReExport'},
},
exports: [{from: './lib/utils2', export: ['Export']}],
}
Expand Down Expand Up @@ -212,7 +213,11 @@ const FILES: Entry = {
},
'metadata_versions': {
'v1.d.ts': `
import {ReExport} from './lib/utils2';
export {ReExport};
export {Export} from './lib/utils2';
export declare class Bar {
ngOnInit() {}
}
Expand Down
36 changes: 27 additions & 9 deletions tools/@angular/tsc-wrapped/src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as ts from 'typescript';

import {Evaluator, errorSymbol, isPrimitive} from './evaluator';
import {ClassMetadata, ConstructorMetadata, FunctionMetadata, MemberMetadata, MetadataEntry, MetadataError, MetadataMap, MetadataObject, MetadataSymbolicBinaryExpression, MetadataSymbolicCallExpression, MetadataSymbolicExpression, MetadataSymbolicIfExpression, MetadataSymbolicIndexExpression, MetadataSymbolicPrefixExpression, MetadataSymbolicReferenceExpression, MetadataSymbolicSelectExpression, MetadataSymbolicSpreadExpression, MetadataValue, MethodMetadata, ModuleExportMetadata, ModuleMetadata, VERSION, isClassMetadata, isConstructorMetadata, isFunctionMetadata, isMetadataError, isMetadataGlobalReferenceExpression, isMetadataSymbolicExpression, isMetadataSymbolicReferenceExpression, isMetadataSymbolicSelectExpression, isMethodMetadata} from './schema';
import {ClassMetadata, ConstructorMetadata, FunctionMetadata, MemberMetadata, MetadataEntry, MetadataError, MetadataMap, MetadataSymbolicBinaryExpression, MetadataSymbolicCallExpression, MetadataSymbolicExpression, MetadataSymbolicIfExpression, MetadataSymbolicIndexExpression, MetadataSymbolicPrefixExpression, MetadataSymbolicReferenceExpression, MetadataSymbolicSelectExpression, MetadataSymbolicSpreadExpression, MetadataValue, MethodMetadata, ModuleExportMetadata, ModuleMetadata, VERSION, isClassMetadata, isConstructorMetadata, isFunctionMetadata, isMetadataError, isMetadataGlobalReferenceExpression, isMetadataSymbolicExpression, isMetadataSymbolicReferenceExpression, isMetadataSymbolicSelectExpression, isMethodMetadata} from './schema';
import {Symbols} from './symbols';


Expand Down Expand Up @@ -234,6 +234,7 @@ export class MetadataCollector {
}
}
break;

case ts.SyntaxKind.FunctionDeclaration:
if (!(node.flags & ts.NodeFlags.Export)) {
// Report references to this function as an error.
Expand All @@ -249,22 +250,36 @@ export class MetadataCollector {
break;
}
});

ts.forEachChild(sourceFile, node => {
switch (node.kind) {
case ts.SyntaxKind.ExportDeclaration:
// Record export declarations
const exportDeclaration = <ts.ExportDeclaration>node;
const moduleSpecifier = exportDeclaration.moduleSpecifier;
const {moduleSpecifier, exportClause} = exportDeclaration;

if (!moduleSpecifier) {
// no module specifier -> export {propName as name};
if (exportClause) {
exportClause.elements.forEach(spec => {
const name = spec.name.text;
const propNode = spec.propertyName || spec.name;
const value: MetadataValue = evaluator.evaluateNode(propNode);
if (!metadata) metadata = {};
metadata[name] = recordEntry(value, node);
});
}
}

if (moduleSpecifier && moduleSpecifier.kind == ts.SyntaxKind.StringLiteral) {
// Ignore exports that don't have string literals as exports.
// This is allowed by the syntax but will be flagged as an error by the type checker.
const from = (<ts.StringLiteral>moduleSpecifier).text;
const moduleExport: ModuleExportMetadata = {from};
if (exportDeclaration.exportClause) {
moduleExport.export = exportDeclaration.exportClause.elements.map(
element => element.propertyName ?
{name: element.propertyName.text, as: element.name.text} :
element.name.text);
if (exportClause) {
moduleExport.export = exportClause.elements.map(
spec => spec.propertyName ? {name: spec.propertyName.text, as: spec.name.text} :
spec.name.text);
}
if (!exports) exports = [];
exports.push(moduleExport);
Expand All @@ -281,6 +296,7 @@ export class MetadataCollector {
}
// Otherwise don't record metadata for the class.
break;

case ts.SyntaxKind.FunctionDeclaration:
// Record functions that return a single value. Record the parameter
// names substitution will be performed by the StaticReflector.
Expand All @@ -297,6 +313,7 @@ export class MetadataCollector {
}
}
break;

case ts.SyntaxKind.EnumDeclaration:
if (node.flags & ts.NodeFlags.Export) {
const enumDeclaration = <ts.EnumDeclaration>node;
Expand Down Expand Up @@ -332,14 +349,15 @@ export class MetadataCollector {
} else {
nextDefaultValue =
recordEntry(errorSym('Unsuppported enum member name', member.name), node);
};
}
}
if (writtenMembers) {
if (!metadata) metadata = {};
metadata[enumName] = recordEntry(enumValueHolder, node);
}
}
break;

case ts.SyntaxKind.VariableStatement:
const variableStatement = <ts.VariableStatement>node;
for (const variableDeclaration of variableStatement.declarationList.declarations) {
Expand Down Expand Up @@ -373,7 +391,7 @@ export class MetadataCollector {
}
} else {
// Destructuring (or binding) declarations are not supported,
// var {<identifier>[, <identifer>]+} = <expression>;
// var {<identifier>[, <identifier>]+} = <expression>;
// or
// var [<identifier>[, <identifier}+] = <expression>;
// are not supported.
Expand Down
24 changes: 24 additions & 0 deletions tools/@angular/tsc-wrapped/test/collector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Collector', () => {
'local-symbol-ref-func-dynamic.ts',
'private-enum.ts',
're-exports.ts',
're-exports-2.ts',
'static-field-reference.ts',
'static-method.ts',
'static-method-call.ts',
Expand Down Expand Up @@ -527,6 +528,23 @@ describe('Collector', () => {
]);
});

it('should be able to collect exports with no module specifier', () => {
const source = program.getSourceFile('/re-exports-2.ts');
const metadata = collector.getMetadata(source);
expect(metadata.metadata).toEqual({
OtherModule: {__symbolic: 'reference', module: './static-field-reference', name: 'Foo'},
MyOtherModule: {__symbolic: 'reference', module: './static-field', name: 'MyModule'},
// TODO(vicb): support exported symbols - https://github.com/angular/angular/issues/13473
MyClass: {
__symbolic: 'error',
message: 'Reference to non-exported class',
line: 3,
character: 4,
context: {className: 'MyClass'}
},
});
});

it('should collect an error symbol if collecting a reference to a non-exported symbol', () => {
const source = program.getSourceFile('/local-symbol-ref.ts');
const metadata = collector.getMetadata(source);
Expand Down Expand Up @@ -983,6 +1001,12 @@ const FILES: Directory = {
export {Foo as OtherModule} from './static-field-reference';
export * from 'angular2/core';
`,
're-exports-2.ts': `
import {MyModule} from './static-field';
import {Foo as OtherModule} from './static-field-reference';
class MyClass {}
export {OtherModule, MyModule as MyOtherModule, MyClass};
`,
'local-symbol-ref.ts': `
import {Component, Validators} from 'angular2/core';
Expand Down

0 comments on commit cd03c77

Please sign in to comment.