|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + fixSpaceAroundKeyword, |
| 4 | + addParenthesizesToReturnOrThrowExpression, |
| 5 | +} = require('./fix/index.js'); |
| 6 | +const { |
| 7 | + needsSemicolon, |
| 8 | + isParenthesized, |
| 9 | + isOnSameLine, |
| 10 | +} = require('./utils/index.js'); |
| 11 | + |
| 12 | +const MESSAGE_ID_ERROR = 'no-negation-in-equality-check/error'; |
| 13 | +const MESSAGE_ID_SUGGESTION = 'no-negation-in-equality-check/suggestion'; |
| 14 | +const messages = { |
| 15 | + [MESSAGE_ID_ERROR]: 'Negated expression in not allowed in equality check.', |
| 16 | + [MESSAGE_ID_SUGGESTION]: 'Switch to \'{{operator}}\' check.', |
| 17 | +}; |
| 18 | + |
| 19 | +const EQUALITY_OPERATORS = new Set([ |
| 20 | + '===', |
| 21 | + '!==', |
| 22 | + '==', |
| 23 | + '!=', |
| 24 | +]); |
| 25 | + |
| 26 | +const isEqualityCheck = node => node.type === 'BinaryExpression' && EQUALITY_OPERATORS.has(node.operator); |
| 27 | +const isNegatedExpression = node => node.type === 'UnaryExpression' && node.prefix && node.operator === '!'; |
| 28 | + |
| 29 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 30 | +const create = context => ({ |
| 31 | + BinaryExpression(binaryExpression) { |
| 32 | + const {operator, left} = binaryExpression; |
| 33 | + |
| 34 | + if ( |
| 35 | + !isEqualityCheck(binaryExpression) |
| 36 | + || !isNegatedExpression(left) |
| 37 | + ) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const {sourceCode} = context; |
| 42 | + const bangToken = sourceCode.getFirstToken(left); |
| 43 | + const negatedOperator = `${operator.startsWith('!') ? '=' : '!'}${operator.slice(1)}`; |
| 44 | + |
| 45 | + return { |
| 46 | + node: bangToken, |
| 47 | + messageId: MESSAGE_ID_ERROR, |
| 48 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 49 | + suggest: [ |
| 50 | + { |
| 51 | + messageId: MESSAGE_ID_SUGGESTION, |
| 52 | + data: { |
| 53 | + operator: negatedOperator, |
| 54 | + }, |
| 55 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 56 | + * fix(fixer) { |
| 57 | + yield * fixSpaceAroundKeyword(fixer, binaryExpression, sourceCode); |
| 58 | + |
| 59 | + const tokenAfterBang = sourceCode.getTokenAfter(bangToken); |
| 60 | + |
| 61 | + const {parent} = binaryExpression; |
| 62 | + if ( |
| 63 | + (parent.type === 'ReturnStatement' || parent.type === 'ThrowStatement') |
| 64 | + && !isParenthesized(binaryExpression, sourceCode) |
| 65 | + ) { |
| 66 | + const returnToken = sourceCode.getFirstToken(parent); |
| 67 | + if (!isOnSameLine(returnToken, tokenAfterBang)) { |
| 68 | + yield * addParenthesizesToReturnOrThrowExpression(fixer, parent, sourceCode); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + yield fixer.remove(bangToken); |
| 73 | + |
| 74 | + const previousToken = sourceCode.getTokenBefore(bangToken); |
| 75 | + if (needsSemicolon(previousToken, sourceCode, tokenAfterBang.value)) { |
| 76 | + yield fixer.insertTextAfter(bangToken, ';'); |
| 77 | + } |
| 78 | + |
| 79 | + const operatorToken = sourceCode.getTokenAfter( |
| 80 | + left, |
| 81 | + token => token.type === 'Punctuator' && token.value === operator, |
| 82 | + ); |
| 83 | + yield fixer.replaceText(operatorToken, negatedOperator); |
| 84 | + }, |
| 85 | + }, |
| 86 | + ], |
| 87 | + }; |
| 88 | + }, |
| 89 | +}); |
| 90 | + |
| 91 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 92 | +module.exports = { |
| 93 | + create, |
| 94 | + meta: { |
| 95 | + type: 'problem', |
| 96 | + docs: { |
| 97 | + description: 'Disallow negated expression in equality check.', |
| 98 | + recommended: true, |
| 99 | + }, |
| 100 | + |
| 101 | + hasSuggestions: true, |
| 102 | + messages, |
| 103 | + }, |
| 104 | +}; |
0 commit comments