Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
fix(@angular-devkit/build-optimizer): avoid marking downlevel namespaces
Browse files Browse the repository at this point in the history
Fixes: #934
  • Loading branch information
clydin committed May 29, 2018
1 parent 82e3699 commit a9310d1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.Source
}

// Add pure function comment to top level functions.
if (topLevelFunctions.indexOf(node) !== -1) {
if (topLevelFunctions.has(node)) {
const newNode = ts.addSyntheticLeadingComment(
node, ts.SyntaxKind.MultiLineCommentTrivia, pureFunctionComment, false);

Expand All @@ -49,13 +49,12 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.Source
};
}

export function findTopLevelFunctions(parentNode: ts.Node): ts.Node[] {
const topLevelFunctions: ts.Node[] = [];
export function findTopLevelFunctions(parentNode: ts.Node): Set<ts.Node> {
const topLevelFunctions = new Set<ts.Node>();

function cb(node: ts.Node) {
// Stop recursing into this branch if it's a function expression or declaration
if (node.kind === ts.SyntaxKind.FunctionDeclaration
|| node.kind === ts.SyntaxKind.FunctionExpression) {
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
return;
}

Expand All @@ -71,9 +70,24 @@ export function findTopLevelFunctions(parentNode: ts.Node): ts.Node[] {
}

if (noPureComment) {
if (innerNode.kind === ts.SyntaxKind.CallExpression
|| innerNode.kind === ts.SyntaxKind.NewExpression) {
topLevelFunctions.push(node);
if (ts.isNewExpression(innerNode)) {
topLevelFunctions.add(node);
} else if (ts.isCallExpression(innerNode)) {
let expression: ts.Expression = innerNode.expression;
while (expression && ts.isParenthesizedExpression(expression)) {
expression = expression.expression;
}
if (expression) {
if (ts.isFunctionExpression(expression)) {
// Skip IIFE's with arguments
// This could be improved to check if there are any references to variables
if (innerNode.arguments.length === 0) {
topLevelFunctions.add(node);
}
} else {
topLevelFunctions.add(node);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('prefix-functions', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('doesn\'t adds comment when inside function declarations or expressions', () => {
it('doesn\'t add comment when inside function declarations or expressions', () => {
const input = tags.stripIndent`
function funcDecl() {
var newClazz = Clazz();
Expand All @@ -105,5 +105,24 @@ describe('prefix-functions', () => {

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('doesn\'t add comment to downlevel namespaces', () => {
const input = tags.stripIndent`
function MyFunction() { }
(function (MyFunction) {
function subFunction() { }
MyFunction.subFunction = subFunction;
})(MyFunction || (MyFunctionn = {}));
export { MyFunction };
`;
const output = tags.stripIndent`
${emptyImportsComment}
${input}
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
});

0 comments on commit a9310d1

Please sign in to comment.