From 56da6be0e9ff9701f4e0dd5e2972539843cde1cf Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 25 Jul 2024 11:41:45 +0800 Subject: [PATCH] fix(es/ast): Accept any case of EsVersion during deserialization (#9329) **Related issue:** - Closes #9287 --- .changeset/cold-chefs-cheat.md | 5 +++ .../fixture/issues-9xxx/9287/input/.swcrc | 8 ++++ .../fixture/issues-9xxx/9287/input/index.js | 1 + .../fixture/issues-9xxx/9287/output/index.js | 1 + crates/swc_ecma_ast/src/lib.rs | 43 ++++++++++++------- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 .changeset/cold-chefs-cheat.md create mode 100644 crates/swc/tests/fixture/issues-9xxx/9287/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-9xxx/9287/input/index.js create mode 100644 crates/swc/tests/fixture/issues-9xxx/9287/output/index.js diff --git a/.changeset/cold-chefs-cheat.md b/.changeset/cold-chefs-cheat.md new file mode 100644 index 000000000000..a65b5304b48a --- /dev/null +++ b/.changeset/cold-chefs-cheat.md @@ -0,0 +1,5 @@ +--- +swc_ecma_ast: patch +--- + +fix(es/base): Accept any case of EsVersion during deserialization diff --git a/crates/swc/tests/fixture/issues-9xxx/9287/input/.swcrc b/crates/swc/tests/fixture/issues-9xxx/9287/input/.swcrc new file mode 100644 index 000000000000..82505b21febd --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9287/input/.swcrc @@ -0,0 +1,8 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript" + }, + "target": "ESNEXT" + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9287/input/index.js b/crates/swc/tests/fixture/issues-9xxx/9287/input/index.js new file mode 100644 index 000000000000..8f8128a22e93 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9287/input/index.js @@ -0,0 +1 @@ +function foo() { } \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9287/output/index.js b/crates/swc/tests/fixture/issues-9xxx/9287/output/index.js new file mode 100644 index 000000000000..b0400ee9b26e --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9287/output/index.js @@ -0,0 +1 @@ +function foo() {} diff --git a/crates/swc_ecma_ast/src/lib.rs b/crates/swc_ecma_ast/src/lib.rs index 966df8481d6e..368e441e6caf 100644 --- a/crates/swc_ecma_ast/src/lib.rs +++ b/crates/swc_ecma_ast/src/lib.rs @@ -107,36 +107,49 @@ impl Take for Invalid { /// Note: This type implements `Serailize` and `Deserialize` if `serde` is /// enabled, instead of requiring `serde-impl` feature. #[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", derive(Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] pub enum EsVersion { - #[cfg_attr(feature = "serde", serde(rename = "es3", alias = "ES3"))] Es3, - #[cfg_attr(feature = "serde", serde(rename = "es5", alias = "ES5"))] #[default] Es5, - #[cfg_attr( - feature = "serde", - serde(rename = "es2015", alias = "ES2015", alias = "ES6", alias = "es6") - )] Es2015, - #[cfg_attr(feature = "serde", serde(rename = "es2016", alias = "ES2016"))] Es2016, - #[cfg_attr(feature = "serde", serde(rename = "es2017", alias = "ES2017"))] Es2017, - #[cfg_attr(feature = "serde", serde(rename = "es2018", alias = "ES2018"))] Es2018, - #[cfg_attr(feature = "serde", serde(rename = "es2019", alias = "ES2019"))] Es2019, - #[cfg_attr(feature = "serde", serde(rename = "es2020", alias = "ES2020"))] Es2020, - #[cfg_attr(feature = "serde", serde(rename = "es2021", alias = "ES2021"))] Es2021, - #[cfg_attr(feature = "serde", serde(rename = "es2022", alias = "ES2022"))] Es2022, - #[cfg_attr(feature = "serde", serde(rename = "esnext", alias = "EsNext"))] EsNext, } +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for EsVersion { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + use serde::de::Error; + + let s = String::deserialize(deserializer)?; + match s.to_lowercase().as_str() { + "es3" => Ok(EsVersion::Es3), + "es5" => Ok(EsVersion::Es5), + "es2015" | "es6" => Ok(EsVersion::Es2015), + "es2016" => Ok(EsVersion::Es2016), + "es2017" => Ok(EsVersion::Es2017), + "es2018" => Ok(EsVersion::Es2018), + "es2019" => Ok(EsVersion::Es2019), + "es2020" => Ok(EsVersion::Es2020), + "es2021" => Ok(EsVersion::Es2021), + "es2022" => Ok(EsVersion::Es2022), + "esnext" => Ok(EsVersion::EsNext), + _ => Err(D::Error::custom(format!("Unknown ES version: {}", s))), + } + } +} + impl EsVersion { pub const fn latest() -> Self { EsVersion::EsNext