Skip to content

Commit

Permalink
fix(@ngtools/webpack): don't elide used imports for transformed Short…
Browse files Browse the repository at this point in the history
…handPropertyAssignment

NGTSC, will transform `ShorthandPropertyAssignment` to `PropertyAssignment`, with this change we handle such cases and retain the import which previously was dropped.

Closes #18149 and closes #17347

(cherry picked from commit 6ac000f)
  • Loading branch information
alan-agius4 committed Jul 6, 2020
1 parent 940c7cb commit f989707
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ export function elideImports(
} else {
switch (node.kind) {
case ts.SyntaxKind.Identifier:
symbol = typeChecker.getSymbolAtLocation(node);
const parent = node.parent;
if (parent && ts.isShorthandPropertyAssignment(parent)) {
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(parent);
if (shorthandSymbol) {
symbol = shorthandSymbol;
}
} else {
symbol = typeChecker.getSymbolAtLocation(node);
}
break;
case ts.SyntaxKind.ExportSpecifier:
symbol = typeChecker.getExportSpecifierLocalTargetSymbol(node as ts.ExportSpecifier);
Expand Down
39 changes: 39 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('@ngtools/webpack transformers', () => {

const additionalFiles: Record<string, string> = {
'const.ts': `
export const animations = [];
export const promise = () => null;
export const take = () => null;
export default promise;
Expand Down Expand Up @@ -528,6 +529,44 @@ describe('@ngtools/webpack transformers', () => {

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

describe('NGTSC - ShorthandPropertyAssignment to PropertyAssignment', () => {
const transformShorthandPropertyAssignment = (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
const visit: ts.Visitor = node => {
if (ts.isShorthandPropertyAssignment(node)) {
return ts.createPropertyAssignment(node.name, node.name);
}

return ts.visitEachChild(node, child => visit(child), context);
};

return node => ts.visitNode(node, visit);
};

it('should not elide import when ShorthandPropertyAssignment is transformed to PropertyAssignment', () => {
const input = tags.stripIndent`
import { animations } from './const';
const used = {
animations
}
${dummyNode}
`;

const output = tags.stripIndent`
import { animations } from './const';
const used = { animations: animations };
`;

const { program, compilerHost } = createTypescriptContext(input, additionalFiles);
const result = transformTypescript(undefined, [
transformShorthandPropertyAssignment,
transformer(program),
], program, compilerHost);

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

0 comments on commit f989707

Please sign in to comment.