From 60f6fccb448d6134236991670df77ee86479fc76 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Tue, 13 Jul 2021 11:11:33 -0500 Subject: [PATCH 01/15] Adds `allowNumericInitialValue` option to `no-array-reduce`. --- docs/rules/no-array-reduce.md | 18 ++++++++++++++ rules/no-array-reduce.js | 22 ++++++++++++++++- test/no-array-reduce.mjs | 45 +++++++++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index a1bd5143fa..6bfafa2e8d 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -46,3 +46,21 @@ for (const element of array) { result += element; } ``` +## Options + +### allowNumericInitialValue + +Type: `boolean`\ +Default: `false` + +Can be enabled to allow `reduce` to be used to sum numbers. + +```js +// eslint unicorn/no-array-reduce: ["error", {"allowNumericInitialValue": true}] +arr.reduce((total, item) => total + item, 0) // Passes +``` + +```js +// eslint unicorn/no-array-reduce: ["error", {"allowNumericInitialValue": false}] +arr.reduce((total, item) => total + item, 0) // Fails +``` diff --git a/rules/no-array-reduce.js b/rules/no-array-reduce.js index 5c8576750e..5d9599d721 100644 --- a/rules/no-array-reduce.js +++ b/rules/no-array-reduce.js @@ -1,6 +1,7 @@ 'use strict'; const {methodCallSelector} = require('./selectors/index.js'); const {arrayPrototypeMethodSelector, notFunctionSelector, matches} = require('./selectors/index.js'); +const {isNumeric} = require('./utils/numeric.js'); const MESSAGE_ID = 'no-reduce'; const messages = { @@ -34,9 +35,27 @@ const selector = matches([ ].join(''), ]); -const create = () => { +const schema = [ + { + type: 'object', + properties: { + allowNumericInitialValue: { + type: 'boolean', + default: false, + }, + }, + }, +]; + +const create = context => { + const {allowNumericInitialValue} = context.options[0] || {}; + return { [selector](node) { + if (allowNumericInitialValue && isNumeric(node.parent.parent.arguments[1])) { + return; + } + return { node, messageId: MESSAGE_ID, @@ -53,6 +72,7 @@ module.exports = { docs: { description: 'Disallow `Array#reduce()` and `Array#reduceRight()`.', }, + schema, messages, }, }; diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 6386ac3335..a11683ca8c 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -93,7 +93,24 @@ test({ // Second argument is not a function ...notFunctionTypes.map(data => `Array.prototype.reduce.call(foo, ${data})`), - ].flatMap(code => [code, code.replace('reduce', 'reduceRight')]), + // Option: allowNumericInitialValue + { + code: 'arr.reduce((total, item) => total + item, 0)', + options: [{allowNumericInitialValue: true}], + }, + { + code: 'arr.reduce(function (total, item) { return total + item }, 0)', + options: [{allowNumericInitialValue: true}], + }, + ].flatMap(testCase => { + const {code, options} = testCase; + + if (options) { + return [testCase, {...testCase, code: code.replace('reduce', 'reduceRight')}]; + } + + return [testCase, testCase.replace('reduce', 'reduceRight')]; + }), invalid: [ 'arr.reduce((total, item) => total + item)', 'arr.reduce((total, item) => total + item, 0)', @@ -120,5 +137,29 @@ test({ '[].reduce.apply(arr, [sum]);', 'Array.prototype.reduce.apply(arr, [(s, i) => s + i])', 'Array.prototype.reduce.apply(arr, [sum]);', - ].flatMap(code => [{code, errors: errorsReduce}, {code: code.replace('reduce', 'reduceRight'), errors: errorsReduceRight}]), + + // Option: allowNumericInitialValue + { + code: 'arr.reduce((total, item) => total + item, 0)', + options: [{allowNumericInitialValue: false}], + }, + { + code: 'arr.reduce(function (total, item) { return total + item }, 0)', + options: [{allowNumericInitialValue: false}], + }, + ].flatMap(testCase => { + const {code, options} = testCase; + + if (options) { + return [ + {code, errors: errorsReduce, options}, + {code: code.replace('reduce', 'reduceRight'), errors: errorsReduceRight, options}, + ]; + } + + return [ + {code: testCase, errors: errorsReduce}, + {code: testCase.replace('reduce', 'reduceRight'), errors: errorsReduceRight}, + ]; + }), }); From bafc94b40c1f0595781cf76da9dfd8fe5bed5a6c Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Tue, 13 Jul 2021 13:05:23 -0500 Subject: [PATCH 02/15] Simplifies if statement. --- rules/no-array-reduce.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/rules/no-array-reduce.js b/rules/no-array-reduce.js index 5d9599d721..98b290b40b 100644 --- a/rules/no-array-reduce.js +++ b/rules/no-array-reduce.js @@ -52,15 +52,13 @@ const create = context => { return { [selector](node) { - if (allowNumericInitialValue && isNumeric(node.parent.parent.arguments[1])) { - return; + if (!(allowNumericInitialValue && isNumeric(node.parent.parent.arguments[1]))) { + return { + node, + messageId: MESSAGE_ID, + data: {method: node.name}, + }; } - - return { - node, - messageId: MESSAGE_ID, - data: {method: node.name}, - }; }, }; }; From 3dc208bceee484b6290437cc10e0254f78e8878d Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Tue, 13 Jul 2021 17:49:21 -0500 Subject: [PATCH 03/15] Defaults `allowNumericInitialValue` to `true`. --- docs/rules/no-array-reduce.md | 14 +++++++++++--- rules/no-array-reduce.js | 9 ++++++--- rules/utils/numeric.js | 2 +- test/no-array-reduce.mjs | 22 +++------------------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index 6bfafa2e8d..1467ce5a3f 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -1,6 +1,8 @@ # Disallow `Array#reduce()` and `Array#reduceRight()` -`Array#reduce()` and `Array#reduceRight()` usually result in [hard-to-read](https://twitter.com/jaffathecake/status/1213077702300852224) and [less performant](https://www.richsnapp.com/article/2019/06-09-reduce-spread-anti-pattern) code. In almost every case, it can be replaced by `.map`, `.filter`, or a `for-of` loop. It's only somewhat useful in the rare case of summing up numbers. +`Array#reduce()` and `Array#reduceRight()` usually result in [hard-to-read](https://twitter.com/jaffathecake/status/1213077702300852224) and [less performant](https://www.richsnapp.com/article/2019/06-09-reduce-spread-anti-pattern) code. In almost every case, it can be replaced by `.map`, `.filter`, or a `for-of` loop. + +It's only somewhat useful in the rare case of summing up numbers, which is allowed by default. Use `eslint-disable` comment if you really need to use it. @@ -39,6 +41,10 @@ Array.prototype.reduce.call(array, reducer); array.reduce(reducer, initialValue); ``` +```js +array.reduce((total, value) => total + value, 0); +``` + ```js let result = initialValue; @@ -51,9 +57,11 @@ for (const element of array) { ### allowNumericInitialValue Type: `boolean`\ -Default: `false` +Default: `true` + +Allow a numeric `initialValue` in a `reduce` call. -Can be enabled to allow `reduce` to be used to sum numbers. +Pass `"allowNumericInitialValue": false` to disable reduce completely. ```js // eslint unicorn/no-array-reduce: ["error", {"allowNumericInitialValue": true}] diff --git a/rules/no-array-reduce.js b/rules/no-array-reduce.js index 98b290b40b..048bea21dd 100644 --- a/rules/no-array-reduce.js +++ b/rules/no-array-reduce.js @@ -1,4 +1,5 @@ 'use strict'; +const {get} = require('lodash'); const {methodCallSelector} = require('./selectors/index.js'); const {arrayPrototypeMethodSelector, notFunctionSelector, matches} = require('./selectors/index.js'); const {isNumeric} = require('./utils/numeric.js'); @@ -41,18 +42,20 @@ const schema = [ properties: { allowNumericInitialValue: { type: 'boolean', - default: false, + default: true, }, }, }, ]; const create = context => { - const {allowNumericInitialValue} = context.options[0] || {}; + const {allowNumericInitialValue} = {allowNumericInitialValue: true, ...context.options[0]}; return { [selector](node) { - if (!(allowNumericInitialValue && isNumeric(node.parent.parent.arguments[1]))) { + const initialValue = get(node, 'parent.parent.arguments[1]'); + + if (!(allowNumericInitialValue && isNumeric(initialValue))) { return { node, messageId: MESSAGE_ID, diff --git a/rules/utils/numeric.js b/rules/utils/numeric.js index 24b922c822..9120d22a3d 100644 --- a/rules/utils/numeric.js +++ b/rules/utils/numeric.js @@ -8,7 +8,7 @@ const isDecimalIntegerNode = node => isNumber(node) && isDecimalInteger(node.raw const isNumber = node => typeof node.value === 'number'; const isBigInt = node => Boolean(node.bigint); -const isNumeric = node => isNumber(node) || isBigInt(node); +const isNumeric = node => node ? isNumber(node) || isBigInt(node) : false; const isLegacyOctal = node => isNumber(node) && /^0\d+$/.test(node.raw); function getPrefix(text) { diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index a11683ca8c..922e0e8d6b 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -94,27 +94,11 @@ test({ ...notFunctionTypes.map(data => `Array.prototype.reduce.call(foo, ${data})`), // Option: allowNumericInitialValue - { - code: 'arr.reduce((total, item) => total + item, 0)', - options: [{allowNumericInitialValue: true}], - }, - { - code: 'arr.reduce(function (total, item) { return total + item }, 0)', - options: [{allowNumericInitialValue: true}], - }, - ].flatMap(testCase => { - const {code, options} = testCase; - - if (options) { - return [testCase, {...testCase, code: code.replace('reduce', 'reduceRight')}]; - } - - return [testCase, testCase.replace('reduce', 'reduceRight')]; - }), - invalid: [ - 'arr.reduce((total, item) => total + item)', 'arr.reduce((total, item) => total + item, 0)', 'arr.reduce(function (total, item) { return total + item }, 0)', + ].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]), + invalid: [ + 'arr.reduce((total, item) => total + item)', 'arr.reduce(function (total, item) { return total + item })', 'arr.reduce((str, item) => str += item, "")', outdent` From 292480e552ae2b4fdec80db7a18d9a1d61903258 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Mon, 19 Jul 2021 09:21:01 -0500 Subject: [PATCH 04/15] Adds the ability to do simple operations. --- docs/rules/no-array-reduce.md | 14 +++++++------- rules/no-array-reduce.js | 36 ++++++++++++++++++++++++++--------- test/no-array-reduce.mjs | 30 +++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index 1467ce5a3f..753cb6593d 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -54,21 +54,21 @@ for (const element of array) { ``` ## Options -### allowNumericInitialValue +### allowSimpleOperations Type: `boolean`\ Default: `true` -Allow a numeric `initialValue` in a `reduce` call. +Allow simple operations (like addition, subtraction, etc.) in a `reduce` call. -Pass `"allowNumericInitialValue": false` to disable reduce completely. +Pass `"allowSimpleOperations": false` to disable reduce completely. ```js -// eslint unicorn/no-array-reduce: ["error", {"allowNumericInitialValue": true}] -arr.reduce((total, item) => total + item, 0) // Passes +// eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": true}] +arr.reduce((total, item) => total + item) // Passes ``` ```js -// eslint unicorn/no-array-reduce: ["error", {"allowNumericInitialValue": false}] -arr.reduce((total, item) => total + item, 0) // Fails +// eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": false}] +arr.reduce((total, item) => total + item) // Fails ``` diff --git a/rules/no-array-reduce.js b/rules/no-array-reduce.js index 048bea21dd..149eb1740e 100644 --- a/rules/no-array-reduce.js +++ b/rules/no-array-reduce.js @@ -40,7 +40,7 @@ const schema = [ { type: 'object', properties: { - allowNumericInitialValue: { + allowSimpleOperations: { type: 'boolean', default: true, }, @@ -49,19 +49,37 @@ const schema = [ ]; const create = context => { - const {allowNumericInitialValue} = {allowNumericInitialValue: true, ...context.options[0]}; + const {allowSimpleOperations} = {allowSimpleOperations: true, ...context.options[0]}; return { [selector](node) { - const initialValue = get(node, 'parent.parent.arguments[1]'); + const callback = get(node, 'parent.parent.arguments[0]', {}); + const problem = { + node, + messageId: MESSAGE_ID, + data: {method: node.name}, + }; - if (!(allowNumericInitialValue && isNumeric(initialValue))) { - return { - node, - messageId: MESSAGE_ID, - data: {method: node.name}, - }; + if (!allowSimpleOperations) { + return problem; } + + if (callback.type === 'ArrowFunctionExpression' && callback.body.type === 'BinaryExpression') { + return; + } + + if ((callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression') && + callback.body.type === 'BlockStatement' && + callback.body.body[0].type === 'ReturnStatement' && + callback.body.body[0].argument.type === 'BinaryExpression') { + return; + } + + if (isNumeric(get(node, 'parent.parent.arguments[1]'))) { + return; + } + + return problem; }, }; }; diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 922e0e8d6b..2e28f6052f 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -93,13 +93,15 @@ test({ // Second argument is not a function ...notFunctionTypes.map(data => `Array.prototype.reduce.call(foo, ${data})`), - // Option: allowNumericInitialValue + // Option: allowSimpleOperations + 'arr.reduce((total, item) => total + item)', + 'arr.reduce((total, item) => { return total + item })', + 'arr.reduce(function (total, item) { return total + item })', 'arr.reduce((total, item) => total + item, 0)', + 'arr.reduce((total, item) => { return total + item }, 0 )', 'arr.reduce(function (total, item) { return total + item }, 0)', ].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]), invalid: [ - 'arr.reduce((total, item) => total + item)', - 'arr.reduce(function (total, item) { return total + item })', 'arr.reduce((str, item) => str += item, "")', outdent` arr.reduce((obj, item) => { @@ -122,14 +124,30 @@ test({ 'Array.prototype.reduce.apply(arr, [(s, i) => s + i])', 'Array.prototype.reduce.apply(arr, [sum]);', - // Option: allowNumericInitialValue + // Option: allowSimpleOperations + { + code: 'arr.reduce((total, item) => total + item)', + options: [{allowSimpleOperations: false}], + }, + { + code: 'arr.reduce((total, item) => { return total + item })', + options: [{allowSimpleOperations: false}], + }, + { + code: 'arr.reduce(function (total, item) { return total + item })', + options: [{allowSimpleOperations: false}], + }, { code: 'arr.reduce((total, item) => total + item, 0)', - options: [{allowNumericInitialValue: false}], + options: [{allowSimpleOperations: false}], + }, + { + code: 'arr.reduce((total, item) => { return total + item }, 0 )', + options: [{allowSimpleOperations: false}], }, { code: 'arr.reduce(function (total, item) { return total + item }, 0)', - options: [{allowNumericInitialValue: false}], + options: [{allowSimpleOperations: false}], }, ].flatMap(testCase => { const {code, options} = testCase; From 41b04abee934b2a9247e4be92b9e449621597ac7 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Mon, 19 Jul 2021 11:11:26 -0500 Subject: [PATCH 05/15] Adds test cases to improve coverage. --- test/no-array-reduce.mjs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 2e28f6052f..48baa4bdf0 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -95,11 +95,18 @@ test({ // Option: allowSimpleOperations 'arr.reduce((total, item) => total + item)', - 'arr.reduce((total, item) => { return total + item })', - 'arr.reduce(function (total, item) { return total + item })', + 'arr.reduce((total, item) => { return total - item })', + 'arr.reduce(function (total, item) { return total * item })', 'arr.reduce((total, item) => total + item, 0)', - 'arr.reduce((total, item) => { return total + item }, 0 )', - 'arr.reduce(function (total, item) { return total + item }, 0)', + 'arr.reduce((total, item) => { return total - item }, 0 )', + 'arr.reduce(function (total, item) { return total * item }, 0)', + outdent` + arr.reduce((total, item) => { + const multiplier = 100; + return (total / item) * multiplier; + }, 0) + `, + 'arr.reduce((total, item) => { return total + item }, 0)', ].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]), invalid: [ 'arr.reduce((str, item) => str += item, "")', @@ -130,11 +137,11 @@ test({ options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce((total, item) => { return total + item })', + code: 'arr.reduce((total, item) => { return total - item })', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce(function (total, item) { return total + item })', + code: 'arr.reduce(function (total, item) { return total * item })', options: [{allowSimpleOperations: false}], }, { @@ -142,11 +149,20 @@ test({ options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce((total, item) => { return total + item }, 0 )', + code: 'arr.reduce((total, item) => { return total - item }, 0 )', + options: [{allowSimpleOperations: false}], + }, + { + code: 'arr.reduce(function (total, item) { return total * item }, 0)', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce(function (total, item) { return total + item }, 0)', + code: outdent` + arr.reduce((total, item) => { + const multiplier = 100; + return (total / item) * multiplier; + }, 0) + `, options: [{allowSimpleOperations: false}], }, ].flatMap(testCase => { From 5aadd69ba3255174bf884da8a7da4b7d4230e937 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Fri, 30 Jul 2021 20:47:40 -0500 Subject: [PATCH 06/15] Removes numeric initial value and enforces simple, single line operations. --- docs/rules/no-array-reduce.md | 2 +- rules/no-array-reduce.js | 6 +----- test/no-array-reduce.mjs | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index 753cb6593d..6762fb9587 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -42,7 +42,7 @@ array.reduce(reducer, initialValue); ``` ```js -array.reduce((total, value) => total + value, 0); +array.reduce((total, value) => total + value; ``` ```js diff --git a/rules/no-array-reduce.js b/rules/no-array-reduce.js index 149eb1740e..4caf3463c7 100644 --- a/rules/no-array-reduce.js +++ b/rules/no-array-reduce.js @@ -2,7 +2,6 @@ const {get} = require('lodash'); const {methodCallSelector} = require('./selectors/index.js'); const {arrayPrototypeMethodSelector, notFunctionSelector, matches} = require('./selectors/index.js'); -const {isNumeric} = require('./utils/numeric.js'); const MESSAGE_ID = 'no-reduce'; const messages = { @@ -70,15 +69,12 @@ const create = context => { if ((callback.type === 'ArrowFunctionExpression' || callback.type === 'FunctionExpression') && callback.body.type === 'BlockStatement' && + callback.body.body.length === 1 && callback.body.body[0].type === 'ReturnStatement' && callback.body.body[0].argument.type === 'BinaryExpression') { return; } - if (isNumeric(get(node, 'parent.parent.arguments[1]'))) { - return; - } - return problem; }, }; diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 48baa4bdf0..0dd7849e7a 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -102,9 +102,8 @@ test({ 'arr.reduce(function (total, item) { return total * item }, 0)', outdent` arr.reduce((total, item) => { - const multiplier = 100; - return (total / item) * multiplier; - }, 0) + return (total / item) * 100; + }, 0); `, 'arr.reduce((total, item) => { return total + item }, 0)', ].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]), @@ -130,6 +129,15 @@ test({ '[].reduce.apply(arr, [sum]);', 'Array.prototype.reduce.apply(arr, [(s, i) => s + i])', 'Array.prototype.reduce.apply(arr, [sum]);', + outdent` + array.reduce((total, item) => { + const doComplicatedThings = (item) => { + return item + 1; + } + + return total + doComplicatedThings(item); + }, 0); + `, // Option: allowSimpleOperations { @@ -159,9 +167,8 @@ test({ { code: outdent` arr.reduce((total, item) => { - const multiplier = 100; - return (total / item) * multiplier; - }, 0) + return (total / item) * 100; + }, 0); `, options: [{allowSimpleOperations: false}], }, From dc5256db87033be99987d90841808eec909e303f Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 13:26:45 -0500 Subject: [PATCH 07/15] Updates option description. Co-authored-by: Sindre Sorhus --- docs/rules/no-array-reduce.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index 6762fb9587..a347949002 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -61,7 +61,7 @@ Default: `true` Allow simple operations (like addition, subtraction, etc.) in a `reduce` call. -Pass `"allowSimpleOperations": false` to disable reduce completely. +Set it to `false` to disable reduce completely. ```js // eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": true}] From 74fe1d7c89e6efb0b9669228079961b12040f24f Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 13:27:36 -0500 Subject: [PATCH 08/15] Updates option example to not use abbreviated words. Co-authored-by: Sindre Sorhus --- docs/rules/no-array-reduce.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index a347949002..ea7805ef82 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -65,7 +65,7 @@ Set it to `false` to disable reduce completely. ```js // eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": true}] -arr.reduce((total, item) => total + item) // Passes +array.reduce((total, item) => total + item) // Passes ``` ```js From 35a7342358baadbd0fa2493d525ba0b3f7f1eedc Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 13:27:43 -0500 Subject: [PATCH 09/15] Updates option example to not use abbreviated words. Co-authored-by: Sindre Sorhus --- docs/rules/no-array-reduce.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index ea7805ef82..5ce2f09f6d 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -70,5 +70,5 @@ array.reduce((total, item) => total + item) // Passes ```js // eslint unicorn/no-array-reduce: ["error", {"allowSimpleOperations": false}] -arr.reduce((total, item) => total + item) // Fails +array.reduce((total, item) => total + item) // Fails ``` From 52c0d27248f2bb857e77e867a2df2870efd484d6 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 13:28:19 -0500 Subject: [PATCH 10/15] Updates option test to not use abbreviated words. Co-authored-by: Sindre Sorhus --- test/no-array-reduce.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 0dd7849e7a..d21b74b0d8 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -94,7 +94,7 @@ test({ ...notFunctionTypes.map(data => `Array.prototype.reduce.call(foo, ${data})`), // Option: allowSimpleOperations - 'arr.reduce((total, item) => total + item)', + 'array.reduce((total, item) => total + item)', 'arr.reduce((total, item) => { return total - item })', 'arr.reduce(function (total, item) { return total * item })', 'arr.reduce((total, item) => total + item, 0)', From eb8b2260c5bf0fc0924d1ebce81ab50f1ce4e2bf Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 13:30:29 -0500 Subject: [PATCH 11/15] Fixes invalid code in documentation examples. --- docs/rules/no-array-reduce.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-array-reduce.md b/docs/rules/no-array-reduce.md index 5ce2f09f6d..8e96182da9 100644 --- a/docs/rules/no-array-reduce.md +++ b/docs/rules/no-array-reduce.md @@ -42,7 +42,7 @@ array.reduce(reducer, initialValue); ``` ```js -array.reduce((total, value) => total + value; +array.reduce((total, value) => total + value); ``` ```js From 960ed280fcc23a1aaac0cc8c4f1e65a416241fda Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 17:16:34 -0500 Subject: [PATCH 12/15] Adds parenthesis around ternary for readability. --- rules/utils/numeric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/utils/numeric.js b/rules/utils/numeric.js index 9120d22a3d..ea078bb634 100644 --- a/rules/utils/numeric.js +++ b/rules/utils/numeric.js @@ -8,7 +8,7 @@ const isDecimalIntegerNode = node => isNumber(node) && isDecimalInteger(node.raw const isNumber = node => typeof node.value === 'number'; const isBigInt = node => Boolean(node.bigint); -const isNumeric = node => node ? isNumber(node) || isBigInt(node) : false; +const isNumeric = node => node ? (isNumber(node) || isBigInt(node)) : false; const isLegacyOctal = node => isNumber(node) && /^0\d+$/.test(node.raw); function getPrefix(text) { From d0a00c0337e90fab916c275c436f570db44d3a08 Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sat, 31 Jul 2021 23:44:38 -0500 Subject: [PATCH 13/15] Updates all `no--array-reduce` tests to not use `arr` abbreviations. --- test/no-array-reduce.mjs | 62 ++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index d21b74b0d8..74233594d4 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -83,52 +83,52 @@ test({ // We are not checking arguments length // `reduce-like` - 'arr.reducex(foo)', - 'arr.xreduce(foo)', - '[].reducex.call(arr, foo)', - '[].xreduce.call(arr, foo)', - 'Array.prototype.reducex.call(arr, foo)', - 'Array.prototype.xreduce.call(arr, foo)', + 'array.reducex(foo)', + 'array.xreduce(foo)', + '[].reducex.call(array, foo)', + '[].xreduce.call(array, foo)', + 'Array.prototype.reducex.call(array, foo)', + 'Array.prototype.xreduce.call(array, foo)', // Second argument is not a function ...notFunctionTypes.map(data => `Array.prototype.reduce.call(foo, ${data})`), // Option: allowSimpleOperations 'array.reduce((total, item) => total + item)', - 'arr.reduce((total, item) => { return total - item })', - 'arr.reduce(function (total, item) { return total * item })', - 'arr.reduce((total, item) => total + item, 0)', - 'arr.reduce((total, item) => { return total - item }, 0 )', - 'arr.reduce(function (total, item) { return total * item }, 0)', + 'array.reduce((total, item) => { return total - item })', + 'array.reduce(function (total, item) { return total * item })', + 'array.reduce((total, item) => total + item, 0)', + 'array.reduce((total, item) => { return total - item }, 0 )', + 'array.reduce(function (total, item) { return total * item }, 0)', outdent` - arr.reduce((total, item) => { + array.reduce((total, item) => { return (total / item) * 100; }, 0); `, - 'arr.reduce((total, item) => { return total + item }, 0)', + 'array.reduce((total, item) => { return total + item }, 0)', ].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]), invalid: [ - 'arr.reduce((str, item) => str += item, "")', + 'array.reduce((str, item) => str += item, "")', outdent` - arr.reduce((obj, item) => { + array.reduce((obj, item) => { obj[item] = null; return obj; }, {}) `, - 'arr.reduce((obj, item) => ({ [item]: null }), {})', + 'array.reduce((obj, item) => ({ [item]: null }), {})', outdent` const hyphenate = (str, char) => \`\${str}-\${char}\`; ["a", "b", "c"].reduce(hyphenate); `, - '[].reduce.call(arr, (s, i) => s + i)', - '[].reduce.call(arr, sum);', + '[].reduce.call(array, (s, i) => s + i)', + '[].reduce.call(array, sum);', '[].reduce.call(sum);', - 'Array.prototype.reduce.call(arr, (s, i) => s + i)', - 'Array.prototype.reduce.call(arr, sum);', - '[].reduce.apply(arr, [(s, i) => s + i])', - '[].reduce.apply(arr, [sum]);', - 'Array.prototype.reduce.apply(arr, [(s, i) => s + i])', - 'Array.prototype.reduce.apply(arr, [sum]);', + 'Array.prototype.reduce.call(array, (s, i) => s + i)', + 'Array.prototype.reduce.call(array, sum);', + '[].reduce.apply(array, [(s, i) => s + i])', + '[].reduce.apply(array, [sum]);', + 'Array.prototype.reduce.apply(array, [(s, i) => s + i])', + 'Array.prototype.reduce.apply(array, [sum]);', outdent` array.reduce((total, item) => { const doComplicatedThings = (item) => { @@ -141,32 +141,32 @@ test({ // Option: allowSimpleOperations { - code: 'arr.reduce((total, item) => total + item)', + code: 'array.reduce((total, item) => total + item)', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce((total, item) => { return total - item })', + code: 'array.reduce((total, item) => { return total - item })', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce(function (total, item) { return total * item })', + code: 'array.reduce(function (total, item) { return total * item })', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce((total, item) => total + item, 0)', + code: 'array.reduce((total, item) => total + item, 0)', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce((total, item) => { return total - item }, 0 )', + code: 'array.reduce((total, item) => { return total - item }, 0 )', options: [{allowSimpleOperations: false}], }, { - code: 'arr.reduce(function (total, item) { return total * item }, 0)', + code: 'array.reduce(function (total, item) { return total * item }, 0)', options: [{allowSimpleOperations: false}], }, { code: outdent` - arr.reduce((total, item) => { + array.reduce((total, item) => { return (total / item) * 100; }, 0); `, From 0c8d15866d328ea442d5b4243b0a48e75cb6bb08 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 2 Aug 2021 09:56:46 +0800 Subject: [PATCH 14/15] Update no-array-reduce.mjs --- test/no-array-reduce.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/no-array-reduce.mjs b/test/no-array-reduce.mjs index 74233594d4..6b370522c0 100644 --- a/test/no-array-reduce.mjs +++ b/test/no-array-reduce.mjs @@ -131,11 +131,10 @@ test({ 'Array.prototype.reduce.apply(array, [sum]);', outdent` array.reduce((total, item) => { - const doComplicatedThings = (item) => { + return total + doComplicatedThings(item); + function doComplicatedThings(item) { return item + 1; } - - return total + doComplicatedThings(item); }, 0); `, From aade2cc1b45907f583c6f7859d6be14ee7eea869 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 2 Aug 2021 09:59:23 +0800 Subject: [PATCH 15/15] Restore unrelated change --- rules/utils/numeric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/utils/numeric.js b/rules/utils/numeric.js index ea078bb634..24b922c822 100644 --- a/rules/utils/numeric.js +++ b/rules/utils/numeric.js @@ -8,7 +8,7 @@ const isDecimalIntegerNode = node => isNumber(node) && isDecimalInteger(node.raw const isNumber = node => typeof node.value === 'number'; const isBigInt = node => Boolean(node.bigint); -const isNumeric = node => node ? (isNumber(node) || isBigInt(node)) : false; +const isNumeric = node => isNumber(node) || isBigInt(node); const isLegacyOctal = node => isNumber(node) && /^0\d+$/.test(node.raw); function getPrefix(text) {