Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f989707

Browse files
committedJul 6, 2020
fix(@ngtools/webpack): don't elide used imports for transformed ShorthandPropertyAssignment
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)
1 parent 940c7cb commit f989707

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎packages/ngtools/webpack/src/transformers/elide_imports.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ export function elideImports(
8585
} else {
8686
switch (node.kind) {
8787
case ts.SyntaxKind.Identifier:
88-
symbol = typeChecker.getSymbolAtLocation(node);
88+
const parent = node.parent;
89+
if (parent && ts.isShorthandPropertyAssignment(parent)) {
90+
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(parent);
91+
if (shorthandSymbol) {
92+
symbol = shorthandSymbol;
93+
}
94+
} else {
95+
symbol = typeChecker.getSymbolAtLocation(node);
96+
}
8997
break;
9098
case ts.SyntaxKind.ExportSpecifier:
9199
symbol = typeChecker.getExportSpecifierLocalTargetSymbol(node as ts.ExportSpecifier);

‎packages/ngtools/webpack/src/transformers/elide_imports_spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('@ngtools/webpack transformers', () => {
2828

2929
const additionalFiles: Record<string, string> = {
3030
'const.ts': `
31+
export const animations = [];
3132
export const promise = () => null;
3233
export const take = () => null;
3334
export default promise;
@@ -528,6 +529,44 @@ describe('@ngtools/webpack transformers', () => {
528529

529530
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
530531
});
532+
533+
describe('NGTSC - ShorthandPropertyAssignment to PropertyAssignment', () => {
534+
const transformShorthandPropertyAssignment = (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
535+
const visit: ts.Visitor = node => {
536+
if (ts.isShorthandPropertyAssignment(node)) {
537+
return ts.createPropertyAssignment(node.name, node.name);
538+
}
539+
540+
return ts.visitEachChild(node, child => visit(child), context);
541+
};
542+
543+
return node => ts.visitNode(node, visit);
544+
};
545+
546+
it('should not elide import when ShorthandPropertyAssignment is transformed to PropertyAssignment', () => {
547+
const input = tags.stripIndent`
548+
import { animations } from './const';
549+
const used = {
550+
animations
551+
}
552+
553+
${dummyNode}
554+
`;
555+
556+
const output = tags.stripIndent`
557+
import { animations } from './const';
558+
const used = { animations: animations };
559+
`;
560+
561+
const { program, compilerHost } = createTypescriptContext(input, additionalFiles);
562+
const result = transformTypescript(undefined, [
563+
transformShorthandPropertyAssignment,
564+
transformer(program),
565+
], program, compilerHost);
566+
567+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
568+
});
569+
});
531570
});
532571
});
533572
});

0 commit comments

Comments
 (0)
Please sign in to comment.