Skip to content
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
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}`);
});
});
});
});
});