Skip to content

Commit

Permalink
fix(tsc-wrapped): skip collecting metadata for default functions
Browse files Browse the repository at this point in the history
Fixes: #17518
  • Loading branch information
chuckjaz authored and hansl committed Jun 20, 2017
1 parent 59299de commit 3390648
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tools/@angular/tsc-wrapped/src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class MetadataCollector {
// Record functions that return a single value. Record the parameter
// names substitution will be performed by the StaticReflector.
const functionDeclaration = <ts.FunctionDeclaration>node;
if (isExported(functionDeclaration)) {
if (isExported(functionDeclaration) && functionDeclaration.name) {
if (!metadata) metadata = {};
const name = exportedName(functionDeclaration);
const maybeFunc = maybeGetSimpleFunction(functionDeclaration);
Expand Down
52 changes: 44 additions & 8 deletions tools/@angular/tsc-wrapped/test/collector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,6 @@ describe('Collector', () => {
});

describe('with interpolations', () => {
function createSource(text: string): ts.SourceFile {
return ts.createSourceFile('', text, ts.ScriptTarget.Latest, true);
}

function e(expr: string, prefix?: string) {
const source = createSource(`${prefix || ''} export let value = ${expr};`);
const metadata = collector.getMetadata(source);
Expand Down Expand Up @@ -821,17 +817,53 @@ describe('Collector', () => {

describe('regerssion', () => {
it('should be able to collect a short-hand property value', () => {
const source = ts.createSourceFile(
'', `
const source = createSource(`
const children = { f1: 1 };
export const r = [
{path: ':locale', children}
];
`,
ts.ScriptTarget.Latest, true);
`);
const metadata = collector.getMetadata(source);
expect(metadata.metadata).toEqual({r: [{path: ':locale', children: {f1: 1}}]});
});

// #17518
it('should skip a default function', () => {
const source = createSource(`
export default function () {
const mainRoutes = [
{name: 'a', abstract: true, component: 'main'},
{name: 'a.welcome', url: '/welcome', component: 'welcome'}
];
return mainRoutes;
}`);
const metadata = collector.getMetadata(source);
expect(metadata).toBeUndefined();
});

it('should skip a named default export', () => {
const source = createSource(`
function mainRoutes() {
const mainRoutes = [
{name: 'a', abstract: true, component: 'main'},
{name: 'a.welcome', url: '/welcome', component: 'welcome'}
];
return mainRoutes;
}
exports = foo;
`);
const metadata = collector.getMetadata(source);
expect(metadata).toBeUndefined();
});
});

function override(fileName: string, content: string) {
Expand Down Expand Up @@ -1321,3 +1353,7 @@ const FILES: Directory = {
}
}
};

function createSource(text: string): ts.SourceFile {
return ts.createSourceFile('', text, ts.ScriptTarget.Latest, true);
}

0 comments on commit 3390648

Please sign in to comment.