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

Turn on no-fallthrough rule #11093

Merged
merged 2 commits into from Feb 7, 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
1 change: 1 addition & 0 deletions babel.config.js
Expand Up @@ -36,6 +36,7 @@ module.exports = function(api) {
case "standalone":
includeRegeneratorRuntime = true;
unambiguousSources.push("packages/babel-runtime/regenerator");
// fall through
Copy link
Member

Choose a reason for hiding this comment

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

I hate that prettier doesn't include this comment, since conceptually it's a control flow statement similar to break/continue.

Copy link
Member

Choose a reason for hiding this comment

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

Classic ambiguous comment indentation 😆

case "rollup":
convertESM = false;
ignoreLib = false;
Expand Down
1 change: 0 additions & 1 deletion eslint/babel-eslint-config-internal/index.js
Expand Up @@ -30,7 +30,6 @@ module.exports = {
"no-console": "off",
"no-constant-condition": "off",
"no-empty": "off",
"no-fallthrough": "off",
"no-inner-declarations": "off",
"no-labels": "off",
"no-loop-func": "off",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/src/config/files/module-types.js
Expand Up @@ -21,6 +21,7 @@ export default function* loadCjsOrMjsDefault(
} catch (e) {
if (e.code !== "ERR_REQUIRE_ESM") throw e;
}
// fall through
case "mjs":
if (yield* isAsync()) {
return yield* waitFor(loadMjsDefault(filepath));
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1058,7 +1058,7 @@ export default class ExpressionParser extends LValParser {

case tt.at:
this.parseDecorators();

// fall through
case tt._class:
node = this.startNode();
this.takeDecorators(node);
Expand Down Expand Up @@ -1111,7 +1111,7 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "PipelinePrimaryTopicReference");
}
}

// fall through
default:
throw this.unexpected();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/flow.js
Expand Up @@ -1441,7 +1441,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);
}

this.unexpected();
throw this.unexpected();
case tt.num:
return this.parseLiteral(
this.state.value,
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -332,7 +332,7 @@ export default class Tokenizer extends LocationParser {
) {
++this.state.pos;
}

// fall through
case charCodes.lineFeed:
case charCodes.lineSeparator:
case charCodes.paragraphSeparator:
Expand Down Expand Up @@ -736,7 +736,7 @@ export default class Tokenizer extends LocationParser {
}
}
// Anything else beginning with a digit is an integer, octal
// number, or float.
// number, or float. (fall through)
case charCodes.digit1:
case charCodes.digit2:
case charCodes.digit3:
Expand Down Expand Up @@ -1221,6 +1221,7 @@ export default class Tokenizer extends LocationParser {
if (this.input.charCodeAt(this.state.pos) === charCodes.lineFeed) {
++this.state.pos;
}
// fall through
case charCodes.lineFeed:
out += "\n";
break;
Expand Down Expand Up @@ -1268,9 +1269,11 @@ export default class Tokenizer extends LocationParser {
if (this.input.charCodeAt(this.state.pos) === charCodes.lineFeed) {
++this.state.pos;
}
// fall through
case charCodes.lineFeed:
this.state.lineStart = this.state.pos;
++this.state.curLine;
// fall through
case charCodes.lineSeparator:
case charCodes.paragraphSeparator:
return "";
Expand All @@ -1279,6 +1282,7 @@ export default class Tokenizer extends LocationParser {
if (inTemplate) {
return null;
}
// fall through
default:
if (ch >= charCodes.digit0 && ch <= charCodes.digit7) {
const codePos = this.state.pos - 1;
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-types/src/validators/isReferenced.js
Expand Up @@ -54,16 +54,18 @@ export default function isReferenced(
if (parent.params.includes(node)) {
return false;
}
// Fall-through to next case clause to check whether the node is the method's name.
// fall through

// yes: { [NODE]: "" }
// no: { NODE: "" }
// depends: { NODE }
// depends: { key: NODE }
// fall through
Copy link
Member

Choose a reason for hiding this comment

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

@kaicataldo The comment on line 57 is not enough. Could eslint/eslint#10608 be reopened? 🙏 😅

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should allow it to be either the first or last, but not the middle? I know it's unlikely, but I think it would be good to avoid something like this triggering it:

/* eslint no-fallthrough: ["error", { "commentPattern": "fall-through[\\s\\w]*not allowed" }] */

switch(foo) {
    case 1:
        doSomething();
        // We don't want to fall through here
        // because fall-through is not allowed,
        // oh look, a run on sentence.

    default:
        doSomething();
}

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this code already have that problem?

/* eslint no-fallthrough: ["error", { "commentPattern": "fall-through[\\s\\w]*not allowed" }] */

switch(foo) {
    case 1:
        doSomething();
        // We don't want to fall through here because fall-through is not allowed.

    default:
        doSomething();
}

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it would. Might just be me, but the grouped line comments seem more error prone. It probably is the same thing in practice, though.

case "ObjectProperty":
// no: class { NODE = value; }
// yes: class { [NODE] = value; }
// yes: class { key = NODE; }
// fall through
case "ClassProperty":
case "ClassPrivateProperty":
if (parent.key === node) {
Expand Down