|
| 1 | +'use strict'; |
| 2 | +const toLocation = require('./utils/to-location.js'); |
| 3 | +const {isMethodCall, isNegativeOne, isNumberLiteral} = require('./ast/index.js'); |
| 4 | + |
| 5 | +const MESSAGE_ID = 'consistent-existence-index-check'; |
| 6 | +const messages = { |
| 7 | + [MESSAGE_ID]: 'Prefer `{{replacementOperator}} {{replacementValue}}` over `{{originalOperator}} {{originalValue}}` to check {{existenceOrNonExistence}}.', |
| 8 | +}; |
| 9 | + |
| 10 | +const isZero = node => isNumberLiteral(node) && node.value === 0; |
| 11 | + |
| 12 | +/** |
| 13 | +@param {parent: import('estree').BinaryExpression} binaryExpression |
| 14 | +@returns {{ |
| 15 | + replacementOperator: string, |
| 16 | + replacementValue: string, |
| 17 | + originalOperator: string, |
| 18 | + originalValue: string, |
| 19 | +} | undefined} |
| 20 | +*/ |
| 21 | +function getReplacement(binaryExpression) { |
| 22 | + const {operator, right} = binaryExpression; |
| 23 | + |
| 24 | + if (operator === '<' && isZero(right)) { |
| 25 | + return { |
| 26 | + replacementOperator: '===', |
| 27 | + replacementValue: '-1', |
| 28 | + originalOperator: operator, |
| 29 | + originalValue: '0', |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + if (operator === '>' && isNegativeOne(right)) { |
| 34 | + return { |
| 35 | + replacementOperator: '!==', |
| 36 | + replacementValue: '-1', |
| 37 | + originalOperator: operator, |
| 38 | + originalValue: '-1', |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + if (operator === '>=' && isZero(right)) { |
| 43 | + return { |
| 44 | + replacementOperator: '!==', |
| 45 | + replacementValue: '-1', |
| 46 | + originalOperator: operator, |
| 47 | + originalValue: '0', |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 53 | +const create = context => ({ |
| 54 | + /** @param {import('estree').VariableDeclarator} variableDeclarator */ |
| 55 | + * VariableDeclarator(variableDeclarator) { |
| 56 | + if (!( |
| 57 | + variableDeclarator.parent.type === 'VariableDeclaration' |
| 58 | + && variableDeclarator.parent.kind === 'const' |
| 59 | + && variableDeclarator.id.type === 'Identifier' |
| 60 | + && isMethodCall(variableDeclarator.init, {methods: ['indexOf', 'lastIndexOf', 'findIndex', 'findLastIndex']}) |
| 61 | + )) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const variableIdentifier = variableDeclarator.id; |
| 66 | + const variables = context.sourceCode.getDeclaredVariables(variableDeclarator); |
| 67 | + const [variable] = variables; |
| 68 | + |
| 69 | + // Just for safety |
| 70 | + if ( |
| 71 | + variables.length !== 1 |
| 72 | + || variable.identifiers.length !== 1 |
| 73 | + || variable.identifiers[0] !== variableIdentifier |
| 74 | + ) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + for (const {identifier} of variable.references) { |
| 79 | + /** @type {{parent: import('estree').BinaryExpression}} */ |
| 80 | + const binaryExpression = identifier.parent; |
| 81 | + |
| 82 | + if (binaryExpression.type !== 'BinaryExpression' || binaryExpression.left !== identifier) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + const replacement = getReplacement(binaryExpression); |
| 87 | + |
| 88 | + if (!replacement) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const {left, operator, right} = binaryExpression; |
| 93 | + const {sourceCode} = context; |
| 94 | + |
| 95 | + const operatorToken = sourceCode.getTokenAfter( |
| 96 | + left, |
| 97 | + token => token.type === 'Punctuator' && token.value === operator, |
| 98 | + ); |
| 99 | + |
| 100 | + yield { |
| 101 | + node: binaryExpression, |
| 102 | + loc: toLocation([operatorToken.range[0], right.range[1]], sourceCode), |
| 103 | + messageId: MESSAGE_ID, |
| 104 | + data: { |
| 105 | + ...replacement, |
| 106 | + existenceOrNonExistence: `${replacement.replacementOperator === '===' ? 'non-' : ''}existence`, |
| 107 | + }, |
| 108 | + * fix(fixer) { |
| 109 | + yield fixer.replaceText(operatorToken, replacement.replacementOperator); |
| 110 | + |
| 111 | + if (replacement.replacementValue !== replacement.originalValue) { |
| 112 | + yield fixer.replaceText(right, replacement.replacementValue); |
| 113 | + } |
| 114 | + }, |
| 115 | + }; |
| 116 | + } |
| 117 | + }, |
| 118 | +}); |
| 119 | + |
| 120 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 121 | +module.exports = { |
| 122 | + create, |
| 123 | + meta: { |
| 124 | + type: 'problem', |
| 125 | + docs: { |
| 126 | + description: |
| 127 | + 'Enforce consistent style for element existence checks with `indexOf()`, `lastIndexOf()`, `findIndex()`, and `findLastIndex()`.', |
| 128 | + recommended: true, |
| 129 | + }, |
| 130 | + fixable: 'code', |
| 131 | + messages, |
| 132 | + }, |
| 133 | +}; |
0 commit comments