-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-negation-in-equality-check
rule (#2353)
- Loading branch information
Showing
6 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Disallow negated expression in equality check | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
Using a negated expression in equality check is most likely a mistake. | ||
|
||
## Fail | ||
|
||
```js | ||
if (!foo === bar) {} | ||
``` | ||
|
||
```js | ||
if (!foo !== bar) {} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
if (foo !== bar) {} | ||
``` | ||
|
||
```js | ||
if (!(foo === bar)) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
const { | ||
fixSpaceAroundKeyword, | ||
addParenthesizesToReturnOrThrowExpression, | ||
} = require('./fix/index.js'); | ||
const { | ||
needsSemicolon, | ||
isParenthesized, | ||
isOnSameLine, | ||
} = require('./utils/index.js'); | ||
|
||
const MESSAGE_ID_ERROR = 'no-negation-in-equality-check/error'; | ||
const MESSAGE_ID_SUGGESTION = 'no-negation-in-equality-check/suggestion'; | ||
const messages = { | ||
[MESSAGE_ID_ERROR]: 'Negated expression in not allowed in equality check.', | ||
[MESSAGE_ID_SUGGESTION]: 'Switch to \'{{operator}}\' check.', | ||
}; | ||
|
||
const EQUALITY_OPERATORS = new Set([ | ||
'===', | ||
'!==', | ||
'==', | ||
'!=', | ||
]); | ||
|
||
const isEqualityCheck = node => node.type === 'BinaryExpression' && EQUALITY_OPERATORS.has(node.operator); | ||
const isNegatedExpression = node => node.type === 'UnaryExpression' && node.prefix && node.operator === '!'; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => ({ | ||
BinaryExpression(binaryExpression) { | ||
const {operator, left} = binaryExpression; | ||
|
||
if ( | ||
!isEqualityCheck(binaryExpression) | ||
|| !isNegatedExpression(left) | ||
) { | ||
return; | ||
} | ||
|
||
const {sourceCode} = context; | ||
const bangToken = sourceCode.getFirstToken(left); | ||
const negatedOperator = `${operator.startsWith('!') ? '=' : '!'}${operator.slice(1)}`; | ||
|
||
return { | ||
node: bangToken, | ||
messageId: MESSAGE_ID_ERROR, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
suggest: [ | ||
{ | ||
messageId: MESSAGE_ID_SUGGESTION, | ||
data: { | ||
operator: negatedOperator, | ||
}, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
* fix(fixer) { | ||
yield * fixSpaceAroundKeyword(fixer, binaryExpression, sourceCode); | ||
|
||
const tokenAfterBang = sourceCode.getTokenAfter(bangToken); | ||
|
||
const {parent} = binaryExpression; | ||
if ( | ||
(parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement') | ||
&& !isParenthesized(binaryExpression, sourceCode) | ||
) { | ||
const returnToken = sourceCode.getFirstToken(parent); | ||
if (!isOnSameLine(returnToken, tokenAfterBang)) { | ||
yield * addParenthesizesToReturnOrThrowExpression(fixer, parent, sourceCode); | ||
} | ||
} | ||
|
||
yield fixer.remove(bangToken); | ||
|
||
const previousToken = sourceCode.getTokenBefore(bangToken); | ||
if (needsSemicolon(previousToken, sourceCode, tokenAfterBang.value)) { | ||
yield fixer.insertTextAfter(bangToken, ';'); | ||
} | ||
|
||
const operatorToken = sourceCode.getTokenAfter( | ||
left, | ||
token => token.type === 'Punctuator' && token.value === operator, | ||
); | ||
yield fixer.replaceText(operatorToken, negatedOperator); | ||
}, | ||
}, | ||
], | ||
}; | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow negated expression in equality check.', | ||
recommended: true, | ||
}, | ||
|
||
hasSuggestions: true, | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import outdent from 'outdent'; | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'!foo instanceof bar', | ||
'+foo === bar', | ||
'!(foo === bar)', | ||
// We are not checking right side | ||
'foo === !bar', | ||
], | ||
invalid: [ | ||
'!foo === bar', | ||
'!foo !== bar', | ||
'!foo == bar', | ||
'!foo != bar', | ||
outdent` | ||
function x() { | ||
return!foo === bar; | ||
} | ||
`, | ||
outdent` | ||
function x() { | ||
return! | ||
foo === bar; | ||
throw! | ||
foo === bar; | ||
} | ||
`, | ||
outdent` | ||
foo | ||
!(a) === b | ||
`, | ||
outdent` | ||
foo | ||
![a, b].join('') === c | ||
`, | ||
outdent` | ||
foo | ||
! [a, b].join('') === c | ||
`, | ||
outdent` | ||
foo | ||
!/* comment */[a, b].join('') === c | ||
`, | ||
'!!foo === bar', | ||
], | ||
}); |
Oops, something went wrong.