diff --git a/crates/swc/tests/fixture/issue-2822/1/input/.swcrc b/crates/swc/tests/fixture/issue-2822/1/input/.swcrc new file mode 100644 index 000000000000..2fdb3e32f3d6 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/1/input/.swcrc @@ -0,0 +1,5 @@ +{ + "jsc": { + "target": "es5" + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2822/1/input/index.js b/crates/swc/tests/fixture/issue-2822/1/input/index.js new file mode 100644 index 000000000000..3ad69b66f52c --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/1/input/index.js @@ -0,0 +1 @@ +console.log`\x22\x21\x224` \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2822/1/output/index.js b/crates/swc/tests/fixture/issue-2822/1/output/index.js new file mode 100644 index 000000000000..ccb5b36c7170 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/1/output/index.js @@ -0,0 +1,22 @@ +function _taggedTemplateLiteral(strings, raw) { + if (!raw) { + raw = strings.slice(0); + } + return Object.freeze(Object.defineProperties(strings, { + raw: { + value: Object.freeze(raw) + } + })); +} +function _templateObject() { + var data = _taggedTemplateLiteral([ + "\"!\"4" + ], [ + "\\x22\\x21\\x224" + ]); + _templateObject = function _templateObject() { + return data; + }; + return data; +} +console.log(_templateObject()); diff --git a/crates/swc/tests/fixture/issue-2822/2/input/.swcrc b/crates/swc/tests/fixture/issue-2822/2/input/.swcrc new file mode 100644 index 000000000000..cedb940d7bd3 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/2/input/.swcrc @@ -0,0 +1,5 @@ +{ + "jsc": { + "target": "es2015" + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2822/2/input/index.js b/crates/swc/tests/fixture/issue-2822/2/input/index.js new file mode 100644 index 000000000000..ef5c859686dc --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/2/input/index.js @@ -0,0 +1 @@ +`\x22\x21\x224` \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2822/2/output/index.js b/crates/swc/tests/fixture/issue-2822/2/output/index.js new file mode 100644 index 000000000000..be8d3419b3e6 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2822/2/output/index.js @@ -0,0 +1 @@ +`\x22\x21\x224`; diff --git a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01/input.ts/es2015.1.normal/output.js b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01/input.ts/es2015.1.normal/output.js index 4eaec0d510b8..ee39cd6f05b7 100644 --- a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01/input.ts/es2015.1.normal/output.js +++ b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01/input.ts/es2015.1.normal/output.js @@ -1 +1 @@ -var x = `\0\0x00\u0000 0 00 0000`; +var x = `\0\x00\u0000 0 00 0000`; diff --git a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts/es2015.1.normal/output.js b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts/es2015.1.normal/output.js index 9e752a2a2ee5..8b29233d47c6 100644 --- a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts/es2015.1.normal/output.js +++ b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts/es2015.1.normal/output.js @@ -1,2 +1,2 @@ // @target: es6 -var x = `\0\0x00\u0000 0 00 0000`; +var x = `\0\x00\u0000 0 00 0000`; diff --git a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03/input.ts/es2015.1.normal/output.js b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03/input.ts/es2015.1.normal/output.js index c383f6239889..142dd4367168 100644 --- a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03/input.ts/es2015.1.normal/output.js +++ b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03/input.ts/es2015.1.normal/output.js @@ -1 +1 @@ -var x = `\x1f\u001f 1F 1f`; +var x = `\x1F\u001f 1F 1f`; diff --git a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts/es2015.1.normal/output.js b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts/es2015.1.normal/output.js index eca2694ce442..768cd233de30 100644 --- a/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts/es2015.1.normal/output.js +++ b/crates/swc/tests/tsc-references/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts/es2015.1.normal/output.js @@ -1,2 +1,2 @@ // @target: es6 -var x = `\x1f\u001f 1F 1f`; +var x = `\x1F\u001f 1F 1f`; diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs index 229ca9e70115..230d4f79d587 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs @@ -23,6 +23,7 @@ where }) && (M::force_str_for_tpl() || (!c.value.contains("\\n") && !c.value.contains("\\r"))) && !c.value.contains("\\0") + && !c.value.contains("\\x") { *e = Expr::Lit(Lit::Str(Str { value: c diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 5055a3bb700c..6b9cea705b3f 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -530,7 +530,7 @@ impl<'a, I: Input> Lexer<'a, I> { // read hexadecimal escape sequences 'x' => { - raw.push_str("0x"); + raw.push_str("x"); self.bump(); // 'x' return self.read_hex_char(start, 2, raw).map(Some); } diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01/input.ts.json index 173d7cee2227..1e4454700f5f 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01/input.ts.json @@ -72,7 +72,7 @@ "end": 33, "ctxt": 0 }, - "value": "\\0\\0x00\\u0000 0 00 0000", + "value": "\\0\\x00\\u0000 0 00 0000", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts.json index 5ab5559f8a13..8f84ed848ca1 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes01_ES6/input.ts.json @@ -72,7 +72,7 @@ "end": 48, "ctxt": 0 }, - "value": "\\0\\0x00\\u0000 0 00 0000", + "value": "\\0\\x00\\u0000 0 00 0000", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02/input.ts.json index 55a6fc20ba1a..97716c639c38 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02/input.ts.json @@ -72,7 +72,7 @@ "end": 24, "ctxt": 0 }, - "value": "\\0x19\\u0019 19", + "value": "\\x19\\u0019 19", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02_ES6/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02_ES6/input.ts.json index afa0549a11ba..3026aacd2db5 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02_ES6/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes02_ES6/input.ts.json @@ -72,7 +72,7 @@ "end": 39, "ctxt": 0 }, - "value": "\\0x19\\u0019 19", + "value": "\\x19\\u0019 19", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03/input.ts.json index ed7daf8a2a0b..cc305ba2f65f 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03/input.ts.json @@ -72,7 +72,7 @@ "end": 27, "ctxt": 0 }, - "value": "\\0x1F\\u001f 1F 1f", + "value": "\\x1F\\u001f 1F 1f", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts.json index 736861986219..f9daa5b47859 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes03_ES6/input.ts.json @@ -72,7 +72,7 @@ "end": 42, "ctxt": 0 }, - "value": "\\0x1F\\u001f 1F 1f", + "value": "\\x1F\\u001f 1F 1f", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04/input.ts.json index 83362184f961..8909e8d70121 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04/input.ts.json @@ -72,7 +72,7 @@ "end": 24, "ctxt": 0 }, - "value": "\\0x20\\u0020 20", + "value": "\\x20\\u0020 20", "hasEscape": true, "kind": { "type": "normal", diff --git a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04_ES6/input.ts.json b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04_ES6/input.ts.json index 565f58c2c292..7b1eaf45934d 100644 --- a/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04_ES6/input.ts.json +++ b/crates/swc_ecma_parser/tests/typescript/tsc/es6/templates/templateStringControlCharacterEscapes04_ES6/input.ts.json @@ -72,7 +72,7 @@ "end": 39, "ctxt": 0 }, - "value": "\\0x20\\u0020 20", + "value": "\\x20\\u0020 20", "hasEscape": true, "kind": { "type": "normal",