Skip to content

Commit

Permalink
feat(codegen): correctly print type-only imports/exports (#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Apr 15, 2024
1 parent 82e00bc commit fd5002b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.add_source_mapping(self.span.start);
p.print_str(b"import ");
if p.options.enable_typescript && self.import_kind.is_type() {
p.print_str(b"type ");
}
if let Some(specifiers) = &self.specifiers {
if specifiers.is_empty() {
p.print(b'\'');
Expand Down Expand Up @@ -770,6 +773,10 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ImportDeclaration<'a> {
p.print(b'{');
}

if p.options.enable_typescript && spec.import_kind.is_type() {
p.print_str(b"type ");
}

let imported_name = match &spec.imported {
ModuleExportName::Identifier(identifier) => {
identifier.gen(p, ctx);
Expand Down Expand Up @@ -842,6 +849,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {
return;
}
p.print_str(b"export ");
if p.options.enable_typescript && self.export_kind.is_type() {
p.print_str(b"type ");
}
match &self.declaration {
Some(decl) => decl.gen(p, ctx),
None => {
Expand All @@ -866,6 +876,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportSpecifier<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if p.options.enable_typescript && self.export_kind.is_type() {
p.print_str(b"type ");
}
self.local.gen(p, ctx);
if self.local.name() != self.exported.name() {
p.print_str(b" as ");
Expand All @@ -892,6 +905,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportAllDeclaration<'a> {
return;
}
p.print_str(b"export ");
if p.options.enable_typescript && self.export_kind.is_type() {
p.print_str(b"type ");
}
p.print(b'*');

if let Some(exported) = &self.exported {
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ fn typescript() {
test_ts("let x: string['length'] = 123;", "let x: string['length'] = 123;\n", false);

test_ts("function isString(value: unknown): asserts value is string {\n\tif (typeof value !== 'string') {\n\t\tthrow new Error('Not a string');\n\t}\n}", "function isString(value: unknown): asserts value is string {\n\tif (typeof value !== 'string') {\n\t\tthrow new Error('Not a string');\n\t}\n}\n", false);

// type-only imports/exports
test_ts("import type { Foo } from 'foo';", "import type {Foo} from 'foo';\n", false);
test_ts("import { Foo, type Bar } from 'foo';", "import {Foo,type Bar} from 'foo';\n", false);
test_ts("export { Foo, type Bar } from 'foo';", "export { Foo, type Bar } from 'foo';", false);
}

0 comments on commit fd5002b

Please sign in to comment.