Skip to content

Commit

Permalink
don't assume transform functions don't mutate AST arrays. Closes #1351
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 23, 2023
1 parent 3c9fc4c commit fa6f164
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
30 changes: 7 additions & 23 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,20 @@ function return_null() { return null; }

var MAP = (function() {
function MAP(a, tw, allow_splicing = true) {
// Loop but try not to build a new array
const new_a = [];

for (let i = 0; i < a.length; ++i) {
let item = a[i];
let ret = item.transform(tw, allow_splicing);

if (ret !== item) {
const a1 = a.slice(0, i);

// Looks like something was transformed. Change the loop.
if (ret instanceof AST_Node) {
a1.push(ret);
} else if (ret instanceof Splice) {
a1.push(...ret.v);
}

while ((item = a[++i])) {
const ret = item.transform(tw, true);

if (ret instanceof AST_Node) {
a1.push(ret);
} else if (ret instanceof Splice) {
a1.push(...ret.v);
}
}

return a1;
if (ret instanceof AST_Node) {
new_a.push(ret);
} else if (ret instanceof Splice) {
new_a.push(...ret.v);
}
}

return a;
return new_a;
}

MAP.splice = function(val) { return new Splice(val); };
Expand Down
43 changes: 42 additions & 1 deletion test/compress/join-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,45 @@ issue_1079_with_mixed: {
var netmaskBinary = ''
for (let i = 0; i < netmaskBits; ++i) netmaskBinary += '1';
}
}
}

join_vars_lose_other_var: {
options = {
defaults: false,
inline: true,
reduce_vars: true,
unused: true,
side_effects: true,
join_vars: true,
}
input: {
global.exports = {
set a(a) {
console.log(a().get_b())
const incremented = a()
incremented.inc_b()
console.log(incremented.get_b())
}
};

(function (factory) {
factory(exports)
})((function (exports) {
exports.a = function () {
var b = "PASS",
_this = {};
_this.get_b = function () {
return b;
}
_this.inc_b = function () {
b += "PASS";
}
return _this;
};
}));
}
expect_stdout: [
"PASS",
"PASSPASS"
]
}

0 comments on commit fa6f164

Please sign in to comment.