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(ecma/lexer): hexadecimal escape sequences #2838

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-2822/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jsc": {
"target": "es5"
}
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issue-2822/1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log`\x22\x21\x224`
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issue-2822/1/output/index.js
Original file line number Diff line number Diff line change
@@ -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());
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issue-2822/2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jsc": {
"target": "es2015"
}
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issue-2822/2/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`\x22\x21\x224`
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issue-2822/2/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`\x22\x21\x224`;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var x = `\0\0x00\u0000 0 00 0000`;
var x = `\0\x00\u0000 0 00 0000`;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// @target: es6
var x = `\0\0x00\u0000 0 00 0000`;
var x = `\0\x00\u0000 0 00 0000`;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var x = `\x1f\u001f 1F 1f`;
var x = `\x1F\u001f 1F 1f`;
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// @target: es6
var x = `\x1f\u001f 1F 1f`;
var x = `\x1F\u001f 1F 1f`;
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/compress/pure/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this, we store

`\x21`

like \0x21, hit line 25.
now it is \x21 so I add this line

{
*e = Expr::Lit(Lit::Str(Str {
value: c
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 24,
"ctxt": 0
},
"value": "\\0x19\\u0019 19",
"value": "\\x19\\u0019 19",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 39,
"ctxt": 0
},
"value": "\\0x19\\u0019 19",
"value": "\\x19\\u0019 19",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 27,
"ctxt": 0
},
"value": "\\0x1F\\u001f 1F 1f",
"value": "\\x1F\\u001f 1F 1f",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 42,
"ctxt": 0
},
"value": "\\0x1F\\u001f 1F 1f",
"value": "\\x1F\\u001f 1F 1f",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 24,
"ctxt": 0
},
"value": "\\0x20\\u0020 20",
"value": "\\x20\\u0020 20",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"end": 39,
"ctxt": 0
},
"value": "\\0x20\\u0020 20",
"value": "\\x20\\u0020 20",
"hasEscape": true,
"kind": {
"type": "normal",
Expand Down