terser
terser copied to clipboard
Incorrect code when compress is enabled.
Bug report or Feature request?
Version (complete output of terser -V
or specific git commit)
-
https://try.terser.org/ (I can't find a version on there)
-
yarn why terser
tells me Terser 5.11.0
Complete CLI command or minify()
options used
- in the REPL,
// edit terser options
{
module: false,
compress: {},
mangle: {},
output: {},
parse: {},
rename: {},
}
- Default webpack 5 settings with [email protected]
terser
input
// write or paste code here
function set(propValues, options) {
var key;
var value;
var isSimpleSet = typeof propValues === 'string';
var allChangeMessages = [];
// Handle when first argument is a key and second is a value
if (isSimpleSet) {
key = arguments[0];
value = arguments[1];
options = arguments[2];
propValues = {};
propValues[key] = value;
}
console.log("arguments", arguments)
console.log("key", key)
console.log("value", value)
console.log("options", options)
console.log("propValues", propValues)
}
set("foo", "val", {force: true})
terser
output or error
(from try.terser.org)
function set(o,e){var l,s,n="string"==typeof o;n&&(e=arguments[2],(o={})[l=arguments[0]]=s=arguments[1]),console.log("arguments",arguments),console.log("key",l),console.log("value",s),console.log("options",e),console.log("propValues",o)}set("foo","val",{force:!0});
Expected result
The same console logs from the un-compressed and compressed versions.
Here's a screenshot of the outputs, top is un-compressed, bottom is compressed
Setting compress: false
does not have the same issue. So the issue is scoped to the compress
functionality.
Ick, this is behavior with arguments[0]
changing values when the o
(which is the first param) binding is changed in sloppy mode code:
function sloppy(o) {
o = 1;
return arguments[0];
}
function strict(o) {
"use strict";
o = 1;
return arguments[0];
}
sloppy(0); // 1
strict(0); // 0
We need be careful about reordering assignments when in sloppy mode and anything in the function is getting arguments[INDEX]
.
Wow quick response! I'm really glad I was able to scope this down to a simple repro to find the bug!
Maybe using arguments at all in a function should disable more optimizations than it does.