Skip to content

Commit

Permalink
Be less clever about eliminating side-effect-free statements.
Browse files Browse the repository at this point in the history
Closes #17.
  • Loading branch information
benjamn committed Oct 21, 2013
1 parent 6aafb34 commit cfb6494
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/emit.js
Expand Up @@ -325,8 +325,12 @@ Ep.explodeStatement = function(path, labelId) {
}

if (!meta.containsLeap(stmt)) {
if (meta.hasSideEffects(stmt))
self.emit(stmt);
// Technically we should be able to avoid emitting the statement
// altogether if !meta.hasSideEffects(stmt), but that leads to
// confusing generated code (for instance, `while (true) {}` just
// disappears) and is probably a more appropriate job for a dedicated
// dead code elimination pass.
self.emit(stmt);
return;
}

Expand Down
21 changes: 21 additions & 0 deletions test/tests.es6.js
Expand Up @@ -739,3 +739,24 @@ describe("catch parameter shadowing", function() {
check(gen(11), [11, 13, 26, 5, 4, 11]);
});
});

describe("empty while loops", function() {
it("should be preserved in generated code", function() {
function *gen(x) {
while (x) {
// empty while loop
}

do {
// empty do-while loop
} while (x);

return gen.toString();
}

var info = gen(false).next();
assert.strictEqual(info.done, true);
assert.ok(/empty while loop/.test(info.value));
assert.ok(/empty do-while loop/.test(info.value));
});
});

0 comments on commit cfb6494

Please sign in to comment.