Skip to content

Commit 73dd8e2

Browse files
anion155danielpza
authored andcommitted
fix: type only import not deleted from result file
closes #9
1 parent 7838d99 commit 73dd8e2

File tree

9 files changed

+38
-36
lines changed

9 files changed

+38
-36
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"build": "tsc",
2828
"prepare": "npm run build",
2929
"release": "standard-version",
30-
"test": "ttsc -p tests/tsconfig.json && node tests/core/index.js"
30+
"test": "ttsc -p tests/tsconfig.json && node tests/out/core/index.js"
3131
},
3232
"dependencies": {
3333
"slash": "^2.0.0"
@@ -37,6 +37,6 @@
3737
"@types/slash": "^2.0.0",
3838
"standard-version": "^4.4.0",
3939
"ttypescript": "^1.5.6",
40-
"typescript": "^3.2.4"
40+
"typescript": "^3.4.5"
4141
}
4242
}

src/index.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
1919
if (match) {
2020
const out = path.resolve.replace(/\*/g, match[1]);
2121
const file = slash(relative(fileDir, resolve(baseUrl, out)));
22-
return file;
22+
return file[0] === "." ? file : `./${file}`;
2323
}
2424
}
2525
return null;
@@ -30,36 +30,14 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
3030
return ts.visitEachChild(node, visit, context);
3131
}
3232
if (
33-
ts.isImportDeclaration(node) &&
34-
ts.isStringLiteral(node.moduleSpecifier)
35-
) {
36-
const file = findFileInPaths(node.moduleSpecifier.text);
37-
if (file) {
38-
return ts.updateImportDeclaration(
39-
node,
40-
node.decorators,
41-
node.modifiers,
42-
node.importClause,
43-
// If it's in the same level or below add the ./
44-
ts.createLiteral(file[0] === "." ? file : `./${file}`)
45-
);
46-
}
47-
}
48-
if (
49-
ts.isExportDeclaration(node) &&
33+
(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
5034
node.moduleSpecifier &&
5135
ts.isStringLiteral(node.moduleSpecifier)
5236
) {
5337
const file = findFileInPaths(node.moduleSpecifier.text);
5438
if (file) {
55-
return ts.updateExportDeclaration(
56-
node,
57-
node.decorators,
58-
node.modifiers,
59-
node.exportClause,
60-
// If it's in the same level or below add the ./
61-
ts.createLiteral(file[0] === "." ? file : `./${file}`)
62-
);
39+
node.moduleSpecifier.text = file;
40+
return node;
6341
}
6442
}
6543
return ts.visitEachChild(node, visit, context);

tests/circular/a.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { B } from "@circular/b";
2+
3+
export class A {
4+
constructor(public name: string) {}
5+
}
6+
7+
export const b = new B();
8+
b.print(new A("This is a random name"));

tests/circular/b.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { A } from "@circular/a";
2+
3+
export class B {
4+
print(a: A) {
5+
console.log(a.name);
6+
}
7+
}

tests/core/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { sum, subs } from '@utils';
1+
import { subs, sum, NoRuntimecodeHere } from "@utils";
2+
import "@circular/b";
3+
import { A } from "@circular/a";
4+
5+
const n: NoRuntimecodeHere = null as any;
26

37
sum(2, 3);
48
subs(2, 3);
9+
const a = new A("");

tests/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
"strict": true,
77

8+
"outDir": "out/",
89
"baseUrl": "./",
910
"paths": {
10-
"@utils/*": ["utils/*"],
11-
"@utils": ["utils"]
11+
"@*": ["*"]
1212
},
1313

1414
"esModuleInterop": true,

tests/utils/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from '@utils/sum';
2-
export * from '@utils/subs';
1+
export * from "@utils/sum";
2+
export * from "@utils/subs";
3+
export * from "@utils/types-only";

tests/utils/types-only.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type NoRuntimecodeHere = "never gonna give you up!";
2+
3+
// throw new Error('Not supposed to be!');

0 commit comments

Comments
 (0)