Skip to content

Commit

Permalink
Feat/symbol conflict special case (#1279)
Browse files Browse the repository at this point in the history
* test: ✅ add case for issue #1274

* fix: 🐛  conflict Symbol conflict case

* release: @umijs/mako@0.6.1-canary.20240614.1

* chore: 🚨 lint happy

* chore: ⬆️ update pnpm-lock
  • Loading branch information
stormslowly authored Jun 17, 2024
1 parent e892555 commit 73e29df
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 29 deletions.
5 changes: 3 additions & 2 deletions crates/mako/src/ast/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ impl TestUtils {
}
}

pub fn gen_js_ast(content: String) -> Self {
pub fn gen_js_ast<T: AsRef<str>>(content: T) -> Self {
let c = content.as_ref().to_string();
let mut test_utils = Self::new(TestUtilsOpts {
file: Some("test.js".to_string()),
content: Some(content),
content: Some(c),
});
let ast = test_utils.ast.js_mut();
let unresolved_mark = ast.unresolved_mark;
Expand Down
2 changes: 2 additions & 0 deletions crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::visitors::default_export_namer::DefaultExportNamer;
use crate::visitors::dynamic_import_to_require::DynamicImportToRequire;
use crate::visitors::env_replacer::{build_env_map, EnvReplacer};
use crate::visitors::fix_helper_inject_position::FixHelperInjectPosition;
use crate::visitors::fix_symbol_conflict::FixSymbolConflict;
use crate::visitors::new_url_assets::NewUrlAssets;
use crate::visitors::provide::Provide;
use crate::visitors::react::react;
Expand Down Expand Up @@ -64,6 +65,7 @@ impl Transform {
// should be removed after upgrade to latest swc
// ref: https://github.com/umijs/mako/issues/1193
Box::new(FixHelperInjectPosition::new()),
Box::new(FixSymbolConflict::new(top_level_mark)),
Box::new(NewUrlAssets {
context: context.clone(),
path: file.path.clone(),
Expand Down
111 changes: 111 additions & 0 deletions crates/mako/src/visitors/fix_symbol_conflict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// a workaround for the issue https://github.com/umijs/mako/issues/1274
// https://github.com/swc-project/swc/issues/9045

use std::collections::HashSet;

use swc_core::common::{Mark, SyntaxContext};
use swc_core::ecma::ast::{Id, Ident, Module};
use swc_core::ecma::utils::IdentRenamer;
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

pub(crate) struct FixSymbolConflict {
idents_named_symbol: HashSet<Id>,
top_level_ctxt: SyntaxContext,
}

impl FixSymbolConflict {
pub fn new(top_level_mark: Mark) -> Self {
Self {
idents_named_symbol: Default::default(),
top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark),
}
}
}

impl VisitMut for FixSymbolConflict {
fn visit_mut_ident(&mut self, n: &mut Ident) {
if n.sym.eq("Symbol") && n.span.ctxt == self.top_level_ctxt {
self.idents_named_symbol.insert(n.to_id());
}
}

fn visit_mut_module(&mut self, n: &mut Module) {
n.visit_mut_children_with(self);

if !self.idents_named_symbol.is_empty() {
let rename_map = self
.idents_named_symbol
.iter()
.map(|id| {
let new_sym = format!("_$m_{}", id.0);
(id.clone(), (new_sym.into(), id.1))
})
.collect();

let mut renamer = IdentRenamer::new(&rename_map);

n.visit_mut_with(&mut renamer);
}
}
}

#[cfg(test)]
mod tests {
use swc_core::common::GLOBALS;

use super::*;
use crate::ast::tests::TestUtils;

#[test]
fn test_global_symbol() {
assert_eq!(
run_with("console.log(Symbol.iterator)"),
"console.log(Symbol.iterator);"
);
}

#[test]
fn test_top_level_redefine_symbol() {
assert_eq!(
run_with("class Symbol {} Symbol.iterator; export { Symbol }"),
r#"
class _$m_Symbol {
}
_$m_Symbol.iterator;
export { _$m_Symbol as Symbol };
"#
.trim()
)
}

#[test]
fn test_redefine_symbol_in_nested_scope() {
assert_eq!(
run_with(
r#"
Symbol.iterator;
(function(){
class Symbol {}
})();"#,
),
r#"
Symbol.iterator;
(function() {
class Symbol1 {
}
})();
"#
.trim()
);
}

fn run_with(code: &str) -> String {
let mut tu = TestUtils::gen_js_ast(code);
let mark = tu.ast.js().top_level_mark;
let mut v = GLOBALS.set(&tu.context.meta.script.globals, || {
FixSymbolConflict::new(mark)
});
tu.ast.js_mut().ast.visit_mut_with(&mut v);
tu.js_ast_to_code()
}
}
1 change: 1 addition & 0 deletions crates/mako/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) mod dynamic_import;
pub(crate) mod dynamic_import_to_require;
pub(crate) mod env_replacer;
pub(crate) mod fix_helper_inject_position;
pub(crate) mod fix_symbol_conflict;
pub(crate) mod mako_require;
pub(crate) mod meta_url_replacer;
pub(crate) mod new_url_assets;
Expand Down
11 changes: 11 additions & 0 deletions e2e/fixtures/javascript.issue-1274/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const assert = require("assert");
const {
parseBuildResult,
moduleReg,
injectSimpleJest,
} = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);
const content = files["index.js"];

injectSimpleJest();
require("./dist/index.js");
1 change: 1 addition & 0 deletions e2e/fixtures/javascript.issue-1274/mako.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "targets": { "ie": 11 } }
22 changes: 22 additions & 0 deletions e2e/fixtures/javascript.issue-1274/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function forOf(y) {
let r = [];
for (let x of y) {
r.push(x + 1);
}
return r;
}

it("for-of should work", () => {
expect(forOf([1])).toStrictEqual([2]);
});

class Symbol {
name() {
return "AnotherSymbol";
}
}

it("should overwrite Symbol", () => {
let s = new Symbol();
expect(s.name()).toBe("AnotherSymbol");
});
2 changes: 1 addition & 1 deletion packages/bundler-mako/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.6.0",
"dependencies": {
"@umijs/bundler-utils": "^4.0.81",
"@umijs/mako": "0.6.0",
"@umijs/mako": "0.6.1-canary.20240614.1",
"chalk": "^4.1.2",
"compression": "^1.7.4",
"connect-history-api-fallback": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mako/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mako-darwin-arm64",
"version": "0.6.0",
"version": "0.6.1-canary.20240614.1",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/mako/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mako-darwin-x64",
"version": "0.6.0",
"version": "0.6.1-canary.20240614.1",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/mako/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mako-linux-x64-gnu",
"version": "0.6.0",
"version": "0.6.1-canary.20240614.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/mako/npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mako-linux-x64-musl",
"version": "0.6.0",
"version": "0.6.1-canary.20240614.1",
"os": [
"linux"
],
Expand Down
10 changes: 5 additions & 5 deletions packages/mako/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umijs/mako",
"version": "0.6.0",
"version": "0.6.1-canary.20240614.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand Down Expand Up @@ -69,10 +69,10 @@
"src:build": "father build"
},
"optionalDependencies": {
"@umijs/mako-darwin-arm64": "0.6.0",
"@umijs/mako-darwin-x64": "0.6.0",
"@umijs/mako-linux-x64-gnu": "0.6.0",
"@umijs/mako-linux-x64-musl": "0.6.0"
"@umijs/mako-darwin-arm64": "0.6.1-canary.20240614.1",
"@umijs/mako-darwin-x64": "0.6.1-canary.20240614.1",
"@umijs/mako-linux-x64-gnu": "0.6.1-canary.20240614.1",
"@umijs/mako-linux-x64-musl": "0.6.1-canary.20240614.1"
},
"repository": "git@github.com:umijs/mako.git"
}
34 changes: 17 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 73e29df

Please sign in to comment.