Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do-statementlist behavior #11724

Merged
merged 8 commits into from Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,15 @@
expect(do {
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
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