From 4c06a56e52184796280d9c19975f5317f3050cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 15 Apr 2023 23:50:52 +0900 Subject: [PATCH] fix(es/minifier): Use UTF16 length for `str.length` (#7275) **Related issue:** - Closes #7274. --- crates/swc_ecma_minifier/tests/exec.rs | 19 +++++++++++++++++++ .../src/simplify/expr/mod.rs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 20b8fe3a8e1..6acfb06d7ac 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -10931,3 +10931,22 @@ fn issue_6914_3() { false, ); } + +#[test] +fn issue_7274() { + run_default_exec_test( + r###" + if ( + // incorrect: + "😋📋👌".length === 3 + + // correct: + // "😋📋👌".length === 6 + ) { + // side effect + new Response(123); + } + console.log('PASS'); + "###, + ); +} diff --git a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs index 509bfadb4f8..701a194843c 100644 --- a/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs +++ b/crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs @@ -154,7 +154,7 @@ impl SimplifyExpr { self.changed = true; *expr = Expr::Lit(Lit::Num(Number { - value: value.chars().count() as f64, + value: value.chars().map(|c| c.len_utf16()).sum::() as _, span: *span, raw: None, }));