Skip to content

fix(find-lazy-modules): Allow for any valid keys/value to be used #1987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions addon/ng2/models/find-lazy-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,47 @@ import * as ts from 'typescript';
import {getSource, findNodes, getContentOfKeyLiteral} from '../utilities/ast-utils';


function flatMap<T, R>(obj: Array<T>, mapFn: (item: T) => R | R[]): Array<R> {
return obj.reduce((arr: R[], current: T) => {
const result = mapFn.call(null, current);
return result !== undefined ? arr.concat(result) : arr;
}, <R[]>[]);
}


export function findLoadChildren(tsFilePath: string): string[] {
const source = getSource(tsFilePath);
const unique: { [path: string]: boolean } = {};

let nodes = flatMap(
findNodes(source, ts.SyntaxKind.ObjectLiteralExpression),
node => findNodes(node, ts.SyntaxKind.PropertyAssignment))
.filter((node: ts.PropertyAssignment) => {
const key = getContentOfKeyLiteral(source, node.name);
if (!key) {
// key is an expression, can't do anything.
return false;
}
return key == 'loadChildren';
})
// Remove initializers that are not files.
.filter((node: ts.PropertyAssignment) => {
return node.initializer.kind === ts.SyntaxKind.StringLiteral;
})
// Get the full text of the initializer.
.map((node: ts.PropertyAssignment) => {
return JSON.parse(node.initializer.getText(source)); // tslint:disable-line
});

return nodes
return (
// Find all object literals.
findNodes(source, ts.SyntaxKind.ObjectLiteralExpression)
// Get all their property assignments.
.map(node => findNodes(node, ts.SyntaxKind.PropertyAssignment))
// Flatten into a single array (from an array of array<property assignments>).
.reduce((prev, curr) => curr ? prev.concat(curr) : prev, [])
// Remove every property assignment that aren't 'loadChildren'.
.filter((node: ts.PropertyAssignment) => {
const key = getContentOfKeyLiteral(source, node.name);
if (!key) {
// key is an expression, can't do anything.
return false;
}
return key == 'loadChildren';
})
// Remove initializers that are not files.
.filter((node: ts.PropertyAssignment) => {
return node.initializer.kind === ts.SyntaxKind.StringLiteral;
})
// Get the full text of the initializer.
.map((node: ts.PropertyAssignment) => {
const literal = node.initializer as ts.StringLiteral;
return literal.text;
})
// Map to the module name itself.
.map((moduleName: string) => moduleName.split('#')[0])
// Only get unique values (there might be multiple modules from a single URL, or a module used
// multiple times).
.filter((value: string) => {
if (unique[value]) {
return false;
} else {
unique[value] = true;
return true;
}
})
.map((moduleName: string) => moduleName.split('#')[0]);
}));
}


Expand Down
8 changes: 2 additions & 6 deletions packages/ast-tools/src/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,9 @@ export function insertAfterLastOccurrence(nodes: ts.Node[], toInsert: string,

export function getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
if (node.kind == ts.SyntaxKind.Identifier) {
return (<ts.Identifier>node).text;
return (node as ts.Identifier).text;
} else if (node.kind == ts.SyntaxKind.StringLiteral) {
try {
return JSON.parse(node.getFullText(source));
} catch (e) {
return null;
}
return (node as ts.StringLiteral).text;
} else {
return null;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/acceptance/find-lazy-module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as mockFs from 'mock-fs';
import {stripIndents} from 'common-tags';
import {expect} from 'chai';

import {findLazyModules} from '../../addon/ng2/models/find-lazy-modules';


describe('find-lazy-module', () => {
beforeEach(() => {
mockFs({
'project-root': {
'fileA.ts': stripIndents`
const r1 = {
"loadChildren": "moduleA"
};
const r2 = {
loadChildren: "moduleB"
};
const r3 = {
'loadChildren': 'moduleC'
};
const r4 = {
"loadChildren": 'app/+workspace/+settings/settings.module#SettingsModule'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hansl - can the value of the loadChildren key be any expression (that evaluates to a string) or must it be a string literal?

cc @ericjim

};
const r5 = {
loadChildren: 'unexistentModule'
};
`,
// Create those files too as they have to exist.
'moduleA.ts': '',
'moduleB.ts': '',
'moduleC.ts': '',
'moduleD.ts': '',
'app': { '+workspace': { '+settings': { 'settings.module.ts': '' } } }
}
});
});
afterEach(() => mockFs.restore());

it('works', () => {
expect(findLazyModules('project-root')).to.eql([
'moduleA',
'moduleB',
'moduleC',
'app/+workspace/+settings/settings.module'
]);
});
});