/
no-lonely-if.js
150 lines (129 loc) · 4.36 KB
/
no-lonely-if.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
const {isParenthesized, isNotSemicolonToken} = require('eslint-utils');
const needsSemicolon = require('./utils/needs-semicolon.js');
const {removeSpacesAfter} = require('./fix/index.js');
const {matches} = require('./selectors/index.js');
const MESSAGE_ID = 'no-lonely-if';
const messages = {
[MESSAGE_ID]: 'Unexpected `if` as the only statement in a `if` block without `else`.',
};
const ifStatementWithoutAlternate = 'IfStatement:not([alternate])';
const selector = matches([
// `if (a) { if (b) {} }`
[
ifStatementWithoutAlternate,
' > ',
'BlockStatement.consequent',
'[body.length=1]',
' > ',
`${ifStatementWithoutAlternate}.body`,
].join(''),
// `if (a) if (b) {}`
`${ifStatementWithoutAlternate} > ${ifStatementWithoutAlternate}.consequent`,
]);
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
// Lower precedence than `&&`
const needParenthesis = node => (
(node.type === 'LogicalExpression' && (node.operator === '||' || node.operator === '??'))
|| node.type === 'ConditionalExpression'
|| node.type === 'AssignmentExpression'
|| node.type === 'YieldExpression'
|| node.type === 'SequenceExpression'
);
function getIfStatementTokens(node, sourceCode) {
const tokens = {};
tokens.ifToken = sourceCode.getFirstToken(node);
tokens.openingParenthesisToken = sourceCode.getFirstToken(node, 1);
const {consequent} = node;
tokens.closingParenthesisToken = sourceCode.getTokenBefore(consequent);
if (consequent.type === 'BlockStatement') {
tokens.openingBraceToken = sourceCode.getFirstToken(consequent);
tokens.closingBraceToken = sourceCode.getLastToken(consequent);
}
return tokens;
}
function fix(innerIfStatement, sourceCode) {
return function * (fixer) {
const outerIfStatement = (
innerIfStatement.parent.type === 'BlockStatement'
? innerIfStatement.parent
: innerIfStatement
).parent;
const outer = {
...outerIfStatement,
...getIfStatementTokens(outerIfStatement, sourceCode),
};
const inner = {
...innerIfStatement,
...getIfStatementTokens(innerIfStatement, sourceCode),
};
// Remove inner `if` token
yield fixer.remove(inner.ifToken);
yield removeSpacesAfter(inner.ifToken, sourceCode, fixer);
// Remove outer `{}`
if (outer.openingBraceToken) {
yield fixer.remove(outer.openingBraceToken);
yield removeSpacesAfter(outer.openingBraceToken, sourceCode, fixer);
yield fixer.remove(outer.closingBraceToken);
const tokenBefore = sourceCode.getTokenBefore(outer.closingBraceToken, {includeComments: true});
yield removeSpacesAfter(tokenBefore, sourceCode, fixer);
}
// Add new `()`
yield fixer.insertTextBefore(outer.openingParenthesisToken, '(');
yield fixer.insertTextAfter(
inner.closingParenthesisToken,
`)${inner.consequent.type === 'EmptyStatement' ? '' : ' '}`,
);
// Add ` && `
yield fixer.insertTextAfter(outer.closingParenthesisToken, ' && ');
// Remove `()` if `test` don't need it
for (const {test, openingParenthesisToken, closingParenthesisToken} of [outer, inner]) {
if (
isParenthesized(test, sourceCode)
|| !needParenthesis(test)
) {
yield fixer.remove(openingParenthesisToken);
yield fixer.remove(closingParenthesisToken);
}
yield removeSpacesAfter(closingParenthesisToken, sourceCode, fixer);
}
// If the `if` statement has no block, and is not followed by a semicolon,
// make sure that fixing the issue would not change semantics due to ASI.
// Similar logic https://github.com/eslint/eslint/blob/2124e1b5dad30a905dc26bde9da472bf622d3f50/lib/rules/no-lonely-if.js#L61-L77
if (inner.consequent.type !== 'BlockStatement') {
const lastToken = sourceCode.getLastToken(inner.consequent);
if (isNotSemicolonToken(lastToken)) {
const nextToken = sourceCode.getTokenAfter(outer);
if (needsSemicolon(lastToken, sourceCode, nextToken.value)) {
yield fixer.insertTextBefore(nextToken, ';');
}
}
}
};
}
const create = context => {
const sourceCode = context.getSourceCode();
return {
[selector](node) {
return {
node,
messageId: MESSAGE_ID,
fix: fix(node, sourceCode),
};
},
};
};
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow `if` statements as the only statement in `if` blocks without `else`.',
},
fixable: 'code',
messages,
},
};