Skip to content

Commit

Permalink
skip following empty statements in toSequenceExpression (#11724)
Browse files Browse the repository at this point in the history
* fix: statementlist behavior

* fixed prettier and babel-types/converter errors

* added check for first node

* remove node length check, only check if node is first

* add reset eLU if proceeding is non-empty

* fix failure for ci tests

* remove .expressions since sequence expression isn't produced

* changed test title
  • Loading branch information
wlawt committed Jun 25, 2020
1 parent 6591114 commit cfaa70d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
@@ -0,0 +1,15 @@
expect(do {
x = do { 1; };
}).toBe(1);

expect(do {
z = do { 1;;;; };
}).toBe(1)

expect(do {
w = (do { 1;;;; });
}).toBe(1);

expect(do {
k = do { ; };
}).toBe(undefined);
11 changes: 9 additions & 2 deletions packages/babel-types/src/converters/gatherSequenceExpressions.js
Expand Up @@ -25,7 +25,11 @@ export default function gatherSequenceExpressions(
let ensureLastUndefined = true;

for (const node of nodes) {
ensureLastUndefined = false;
// if we encounter emptyStatement before a non-emptyStatement
// we want to disregard that
if (!isEmptyStatement(node)) {
ensureLastUndefined = false;
}

if (isExpression(node)) {
exprs.push(node);
Expand Down Expand Up @@ -66,7 +70,10 @@ export default function gatherSequenceExpressions(
exprs.push(body);
} else if (isEmptyStatement(node)) {
// empty statement so ensure the last item is undefined if we're last
ensureLastUndefined = true;
// checks if emptyStatement is first
if (nodes.indexOf(node) === 0) {
ensureLastUndefined = true;
}
} else {
// bailed, we can't turn this statement into an expression
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-types/test/converters.js
Expand Up @@ -317,10 +317,10 @@ describe("converters", function () {
const sequence = t.toSequenceExpression([undefinedNode, node], scope);
expect(sequence).toBeUndefined();
});
it("gathers empty statements", function () {
it("gathers empty statements if first element", function () {
const node = parseCode(";");
const sequence = t.toSequenceExpression([undefinedNode, node], scope);
expect(generateCode(sequence.expressions[1])).toBe("undefined");
expect(generateCode(sequence)).toBe("undefined");
});
it("skips empty statement if expression afterwards", function () {
const node = parseCode("{ ; true }");
Expand Down

0 comments on commit cfaa70d

Please sign in to comment.