From 89235b8294dedb4dd50c85e2a3b3ce41bddac85e Mon Sep 17 00:00:00 2001 From: Austaras Date: Mon, 17 Jan 2022 05:20:02 -0800 Subject: [PATCH] fix(es/compat): Apply `static_blocks` before `class_properties` (#3292) --- crates/swc_ecma_preset_env/src/lib.rs | 4 +- .../fixtures/transform/static-block/input.mjs | 5 ++ .../transform/static-block/options.json | 10 +++ .../transform/static-block/output.mjs | 8 +++ crates/swc_ecma_preset_env/tests/test.rs | 62 ++++++++++--------- .../src/es2022/mod.rs | 2 +- 6 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/input.mjs create mode 100644 crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/options.json create mode 100644 crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/output.mjs diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index 84bec8ac4ea..e9fea0c557c 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -87,12 +87,14 @@ where // Proposals // ES2022 + // static block needs to be placed before class property + // because it transforms into private static property + let pass = add!(pass, ClassStaticBlock, es2022::static_blocks()); let pass = add!( pass, ClassProperties, es2022::class_properties(es2022::class_properties::Config { loose }) ); - let pass = add!(pass, ClassStaticBlock, es2022::static_blocks()); let pass = add!(pass, PrivatePropertyInObject, es2022::private_in_object()); // ES2021 diff --git a/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/input.mjs b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/input.mjs new file mode 100644 index 00000000000..6b23b26dfa4 --- /dev/null +++ b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/input.mjs @@ -0,0 +1,5 @@ +class A { + static { + this.abc = 123 + } +} diff --git a/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/options.json b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/options.json new file mode 100644 index 00000000000..f3156d23133 --- /dev/null +++ b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/options.json @@ -0,0 +1,10 @@ +{ + "presets": [ + [ + "../../../../lib", + { + "targets": "chrome 70" + } + ] + ] +} diff --git a/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/output.mjs b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/output.mjs new file mode 100644 index 00000000000..ff24e88d175 --- /dev/null +++ b/crates/swc_ecma_preset_env/tests/fixtures/transform/static-block/output.mjs @@ -0,0 +1,8 @@ +class A { +} +var __ = { + writable: true, + value: (()=>{ + A.abc = 123; + })() +}; diff --git a/crates/swc_ecma_preset_env/tests/test.rs b/crates/swc_ecma_preset_env/tests/test.rs index a5a290faabd..12e10a33680 100644 --- a/crates/swc_ecma_preset_env/tests/test.rs +++ b/crates/swc_ecma_preset_env/tests/test.rs @@ -12,12 +12,14 @@ use std::{ path::{Path, PathBuf}, }; use swc_common::{ - collections::AHashMap, comments::SingleThreadedComments, input::StringInput, FromVariant, Mark, + chain, collections::AHashMap, comments::SingleThreadedComments, input::StringInput, + FromVariant, Mark, }; use swc_ecma_ast::*; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{EsConfig, Parser, Syntax}; use swc_ecma_preset_env::{preset_env, Config, FeatureOrModule, Mode, Targets, Version}; +use swc_ecma_transforms::fixer; use swc_ecma_utils::drop_span; use swc_ecma_visit::{as_folder, FoldWith, VisitMut}; use testing::{NormalizedOutput, Tester}; @@ -119,34 +121,37 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { Tester::new() .print_errors(|cm, handler| { - let mut pass = preset_env( - Mark::fresh(Mark::root()), - Some(SingleThreadedComments::default()), - Config { - debug: c.debug, - mode: match c.use_built_ins { - UseBuiltIns::Bool(false) => None, - UseBuiltIns::Str(ref s) if s == "usage" => Some(Mode::Usage), - UseBuiltIns::Str(ref s) if s == "entry" => Some(Mode::Entry), - v => unreachable!("invalid: {:?}", v), + let mut pass = chain!( + preset_env( + Mark::fresh(Mark::root()), + Some(SingleThreadedComments::default()), + Config { + debug: c.debug, + mode: match c.use_built_ins { + UseBuiltIns::Bool(false) => None, + UseBuiltIns::Str(ref s) if s == "usage" => Some(Mode::Usage), + UseBuiltIns::Str(ref s) if s == "entry" => Some(Mode::Entry), + v => unreachable!("invalid: {:?}", v), + }, + skip: vec![], + // TODO + loose: true, + // TODO + dynamic_import: true, + bugfixes: false, + include: c.include, + exclude: c.exclude, + core_js: match c.corejs { + CoreJs::Ver(v) => Some(v), + ref s => unimplemented!("Unknown core js version: {:?}", s), + }, + force_all_transforms: c.force_all_transforms, + shipped_proposals: c.shipped_proposals, + targets: c.targets, + path: std::env::current_dir().unwrap(), }, - skip: vec![], - // TODO - loose: true, - // TODO - dynamic_import: true, - bugfixes: false, - include: c.include, - exclude: c.exclude, - core_js: match c.corejs { - CoreJs::Ver(v) => Some(v), - ref s => unimplemented!("Unknown core js version: {:?}", s), - }, - force_all_transforms: c.force_all_transforms, - shipped_proposals: c.shipped_proposals, - targets: c.targets, - path: std::env::current_dir().unwrap(), - }, + ), + fixer(None) ); let print = |m: &Module| { @@ -174,6 +179,7 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { .expect("failed to load file"); let mut p = Parser::new( Syntax::Es(EsConfig { + static_blocks: true, ..Default::default() }), StringInput::from(&*fm), diff --git a/crates/swc_ecma_transforms_compat/src/es2022/mod.rs b/crates/swc_ecma_transforms_compat/src/es2022/mod.rs index e518e0fbfb4..e95289c7165 100644 --- a/crates/swc_ecma_transforms_compat/src/es2022/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2022/mod.rs @@ -11,10 +11,10 @@ pub mod static_blocks; pub fn es2022(config: Config) -> impl Fold { chain!( + static_blocks(), class_properties(class_properties::Config { loose: config.loose, }), - static_blocks(), private_in_object(), ) }