From aeecec65ba3c22db28b6e2ccb048c9f17b3c5b0b Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 29 Jul 2024 08:05:02 +0200 Subject: [PATCH] fix: Use `hygiene_with_config` instead of `hygiene` (#8848) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description ### Testing Instructions --------- Co-authored-by: 강동윤 (Donny) --- crates/turbopack-ecmascript/src/lib.rs | 9 +++++++- .../turbopack/renaming/eval/input/index.js | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 crates/turbopack-tests/tests/execution/turbopack/renaming/eval/input/index.js diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index ea2d6e7ed3536..1a953ee00594a 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -803,7 +803,14 @@ async fn gen_content_with_visitors( for visitor in root_visitors { program.visit_mut_with(&mut visitor.create()); } - program.visit_mut_with(&mut swc_core::ecma::transforms::base::hygiene::hygiene()); + program.visit_mut_with( + &mut swc_core::ecma::transforms::base::hygiene::hygiene_with_config( + swc_core::ecma::transforms::base::hygiene::Config { + top_level_mark: eval_context.top_level_mark, + ..Default::default() + }, + ), + ); program.visit_mut_with(&mut swc_core::ecma::transforms::base::fixer::fixer(None)); // we need to remove any shebang before bundling as it's only valid as the first diff --git a/crates/turbopack-tests/tests/execution/turbopack/renaming/eval/input/index.js b/crates/turbopack-tests/tests/execution/turbopack/renaming/eval/input/index.js new file mode 100644 index 0000000000000..6eb723ffc7200 --- /dev/null +++ b/crates/turbopack-tests/tests/execution/turbopack/renaming/eval/input/index.js @@ -0,0 +1,21 @@ +module; // <- This is important! + +it("should not rename variables when eval is used", () => { + const modules = { + x: (module) => { + "use strict"; + return eval("module"); + }, + }; + const moduleArrowFunction = (module) => { + "use strict"; + return eval("module"); + }; + function moduleFunciton(module) { + "use strict"; + return eval("module"); + } + expect(modules.x(42)).toBe(42); + expect(moduleArrowFunction(42)).toBe(42); + expect(moduleFunciton(42)).toBe(42); +});