mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 12:49:21 +03:00
fix(es/codegen): Emit numeric value for Binary and Octal literals (#5083)
This commit is contained in:
parent
68c049c8a0
commit
8df6e047cc
12
crates/swc/tests/fixture/issues-4xxx/4870/1/input/.swcrc
Normal file
12
crates/swc/tests/fixture/issues-4xxx/4870/1/input/.swcrc
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"jsc": {
|
||||
"target": "es5",
|
||||
"parser": {
|
||||
"syntax": "ecmascript"
|
||||
}
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
},
|
||||
"sourceMaps": true
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
function test() {
|
||||
return 0b1_1111_0111 === 503 && 0o767 === 503;
|
||||
}
|
||||
test();
|
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
function test() {
|
||||
return 503 === 503 && 503 === 503;
|
||||
}
|
||||
test();
|
13
crates/swc/tests/fixture/issues-4xxx/4870/1/output/index.map
Normal file
13
crates/swc/tests/fixture/issues-4xxx/4870/1/output/index.map
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"mappings": "AAAA;AAAA,SAASA,IAAI,GAAG;IACZ,OAAO,GAAa,KAAK,GAAG,IAAI,GAAK,KAAK,GAAG,CAAC;CACjD;AACDA,IAAI,EAAE,CAAC",
|
||||
"names": [
|
||||
"test"
|
||||
],
|
||||
"sources": [
|
||||
"../../input/index.js"
|
||||
],
|
||||
"sourcesContent": [
|
||||
"function test() {\n return 0b1_1111_0111 === 503 && 0o767 === 503;\n}\ntest();\n"
|
||||
],
|
||||
"version": 3
|
||||
}
|
12
crates/swc/tests/fixture/issues-4xxx/4870/2/input/.swcrc
Normal file
12
crates/swc/tests/fixture/issues-4xxx/4870/2/input/.swcrc
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"jsc": {
|
||||
"target": "es2020",
|
||||
"parser": {
|
||||
"syntax": "ecmascript"
|
||||
}
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
},
|
||||
"sourceMaps": true
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
function test() {
|
||||
// only remove number separators, but keep Binary literals
|
||||
return 0b1_1111_0111 === 503 && 0o767 === 503;
|
||||
}
|
||||
test();
|
@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
function test() {
|
||||
// only remove number separators, but keep Binary literals
|
||||
return 0b111110111 === 503 && 0o767 === 503;
|
||||
}
|
||||
test();
|
13
crates/swc/tests/fixture/issues-4xxx/4870/2/output/index.map
Normal file
13
crates/swc/tests/fixture/issues-4xxx/4870/2/output/index.map
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"mappings": "AAAA;AAAA,SAASA,IAAI,GAAG;IACZ,0DAA0D;IAC1D,OAAO,WAAa,KAAK,GAAG,IAAI,KAAK,KAAK,GAAG,CAAC;CACjD;AACDA,IAAI,EAAE,CAAC",
|
||||
"names": [
|
||||
"test"
|
||||
],
|
||||
"sources": [
|
||||
"../../input/index.js"
|
||||
],
|
||||
"sourcesContent": [
|
||||
"function test() {\n // only remove number separators, but keep Binary literals\n return 0b1_1111_0111 === 503 && 0o767 === 503;\n}\ntest();\n"
|
||||
],
|
||||
"version": 3
|
||||
}
|
@ -1,23 +1,23 @@
|
||||
// @target: es6
|
||||
var bin1 = 0b11010;
|
||||
var bin2 = 0B11010;
|
||||
var bin3 = 0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111;
|
||||
var bin1 = 26;
|
||||
var bin2 = 26;
|
||||
var bin3 = 9671406556917009000000000;
|
||||
var bin4 = Infinity;
|
||||
var obj1 = {
|
||||
0b11010: "Hello",
|
||||
26: "Hello",
|
||||
a: bin1,
|
||||
bin1: bin1,
|
||||
b: 0b11010,
|
||||
b: 26,
|
||||
Infinity: true
|
||||
};
|
||||
var obj2 = {
|
||||
0B11010: "World",
|
||||
26: "World",
|
||||
a: bin2,
|
||||
bin2: bin2,
|
||||
b: 0B11010,
|
||||
0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111: false
|
||||
b: 26,
|
||||
9671406556917009000000000: false
|
||||
};
|
||||
obj1[0b11010]; // string
|
||||
obj1[26]; // string
|
||||
obj1[26]; // string
|
||||
obj1["26"]; // string
|
||||
obj1["0b11010"]; // any
|
||||
@ -25,7 +25,7 @@ obj1["a"]; // number
|
||||
obj1["b"]; // number
|
||||
obj1["bin1"]; // number
|
||||
obj1["Infinity"]; // boolean
|
||||
obj2[0B11010]; // string
|
||||
obj2[26]; // string
|
||||
obj2[26]; // string
|
||||
obj2["26"]; // string
|
||||
obj2["0B11010"]; // any
|
||||
|
@ -1,14 +1,14 @@
|
||||
var obj1 = {
|
||||
0b11010: "Hello",
|
||||
a: 0b11010,
|
||||
bin1: 0b11010,
|
||||
b: 0b11010,
|
||||
26: "Hello",
|
||||
a: 26,
|
||||
bin1: 26,
|
||||
b: 26,
|
||||
Infinity: !0
|
||||
}, obj2 = {
|
||||
0B11010: "World",
|
||||
a: 0B11010,
|
||||
bin2: 0B11010,
|
||||
b: 0B11010,
|
||||
0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111: !1
|
||||
26: "World",
|
||||
a: 26,
|
||||
bin2: 26,
|
||||
b: 26,
|
||||
9671406556917009000000000: !1
|
||||
};
|
||||
obj1[0b11010], obj1[26], obj1["26"], obj1["0b11010"], obj1.a, obj1.b, obj1.bin1, obj1.Infinity, obj2[0B11010], obj2[26], obj2["26"], obj2["0B11010"], obj2.a, obj2.b, obj2.bin2, obj2[9.671406556917009e+24], obj2["9.671406556917009e+24"], obj2.Infinity;
|
||||
obj1[26], obj1[26], obj1["26"], obj1["0b11010"], obj1.a, obj1.b, obj1.bin1, obj1.Infinity, obj2[26], obj2[26], obj2["26"], obj2["0B11010"], obj2.a, obj2.b, obj2.bin2, obj2[9.671406556917009e+24], obj2["9.671406556917009e+24"], obj2.Infinity;
|
||||
|
@ -1,23 +1,23 @@
|
||||
// @target: es5
|
||||
var bin1 = 0b11010;
|
||||
var bin2 = 0B11010;
|
||||
var bin3 = 0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111;
|
||||
var bin1 = 26;
|
||||
var bin2 = 26;
|
||||
var bin3 = 9671406556917009000000000;
|
||||
var bin4 = Infinity;
|
||||
var obj1 = {
|
||||
0b11010: "Hello",
|
||||
26: "Hello",
|
||||
a: bin1,
|
||||
bin1: bin1,
|
||||
b: 0b11010,
|
||||
b: 26,
|
||||
Infinity: true
|
||||
};
|
||||
var obj2 = {
|
||||
0B11010: "World",
|
||||
26: "World",
|
||||
a: bin2,
|
||||
bin2: bin2,
|
||||
b: 0B11010,
|
||||
0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111: false
|
||||
b: 26,
|
||||
9671406556917009000000000: false
|
||||
};
|
||||
obj1[0b11010]; // string
|
||||
obj1[26]; // string
|
||||
obj1[26]; // string
|
||||
obj1["26"]; // string
|
||||
obj1["0b11010"]; // any
|
||||
@ -25,7 +25,7 @@ obj1["a"]; // number
|
||||
obj1["b"]; // number
|
||||
obj1["bin1"]; // number
|
||||
obj1["Infinity"]; // boolean
|
||||
obj2[0B11010]; // string
|
||||
obj2[26]; // string
|
||||
obj2[26]; // string
|
||||
obj2["26"]; // string
|
||||
obj2["0B11010"]; // any
|
||||
|
@ -1,14 +1,14 @@
|
||||
var obj1 = {
|
||||
0b11010: "Hello",
|
||||
a: 0b11010,
|
||||
bin1: 0b11010,
|
||||
b: 0b11010,
|
||||
26: "Hello",
|
||||
a: 26,
|
||||
bin1: 26,
|
||||
b: 26,
|
||||
Infinity: !0
|
||||
}, obj2 = {
|
||||
0B11010: "World",
|
||||
a: 0B11010,
|
||||
bin2: 0B11010,
|
||||
b: 0B11010,
|
||||
0B11111111111111111111111111111111111111111111111101001010100000010111110001111111111: !1
|
||||
26: "World",
|
||||
a: 26,
|
||||
bin2: 26,
|
||||
b: 26,
|
||||
9671406556917009000000000: !1
|
||||
};
|
||||
obj1[0b11010], obj1[26], obj1["26"], obj1["0b11010"], obj1.a, obj1.b, obj1.bin1, obj1.Infinity, obj2[0B11010], obj2[26], obj2["26"], obj2["0B11010"], obj2.a, obj2.b, obj2.bin2, obj2[9.671406556917009e+24], obj2["9.671406556917009e+24"], obj2.Infinity;
|
||||
obj1[26], obj1[26], obj1["26"], obj1["0b11010"], obj1.a, obj1.b, obj1.bin1, obj1.Infinity, obj2[26], obj2[26], obj2["26"], obj2["0B11010"], obj2.a, obj2.b, obj2.bin2, obj2[9.671406556917009e+24], obj2["9.671406556917009e+24"], obj2.Infinity;
|
||||
|
@ -5,18 +5,18 @@ var B = /*#__PURE__*/ function() {
|
||||
function B() {
|
||||
_class_call_check(this, B);
|
||||
this["hello"] = 10;
|
||||
this[0b110] = "world";
|
||||
this[0o23534] = "WORLD";
|
||||
this[6] = "world";
|
||||
this[10076] = "WORLD";
|
||||
this[20] = "twenty";
|
||||
}
|
||||
var _proto = B.prototype;
|
||||
_proto["foo"] = function foo() {};
|
||||
_proto[0b1110] = function() {};
|
||||
_proto[14] = function() {};
|
||||
_proto[11] = function() {};
|
||||
_proto.interface = function _interface() {};
|
||||
return B;
|
||||
}();
|
||||
B["hi"] = 10000;
|
||||
B[22] = "twenty-two";
|
||||
B[0b101] = "binary";
|
||||
B[0o3235] = "octal";
|
||||
B[5] = "binary";
|
||||
B[1693] = "octal";
|
||||
|
@ -2,9 +2,9 @@ import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
var B = function() {
|
||||
"use strict";
|
||||
function B() {
|
||||
_class_call_check(this, B), this.hello = 10, this[0b110] = "world", this[0o23534] = "WORLD", this[20] = "twenty";
|
||||
_class_call_check(this, B), this.hello = 10, this[6] = "world", this[10076] = "WORLD", this[20] = "twenty";
|
||||
}
|
||||
var _proto = B.prototype;
|
||||
return _proto.foo = function() {}, _proto[0b1110] = function() {}, _proto[11] = function() {}, _proto.interface = function() {}, B;
|
||||
return _proto.foo = function() {}, _proto[14] = function() {}, _proto[11] = function() {}, _proto.interface = function() {}, B;
|
||||
}();
|
||||
B.hi = 10000, B[22] = "twenty-two", B[0b101] = "binary", B[0o3235] = "octal";
|
||||
B.hi = 10000, B[22] = "twenty-two", B[5] = "binary", B[1693] = "octal";
|
||||
|
@ -1,23 +1,23 @@
|
||||
// @target: es6
|
||||
var oct1 = 0o45436;
|
||||
var oct2 = 0O45436;
|
||||
var oct1 = 19230;
|
||||
var oct2 = 19230;
|
||||
var oct3 = Infinity;
|
||||
var oct4 = 0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777;
|
||||
var oct4 = 54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
|
||||
var obj1 = {
|
||||
0o45436: "Hello",
|
||||
a: 0o45436,
|
||||
19230: "Hello",
|
||||
a: 19230,
|
||||
b: oct1,
|
||||
oct1: oct1,
|
||||
Infinity: true
|
||||
};
|
||||
var obj2 = {
|
||||
0O45436: "hi",
|
||||
a: 0O45436,
|
||||
19230: "hi",
|
||||
a: 19230,
|
||||
b: oct2,
|
||||
oct2: oct2,
|
||||
0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777: false
|
||||
54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: false
|
||||
};
|
||||
obj1[0o45436]; // string
|
||||
obj1[19230]; // string
|
||||
obj1["0o45436"]; // any
|
||||
obj1["19230"]; // string
|
||||
obj1[19230]; // string
|
||||
@ -25,7 +25,7 @@ obj1["a"]; // number
|
||||
obj1["b"]; // number
|
||||
obj1["oct1"]; // number
|
||||
obj1["Infinity"]; // boolean
|
||||
obj2[0O45436]; // string
|
||||
obj2[19230]; // string
|
||||
obj2["0O45436"]; // any
|
||||
obj2["19230"]; // string
|
||||
obj2[19230]; // string
|
||||
|
@ -1,14 +1,14 @@
|
||||
var obj1 = {
|
||||
0o45436: "Hello",
|
||||
a: 0o45436,
|
||||
b: 0o45436,
|
||||
oct1: 0o45436,
|
||||
19230: "Hello",
|
||||
a: 19230,
|
||||
b: 19230,
|
||||
oct1: 19230,
|
||||
Infinity: !0
|
||||
}, obj2 = {
|
||||
0O45436: "hi",
|
||||
a: 0O45436,
|
||||
b: 0O45436,
|
||||
oct2: 0O45436,
|
||||
0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777: !1
|
||||
19230: "hi",
|
||||
a: 19230,
|
||||
b: 19230,
|
||||
oct2: 19230,
|
||||
54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: !1
|
||||
};
|
||||
obj1[0o45436], obj1["0o45436"], obj1["19230"], obj1[19230], obj1.a, obj1.b, obj1.oct1, obj1.Infinity, obj2[0O45436], obj2["0O45436"], obj2["19230"], obj2[19230], obj2.a, obj2.b, obj2.oct2, obj2[5.462437423415177e+244], obj2["5.462437423415177e+244"], obj2.Infinity;
|
||||
obj1[19230], obj1["0o45436"], obj1["19230"], obj1[19230], obj1.a, obj1.b, obj1.oct1, obj1.Infinity, obj2[19230], obj2["0O45436"], obj2["19230"], obj2[19230], obj2.a, obj2.b, obj2.oct2, obj2[5.462437423415177e+244], obj2["5.462437423415177e+244"], obj2.Infinity;
|
||||
|
@ -1,23 +1,23 @@
|
||||
// @target: es5
|
||||
var oct1 = 0o45436;
|
||||
var oct2 = 0O45436;
|
||||
var oct1 = 19230;
|
||||
var oct2 = 19230;
|
||||
var oct3 = Infinity;
|
||||
var oct4 = 0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777;
|
||||
var oct4 = 54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
|
||||
var obj1 = {
|
||||
0o45436: "Hello",
|
||||
a: 0o45436,
|
||||
19230: "Hello",
|
||||
a: 19230,
|
||||
b: oct1,
|
||||
oct1: oct1,
|
||||
Infinity: true
|
||||
};
|
||||
var obj2 = {
|
||||
0O45436: "hi",
|
||||
a: 0O45436,
|
||||
19230: "hi",
|
||||
a: 19230,
|
||||
b: oct2,
|
||||
oct2: oct2,
|
||||
0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777: false
|
||||
54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: false
|
||||
};
|
||||
obj1[0o45436]; // string
|
||||
obj1[19230]; // string
|
||||
obj1["0o45436"]; // any
|
||||
obj1["19230"]; // string
|
||||
obj1[19230]; // string
|
||||
@ -25,7 +25,7 @@ obj1["a"]; // number
|
||||
obj1["b"]; // number
|
||||
obj1["oct1"]; // number
|
||||
obj1["Infinity"]; // boolean
|
||||
obj2[0O45436]; // string
|
||||
obj2[19230]; // string
|
||||
obj2["0O45436"]; // any
|
||||
obj2["19230"]; // string
|
||||
obj2[19230]; // string
|
||||
|
@ -1,14 +1,14 @@
|
||||
var obj1 = {
|
||||
0o45436: "Hello",
|
||||
a: 0o45436,
|
||||
b: 0o45436,
|
||||
oct1: 0o45436,
|
||||
19230: "Hello",
|
||||
a: 19230,
|
||||
b: 19230,
|
||||
oct1: 19230,
|
||||
Infinity: !0
|
||||
}, obj2 = {
|
||||
0O45436: "hi",
|
||||
a: 0O45436,
|
||||
b: 0O45436,
|
||||
oct2: 0O45436,
|
||||
0o7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777: !1
|
||||
19230: "hi",
|
||||
a: 19230,
|
||||
b: 19230,
|
||||
oct2: 19230,
|
||||
54624374234151770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: !1
|
||||
};
|
||||
obj1[0o45436], obj1["0o45436"], obj1["19230"], obj1[19230], obj1.a, obj1.b, obj1.oct1, obj1.Infinity, obj2[0O45436], obj2["0O45436"], obj2["19230"], obj2[19230], obj2.a, obj2.b, obj2.oct2, obj2[5.462437423415177e+244], obj2["5.462437423415177e+244"], obj2.Infinity;
|
||||
obj1[19230], obj1["0o45436"], obj1["19230"], obj1[19230], obj1.a, obj1.b, obj1.oct1, obj1.Infinity, obj2[19230], obj2["0O45436"], obj2["19230"], obj2[19230], obj2.a, obj2.b, obj2.oct2, obj2[5.462437423415177e+244], obj2["5.462437423415177e+244"], obj2.Infinity;
|
||||
|
@ -1,4 +1,4 @@
|
||||
0b0011;
|
||||
0B01;
|
||||
0b11000011;
|
||||
0B0110101;
|
||||
3;
|
||||
1;
|
||||
195;
|
||||
53;
|
||||
|
@ -1,4 +1,4 @@
|
||||
0o0011;
|
||||
0O01;
|
||||
0o11000011;
|
||||
0O0110101;
|
||||
9;
|
||||
1;
|
||||
2359305;
|
||||
36929;
|
||||
|
@ -1,6 +1,6 @@
|
||||
// @target: es6
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
668..toString();
|
||||
109..toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
|
@ -1 +1 @@
|
||||
0xffffffff.toString(), 0o01234.toString(), 0b01101101.toString(), 1234..toString(), 1e0.toString();
|
||||
0xffffffff.toString(), 668..toString(), 109..toString(), 1234..toString(), 1e0.toString();
|
||||
|
@ -562,6 +562,16 @@ where
|
||||
|
||||
#[emitter]
|
||||
fn emit_num_lit(&mut self, num: &Number) -> Result {
|
||||
self.emit_num_lit_internal(num, false)?;
|
||||
}
|
||||
|
||||
/// `1.toString` is an invalid property access,
|
||||
/// should emit a dot after the literal if return true
|
||||
fn emit_num_lit_internal(
|
||||
&mut self,
|
||||
num: &Number,
|
||||
detect_dot: bool,
|
||||
) -> std::result::Result<bool, io::Error> {
|
||||
self.emit_leading_comments_of_span(num.span(), false)?;
|
||||
|
||||
// Handle infinity
|
||||
@ -570,24 +580,78 @@ where
|
||||
self.wr.write_str_lit(num.span, "-")?;
|
||||
}
|
||||
self.wr.write_str_lit(num.span, "Infinity")?;
|
||||
} else if self.cfg.minify {
|
||||
let minified = minify_number(num.value);
|
||||
|
||||
self.wr.write_str_lit(num.span, &minified)?;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let mut striped_raw = None;
|
||||
let mut value = String::default();
|
||||
|
||||
if self.cfg.minify {
|
||||
value = minify_number(num.value);
|
||||
self.wr.write_str_lit(num.span, &value)?;
|
||||
} else {
|
||||
match &num.raw {
|
||||
Some(raw) => {
|
||||
if self.cfg.target < EsVersion::Es2021 && raw.contains('_') {
|
||||
self.wr.write_str_lit(num.span, &raw.replace('_', ""))?;
|
||||
if raw.len() > 2 && self.cfg.target < EsVersion::Es2015 && {
|
||||
let slice = &raw.as_bytes()[..2];
|
||||
slice == b"0b" || slice == b"0o" || slice == b"0B" || slice == b"0O"
|
||||
} {
|
||||
value = num.value.to_string();
|
||||
self.wr.write_str_lit(num.span, &value)?;
|
||||
} else if raw.len() > 2
|
||||
&& self.cfg.target < EsVersion::Es2021
|
||||
&& raw.contains('_')
|
||||
{
|
||||
let value = raw.replace('_', "");
|
||||
self.wr.write_str_lit(num.span, &value)?;
|
||||
|
||||
striped_raw = Some(value);
|
||||
} else {
|
||||
self.wr.write_str_lit(num.span, raw)?;
|
||||
|
||||
if !detect_dot {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
striped_raw = Some(raw.replace('_', ""));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.wr.write_str_lit(num.span, &num.value.to_string())?;
|
||||
value = num.value.to_string();
|
||||
self.wr.write_str_lit(num.span, &value)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fast return
|
||||
if !detect_dot {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
Ok(striped_raw
|
||||
.map(|raw| {
|
||||
if raw.bytes().all(|c| c.is_ascii_digit()) {
|
||||
// Legacy octal contains only digits, but `value` and `raw` are
|
||||
// different
|
||||
if !num.value.to_string().eq(&raw) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
let bytes = value.as_bytes();
|
||||
|
||||
if !bytes.contains(&b'.') && !bytes.contains(&b'e') {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}))
|
||||
}
|
||||
|
||||
#[emitter]
|
||||
@ -600,7 +664,7 @@ where
|
||||
} else {
|
||||
match &v.raw {
|
||||
Some(raw) => {
|
||||
if self.cfg.target < EsVersion::Es2021 && raw.contains('_') {
|
||||
if raw.len() > 2 && self.cfg.target < EsVersion::Es2021 && raw.contains('_') {
|
||||
self.wr.write_str_lit(v.span, &raw.replace('_', ""))?;
|
||||
} else {
|
||||
self.wr.write_str_lit(v.span, raw)?;
|
||||
@ -814,16 +878,24 @@ where
|
||||
|
||||
srcmap!(node, true);
|
||||
|
||||
if let Expr::New(new) = &*node.obj {
|
||||
self.emit_new(new, false)?;
|
||||
} else {
|
||||
emit!(node.obj);
|
||||
let mut needs_2dots_for_property_access = false;
|
||||
|
||||
match &*node.obj {
|
||||
Expr::New(new) => {
|
||||
self.emit_new(new, false)?;
|
||||
}
|
||||
Expr::Lit(Lit::Num(num)) => {
|
||||
needs_2dots_for_property_access = self.emit_num_lit_internal(num, true)?;
|
||||
}
|
||||
_ => {
|
||||
emit!(node.obj);
|
||||
}
|
||||
}
|
||||
|
||||
match &node.prop {
|
||||
MemberProp::Computed(computed) => emit!(computed),
|
||||
MemberProp::Ident(ident) => {
|
||||
if self.needs_2dots_for_property_access(&node.obj) {
|
||||
if needs_2dots_for_property_access {
|
||||
if node.prop.span().lo() >= BytePos(2) {
|
||||
self.emit_leading_comments(node.prop.span().lo() - BytePos(2), false)?;
|
||||
}
|
||||
@ -836,7 +908,7 @@ where
|
||||
emit!(ident);
|
||||
}
|
||||
MemberProp::PrivateName(private) => {
|
||||
if self.needs_2dots_for_property_access(&node.obj) {
|
||||
if needs_2dots_for_property_access {
|
||||
if node.prop.span().lo() >= BytePos(2) {
|
||||
self.emit_leading_comments(node.prop.span().lo() - BytePos(2), false)?;
|
||||
}
|
||||
@ -853,55 +925,6 @@ where
|
||||
srcmap!(node, false);
|
||||
}
|
||||
|
||||
/// `1..toString` is a valid property access, emit a dot after the literal
|
||||
pub fn needs_2dots_for_property_access(&self, expr: &Expr) -> bool {
|
||||
if let Expr::Lit(Lit::Num(Number { span, value, raw })) = expr {
|
||||
// TODO we store `NaN` in `swc_ecma_minifier`, but we should not do it
|
||||
if value.is_nan() || value.is_infinite() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.cfg.minify {
|
||||
let s = minify_number(*value);
|
||||
let bytes = s.as_bytes();
|
||||
|
||||
if !bytes.contains(&b'.') && !bytes.contains(&b'e') {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
} else {
|
||||
match raw {
|
||||
Some(raw) => {
|
||||
if raw.bytes().all(|c| c.is_ascii_digit()) {
|
||||
// Legacy octal contains only digits, but `value` and `raw` are
|
||||
// different
|
||||
if !value.to_string().eq(raw.as_ref()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
_ => {
|
||||
let s = value.to_string();
|
||||
let bytes = s.as_bytes();
|
||||
|
||||
if !bytes.contains(&b'.') && !bytes.contains(&b'e') {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[emitter]
|
||||
fn emit_super_expr(&mut self, node: &SuperPropExpr) -> Result {
|
||||
self.emit_leading_comments_of_span(node.span(), false)?;
|
||||
|
@ -57,7 +57,7 @@ const foo39 = 0.00001543000;
|
||||
const foo40 = (0.00001543000);
|
||||
const foo41 = { 1000050000: "foo" };
|
||||
const foo42 = 1000000003242;
|
||||
const foo43 = -1000000003242;
|
||||
const foo43_ = -1000000003242;
|
||||
const foo43 = 0.0;
|
||||
const foo44 = -0.0;
|
||||
const foo45 = +0.0;
|
||||
@ -114,6 +114,7 @@ const hugefoo = 1000000000001..test();
|
||||
const hugefoo1 = 0xEE.test();
|
||||
|
||||
1_2_3_4..toString();
|
||||
1_2_3_4 .toString();
|
||||
|
||||
const foo72 = 86400000;
|
||||
const foo73 = 65535;
|
||||
|
@ -59,7 +59,7 @@ const foo41 = {
|
||||
1000050000: "foo"
|
||||
};
|
||||
const foo42 = 1000000003242;
|
||||
const foo43 = -1000000003242;
|
||||
const foo43_ = -1000000003242;
|
||||
const foo43 = 0.0;
|
||||
const foo44 = -0.0;
|
||||
const foo45 = +0.0;
|
||||
@ -106,6 +106,7 @@ const foo71 = 0XfF;
|
||||
const hugefoo = 1000000000001..test();
|
||||
const hugefoo1 = 0xEE.test();
|
||||
1_2_3_4..toString();
|
||||
1_2_3_4..toString();
|
||||
const foo72 = 86400000;
|
||||
const foo73 = 65535;
|
||||
const foo74 = 0xffff;
|
||||
|
@ -1 +1 @@
|
||||
const exp=1e3;const Exp=1e12;const negativeExp=1e-8;const huge=0xe8d4a51001;const big=100000000001;const fractional=100.23002;const numeric_separators=1e12;const one=1e3;const two=1e6;const three=-1e6;const bin=85;const oct=342391;const hex=3735928559;const fractional2=1000.0001;const identifier=_1000;const negate_identifier=-_1000;const foo=.1;const foo1=+.1;const foo2=-.1;const foo3=1050;const foo4=100500;const foo5=10005e3;const foo6=100005e4;const foo7=.1;const foo8=.01;const foo9=.001;const foo10=1e-4;const foo11=1e-5;const foo12=1e-6;const foo13=24e-6;const foo14=-24e-6;const foo15=10;const foo16=100;const foo17=1e3;const foo18=1e4;const foo19=1e5;const foo20=.1;const foo21=.01;const foo22=.001;const foo23=1e-4;const foo24=1e-5;const foo25=-.1;const foo26=-.01;const foo27=-.001;const foo28=-1e-4;const foo29=-1e-5;const foo30=.001;const foo31=.00321;const foo32=321e-6;const foo33=.1;const foo34=1e-5;const foo35=0;const foo36=-0;const foo37=+0;const foo38=1543e-8;const foo39=1543e-8;const foo40=(1543e-8);const foo41={100005e4:"foo"};const foo42=0xe8d4a51caa;const foo43=-0xe8d4a51caa;const foo43=0;const foo44=-0;const foo45=+0;const foo46=1e9;const foo47=1.10001;const foo48=1e10;const foo49=1e10;const foo50=1e-10;const foo51=11e99;const foo52=11e99;const foo53=11e-101;const foo54=123456;const foo55=122333;const foo56=12.34;const foo57=1234e54;const foo58=1234e54;const foo59=1234e-58;const foo60=.1;const foo61=10;const foo62=5e9;const foo63=255;const foo64=255;const foo65=255;const foo66=0;const foo67=0;const foo68=0;const foo69=0;const foo70=0;const foo71=255;26..toString();4294967295..toString();4294967295..toString();668..toString();668..toString();109..toString();109..toString();1234..toString();1..toString();77..toExponential();77..toExponential();(77).toExponential();77..toExponential();77..toExponential();const hugefoo=0xe8d4a51001.test();const hugefoo1=238..test();1234..toString();const foo72=864e5;const foo73=65535;const foo74=65535;const foo75=Infinity;const foo76=Infinity.toString()
|
||||
const exp=1e3;const Exp=1e12;const negativeExp=1e-8;const huge=0xe8d4a51001;const big=100000000001;const fractional=100.23002;const numeric_separators=1e12;const one=1e3;const two=1e6;const three=-1e6;const bin=85;const oct=342391;const hex=3735928559;const fractional2=1000.0001;const identifier=_1000;const negate_identifier=-_1000;const foo=.1;const foo1=+.1;const foo2=-.1;const foo3=1050;const foo4=100500;const foo5=10005e3;const foo6=100005e4;const foo7=.1;const foo8=.01;const foo9=.001;const foo10=1e-4;const foo11=1e-5;const foo12=1e-6;const foo13=24e-6;const foo14=-24e-6;const foo15=10;const foo16=100;const foo17=1e3;const foo18=1e4;const foo19=1e5;const foo20=.1;const foo21=.01;const foo22=.001;const foo23=1e-4;const foo24=1e-5;const foo25=-.1;const foo26=-.01;const foo27=-.001;const foo28=-1e-4;const foo29=-1e-5;const foo30=.001;const foo31=.00321;const foo32=321e-6;const foo33=.1;const foo34=1e-5;const foo35=0;const foo36=-0;const foo37=+0;const foo38=1543e-8;const foo39=1543e-8;const foo40=(1543e-8);const foo41={100005e4:"foo"};const foo42=0xe8d4a51caa;const foo43_=-0xe8d4a51caa;const foo43=0;const foo44=-0;const foo45=+0;const foo46=1e9;const foo47=1.10001;const foo48=1e10;const foo49=1e10;const foo50=1e-10;const foo51=11e99;const foo52=11e99;const foo53=11e-101;const foo54=123456;const foo55=122333;const foo56=12.34;const foo57=1234e54;const foo58=1234e54;const foo59=1234e-58;const foo60=.1;const foo61=10;const foo62=5e9;const foo63=255;const foo64=255;const foo65=255;const foo66=0;const foo67=0;const foo68=0;const foo69=0;const foo70=0;const foo71=255;26..toString();4294967295..toString();4294967295..toString();668..toString();668..toString();109..toString();109..toString();1234..toString();1..toString();77..toExponential();77..toExponential();(77).toExponential();77..toExponential();77..toExponential();const hugefoo=0xe8d4a51001.test();const hugefoo1=238..test();1234..toString();1234..toString();const foo72=864e5;const foo73=65535;const foo74=65535;const foo75=Infinity;const foo76=Infinity.toString()
|
||||
|
@ -1 +1 @@
|
||||
(0o0);
|
||||
(0);
|
||||
|
@ -1,4 +1,4 @@
|
||||
function a() {
|
||||
"use strict";
|
||||
0O0;
|
||||
0;
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
0O0;
|
||||
0;
|
||||
|
@ -1 +1 @@
|
||||
0o10;
|
||||
8;
|
||||
|
@ -1,4 +1,4 @@
|
||||
function a() {
|
||||
"use strict";
|
||||
0o0;
|
||||
0;
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
0B0;
|
||||
0;
|
||||
|
@ -1 +1 @@
|
||||
0b10;
|
||||
2;
|
||||
|
@ -1 +1 @@
|
||||
0o2;
|
||||
2;
|
||||
|
@ -1 +1 @@
|
||||
0O12;
|
||||
10;
|
||||
|
@ -1,2 +1,2 @@
|
||||
"use strict";
|
||||
0o0;
|
||||
0;
|
||||
|
@ -1 +1 @@
|
||||
0b0;
|
||||
0;
|
||||
|
@ -1,2 +1,2 @@
|
||||
"use strict";
|
||||
0b0;
|
||||
0;
|
||||
|
@ -1 +1 @@
|
||||
0O2;
|
||||
2;
|
||||
|
@ -1 +1 @@
|
||||
0B1;
|
||||
1;
|
||||
|
@ -1 +1 @@
|
||||
0b1;
|
||||
1;
|
||||
|
@ -1 +1 @@
|
||||
0o1;
|
||||
1;
|
||||
|
@ -1 +1 @@
|
||||
0o0;
|
||||
0;
|
||||
|
@ -1 +1 @@
|
||||
0o12;
|
||||
10;
|
||||
|
@ -1,4 +1,4 @@
|
||||
0b1001;
|
||||
0B1001;
|
||||
0o11;
|
||||
0O11;
|
||||
9;
|
||||
9;
|
||||
9;
|
||||
9;
|
||||
|
@ -1 +1 @@
|
||||
0B10;
|
||||
2;
|
||||
|
Loading…
Reference in New Issue
Block a user