|
| 1 | +'use strict'; |
| 2 | +const {getFunctionHeadLocation, getFunctionNameWithKind} = require('eslint-utils'); |
| 3 | +const {not} = require('./selectors/index.js'); |
| 4 | + |
| 5 | +const MESSAGE_ID = 'prefer-native-coercion-functions'; |
| 6 | +const messages = { |
| 7 | + [MESSAGE_ID]: '{{functionNameWithKind}} is equivalent to `{{replacementFunction}}`. Use `{{replacementFunction}}` directly.', |
| 8 | +}; |
| 9 | + |
| 10 | +const nativeCoercionFunctionNames = new Set(['String', 'Number', 'BigInt', 'Boolean', 'Symbol']); |
| 11 | +const arrayMethodsWithBooleanCallback = new Set(['every', 'filter', 'find', 'findIndex', 'some']); |
| 12 | + |
| 13 | +const isNativeCoercionFunctionCall = (node, firstArgumentName) => |
| 14 | + node |
| 15 | + && node.type === 'CallExpression' |
| 16 | + && !node.optional |
| 17 | + && node.callee.type === 'Identifier' |
| 18 | + && nativeCoercionFunctionNames.has(node.callee.name) |
| 19 | + && node.arguments[0] |
| 20 | + && node.arguments[0].type === 'Identifier' |
| 21 | + && node.arguments[0].name === firstArgumentName; |
| 22 | + |
| 23 | +const isIdentityFunction = node => |
| 24 | + ( |
| 25 | + // `v => v` |
| 26 | + node.type === 'ArrowFunctionExpression' |
| 27 | + && node.body.type === 'Identifier' |
| 28 | + && node.body.name === node.params[0].name |
| 29 | + ) |
| 30 | + || ( |
| 31 | + // `(v) => {return v;}` |
| 32 | + // `function (v) {return v;}` |
| 33 | + node.body.type === 'BlockStatement' |
| 34 | + && node.body.body.length === 1 |
| 35 | + && node.body.body[0].type === 'ReturnStatement' |
| 36 | + && node.body.body[0].argument |
| 37 | + && node.body.body[0].argument.type === 'Identifier' |
| 38 | + && node.body.body[0].argument.name === node.params[0].name |
| 39 | + ); |
| 40 | + |
| 41 | +const isArrayIdentityCallback = node => |
| 42 | + isIdentityFunction(node) |
| 43 | + && node.parent.type === 'CallExpression' |
| 44 | + && !node.parent.optional |
| 45 | + && node.parent.arguments[0] === node |
| 46 | + && node.parent.callee.type === 'MemberExpression' |
| 47 | + && !node.parent.callee.computed |
| 48 | + && !node.parent.callee.optional |
| 49 | + && node.parent.callee.property.type === 'Identifier' |
| 50 | + && arrayMethodsWithBooleanCallback.has(node.parent.callee.property.name); |
| 51 | + |
| 52 | +function getCallExpression(node) { |
| 53 | + const firstParameterName = node.params[0].name; |
| 54 | + |
| 55 | + // `(v) => String(v)` |
| 56 | + if ( |
| 57 | + node.type === 'ArrowFunctionExpression' |
| 58 | + && isNativeCoercionFunctionCall(node.body, firstParameterName) |
| 59 | + ) { |
| 60 | + return node.body; |
| 61 | + } |
| 62 | + |
| 63 | + // `(v) => {return String(v);}` |
| 64 | + // `function (v) {return String(v);}` |
| 65 | + if ( |
| 66 | + node.body.type === 'BlockStatement' |
| 67 | + && node.body.body.length === 1 |
| 68 | + && node.body.body[0].type === 'ReturnStatement' |
| 69 | + && isNativeCoercionFunctionCall(node.body.body[0].argument, firstParameterName) |
| 70 | + ) { |
| 71 | + return node.body.body[0].argument; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const functionsSelector = [ |
| 76 | + ':function', |
| 77 | + '[async!=true]', |
| 78 | + '[generator!=true]', |
| 79 | + '[params.length>0]', |
| 80 | + '[params.0.type="Identifier"]', |
| 81 | + not([ |
| 82 | + 'MethodDefinition[kind="constructor"] > .value', |
| 83 | + 'MethodDefinition[kind="set"] > .value', |
| 84 | + 'Property[kind="set"] > .value', |
| 85 | + ]), |
| 86 | +].join(''); |
| 87 | + |
| 88 | +function getArrayCallbackProblem(node) { |
| 89 | + if (!isArrayIdentityCallback(node)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + replacementFunction: 'Boolean', |
| 95 | + fix: fixer => fixer.replaceText(node, 'Boolean'), |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +function getCoercionFunctionProblem(node) { |
| 100 | + const callExpression = getCallExpression(node); |
| 101 | + |
| 102 | + if (!callExpression) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const {name} = callExpression.callee; |
| 107 | + |
| 108 | + const problem = {replacementFunction: name}; |
| 109 | + |
| 110 | + if (node.type === 'FunctionDeclaration' || callExpression.arguments.length !== 1) { |
| 111 | + return problem; |
| 112 | + } |
| 113 | + |
| 114 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 115 | + problem.fix = fixer => { |
| 116 | + let text = name; |
| 117 | + |
| 118 | + if ( |
| 119 | + node.parent.type === 'Property' |
| 120 | + && node.parent.method |
| 121 | + && node.parent.value === node |
| 122 | + ) { |
| 123 | + text = `: ${text}`; |
| 124 | + } else if (node.parent.type === 'MethodDefinition') { |
| 125 | + text = ` = ${text};`; |
| 126 | + } |
| 127 | + |
| 128 | + return fixer.replaceText(node, text); |
| 129 | + }; |
| 130 | + |
| 131 | + return problem; |
| 132 | +} |
| 133 | + |
| 134 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 135 | +const create = context => ({ |
| 136 | + [functionsSelector](node) { |
| 137 | + let problem = getArrayCallbackProblem(node) || getCoercionFunctionProblem(node); |
| 138 | + |
| 139 | + if (!problem) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + const sourceCode = context.getSourceCode(); |
| 144 | + const {replacementFunction, fix} = problem; |
| 145 | + |
| 146 | + problem = { |
| 147 | + node, |
| 148 | + loc: getFunctionHeadLocation(node, sourceCode), |
| 149 | + messageId: MESSAGE_ID, |
| 150 | + data: { |
| 151 | + functionNameWithKind: getFunctionNameWithKind(node, sourceCode), |
| 152 | + replacementFunction, |
| 153 | + }, |
| 154 | + }; |
| 155 | + |
| 156 | + /* |
| 157 | + We do not fix if there are: |
| 158 | + - Comments: No proper place to put them. |
| 159 | + - Extra parameters: Removing them may break types. |
| 160 | + */ |
| 161 | + if (!fix || node.params.length !== 1 || sourceCode.getCommentsInside(node).length > 0) { |
| 162 | + return problem; |
| 163 | + } |
| 164 | + |
| 165 | + problem.fix = fix; |
| 166 | + |
| 167 | + return problem; |
| 168 | + }, |
| 169 | +}); |
| 170 | + |
| 171 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 172 | +module.exports = { |
| 173 | + create, |
| 174 | + meta: { |
| 175 | + type: 'suggestion', |
| 176 | + docs: { |
| 177 | + description: 'Prefer using `String`, `Number`, `BigInt`, `Boolean`, and `Symbol` directly.', |
| 178 | + }, |
| 179 | + fixable: 'code', |
| 180 | + messages, |
| 181 | + }, |
| 182 | +}; |
0 commit comments