Skip to content

Commit

Permalink
fix(merge-sibling-var): recalculate dedeclaratios when concatenating …
Browse files Browse the repository at this point in the history
…variables (#826)
  • Loading branch information
vigneshshanmugam authored and boopathi committed May 1, 2018
1 parent b5bafae commit 5527669
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -57,14 +57,24 @@ module.exports = function({ types: t }) {

let sibling = path.getSibling(path.key + 1);

let declarations = [];

while (sibling.isVariableDeclaration({ kind: node.kind })) {
node.declarations = node.declarations.concat(
sibling.node.declarations
);
declarations = declarations.concat(sibling.node.declarations);

sibling.remove();

sibling = path.getSibling(path.key + 1);
}

if (declarations.length > 0) {
path.replaceWith(
t.variableDeclaration(node.kind, [
...node.declarations,
...declarations
])
);
}
},

// concat `var` declarations next to for loops with it's initialisers.
Expand Down
23 changes: 23 additions & 0 deletions packages/babel-preset-minify/__tests__/minify-env-tests.js
Expand Up @@ -158,4 +158,27 @@ describe("preset along with env", () => {
}
`
);

thePlugin(
"should fix issue#825-merge-sibling-vars",
`
(function() {
const blah = 71;
var start = 1, navx = '';
while (start < 71) {
navx += 'a';
start += 10;
}
return 'b' + navx;
})();
`,
`
(function () {
for (var a = 1, b = ''; 71 > a;) b += 'a', a += 10;
return 'b' + b;
})();
`
);
});

0 comments on commit 5527669

Please sign in to comment.