Skip to content

Commit

Permalink
lift var declarations to initializer (fix #290)
Browse files Browse the repository at this point in the history
fix a empty loop scenario

add more tests and fixes

handle for statments without block

lift both var/let declarations

move function out of ForStatment
  • Loading branch information
vigneshshanmugam authored and boopathi committed Nov 27, 2016
1 parent deff8c6 commit 4c9a11c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
Expand Up @@ -70,4 +70,86 @@ describe("transform-merge-sibling-variables-plugin", () => {

expect(transform(source)).toBe(expected);
});

it("lift var declarations to loop intializer", () => {
const source = unpad(`
for (var i = 0; i < 0; i++) {
var j = jj();
}
for (var i=0;;) var j = 0;
`);
const expected = unpad(`
for (var i = 0, j; i < 0; i++) {
j = jj();
}
for (var i = 0, j;;) j = 0;
`);

expect(transform(source)).toBe(expected);
});

it("lift let declarations to loop intializer", () => {
const source = unpad(`
for (let i = 0; i < 0; i++) {
let j = jj();
}
`);
const expected = unpad(`
for (let i = 0, j; i < 0; i++) {
j = jj();
}
`);

expect(transform(source)).toBe(expected);
});

it("dont lift declarations on object/array pattern", () => {
const source = unpad(`
for (var i = 0; i < 0; i++) {
var [j] = jj();
}
for (var i = 0; i < 0; i++) {
var { j } = jj();
}
`);

expect(transform(source)).toBe(source);
});

it("dont lift declarations when no body is present", () => {
const source = unpad(`
for (;;) {}
for (;;) var i = 0;
`);

expect(transform(source)).toBe(source);
});

it("dont lift when the declarations are of different kind", () => {
const source = unpad(`
for (let i = 0; i < 0; i++) {
var i = 0;
}
`);

expect(transform(source)).toBe(source);
});

it("dont lift when there are multiple declarations", () => {
const source = unpad(`
for (var i = 0; i < 0; i++) {
var i = 0, k = 0;
}
`);

const expected = unpad(`
for (var i = 0; i < 0; i++) {
var i = 0,
k = 0;
}
`);

expect(transform(source)).toBe(expected);
});

});
@@ -1,9 +1,53 @@
"use strict";

module.exports = function() {
module.exports = function({ types: t }) {

function liftDeclaration(path, body, kind) {
if (body[0] && body[0].isVariableDeclaration({ kind: kind })) {

if (body[0].node.declarations.length > 1) {
return;
}

if (body[1] && body[1].isVariableDeclaration({ kind: kind })) {
return;
}

let firstNode = body[0].node.declarations[0];

if (!t.isIdentifier(firstNode.id)) {
return;
}

let init = path.get("init");
if (!init.isVariableDeclaration({ kind: kind })) {
return;
}

init.node.declarations = init.node.declarations.concat(
firstNode.id
);

body[0].replaceWith(t.assignmentExpression(
"=",
t.clone(firstNode.id),
t.clone(firstNode.init)
));
}
}

return {
name: "transform-merge-sibling-variables",
visitor: {
ForStatement(path) {

// Lift declarations to the loop initializer
let body = path.get("body");
body = body.isBlockStatement() ? body.get("body") : [ body ];

liftDeclaration(path, body, "var");
liftDeclaration(path, body, "let");
},
VariableDeclaration: {
enter: [
// concat variables of the same kind with their siblings
Expand Down

0 comments on commit 4c9a11c

Please sign in to comment.