Skip to content

Commit 252fe25

Browse files
clydinfilipesilva
authored andcommitted
test(@ngtools/webpack): directly test elideImports function
The `elideImports` function's unit tests now directly test the function instead of relying on the `makeTransform` abstraction. This change more closely reflects the current usage of `elideImports`. (cherry picked from commit 98f0ba2)
1 parent c53a178 commit 252fe25

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,50 @@
88
// tslint:disable:no-big-function
99
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
1010
import * as ts from 'typescript';
11-
import { getLastNode } from './ast_helpers';
12-
import { RemoveNodeOperation } from './interfaces';
13-
import { makeTransform } from './make_transform';
11+
import { elideImports } from './elide_imports';
1412
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1513

1614
describe('@ngtools/webpack transformers', () => {
1715
describe('elide_imports', () => {
1816

1917
const dummyNode = `const remove = ''`;
2018

21-
const transformer = (program: ts.Program) => (
22-
makeTransform(
23-
(sourceFile: ts.SourceFile) =>
24-
[new RemoveNodeOperation(sourceFile, getLastNode(sourceFile) as ts.Node)],
25-
() => program.getTypeChecker(),
26-
)
27-
);
19+
// Transformer that removes the last node and then elides unused imports
20+
const transformer = (program: ts.Program) => {
21+
return (context: ts.TransformationContext) => {
22+
23+
return (sourceFile: ts.SourceFile) => {
24+
const lastNode = sourceFile.statements[sourceFile.statements.length - 1];
25+
const updatedSourceFile = context.factory.updateSourceFile(
26+
sourceFile,
27+
ts.setTextRange(
28+
context.factory.createNodeArray(sourceFile.statements.slice(0, -1)),
29+
sourceFile.statements,
30+
),
31+
);
32+
33+
const importRemovals = elideImports(
34+
updatedSourceFile,
35+
[lastNode],
36+
() => program.getTypeChecker(),
37+
context.getCompilerOptions(),
38+
).map((op) => op.target);
39+
if (importRemovals.length > 0) {
40+
return ts.visitEachChild(
41+
updatedSourceFile,
42+
function visitForRemoval(node): ts.Node | undefined {
43+
return importRemovals.includes(node)
44+
? undefined
45+
: ts.visitEachChild(node, visitForRemoval, context);
46+
},
47+
context,
48+
);
49+
}
50+
51+
return updatedSourceFile;
52+
};
53+
};
54+
};
2855

2956
const additionalFiles: Record<string, string> = {
3057
'const.ts': `

0 commit comments

Comments
 (0)