diff --git a/.eslintrc b/.eslintrc index b3af93dd31..d9d44c0a45 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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" diff --git a/CHANGELOG.md b/CHANGELOG.md index 49987d904b..f2e96e9af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-max-depth.js b/lib/rules/jsx-max-depth.js index b4554c28d3..417c64188a 100644 --- a/lib/rules/jsx-max-depth.js +++ b/lib/rules/jsx-max-depth.js @@ -70,7 +70,7 @@ module.exports = { while (jsxUtil.isJSX(node.parent) || isExpression(node.parent)) { node = node.parent; if (jsxUtil.isJSX(node)) { - count++; + count += 1; } } @@ -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; @@ -121,7 +119,7 @@ module.exports = { } function checkDescendant(baseDepth, children) { - baseDepth++; + baseDepth += 1; (children || []).forEach((node) => { if (!hasJSX(node)) { return; diff --git a/lib/rules/jsx-pascal-case.js b/lib/rules/jsx-pascal-case.js index 901294fa52..a1bb48116a 100644 --- a/lib/rules/jsx-pascal-case.js +++ b/lib/rules/jsx-pascal-case.js @@ -155,7 +155,7 @@ module.exports = { }); break; } - index++; + index += 1; } while (index < checkNames.length && !allowNamespace); }, }; diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index 8daf981908..ea2e8c88d3 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -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') { diff --git a/lib/rules/require-render-return.js b/lib/rules/require-render-return.js index 460683e4c5..fa4afcbe49 100644 --- a/lib/rules/require-render-return.js +++ b/lib/rules/require-render-return.js @@ -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) diff --git a/lib/rules/sort-comp.js b/lib/rules/sort-comp.js index 842dd561e9..120fceee22 100644 --- a/lib/rules/sort-comp.js +++ b/lib/rules/sort-comp.js @@ -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; diff --git a/lib/util/ast.js b/lib/util/ast.js index d8172289d8..b1c3c130da 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -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 * @@ -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); } /** diff --git a/lib/util/makeNoMethodSetStateRule.js b/lib/util/makeNoMethodSetStateRule.js index 8f93762c60..f1be5c16ae 100644 --- a/lib/util/makeNoMethodSetStateRule.js +++ b/lib/util/makeNoMethodSetStateRule.js @@ -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')