|
8 | 8 | // tslint:disable:no-big-function
|
9 | 9 | import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
|
10 | 10 | 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'; |
14 | 12 | import { createTypescriptContext, transformTypescript } from './spec_helpers';
|
15 | 13 |
|
16 | 14 | describe('@ngtools/webpack transformers', () => {
|
17 | 15 | describe('elide_imports', () => {
|
18 | 16 |
|
19 | 17 | const dummyNode = `const remove = ''`;
|
20 | 18 |
|
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 | + }; |
28 | 55 |
|
29 | 56 | const additionalFiles: Record<string, string> = {
|
30 | 57 | 'const.ts': `
|
|
0 commit comments