Skip to content

Commit

Permalink
fix(es): Handle import assertions correctly (#3113)
Browse files Browse the repository at this point in the history
swc_ecma_codegen:
 - Implement codegen for static import assertions.

swc_ecma_transforms_proposal:
  - `import_assertions`: Support `export`s.

swc:
 - Add `jsc.experimental.keep_import_assertions`.

node-swc:
 - Fix typo.
  • Loading branch information
andreubotella authored Dec 28, 2021
1 parent cec325b commit c9adf03
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
7 changes: 6 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ impl Options {
}),
syntax.decorators()
),
import_assertions(),
// The transform strips import assertions, so it's only enabled if
// keep_import_assertions is false.
Optional::new(import_assertions(), !experimental.keep_import_assertions),
// Do a resolver pass after decorators as it might
// emit runtime declarations and do it before
// type stripping as we need to know scope information
Expand Down Expand Up @@ -932,6 +934,9 @@ pub struct JscExperimental {
/// This requires cargo feature `plugin`.
#[serde(default)]
pub plugins: Option<Vec<PluginConfig>>,
/// If true, keeps import assertions in the output.
#[serde(default)]
pub keep_import_assertions: bool,
}

impl Merge for JscExperimental {
Expand Down
23 changes: 23 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ where
}

emit!(n.src);

if let Some(asserts) = &n.asserts {
formatting_space!();
keyword!("assert");
formatting_space!();
emit!(asserts);
}

formatting_semi!();
}

Expand Down Expand Up @@ -402,6 +410,13 @@ where
keyword!("from");
formatting_space!();
emit!(src);

if let Some(asserts) = &node.asserts {
formatting_space!();
keyword!("assert");
formatting_space!();
emit!(asserts);
}
}
formatting_semi!();
}
Expand All @@ -417,6 +432,14 @@ where
keyword!("from");
formatting_space!();
emit!(node.src);

if let Some(asserts) = &node.asserts {
formatting_space!();
keyword!("assert");
formatting_space!();
emit!(asserts);
}

formatting_semi!();
}

Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issues/3110/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from "./data.json" assert { type: "json" };
export { default as data2 } from "./data2.json" assert { type: "json" };
export * as data3 from "./data3.json" assert { type: "json" };

console.log(data);
10 changes: 10 additions & 0 deletions crates/swc_ecma_codegen/tests/fixture/issues/3110/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import data from "./data.json" assert {
type: "json"
};
export { default as data2 } from "./data2.json" assert {
type: "json"
};
export * as data3 from "./data3.json" assert {
type: "json"
};
console.log(data);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import data from"./data.json"assert{type:"json"};export{default as data2}from"./data2.json"assert{type:"json"};export*as data3 from"./data3.json"assert{type:"json"};console.log(data)
10 changes: 9 additions & 1 deletion crates/swc_ecma_transforms_proposal/src/import_assertions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use swc_ecma_ast::ImportDecl;
use swc_ecma_ast::{ExportAll, ImportDecl, NamedExport};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut};

pub fn import_assertions() -> impl VisitMut + Fold {
Expand All @@ -12,4 +12,12 @@ impl VisitMut for ImportAssertions {
fn visit_mut_import_decl(&mut self, n: &mut ImportDecl) {
n.asserts = None;
}

fn visit_mut_export_all(&mut self, n: &mut ExportAll) {
n.asserts = None;
}

fn visit_mut_named_export(&mut self, n: &mut NamedExport) {
n.asserts = None;
}
}
39 changes: 39 additions & 0 deletions crates/swc_ecma_transforms_proposal/tests/import_assertions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use swc_ecma_parser::{EsConfig, Syntax};
use swc_ecma_transforms_proposal::import_assertions;
use swc_ecma_transforms_testing::test;
use swc_ecma_visit::Fold;

fn tr() -> impl Fold {
import_assertions()
}

fn syntax() -> Syntax {
Syntax::Es(EsConfig {
import_assertions: true,
..Default::default()
})
}

test!(
syntax(),
|_| tr(),
import_with_assertions,
r#"import test from "./test.json" assert {type: "json"};"#,
r#"import test from "./test.json";"#
);

test!(
syntax(),
|_| tr(),
named_export_with_assertions,
r#"export {default as test} from "./test.json" assert {type: "json"};"#,
r#"export {default as test} from "./test.json";"#
);

test!(
syntax(),
|_| tr(),
export_all_with_assertions,
r#"export * from "./test.json" assert {type: "json"};"#,
r#"export * from "./test.json";"#
);
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn identity(entry: PathBuf) {
decorators_before_export: true,
export_default_from: true,
private_in_object: true,
import_assertions: true,
..Default::default()
}),
(&*js_fm).into(),
Expand Down
5 changes: 3 additions & 2 deletions node-swc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ export interface JscConfig {
*/
keepClassNames?: boolean

experimetal?: {
optimizeHygiene?: boolean
experimental?: {
optimizeHygiene?: boolean,
keepImportAssertions?: boolean
},

baseUrl?: string
Expand Down

1 comment on commit c9adf03

@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: c9adf03 Previous: cec325b Ratio
full_es2015 187498580 ns/iter (± 7260432) 189076559 ns/iter (± 7655898) 0.99
full_es2016 155759014 ns/iter (± 14395507) 156019303 ns/iter (± 6551378) 1.00
full_es2017 144298043 ns/iter (± 9650998) 160841750 ns/iter (± 5900545) 0.90
full_es2018 146159690 ns/iter (± 22739513) 161142346 ns/iter (± 9944646) 0.91
full_es2019 157485032 ns/iter (± 4631327) 160272282 ns/iter (± 5997001) 0.98
full_es2020 155930256 ns/iter (± 28949146) 161095291 ns/iter (± 8169884) 0.97
full_es3 191438500 ns/iter (± 25794382) 224226893 ns/iter (± 9766868) 0.85
full_es5 195575677 ns/iter (± 25387112) 201317059 ns/iter (± 10338457) 0.97
parser 689566 ns/iter (± 18212) 689438 ns/iter (± 21498) 1.00

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

Please sign in to comment.