Skip to content

Commit

Permalink
Merge pull request #1449 from liaoyinglong/fix/imports
Browse files Browse the repository at this point in the history
fix: set flags from old node
  • Loading branch information
kamilmysliwiec committed Dec 13, 2021
2 parents c0a2533 + 9069343 commit f5c62ec
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/compiler/hooks/tsconfig-paths.hook.ts
Expand Up @@ -36,6 +36,7 @@ export function tsconfigPathsBeforeHookFactory(
}
(newNode as any).moduleSpecifier = tsBinary.createLiteral(result);
(newNode as any).moduleSpecifier.parent = (node as any).moduleSpecifier.parent;
(newNode as any).flags = node.flags;
return newNode;
} catch {
return node;
Expand Down
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tsconfig paths hooks should remove unused imports 1`] = `
Map {
"dist/foo.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Foo = void 0;
class Foo {
}
exports.Foo = Foo;
",
"dist/bar.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
exports.Bar = void 0;
class Bar {
}
exports.Bar = Bar;
",
"dist/main.js" => "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
",
}
`;
4 changes: 4 additions & 0 deletions test/lib/compiler/hooks/fixtures/type-imports/src/main.ts
@@ -0,0 +1,4 @@
import { TypeA } from 'src/type-a';
import type { TypeB } from 'src/type-b';
import { TypeC } from './type-c';
import type { TypeD } from './type-d';
@@ -0,0 +1 @@
export type TypeA = number;
@@ -0,0 +1 @@
export type TypeB = number;
@@ -0,0 +1 @@
export type TypeC = number;
@@ -0,0 +1 @@
export type TypeD = number;
1 change: 1 addition & 0 deletions test/lib/compiler/hooks/fixtures/unused-imports/src/bar.ts
@@ -0,0 +1 @@
export class Bar {}
1 change: 1 addition & 0 deletions test/lib/compiler/hooks/fixtures/unused-imports/src/foo.ts
@@ -0,0 +1 @@
export class Foo {}
2 changes: 2 additions & 0 deletions test/lib/compiler/hooks/fixtures/unused-imports/src/main.ts
@@ -0,0 +1,2 @@
import { Foo } from './foo';
import { Bar } from 'src/bar';
55 changes: 55 additions & 0 deletions test/lib/compiler/hooks/tsconfig-paths.hook.spec.ts
@@ -0,0 +1,55 @@
import * as path from 'path';
import * as ts from 'typescript';
import { tsconfigPathsBeforeHookFactory } from '../../../../lib/compiler/hooks/tsconfig-paths.hook';

function createSpec(baseUrl: string, fileNames: string[]) {
const options: ts.CompilerOptions = {
baseUrl,
outDir: path.join(baseUrl, 'dist'),
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.CommonJS,
};

const program = ts.createProgram({
rootNames: fileNames.map((name) => path.join(baseUrl, name)),
options,
});
const output = new Map<string, string>();
program.emit(
undefined,
(fileName, data) => {
output.set(path.relative(baseUrl, fileName), data);
},
undefined,
undefined,
{
before: [tsconfigPathsBeforeHookFactory(options)],
},
);
return output;
}

describe('tsconfig paths hooks', () => {
it('should remove type imports', async () => {
const output = createSpec(path.join(__dirname, './fixtures/type-imports'), [
'src/main.ts',
'src/type-a.ts',
'src/type-b.ts',
'src/type-c.ts',
'src/type-d.ts',
]);
output.forEach((value) => {
expect(value).toEqual(
`"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\n`,
);
});
});

it('should remove unused imports', async () => {
const output = createSpec(
path.join(__dirname, './fixtures/unused-imports'),
['src/main.ts', 'src/foo.ts', 'src/bar.ts'],
);
expect(output).toMatchSnapshot();
});
});

0 comments on commit f5c62ec

Please sign in to comment.