exclude mangling of special property names

- `null`
- `true`
- `false`
- numeric literals
This commit is contained in:
alexlamsl 2017-04-04 01:35:58 +08:00
parent 48b3fe9952
commit dd3bde86de
2 changed files with 61 additions and 34 deletions

View File

@ -46,6 +46,9 @@
function find_builtins() { function find_builtins() {
// NaN will be included due to Number.NaN // NaN will be included due to Number.NaN
var a = [ var a = [
"null",
"true",
"false",
"Infinity", "Infinity",
"-Infinity", "-Infinity",
"undefined", "undefined",
@ -159,7 +162,7 @@ function mangle_properties(ast, options) {
if (options.only_cache) { if (options.only_cache) {
return cache.props.has(name); return cache.props.has(name);
} }
if (/^[0-9.]+$/.test(name)) return false; if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
return true; return true;
} }

View File

@ -7,6 +7,9 @@ mangle_props: {
Infinity: 3, Infinity: 3,
"-Infinity": 4, "-Infinity": 4,
"-0": 5, "-0": 5,
null: 6,
0x25: 7,
1e100: 8,
}; };
console.log( console.log(
obj[void 0], obj[void 0],
@ -23,7 +26,16 @@ mangle_props: {
obj["-Infinity"], obj["-Infinity"],
obj[-0], obj[-0],
obj[-""], obj[-""],
obj["-0"] obj["-0"],
obj[null],
obj["null"],
obj[0x25],
obj["0x25"],
obj[37],
obj["37"],
obj[1e100],
obj["1e100"],
obj["1e+100"]
); );
} }
expect: { expect: {
@ -32,7 +44,10 @@ mangle_props: {
NaN: 2, NaN: 2,
Infinity: 3, Infinity: 3,
"-Infinity": 4, "-Infinity": 4,
a: 5, "-0": 5,
null: 6,
37: 7,
1e100: 8,
}; };
console.log( console.log(
obj[void 0], obj[void 0],
@ -49,10 +64,19 @@ mangle_props: {
obj["-Infinity"], obj["-Infinity"],
obj[-0], obj[-0],
obj[-""], obj[-""],
obj["a"] obj["-0"],
obj[null],
obj["null"],
obj[37],
obj["a"],
obj[37],
obj["37"],
obj[1e100],
obj["b"],
obj["1e+100"]
); );
} }
expect_stdout: "1 1 1 2 2 2 3 3 3 4 4 4 undefined undefined 5" expect_stdout: "1 1 1 2 2 2 3 3 3 4 4 4 undefined undefined 5 6 6 7 undefined 7 7 8 undefined 8"
} }
identifier: { identifier: {
@ -156,35 +180,35 @@ identifier: {
D: 30, D: 30,
F: 31, F: 31,
G: 32, G: 32,
H: 33, false: 33,
I: 34, null: 34,
J: 35, true: 35,
K: 36, H: 36,
L: 37, I: 37,
M: 38, J: 38,
N: 39, K: 39,
O: 40, L: 40,
P: 41, M: 41,
Q: 42, N: 42,
R: 43, O: 43,
S: 44, P: 44,
T: 45, Q: 45,
U: 46, R: 46,
V: 47, S: 47,
W: 48, T: 48,
X: 49, U: 49,
Y: 50, V: 50,
Z: 51, W: 51,
$: 52, X: 52,
_: 53, Y: 53,
aa: 54, Z: 54,
ba: 55, $: 55,
ca: 56, _: 56,
da: 57, aa: 57,
ea: 58, ba: 58,
fa: 59, ca: 59,
ga: 60, da: 60,
ha: 61 ea: 61,
}; };
} }
} }