|
1 | 1 | 'use strict';
|
2 | 2 | const {isParenthesized, isNotSemicolonToken} = require('@eslint-community/eslint-utils');
|
3 |
| -const needsSemicolon = require('./utils/needs-semicolon.js'); |
| 3 | +const {needsSemicolon} = require('./utils/index.js'); |
4 | 4 | const {removeSpacesAfter} = require('./fix/index.js');
|
5 |
| -const {matches} = require('./selectors/index.js'); |
6 | 5 |
|
7 | 6 | const MESSAGE_ID = 'no-lonely-if';
|
8 | 7 | const messages = {
|
9 | 8 | [MESSAGE_ID]: 'Unexpected `if` as the only statement in a `if` block without `else`.',
|
10 | 9 | };
|
11 | 10 |
|
12 |
| -const ifStatementWithoutAlternate = 'IfStatement:not([alternate])'; |
13 |
| -const selector = matches([ |
14 |
| - // `if (a) { if (b) {} }` |
15 |
| - [ |
16 |
| - ifStatementWithoutAlternate, |
17 |
| - ' > ', |
18 |
| - 'BlockStatement.consequent', |
19 |
| - '[body.length=1]', |
20 |
| - ' > ', |
21 |
| - `${ifStatementWithoutAlternate}.body`, |
22 |
| - ].join(''), |
23 |
| - |
24 |
| - // `if (a) if (b) {}` |
25 |
| - `${ifStatementWithoutAlternate} > ${ifStatementWithoutAlternate}.consequent`, |
26 |
| -]); |
| 11 | +const isIfStatementWithoutAlternate = node => node.type === 'IfStatement' && !node.alternate; |
27 | 12 |
|
28 | 13 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
|
29 | 14 | // Lower precedence than `&&`
|
@@ -121,19 +106,36 @@ function fix(innerIfStatement, sourceCode) {
|
121 | 106 | }
|
122 | 107 |
|
123 | 108 | /** @param {import('eslint').Rule.RuleContext} context */
|
124 |
| -const create = context => { |
125 |
| - const {sourceCode} = context; |
126 |
| - |
127 |
| - return { |
128 |
| - [selector](node) { |
129 |
| - return { |
130 |
| - node, |
131 |
| - messageId: MESSAGE_ID, |
132 |
| - fix: fix(node, sourceCode), |
133 |
| - }; |
134 |
| - }, |
135 |
| - }; |
136 |
| -}; |
| 109 | +const create = context => ({ |
| 110 | + IfStatement(ifStatement) { |
| 111 | + if (!( |
| 112 | + isIfStatementWithoutAlternate(ifStatement) |
| 113 | + && ( |
| 114 | + // `if (a) { if (b) {} }` |
| 115 | + ( |
| 116 | + ifStatement.parent.type === 'BlockStatement' |
| 117 | + && ifStatement.parent.body.length === 1 |
| 118 | + && ifStatement.parent.body[0] === ifStatement |
| 119 | + && isIfStatementWithoutAlternate(ifStatement.parent.parent) |
| 120 | + && ifStatement.parent.parent.consequent === ifStatement.parent |
| 121 | + ) |
| 122 | + // `if (a) if (b) {}` |
| 123 | + || ( |
| 124 | + isIfStatementWithoutAlternate(ifStatement.parent) |
| 125 | + && ifStatement.parent.consequent === ifStatement |
| 126 | + ) |
| 127 | + ) |
| 128 | + )) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + node: ifStatement, |
| 134 | + messageId: MESSAGE_ID, |
| 135 | + fix: fix(ifStatement, context.sourceCode), |
| 136 | + }; |
| 137 | + }, |
| 138 | +}); |
137 | 139 |
|
138 | 140 | /** @type {import('eslint').Rule.RuleModule} */
|
139 | 141 | module.exports = {
|
|
0 commit comments