123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 'use strict';
- const {isParenthesized} = require('@eslint-community/eslint-utils');
- const {methodCallSelector, notFunctionSelector} = require('./selectors/index.js');
- const {isNodeMatches} = require('./utils/is-node-matches.js');
- const ERROR_WITH_NAME_MESSAGE_ID = 'error-with-name';
- const ERROR_WITHOUT_NAME_MESSAGE_ID = 'error-without-name';
- const REPLACE_WITH_NAME_MESSAGE_ID = 'replace-with-name';
- const REPLACE_WITHOUT_NAME_MESSAGE_ID = 'replace-without-name';
- const messages = {
- [ERROR_WITH_NAME_MESSAGE_ID]: 'Do not pass function `{{name}}` directly to `.{{method}}(…)`.',
- [ERROR_WITHOUT_NAME_MESSAGE_ID]: 'Do not pass function directly to `.{{method}}(…)`.',
- [REPLACE_WITH_NAME_MESSAGE_ID]: 'Replace function `{{name}}` with `… => {{name}}({{parameters}})`.',
- [REPLACE_WITHOUT_NAME_MESSAGE_ID]: 'Replace function with `… => …({{parameters}})`.',
- };
- const iteratorMethods = [
- [
- 'every',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'filter', {
- extraSelector: '[callee.object.name!="Vue"]',
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'find',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'findLast',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'findIndex',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'findLastIndex',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- [
- 'flatMap',
- ],
- [
- 'forEach',
- {
- returnsUndefined: true,
- },
- ],
- [
- 'map',
- {
- extraSelector: '[callee.object.name!="types"]',
- ignore: [
- 'String',
- 'Number',
- 'BigInt',
- 'Boolean',
- 'Symbol',
- ],
- },
- ],
- [
- 'reduce',
- {
- parameters: [
- 'accumulator',
- 'element',
- 'index',
- 'array',
- ],
- minParameters: 2,
- },
- ],
- [
- 'reduceRight',
- {
- parameters: [
- 'accumulator',
- 'element',
- 'index',
- 'array',
- ],
- minParameters: 2,
- },
- ],
- [
- 'some',
- {
- ignore: [
- 'Boolean',
- ],
- },
- ],
- ].map(([method, options]) => {
- options = {
- parameters: ['element', 'index', 'array'],
- ignore: [],
- minParameters: 1,
- extraSelector: '',
- returnsUndefined: false,
- ...options,
- };
- return [method, options];
- });
- const ignoredCallee = [
- // http://bluebirdjs.com/docs/api/promise.map.html
- 'Promise',
- 'React.Children',
- 'Children',
- 'lodash',
- 'underscore',
- '_',
- 'Async',
- 'async',
- 'this',
- '$',
- 'jQuery',
- ];
- function getProblem(context, node, method, options) {
- const {type} = node;
- const name = type === 'Identifier' ? node.name : '';
- if (type === 'Identifier' && options.ignore.includes(name)) {
- return;
- }
- const problem = {
- node,
- messageId: name ? ERROR_WITH_NAME_MESSAGE_ID : ERROR_WITHOUT_NAME_MESSAGE_ID,
- data: {
- name,
- method,
- },
- suggest: [],
- };
- const {parameters, minParameters, returnsUndefined} = options;
- for (let parameterLength = minParameters; parameterLength <= parameters.length; parameterLength++) {
- const suggestionParameters = parameters.slice(0, parameterLength).join(', ');
- const suggest = {
- messageId: name ? REPLACE_WITH_NAME_MESSAGE_ID : REPLACE_WITHOUT_NAME_MESSAGE_ID,
- data: {
- name,
- parameters: suggestionParameters,
- },
- fix(fixer) {
- const sourceCode = context.getSourceCode();
- let nodeText = sourceCode.getText(node);
- if (isParenthesized(node, sourceCode) || type === 'ConditionalExpression') {
- nodeText = `(${nodeText})`;
- }
- return fixer.replaceText(
- node,
- returnsUndefined
- ? `(${suggestionParameters}) => { ${nodeText}(${suggestionParameters}); }`
- : `(${suggestionParameters}) => ${nodeText}(${suggestionParameters})`,
- );
- },
- };
- problem.suggest.push(suggest);
- }
- return problem;
- }
- const ignoredFirstArgumentSelector = [
- notFunctionSelector('arguments.0'),
- // Ignore all `CallExpression`s include `function.bind()`
- '[arguments.0.type!="CallExpression"]',
- '[arguments.0.type!="FunctionExpression"]',
- '[arguments.0.type!="ArrowFunctionExpression"]',
- ].join('');
- /** @param {import('eslint').Rule.RuleContext} context */
- const create = context => {
- const rules = {};
- for (const [method, options] of iteratorMethods) {
- const selector = [
- method === 'reduce' || method === 'reduceRight' ? '' : ':not(AwaitExpression) > ',
- methodCallSelector({
- method,
- minimumArguments: 1,
- maximumArguments: 2,
- }),
- options.extraSelector,
- ignoredFirstArgumentSelector,
- ].join('');
- rules[selector] = node => {
- if (isNodeMatches(node.callee.object, ignoredCallee)) {
- return;
- }
- if (node.callee.object.type === 'CallExpression' && isNodeMatches(node.callee.object.callee, ignoredCallee)) {
- return;
- }
- const [iterator] = node.arguments;
- return getProblem(context, iterator, method, options);
- };
- }
- return rules;
- };
- /** @type {import('eslint').Rule.RuleModule} */
- module.exports = {
- create,
- meta: {
- type: 'problem',
- docs: {
- description: 'Prevent passing a function reference directly to iterator methods.',
- },
- hasSuggestions: true,
- messages,
- },
- };
|