Skip to content

Commit

Permalink
Remove undef from seqExpr - (Fix #373)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Jan 23, 2017
1 parent 2b98b98 commit d228fc6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Expand Up @@ -171,4 +171,36 @@ describe("transform-remove-undefined-plugin", () => {
}`);
expect(transform(source)).toBe(source);
});

it("should remove from sequence expressions", () => {
const source = unpad(`
a = b, void 0, b = c, d.e.f(), void 0, hello.world();
`);
const expected = unpad(`
a = b, b = c, d.e.f(), hello.world();
`);
expect(transform(source)).toBe(expected);
});

it("should NOT remove last undefined from sequence expressions", () => {
const source = unpad(`
if (foo.bar(), void 0) {
foo.baz();
}
function bar() {
return a.b(), void 0;
}
`);
expect(transform(source)).toBe(source);
});

it("should remove last undefined from sequence expressions if safe", () => {
const source = unpad(`
a = b, void 0, b = c, d.e.f(), void 0, hello.world(), void 0;
`);
const expected = unpad(`
a = b, b = c, d.e.f(), hello.world();
`);
expect(transform(source)).toBe(expected);
});
});
18 changes: 18 additions & 0 deletions packages/babel-plugin-transform-remove-undefined/src/index.js
Expand Up @@ -80,6 +80,24 @@ module.exports = function() {
return {
name: "transform-remove-undefined",
visitor: {
SequenceExpression(path) {
const expressions = path.get("expressions");

for (let i = 0; i < expressions.length; i++) {
const expr = expressions[i];
if (!isPureAndUndefined(expr)) continue;

// last value
if (i === expressions.length - 1) {
if (path.parentPath.isExpressionStatement()) {
expr.remove();
}
} else {
expr.remove();
}
}
},

ReturnStatement(path) {
if (path.node.argument !== null) {
if (isPureAndUndefined(path.get("argument"))) {
Expand Down

0 comments on commit d228fc6

Please sign in to comment.