Skip to content

Commit d4be383

Browse files
authored
fix(es/minifier): Fix a bug about Tpl => Str (#8934)
**Description:** I added the method to `swc_ecma_ast` to reuse it from plugins. **Related issue:** - Closes #8931
1 parent 2c27925 commit d4be383

File tree

7 files changed

+78
-11
lines changed

7 files changed

+78
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Enable minification
3+
"minify": true,
4+
// Optional, configure minification options
5+
"jsc": {
6+
"minify": {
7+
"compress": {
8+
"unused": true
9+
},
10+
"mangle": true
11+
},
12+
"target": "es2018"
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function _jsx(a, b) {
2+
return b;
3+
}
4+
5+
export const a = _jsx("math", `P \\vdash q`)
6+
export const b = _jsx("math", "P \\vdash q")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function _jsx(a, b) {
2+
return b;
3+
}
4+
5+
export const a = _jsx("math", `\` \\vdash q`)
6+
export const b = _jsx("math", "P \\vdash q")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function _jsx(t,s){return s}export const a=_jsx("math","P \\vdash q");export const b=_jsx("math","P \\vdash q");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function _jsx(t,s){return s}export const a=_jsx("math","` \\vdash q");export const b=_jsx("math","P \\vdash q");

crates/swc_ecma_ast/src/lit.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,54 @@ impl Str {
196196
pub fn is_empty(&self) -> bool {
197197
self.value.is_empty()
198198
}
199+
200+
pub fn from_tpl_raw(tpl_raw: &str) -> Atom {
201+
let mut buf = String::with_capacity(tpl_raw.len());
202+
203+
let mut iter = tpl_raw.chars();
204+
205+
while let Some(c) = iter.next() {
206+
match c {
207+
'\\' => {
208+
if let Some(next) = iter.next() {
209+
match next {
210+
'`' | '$' | '\\' => {
211+
buf.push(next);
212+
}
213+
'b' => {
214+
buf.push('\u{0008}');
215+
}
216+
'f' => {
217+
buf.push('\u{000C}');
218+
}
219+
'n' => {
220+
buf.push('\n');
221+
}
222+
'r' => {
223+
buf.push('\r');
224+
}
225+
't' => {
226+
buf.push('\t');
227+
}
228+
'v' => {
229+
buf.push('\u{000B}');
230+
}
231+
_ => {
232+
buf.push('\\');
233+
buf.push(next);
234+
}
235+
}
236+
}
237+
}
238+
239+
c => {
240+
buf.push(c);
241+
}
242+
}
243+
}
244+
245+
buf.into()
246+
}
199247
}
200248

201249
impl EqIgnoreSpan for Str {

crates/swc_ecma_minifier/src/compress/pure/strings.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,14 @@ impl Pure<'_> {
220220
&& !c.contains("\\x")
221221
&& !c.contains("\\u")
222222
{
223-
let value = c
224-
.replace("\\`", "`")
225-
.replace("\\$", "$")
226-
.replace("\\b", "\u{0008}")
227-
.replace("\\f", "\u{000C}")
228-
.replace("\\n", "\n")
229-
.replace("\\r", "\r")
230-
.replace("\\t", "\t")
231-
.replace("\\v", "\u{000B}")
232-
.replace("\\\\", "\\");
223+
let value = Str::from_tpl_raw(c);
233224

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

236227
*e = Expr::Lit(Lit::Str(Str {
237228
span: t.span,
238229
raw: None,
239-
value: value.into(),
230+
value,
240231
}));
241232
}
242233
}

0 commit comments

Comments
 (0)