Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for TS type-only imports and exports #532

Merged
merged 1 commit into from
May 11, 2020
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
11 changes: 11 additions & 0 deletions src/transformers/CJSImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ export default class CJSImportTransformer extends Transformer {
} else if (this.tokens.matches2(tt._export, tt.star)) {
this.processExportStar();
return true;
} else if (
this.tokens.matches3(tt._export, tt.name, tt.braceL) &&
this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, ContextualKeyword._type)
) {
// TS `export type {` case: just remove the export entirely.
this.tokens.removeInitialToken();
while (!this.tokens.matches1(tt.braceR)) {
this.tokens.removeToken();
}
this.tokens.removeToken();
return true;
} else {
throw new Error("Unrecognized export syntax.");
}
Expand Down
12 changes: 12 additions & 0 deletions src/transformers/ESMImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export default class ESMImportTransformer extends Transformer {
if (this.tokens.matches2(tt._export, tt.braceL)) {
return this.processNamedExports();
}
if (
this.tokens.matches3(tt._export, tt.name, tt.braceL) &&
this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, ContextualKeyword._type)
) {
// TS `export type {` case: just remove the export entirely.
this.tokens.removeInitialToken();
while (!this.tokens.matches1(tt.braceR)) {
this.tokens.removeToken();
}
this.tokens.removeToken();
return true;
}
return false;
}

Expand Down
83 changes: 80 additions & 3 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1962,15 +1962,92 @@ describe("typescript transform", () => {
);
});

it("parses and removes import type statements", () => {
it("parses and removes import type statements in CJS mode", () => {
assertTypeScriptResult(
`
import type foo from 'foo';
console.log(foo);
import bar from 'bar';
console.log(foo, bar);
`,
`"use strict";${IMPORT_DEFAULT_PREFIX}

var _bar = require('bar'); var _bar2 = _interopRequireDefault(_bar);
console.log(foo, _bar2.default);
`,
);
});

it("parses and removes named import type statements in CJS mode", () => {
assertTypeScriptResult(
`
import type {foo} from 'foo';
import {bar} from 'bar';
console.log(foo, bar);
`,
`"use strict";

console.log(foo);
var _bar = require('bar');
console.log(foo, _bar.bar);
`,
);
});

it("parses and removes import type statements in ESM mode", () => {
assertTypeScriptESMResult(
`
import type foo from 'foo';
import bar from 'bar';
console.log(foo, bar);
`,
`

import bar from 'bar';
console.log(foo, bar);
`,
);
});

it("parses and removes named import type statements in ESM mode", () => {
assertTypeScriptESMResult(
`
import type {foo} from 'foo';
import {bar} from 'bar';
console.log(foo, bar);
`,
`

import {bar} from 'bar';
console.log(foo, bar);
`,
);
});

it("parses and removes export type statements in CJS mode", () => {
assertTypeScriptResult(
`
import T from './T';
let x: T;
export type {T};
`,
`"use strict";${ESMODULE_PREFIX}${IMPORT_DEFAULT_PREFIX}
var _T = require('./T'); var _T2 = _interopRequireDefault(_T);
let x;
;
`,
);
});

it("parses and removes export type statements in ESM mode", () => {
assertTypeScriptESMResult(
`
import T from './T';
let x: T;
export type {T};
`,
`
import T from './T';
let x;
;
`,
);
});
Expand Down