Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Fix a bug about Tpl => Str #8934

Merged
merged 8 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading