[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to disallow if as the only statement in an else block
|
---|
| 3 | * @author Brandon Mills
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | //------------------------------------------------------------------------------
|
---|
| 8 | // Rule Definition
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | /** @type {import('../shared/types').Rule} */
|
---|
| 12 | module.exports = {
|
---|
| 13 | meta: {
|
---|
| 14 | type: "suggestion",
|
---|
| 15 |
|
---|
| 16 | docs: {
|
---|
| 17 | description: "Disallow `if` statements as the only statement in `else` blocks",
|
---|
| 18 | recommended: false,
|
---|
| 19 | url: "https://eslint.org/docs/latest/rules/no-lonely-if"
|
---|
| 20 | },
|
---|
| 21 |
|
---|
| 22 | schema: [],
|
---|
| 23 | fixable: "code",
|
---|
| 24 |
|
---|
| 25 | messages: {
|
---|
| 26 | unexpectedLonelyIf: "Unexpected if as the only statement in an else block."
|
---|
| 27 | }
|
---|
| 28 | },
|
---|
| 29 |
|
---|
| 30 | create(context) {
|
---|
| 31 | const sourceCode = context.sourceCode;
|
---|
| 32 |
|
---|
| 33 | return {
|
---|
| 34 | IfStatement(node) {
|
---|
| 35 | const parent = node.parent,
|
---|
| 36 | grandparent = parent.parent;
|
---|
| 37 |
|
---|
| 38 | if (parent && parent.type === "BlockStatement" &&
|
---|
| 39 | parent.body.length === 1 && grandparent &&
|
---|
| 40 | grandparent.type === "IfStatement" &&
|
---|
| 41 | parent === grandparent.alternate) {
|
---|
| 42 | context.report({
|
---|
| 43 | node,
|
---|
| 44 | messageId: "unexpectedLonelyIf",
|
---|
| 45 | fix(fixer) {
|
---|
| 46 | const openingElseCurly = sourceCode.getFirstToken(parent);
|
---|
| 47 | const closingElseCurly = sourceCode.getLastToken(parent);
|
---|
| 48 | const elseKeyword = sourceCode.getTokenBefore(openingElseCurly);
|
---|
| 49 | const tokenAfterElseBlock = sourceCode.getTokenAfter(closingElseCurly);
|
---|
| 50 | const lastIfToken = sourceCode.getLastToken(node.consequent);
|
---|
| 51 | const sourceText = sourceCode.getText();
|
---|
| 52 |
|
---|
| 53 | if (sourceText.slice(openingElseCurly.range[1],
|
---|
| 54 | node.range[0]).trim() || sourceText.slice(node.range[1], closingElseCurly.range[0]).trim()) {
|
---|
| 55 |
|
---|
| 56 | // Don't fix if there are any non-whitespace characters interfering (e.g. comments)
|
---|
| 57 | return null;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if (
|
---|
| 61 | node.consequent.type !== "BlockStatement" && lastIfToken.value !== ";" && tokenAfterElseBlock &&
|
---|
| 62 | (
|
---|
| 63 | node.consequent.loc.end.line === tokenAfterElseBlock.loc.start.line ||
|
---|
| 64 | /^[([/+`-]/u.test(tokenAfterElseBlock.value) ||
|
---|
| 65 | lastIfToken.value === "++" ||
|
---|
| 66 | lastIfToken.value === "--"
|
---|
| 67 | )
|
---|
| 68 | ) {
|
---|
| 69 |
|
---|
| 70 | /*
|
---|
| 71 | * If the `if` statement has no block, and is not followed by a semicolon, make sure that fixing
|
---|
| 72 | * the issue would not change semantics due to ASI. If this would happen, don't do a fix.
|
---|
| 73 | */
|
---|
| 74 | return null;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return fixer.replaceTextRange(
|
---|
| 78 | [openingElseCurly.range[0], closingElseCurly.range[1]],
|
---|
| 79 | (elseKeyword.range[1] === openingElseCurly.range[0] ? " " : "") + sourceCode.getText(node)
|
---|
| 80 | );
|
---|
| 81 | }
|
---|
| 82 | });
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 | };
|
---|