Skip to content

Commit

Permalink
fix(es/minifier): Fix a bug about Tpl => Str (#8934)
Browse files Browse the repository at this point in the history
**Description:**

I added the method to `swc_ecma_ast` to reuse it from plugins.

**Related issue:**

 - Closes #8931
  • Loading branch information
kdy1 committed May 8, 2024
1 parent 2c27925 commit d4be383
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
14 changes: 14 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8931/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Enable minification
"minify": true,
// Optional, configure minification options
"jsc": {
"minify": {
"compress": {
"unused": true
},
"mangle": true
},
"target": "es2018"
}
}
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8931/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function _jsx(a, b) {
return b;
}

export const a = _jsx("math", `P \\vdash q`)
export const b = _jsx("math", "P \\vdash q")
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8931/input/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function _jsx(a, b) {
return b;
}

export const a = _jsx("math", `\` \\vdash q`)
export const b = _jsx("math", "P \\vdash q")
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8931/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function _jsx(t,s){return s}export const a=_jsx("math","P \\vdash q");export const b=_jsx("math","P \\vdash q");
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8931/output/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function _jsx(t,s){return s}export const a=_jsx("math","` \\vdash q");export const b=_jsx("math","P \\vdash q");
48 changes: 48 additions & 0 deletions crates/swc_ecma_ast/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,54 @@ impl Str {
pub fn is_empty(&self) -> bool {
self.value.is_empty()
}

pub fn from_tpl_raw(tpl_raw: &str) -> Atom {
let mut buf = String::with_capacity(tpl_raw.len());

let mut iter = tpl_raw.chars();

while let Some(c) = iter.next() {
match c {
'\\' => {
if let Some(next) = iter.next() {
match next {
'`' | '$' | '\\' => {
buf.push(next);
}
'b' => {
buf.push('\u{0008}');
}
'f' => {
buf.push('\u{000C}');
}
'n' => {
buf.push('\n');
}
'r' => {
buf.push('\r');
}
't' => {
buf.push('\t');
}
'v' => {
buf.push('\u{000B}');
}
_ => {
buf.push('\\');
buf.push(next);
}
}
}
}

c => {
buf.push(c);
}
}
}

buf.into()
}
}

impl EqIgnoreSpan for Str {
Expand Down
13 changes: 2 additions & 11 deletions crates/swc_ecma_minifier/src/compress/pure/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,14 @@ impl Pure<'_> {
&& !c.contains("\\x")
&& !c.contains("\\u")
{
let value = c
.replace("\\`", "`")
.replace("\\$", "$")
.replace("\\b", "\u{0008}")
.replace("\\f", "\u{000C}")
.replace("\\n", "\n")
.replace("\\r", "\r")
.replace("\\t", "\t")
.replace("\\v", "\u{000B}")
.replace("\\\\", "\\");
let value = Str::from_tpl_raw(c);

report_change!("converting a template literal to a string literal");

*e = Expr::Lit(Lit::Str(Str {
span: t.span,
raw: None,
value: value.into(),
value,
}));
}
}
Expand Down

0 comments on commit d4be383

Please sign in to comment.