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
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)
6 changes: 6 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ pub enum SyntaxError {

NumericSeparatorIsAllowedOnlyBetweenTwoDigits,

ImportAssertions,

TS1003,
TS1005,
TS1009,
Expand Down Expand Up @@ -430,6 +432,10 @@ impl SyntaxError {
.into()
}

SyntaxError::ImportAssertions => "import assertions require target to es2022 or \
higher and importAssertions:true for ecmascript"
.into(),

SyntaxError::TS1056 => {
"jsc.target should be es5 or upper to use getter / setter".into()
}
Expand Down
20 changes: 18 additions & 2 deletions crates/swc_ecma_parser/src/parser/stmt/module_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,16 @@ impl<'a, I: Tokens> Parser<I> {
let _ = cur!(self, false);
let asserts = if self.input.syntax().import_assertions()
&& !self.input.had_line_break_before_cur()
&& eat!(self, "assert")
&& is!(self, "assert")
{
let valid = self.target() >= EsVersion::Es2022;
andreubotella marked this conversation as resolved.
Show resolved Hide resolved

if !valid {
self.emit_err(self.input.cur_span(), SyntaxError::ImportAssertions);
}

eat!(self, "assert");

match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(v),
_ => unreachable!(),
Expand Down Expand Up @@ -755,8 +763,16 @@ impl<'a, I: Tokens> Parser<I> {
let _ = cur!(self, false);
let asserts = if self.input.syntax().import_assertions()
&& !self.input.had_line_break_before_cur()
&& eat!(self, "assert")
&& is!(self, "assert")
{
let valid = self.target() >= EsVersion::Es2022;

if !valid {
self.emit_err(self.input.cur_span(), SyntaxError::ImportAssertions);
}

eat!(self, "assert");

match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(v),
_ => unreachable!(),
Expand Down