Skip to content

Commit ca162fd

Browse files
committed
[Refactor] better use of .reverse, etc
1 parent b4b7497 commit ca162fd

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

lib/rules/jsx-no-bind.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ module.exports = {
123123
}
124124

125125
function getBlockStatementAncestors(node) {
126-
return context.getAncestors(node).reverse().filter(
126+
return context.getAncestors(node).filter(
127127
(ancestor) => ancestor.type === 'BlockStatement'
128-
);
128+
).reverse();
129129
}
130130

131131
function reportVariableViolation(node, name, blockStart) {

lib/util/Components.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ function componentRule(rule, context) {
671671
}
672672

673673
// Try to find the component using variable references
674-
const refs = variableInScope.references;
675-
refs.some((ref) => {
674+
variableInScope.references.some((ref) => {
676675
let refId = ref.identifier;
677676
if (refId.parent && refId.parent.type === 'MemberExpression') {
678677
refId = refId.parent;

lib/util/makeNoMethodSetStateRule.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const findLast = require('array.prototype.findlast');
9+
810
const docsUrl = require('./docsUrl');
911
const report = require('./report');
1012
const testReactVersion = require('./version').testReactVersion;
@@ -91,9 +93,10 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe
9193
) {
9294
return;
9395
}
94-
const ancestors = context.getAncestors(callee).reverse();
96+
const ancestors = context.getAncestors(callee);
9597
let depth = 0;
96-
ancestors.some((ancestor) => {
98+
findLast(ancestors, (ancestor) => {
99+
// ancestors.some((ancestor) => {
97100
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
98101
depth += 1;
99102
}

lib/util/variable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const toReversed = require('array.prototype.toreversed');
9+
810
/**
911
* Search a particular variable in a list
1012
* @param {Array} variables The variables list.
@@ -47,9 +49,8 @@ function variablesInScope(context) {
4749
variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
4850
}
4951
}
50-
variables.reverse();
5152

52-
return variables;
53+
return toReversed(variables);
5354
}
5455

5556
/**

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"bugs": "https://github.com/jsx-eslint/eslint-plugin-react/issues",
2727
"dependencies": {
2828
"array-includes": "^3.1.7",
29+
"array.prototype.findlast": "^1.2.3",
2930
"array.prototype.flatmap": "^1.3.2",
31+
"array.prototype.toreversed": "^1.1.2",
3032
"array.prototype.tosorted": "^1.1.2",
3133
"doctrine": "^2.1.0",
3234
"es-iterator-helpers": "^1.0.15",

0 commit comments

Comments
 (0)