Skip to content

Commit

Permalink
fix(es/lexer): Fix lexing of hexadecimal escape sequences (#2838)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiESAEX authored Nov 24, 2021
1 parent a8bda3b commit cf777c5
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 13 deletions.
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")
{
*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

1 comment on commit cf777c5

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: cf777c5 Previous: a8bda3b Ratio
base_tr_fixer 27641 ns/iter (± 323) 23642 ns/iter (± 276) 1.17
base_tr_resolver_and_hygiene 153930 ns/iter (± 28932) 130288 ns/iter (± 24860) 1.18
codegen_es2015 61030 ns/iter (± 372) 51393 ns/iter (± 431) 1.19
codegen_es2016 61298 ns/iter (± 408) 51605 ns/iter (± 383) 1.19
codegen_es2017 61375 ns/iter (± 450) 51568 ns/iter (± 398) 1.19
codegen_es2018 61112 ns/iter (± 440) 51556 ns/iter (± 320) 1.19
codegen_es2019 60915 ns/iter (± 376) 51626 ns/iter (± 414) 1.18
codegen_es2020 61612 ns/iter (± 10449) 51585 ns/iter (± 365) 1.19
codegen_es3 61173 ns/iter (± 330) 51499 ns/iter (± 328) 1.19
codegen_es5 61345 ns/iter (± 376) 51668 ns/iter (± 377) 1.19
full_es2015 206584311 ns/iter (± 8019457) 181224590 ns/iter (± 8385947) 1.14
full_es2016 163413114 ns/iter (± 5959340) 145550053 ns/iter (± 8288243) 1.12
full_es2017 174419093 ns/iter (± 11947824) 157853695 ns/iter (± 9749004) 1.10
full_es2018 172437147 ns/iter (± 8294328) 156687310 ns/iter (± 6160645) 1.10
full_es2019 170150641 ns/iter (± 8229759) 154457376 ns/iter (± 8865541) 1.10
full_es2020 171622876 ns/iter (± 10600575) 153694169 ns/iter (± 5611120) 1.12
full_es3 246404415 ns/iter (± 16128581) 210457792 ns/iter (± 17128898) 1.17
full_es5 225233025 ns/iter (± 11944865) 197997281 ns/iter (± 12623345) 1.14
parser 758544 ns/iter (± 26780) 654591 ns/iter (± 17106) 1.16
ser_ast_node 179 ns/iter (± 9) 147 ns/iter (± 9) 1.22
ser_serde 192 ns/iter (± 5) 159 ns/iter (± 4) 1.21
emit_colors 21076733 ns/iter (± 26854946) 18576217 ns/iter (± 24517337) 1.13
emit_large 110393160 ns/iter (± 168478079) 119598771 ns/iter (± 182837125) 0.92
base_clone 2706277 ns/iter (± 249446) 2331803 ns/iter (± 399503) 1.16
fold_span 4540883 ns/iter (± 399162) 3808626 ns/iter (± 203931) 1.19
fold_span_panic 4915342 ns/iter (± 421866) 4032999 ns/iter (± 146948) 1.22
visit_mut_span 3276820 ns/iter (± 286126) 2793427 ns/iter (± 79052) 1.17
visit_mut_span_panic 3324890 ns/iter (± 197198) 3028097 ns/iter (± 246525) 1.10
ast_clone 19558 ns/iter (± 253) 16744 ns/iter (± 394) 1.17
ast_clone_to_stable 59694 ns/iter (± 483) 51250 ns/iter (± 344) 1.16
ast_clone_to_stable_then_to_unstable 108503 ns/iter (± 645) 92981 ns/iter (± 495) 1.17
json_deserialize 2319353 ns/iter (± 8069) 1932153 ns/iter (± 5292) 1.20
json_serialize 101037 ns/iter (± 413) 85263 ns/iter (± 292) 1.19
boxing_boxed 170 ns/iter (± 0) 151 ns/iter (± 2) 1.13
boxing_boxed_clone 81 ns/iter (± 0) 68 ns/iter (± 1) 1.19
boxing_unboxed 193 ns/iter (± 1) 134 ns/iter (± 0) 1.44
boxing_unboxed_clone 77 ns/iter (± 0) 63 ns/iter (± 0) 1.22
time_10 371 ns/iter (± 4) 309 ns/iter (± 3) 1.20
time_15 765 ns/iter (± 6) 665 ns/iter (± 2) 1.15
time_20 1520 ns/iter (± 8) 1259 ns/iter (± 2) 1.21
time_40 5155 ns/iter (± 21) 7169 ns/iter (± 20) 0.72
time_5 121 ns/iter (± 2) 102 ns/iter (± 1) 1.19
time_60 10200 ns/iter (± 40) 16334 ns/iter (± 68) 0.62

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.