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-expr): SwitchStatement with IfStatement cases #11728

Merged
merged 12 commits into from Sep 18, 2020
@@ -0,0 +1,10 @@
const x = (n) => do {
switch (n) {
case 0:
if (true) {
break;
}
}
}

expect(x(0)).toBe(undefined);
@@ -0,0 +1,8 @@
const x = (n) => do {
switch (n) {
case 0:
if (true) {
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case where BreakStatement does not follow an ExpressionStatement?

case 1:
  if (true) {
    break;
  }

}
}
@@ -0,0 +1,6 @@
const x = n => function () {
switch (n) {
case 0:
if (true) return void 0;
}
}();
@@ -0,0 +1,10 @@
const x = (n) => do {
switch (n) {
case 0:
if (true) {
'out';
break;
}

}
}
@@ -0,0 +1,9 @@
const x = n => function () {
switch (n) {
case 0:
if (true) {
return 'out';
}

}
}();
49 changes: 35 additions & 14 deletions packages/babel-traverse/src/path/family.js
Expand Up @@ -18,27 +18,48 @@ function addCompletionRecords(path, paths) {
return paths;
}

function findBreak(statements): ?NodePath {
let breakStatement;
if (!Array.isArray(statements)) {
statements = [statements];
}

for (const statement of statements) {
if (
statement.isDoExpression() ||
statement.isProgram() ||
statement.isBlockStatement() ||
statement.isCatchClause() ||
statement.isLabeledStatement()
) {
breakStatement = findBreak(statement.get("body"));
} else if (statement.isIfStatement()) {
breakStatement =
findBreak(statement.get("consequent")) ??
findBreak(statement.get("alternate"));
} else if (statement.isTryStatement()) {
breakStatement =
findBreak(statement.get("block")) ??
findBreak(statement.get("handler"));
} else if (statement.isBreakStatement()) {
breakStatement = statement;
}

if (breakStatement) {
return breakStatement;
}
}
return null;
}

function completionRecordForSwitch(cases, paths) {
let isLastCaseWithConsequent = true;

for (let i = cases.length - 1; i >= 0; i--) {
const switchCase = cases[i];
const consequent = switchCase.get("consequent");

let breakStatement;
findBreak: for (const statement of consequent) {
if (statement.isBlockStatement()) {
for (const statementInBlock of statement.get("body")) {
if (statementInBlock.isBreakStatement()) {
breakStatement = statementInBlock;
break findBreak;
}
}
} else if (statement.isBreakStatement()) {
breakStatement = statement;
break;
}
}
let breakStatement = findBreak(consequent);

if (breakStatement) {
while (
Expand Down