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

fix(es/codegen): Implement codegen for static import assertions #3113

Merged
merged 14 commits into from
Dec 28, 2021
Merged
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