From 6d8894a1f9ba6f6f4a92b4b7dfa97d67bc2add52 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 11 May 2024 10:38:57 +0800 Subject: [PATCH] Lint --- docs/rules/no-negation-in-equality-check.md | 4 +- readme.md | 2 +- rules/no-negation-in-equality-check.js | 87 ++++++++++----------- test/no-negation-in-equality-check.mjs | 1 + 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/docs/rules/no-negation-in-equality-check.md b/docs/rules/no-negation-in-equality-check.md index 73dbd21c2a..248261ebeb 100644 --- a/docs/rules/no-negation-in-equality-check.md +++ b/docs/rules/no-negation-in-equality-check.md @@ -1,4 +1,4 @@ -# Disallow negated expreesion in equality check +# Disallow negated expression in equality check 💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). @@ -7,7 +7,7 @@ - +Using a negated expression in equality check is most likely a mistake. ## Fail diff --git a/readme.md b/readme.md index 3d97143369..163a4b7572 100644 --- a/readme.md +++ b/readme.md @@ -145,7 +145,7 @@ If you don't use the preset, ensure you use the same `env` and `parserOptions` c | [no-lonely-if](docs/rules/no-lonely-if.md) | Disallow `if` statements as the only statement in `if` blocks without `else`. | ✅ | 🔧 | | | [no-magic-array-flat-depth](docs/rules/no-magic-array-flat-depth.md) | Disallow a magic number as the `depth` argument in `Array#flat(…).` | ✅ | | | | [no-negated-condition](docs/rules/no-negated-condition.md) | Disallow negated conditions. | ✅ | 🔧 | | -| [no-negation-in-equality-check](docs/rules/no-negation-in-equality-check.md) | Disallow negated expreesion in equality check. | ✅ | | 💡 | +| [no-negation-in-equality-check](docs/rules/no-negation-in-equality-check.md) | Disallow negated expression in equality check. | ✅ | | 💡 | | [no-nested-ternary](docs/rules/no-nested-ternary.md) | Disallow nested ternary expressions. | ✅ | 🔧 | | | [no-new-array](docs/rules/no-new-array.md) | Disallow `new Array()`. | ✅ | 🔧 | 💡 | | [no-new-buffer](docs/rules/no-new-buffer.md) | Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`. | ✅ | 🔧 | 💡 | diff --git a/rules/no-negation-in-equality-check.js b/rules/no-negation-in-equality-check.js index b48a179b1f..f76b0549d7 100644 --- a/rules/no-negation-in-equality-check.js +++ b/rules/no-negation-in-equality-check.js @@ -20,56 +20,53 @@ const isEqualityCheck = node => node.type === 'BinaryExpression' && EQUALITY_OPE const isNegatedExpression = node => node.type === 'UnaryExpression' && node.prefix && node.operator === '!'; /** @param {import('eslint').Rule.RuleContext} context */ -const create = context => { - return { - BinaryExpression(binaryExpression) { - const {operator, left} = binaryExpression; +const create = context => ({ + BinaryExpression(binaryExpression) { + const {operator, left} = binaryExpression; - if ( - !isEqualityCheck(binaryExpression) - || !isNegatedExpression(left) - ) { - return; - } + if ( + !isEqualityCheck(binaryExpression) + || !isNegatedExpression(left) + ) { + return; + } + const {sourceCode} = context; + const bangToken = sourceCode.getFirstToken(left); + const negatedOperator = `${operator.startsWith('!') ? '=' : '!'}${operator.slice(1)}`; - 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); + yield fixer.remove(bangToken); - 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) - yield fixer.remove(bangToken); + const previousToken = sourceCode.getTokenBefore(bangToken); + const textAfter = sourceCode.getTokenAfter(bangToken).value; + if (needsSemicolon(previousToken, sourceCode, textAfter)) { + yield fixer.insertTextAfter(bangToken, ';'); + } - const previousToken = sourceCode.getTokenBefore(bangToken); - const textAfter = sourceCode.getTokenAfter(bangToken).value; - if (needsSemicolon(previousToken, sourceCode, textAfter)) { - yield fixer.insertTextAfter(bangToken, ';'); - } - - const operatorToken = sourceCode.getTokenAfter( - left, - token => token.type == 'Punctuator' && token.value === operator, - ); - yield fixer.replaceText(operatorToken, negatedOperator); - }, - } - ], - }; - }, - }; -}; + const operatorToken = sourceCode.getTokenAfter( + left, + token => token.type == 'Punctuator' && token.value === operator, + ); + yield fixer.replaceText(operatorToken, negatedOperator); + }, + }, + ], + }; + }, +}); /** @type {import('eslint').Rule.RuleModule} */ module.exports = { diff --git a/test/no-negation-in-equality-check.mjs b/test/no-negation-in-equality-check.mjs index 421b669b49..c514be1499 100644 --- a/test/no-negation-in-equality-check.mjs +++ b/test/no-negation-in-equality-check.mjs @@ -7,6 +7,7 @@ test.snapshot({ valid: [ '!foo instanceof bar', '+foo === bar', + '!(foo === bar)', // We are not checking right side 'foo === !bar', ],