Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] fix linter errors #3261

Merged
merged 1 commit into from Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc
Expand Up @@ -32,9 +32,8 @@
"prefer-spread": 0, // until node 6 is required
"function-call-argument-newline": 1, // TODO: enable
"function-paren-newline": 0,
"no-plusplus": 1,
"no-plusplus": [2, {"allowForLoopAfterthoughts": true}],
"no-param-reassign": 1,
"no-unreachable-loop": 1, // TODO: enable
"no-restricted-syntax": [2, {
"selector": "ObjectPattern",
"message": "Object destructuring is not compatible with Node v4"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,7 +15,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Changed
* [readme] remove global usage and eslint version from readme ([#3254][] @aladdin-add)
* [Refactor] fix linter errors ([#3261][] @golopot)

[#3261]: https://github.com/yannickcr/eslint-plugin-react/pull/3261
[#3254]: https://github.com/yannickcr/eslint-plugin-react/pull/3254
[#3251]: https://github.com/yannickcr/eslint-plugin-react/pull/3251
[#3244]: https://github.com/yannickcr/eslint-plugin-react/pull/3244
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/jsx-max-depth.js
Expand Up @@ -70,7 +70,7 @@ module.exports = {
while (jsxUtil.isJSX(node.parent) || isExpression(node.parent)) {
node = node.parent;
if (jsxUtil.isJSX(node)) {
count++;
count += 1;
}
}

Expand All @@ -89,9 +89,7 @@ module.exports = {

function findJSXElementOrFragment(variables, name, previousReferences) {
function find(refs, prevRefs) {
let i = refs.length;

while (--i >= 0) {
for (let i = refs.length - 1; i >= 0; i--) {
if (has(refs[i], 'writeExpr')) {
const writeExpr = refs[i].writeExpr;

Expand Down Expand Up @@ -121,7 +119,7 @@ module.exports = {
}

function checkDescendant(baseDepth, children) {
baseDepth++;
baseDepth += 1;
(children || []).forEach((node) => {
if (!hasJSX(node)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-pascal-case.js
Expand Up @@ -155,7 +155,7 @@ module.exports = {
});
break;
}
index++;
index += 1;
} while (index < checkNames.length && !allowNamespace);
},
};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-sort-props.js
Expand Up @@ -133,7 +133,7 @@ function getGroupsOfSortableAttributes(attributes) {
|| (lastAttr.type === 'JSXSpreadAttribute'
&& attributes[i].type !== 'JSXSpreadAttribute')
) {
groupCount++;
groupCount += 1;
sortableAttributeGroups[groupCount - 1] = [];
}
if (attributes[i].type !== 'JSXSpreadAttribute') {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-render-return.js
Expand Up @@ -61,7 +61,7 @@ module.exports = {
let depth = 0;
ancestors.forEach((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
depth++;
depth += 1;
}
if (
/(MethodDefinition|Property|ClassProperty|PropertyDefinition)$/.test(ancestor.type)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/sort-comp.js
Expand Up @@ -266,7 +266,7 @@ module.exports = {
};
}
// Increment the prop score
errors[propA.index].score++;
errors[propA.index].score += 1;
// Stop here if we already have pushed another node at this position
if (getPropertyName(errors[propA.index].node) !== getPropertyName(propA.node)) {
return;
Expand Down
33 changes: 17 additions & 16 deletions lib/util/ast.js
Expand Up @@ -28,6 +28,21 @@ function traverse(ASTnode, visitor) {
estraverse.traverse(ASTnode, opts);
}

function loopNodes(nodes) {
for (let i = nodes.length - 1; i >= 0; i--) {
if (nodes[i].type === 'ReturnStatement') {
return nodes[i];
}
if (nodes[i].type === 'SwitchStatement') {
const j = nodes[i].cases.length - 1;
if (j >= 0) {
return loopNodes(nodes[i].cases[j].consequent);
}
}
}
return false;
}

/**
* Find a return statment in the current node
*
Expand All @@ -42,23 +57,9 @@ function findReturnStatement(node) {
return false;
}

const bodyNodes = (node.value ? node.value.body.body : node.body.body);
const bodyNodes = node.value ? node.value.body.body : node.body.body;

return (function loopNodes(nodes) {
let i = nodes.length - 1;
for (; i >= 0; i--) {
if (nodes[i].type === 'ReturnStatement') {
return nodes[i];
}
if (nodes[i].type === 'SwitchStatement') {
let j = nodes[i].cases.length - 1;
for (; j >= 0; j--) {
return loopNodes(nodes[i].cases[j].consequent);
}
}
}
return false;
}(bodyNodes));
return loopNodes(bodyNodes);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/util/makeNoMethodSetStateRule.js
Expand Up @@ -95,7 +95,7 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe
let depth = 0;
ancestors.some((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
depth++;
depth += 1;
}
if (
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition' && ancestor.type !== 'ClassProperty' && ancestor.type !== 'PropertyDefinition')
Expand Down