|
| 1 | +import {isMethodCall} from '../ast/index.js'; |
| 2 | +import {getParenthesizedText} from '../utils/index.js'; |
| 3 | + |
| 4 | +const schema = [ |
| 5 | + { |
| 6 | + type: 'object', |
| 7 | + additionalProperties: false, |
| 8 | + properties: { |
| 9 | + allowExpressionStatement: { |
| 10 | + type: 'boolean', |
| 11 | + }, |
| 12 | + }, |
| 13 | + }, |
| 14 | +]; |
| 15 | + |
| 16 | +const MESSAGE_ID_ERROR = 'error'; |
| 17 | +const MESSAGE_ID_SUGGESTION_APPLY_REPLACEMENT = 'suggestion-apply-replacement'; |
| 18 | +const MESSAGE_ID_SUGGESTION_SPREADING_ARRAY = 'suggestion-spreading-array'; |
| 19 | +const MESSAGE_ID_SUGGESTION_NOT_SPREADING_ARRAY = 'suggestion-not-spreading-array'; |
| 20 | + |
| 21 | +const methods = new Map([ |
| 22 | + [ |
| 23 | + 'reverse', |
| 24 | + { |
| 25 | + replacement: 'toReversed', |
| 26 | + predicate: callExpression => isMethodCall(callExpression, { |
| 27 | + method: 'reverse', |
| 28 | + argumentsLength: 0, |
| 29 | + optionalCall: false, |
| 30 | + }), |
| 31 | + }, |
| 32 | + ], |
| 33 | +]); |
| 34 | + |
| 35 | +function noArrayMutateRule(methodName) { |
| 36 | + const { |
| 37 | + replacement, |
| 38 | + predicate, |
| 39 | + } = methods.get(methodName); |
| 40 | + |
| 41 | + const messages = { |
| 42 | + [MESSAGE_ID_ERROR]: `Use \`Array#${replacement}()\` instead of \`Array#${methodName}()\`.`, |
| 43 | + [MESSAGE_ID_SUGGESTION_APPLY_REPLACEMENT]: `Switch to \`.${replacement}()\`.`, |
| 44 | + [MESSAGE_ID_SUGGESTION_SPREADING_ARRAY]: 'The spreading object is an array', |
| 45 | + [MESSAGE_ID_SUGGESTION_NOT_SPREADING_ARRAY]: 'The spreading object is NOT an array', |
| 46 | + }; |
| 47 | + |
| 48 | + /** @param {import('eslint').Rule.RuleContext} context */ |
| 49 | + const create = context => { |
| 50 | + const {sourceCode} = context; |
| 51 | + const {allowExpressionStatement} = context.options[0]; |
| 52 | + |
| 53 | + return { |
| 54 | + CallExpression(callExpression) { |
| 55 | + if (!predicate(callExpression)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const array = callExpression.callee.object; |
| 60 | + |
| 61 | + // `[...array].reverse()` |
| 62 | + const isSpreadAndMutate = array.type === 'ArrayExpression' |
| 63 | + && array.elements.length === 1 |
| 64 | + && array.elements[0].type === 'SpreadElement'; |
| 65 | + |
| 66 | + if (allowExpressionStatement && !isSpreadAndMutate) { |
| 67 | + const maybeExpressionStatement = callExpression.parent.type === 'ChainExpression' |
| 68 | + ? callExpression.parent.parent |
| 69 | + : callExpression.parent; |
| 70 | + if (maybeExpressionStatement.type === 'ExpressionStatement') { |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const methodProperty = callExpression.callee.property; |
| 76 | + const suggestions = []; |
| 77 | + const fixMethodName = fixer => fixer.replaceText(methodProperty, replacement); |
| 78 | + |
| 79 | + /* |
| 80 | + For `[...array].reverse()`, provide two suggestions, let user choose if the object can be unwrapped, |
| 81 | + otherwise only change `.reverse()` to `.toReversed()` |
| 82 | + */ |
| 83 | + if (isSpreadAndMutate) { |
| 84 | + suggestions.push({ |
| 85 | + messageId: MESSAGE_ID_SUGGESTION_SPREADING_ARRAY, |
| 86 | + * fix(fixer) { |
| 87 | + const text = getParenthesizedText(array.elements[0].argument, sourceCode); |
| 88 | + yield fixer.replaceText(array, text); |
| 89 | + yield fixMethodName(fixer); |
| 90 | + }, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + suggestions.push({ |
| 95 | + messageId: isSpreadAndMutate |
| 96 | + ? MESSAGE_ID_SUGGESTION_NOT_SPREADING_ARRAY |
| 97 | + : MESSAGE_ID_SUGGESTION_APPLY_REPLACEMENT, |
| 98 | + fix: fixMethodName, |
| 99 | + }); |
| 100 | + |
| 101 | + return { |
| 102 | + node: methodProperty, |
| 103 | + messageId: MESSAGE_ID_ERROR, |
| 104 | + suggest: suggestions, |
| 105 | + }; |
| 106 | + }, |
| 107 | + }; |
| 108 | + }; |
| 109 | + |
| 110 | + /** @type {import('eslint').Rule.RuleModule} */ |
| 111 | + const config = { |
| 112 | + create, |
| 113 | + meta: { |
| 114 | + type: 'suggestion', |
| 115 | + docs: { |
| 116 | + description: `Prefer \`Array#${replacement}()\` over \`Array#${methodName}()\`.`, |
| 117 | + recommended: true, |
| 118 | + }, |
| 119 | + hasSuggestions: true, |
| 120 | + schema, |
| 121 | + defaultOptions: [{allowExpressionStatement: true}], |
| 122 | + messages, |
| 123 | + }, |
| 124 | + }; |
| 125 | + |
| 126 | + return config; |
| 127 | +} |
| 128 | + |
| 129 | +export default noArrayMutateRule; |
0 commit comments