Skip to content

Commit

Permalink
fix(es/codegen): Fix codegen of type-only imports (#8070)
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 authored Oct 6, 2023
1 parent a6527bf commit 6f45b46
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
11 changes: 11 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ where
srcmap!(n, true);

keyword!("import");

if n.type_only {
space!();
keyword!("type");
}

let starts_with_ident = !n.specifiers.is_empty()
&& match &n.specifiers[0] {
ImportSpecifier::Default(_) => true,
Expand Down Expand Up @@ -328,6 +334,11 @@ where
fn emit_import_specific(&mut self, node: &ImportNamedSpecifier) -> Result {
srcmap!(node, true);

if node.is_type_only {
keyword!("type");
space!();
}

if let Some(ref imported) = node.imported {
emit!(imported);
space!();
Expand Down
51 changes: 51 additions & 0 deletions crates/swc_ecma_codegen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,57 @@ export default {
);
}

#[test]
fn emit_type_import_statement_named() {
let from = r#"
import type { X } from "y";
"#;
let to = r#"
import type { X } from "y";
"#;

let out = parse_then_emit(
from,
Default::default(),
Syntax::Typescript(Default::default()),
);
assert_eq!(DebugUsingDisplay(out.trim()), DebugUsingDisplay(to.trim()),);
}

#[test]
fn emit_type_import_statement_default() {
let from = r#"
import type X from "y";
"#;
let to = r#"
import type X from "y";
"#;

let out = parse_then_emit(
from,
Default::default(),
Syntax::Typescript(Default::default()),
);
assert_eq!(DebugUsingDisplay(out.trim()), DebugUsingDisplay(to.trim()),);
}

#[test]
fn emit_type_import_specifier() {
let from = r#"
import { type X } from "y";
"#;
let to = r#"
import { type X } from "y";
"#;

let out = parse_then_emit(
from,
Default::default(),
Syntax::Typescript(Default::default()),
);
assert_eq!(DebugUsingDisplay(out.trim()), DebugUsingDisplay(to.trim()),);
}

#[testing::fixture("tests/str-lits/**/*.txt")]
fn test_str_lit(input: PathBuf) {
test_str_lit_inner(input)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Foo__2 } from "./foo.ts";
import type { Foo__2 } from "./foo.ts";
function _bar__2(...Foo__3: Foo__2) {
console.log(Foo__3);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as F__2 from "foo";
import { Thing__2 } from "anywhere";
import type { Thing__2 } from "anywhere";
console.log(F__2);
export const inner__2 = <F__3>(a__3: F__3)=>{
console.log(F__2);
Expand Down

1 comment on commit 6f45b46

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 6f45b46 Previous: 9b9bc51 Ratio
es/full/bugs-1 282514 ns/iter (± 4225) 281896 ns/iter (± 2888) 1.00
es/full/minify/libraries/antd 1386728873 ns/iter (± 9995049) 1368860194 ns/iter (± 21046093) 1.01
es/full/minify/libraries/d3 288088867 ns/iter (± 5470010) 282043663 ns/iter (± 5237456) 1.02
es/full/minify/libraries/echarts 1113698097 ns/iter (± 16152112) 1097919520 ns/iter (± 17252749) 1.01
es/full/minify/libraries/jquery 85741932 ns/iter (± 664922) 85427553 ns/iter (± 430722) 1.00
es/full/minify/libraries/lodash 99824424 ns/iter (± 888801) 98965167 ns/iter (± 791079) 1.01
es/full/minify/libraries/moment 50985872 ns/iter (± 281460) 50550306 ns/iter (± 295107) 1.01
es/full/minify/libraries/react 18345503 ns/iter (± 76658) 18095713 ns/iter (± 79070) 1.01
es/full/minify/libraries/terser 226760805 ns/iter (± 2469792) 222335122 ns/iter (± 1490630) 1.02
es/full/minify/libraries/three 404891294 ns/iter (± 4553182) 400503741 ns/iter (± 4215029) 1.01
es/full/minify/libraries/typescript 2733751010 ns/iter (± 15200497) 2719929251 ns/iter (± 25491532) 1.01
es/full/minify/libraries/victory 595722007 ns/iter (± 5934064) 598221240 ns/iter (± 10405941) 1.00
es/full/minify/libraries/vue 123224184 ns/iter (± 1123913) 122203614 ns/iter (± 881329) 1.01
es/full/codegen/es3 34550 ns/iter (± 1360) 33996 ns/iter (± 154) 1.02
es/full/codegen/es5 34544 ns/iter (± 97) 33975 ns/iter (± 122) 1.02
es/full/codegen/es2015 34398 ns/iter (± 115) 33995 ns/iter (± 78) 1.01
es/full/codegen/es2016 34497 ns/iter (± 79) 33985 ns/iter (± 73) 1.02
es/full/codegen/es2017 34382 ns/iter (± 50) 33912 ns/iter (± 145) 1.01
es/full/codegen/es2018 34433 ns/iter (± 67) 33939 ns/iter (± 104) 1.01
es/full/codegen/es2019 34392 ns/iter (± 101) 34023 ns/iter (± 87) 1.01
es/full/codegen/es2020 34488 ns/iter (± 193) 34276 ns/iter (± 68) 1.01
es/full/all/es3 169514498 ns/iter (± 1055526) 167396673 ns/iter (± 1242521) 1.01
es/full/all/es5 161745271 ns/iter (± 1593483) 159965663 ns/iter (± 1700058) 1.01
es/full/all/es2015 120043806 ns/iter (± 622415) 120167091 ns/iter (± 680968) 1.00
es/full/all/es2016 119616869 ns/iter (± 1536784) 119501468 ns/iter (± 1288317) 1.00
es/full/all/es2017 119092626 ns/iter (± 1127729) 118605730 ns/iter (± 685668) 1.00
es/full/all/es2018 115811417 ns/iter (± 897926) 116533170 ns/iter (± 1201080) 0.99
es/full/all/es2019 116079690 ns/iter (± 634166) 115530801 ns/iter (± 758867) 1.00
es/full/all/es2020 112017681 ns/iter (± 642547) 111308252 ns/iter (± 1906724) 1.01
es/full/parser 512793 ns/iter (± 5126) 508101 ns/iter (± 5180) 1.01
es/full/base/fixer 17767 ns/iter (± 144) 18669 ns/iter (± 164) 0.95
es/full/base/resolver_and_hygiene 80966 ns/iter (± 204) 81531 ns/iter (± 493) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.