From ca162fdc5dc37f9f3447640a5a14a91daf73ea47 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 20 Dec 2023 12:21:55 -0800 Subject: [PATCH] [Refactor] better use of `.reverse`, etc --- lib/rules/jsx-no-bind.js | 4 ++-- lib/util/Components.js | 3 +-- lib/util/makeNoMethodSetStateRule.js | 7 +++++-- lib/util/variable.js | 5 +++-- package.json | 2 ++ 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/rules/jsx-no-bind.js b/lib/rules/jsx-no-bind.js index 0a4c38e61a..17e56e2e04 100644 --- a/lib/rules/jsx-no-bind.js +++ b/lib/rules/jsx-no-bind.js @@ -123,9 +123,9 @@ module.exports = { } function getBlockStatementAncestors(node) { - return context.getAncestors(node).reverse().filter( + return context.getAncestors(node).filter( (ancestor) => ancestor.type === 'BlockStatement' - ); + ).reverse(); } function reportVariableViolation(node, name, blockStart) { diff --git a/lib/util/Components.js b/lib/util/Components.js index 99920b8a18..a31989702f 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -671,8 +671,7 @@ function componentRule(rule, context) { } // Try to find the component using variable references - const refs = variableInScope.references; - refs.some((ref) => { + variableInScope.references.some((ref) => { let refId = ref.identifier; if (refId.parent && refId.parent.type === 'MemberExpression') { refId = refId.parent; diff --git a/lib/util/makeNoMethodSetStateRule.js b/lib/util/makeNoMethodSetStateRule.js index 20049cb484..1225092802 100644 --- a/lib/util/makeNoMethodSetStateRule.js +++ b/lib/util/makeNoMethodSetStateRule.js @@ -5,6 +5,8 @@ 'use strict'; +const findLast = require('array.prototype.findlast'); + const docsUrl = require('./docsUrl'); const report = require('./report'); const testReactVersion = require('./version').testReactVersion; @@ -91,9 +93,10 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe ) { return; } - const ancestors = context.getAncestors(callee).reverse(); + const ancestors = context.getAncestors(callee); let depth = 0; - ancestors.some((ancestor) => { + findLast(ancestors, (ancestor) => { + // ancestors.some((ancestor) => { if (/Function(Expression|Declaration)$/.test(ancestor.type)) { depth += 1; } diff --git a/lib/util/variable.js b/lib/util/variable.js index 4375efd69f..a93cf18b5e 100644 --- a/lib/util/variable.js +++ b/lib/util/variable.js @@ -5,6 +5,8 @@ 'use strict'; +const toReversed = require('array.prototype.toreversed'); + /** * Search a particular variable in a list * @param {Array} variables The variables list. @@ -47,9 +49,8 @@ function variablesInScope(context) { variables = scope.childScopes[0].childScopes[0].variables.concat(variables); } } - variables.reverse(); - return variables; + return toReversed(variables); } /** diff --git a/package.json b/package.json index 48384bdced..febbcc6a0e 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,9 @@ "bugs": "https://github.com/jsx-eslint/eslint-plugin-react/issues", "dependencies": { "array-includes": "^3.1.7", + "array.prototype.findlast": "^1.2.3", "array.prototype.flatmap": "^1.3.2", + "array.prototype.toreversed": "^1.1.2", "array.prototype.tosorted": "^1.1.2", "doctrine": "^2.1.0", "es-iterator-helpers": "^1.0.15",