From d9a2a3771aeb639919c9a2f938319b5c372c0513 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Mon, 25 Oct 2021 22:56:28 -0500 Subject: [PATCH] style: apply auto fixes --- package.json | 2 +- src/bin/addAssertions.js | 4 ++-- src/bin/checkTests.js | 1 + src/rules/defineFlowType.js | 4 ++-- src/rules/noDupeKeys.js | 4 ++-- src/rules/noFlowFixMeComments.js | 13 ++++++------ src/rules/requireInexactType.js | 2 +- src/rules/requireParameterType.js | 1 + src/rules/requireReturnType.js | 2 ++ src/rules/requireVariableType.js | 1 + src/rules/sortKeys.js | 20 ++++++++++--------- src/rules/spreadExactType.js | 4 ++-- .../typeColonSpacing/evaluateFunctions.js | 6 ++++-- .../typeColonSpacing/evaluateVariables.js | 4 ++-- src/rules/typeImportStyle.js | 4 ++-- src/rules/unionIntersectionSpacing.js | 6 +++--- src/rules/useFlowType.js | 8 ++++---- src/rules/validSyntax.js | 4 ++-- 18 files changed, 50 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 90c93e82..a6305db7 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "ajv": "^8.6.3", "babel-plugin-add-module-exports": "^1.0.4", "eslint": "^8.1.0", - "eslint-config-canonical": "^32.1.1", + "eslint-config-canonical": "^32.6.0", "eslint-plugin-eslint-plugin": "^4.0.1", "gitdown": "^3.1.4", "glob": "^7.2.0", diff --git a/src/bin/addAssertions.js b/src/bin/addAssertions.js index 76906743..028cd470 100644 --- a/src/bin/addAssertions.js +++ b/src/bin/addAssertions.js @@ -23,9 +23,9 @@ const formatCodeSnippet = (setup) => { paragraphs.push(setup.code); if (setup.errors) { - setup.errors.forEach((message) => { + for (const message of setup.errors) { paragraphs.push('// Message: ' + message.message); - }); + } } if (setup.rules) { diff --git a/src/bin/checkTests.js b/src/bin/checkTests.js index 957234b6..cd9d30bf 100644 --- a/src/bin/checkTests.js +++ b/src/bin/checkTests.js @@ -10,6 +10,7 @@ import { const getTestIndexRules = () => { const content = fs.readFileSync(path.resolve(__dirname, '../../tests/rules/index.js'), 'utf-8'); + // eslint-disable-next-line unicorn/no-array-reduce const result = content.split('\n').reduce((acc, line) => { if (acc.inRulesArray) { if (line === '];') { diff --git a/src/rules/defineFlowType.js b/src/rules/defineFlowType.js index 5caf26ab..5c696df2 100644 --- a/src/rules/defineFlowType.js +++ b/src/rules/defineFlowType.js @@ -68,9 +68,9 @@ const create = (context) => { globalScope = context.getScope(); }, TypeParameterDeclaration (node) { - node.params.forEach((param) => { + for (const param of node.params) { makeDefined(param); - }); + } }, }; }; diff --git a/src/rules/noDupeKeys.js b/src/rules/noDupeKeys.js index 8a5f569c..26964bf6 100644 --- a/src/rules/noDupeKeys.js +++ b/src/rules/noDupeKeys.js @@ -68,7 +68,7 @@ const create = (context) => { // filter out complex object types, like ObjectTypeSpreadProperty const identifierNodes = _.filter(node.properties, {type: 'ObjectTypeProperty'}); - _.forEach(identifierNodes, (identifierNode) => { + for (const identifierNode of identifierNodes) { const needle = {name: getParameterName(identifierNode, context)}; if (identifierNode.value.type === 'FunctionTypeAnnotation') { @@ -86,7 +86,7 @@ const create = (context) => { } else { haystack.push(needle); } - }); + } }; return { diff --git a/src/rules/noFlowFixMeComments.js b/src/rules/noFlowFixMeComments.js index ea7a33d8..17b0e9b6 100644 --- a/src/rules/noFlowFixMeComments.js +++ b/src/rules/noFlowFixMeComments.js @@ -25,7 +25,7 @@ const create = (context) => { const handleComment = function (comment) { const value = comment.value.trim(); - if (value.match(/\$FlowFixMe/u) && !passesExtraRegex(value)) { + if (/\$FlowFixMe/u.test(value) && !passesExtraRegex(value)) { context.report(comment, message + extraMessage); } }; @@ -41,13 +41,14 @@ const create = (context) => { }, Program () { - context + for (const comment of context .getSourceCode() .getAllComments() - .filter((comment) => { - return comment.type === 'Block' || comment.type === 'Line'; - }) - .forEach(handleComment); + .filter((node) => { + return node.type === 'Block' || node.type === 'Line'; + })) { + handleComment(comment); + } }, }; }; diff --git a/src/rules/requireInexactType.js b/src/rules/requireInexactType.js index 872cf9a8..90ca10e0 100644 --- a/src/rules/requireInexactType.js +++ b/src/rules/requireInexactType.js @@ -12,7 +12,7 @@ const create = (context) => { ObjectTypeAnnotation (node) { const {inexact, exact} = node; - if (!node.hasOwnProperty('inexact')) { + if (!Object.prototype.hasOwnProperty.call(node, 'inexact')) { return; } diff --git a/src/rules/requireParameterType.js b/src/rules/requireParameterType.js index 2c1bf365..90178b1f 100644 --- a/src/rules/requireParameterType.js +++ b/src/rules/requireParameterType.js @@ -38,6 +38,7 @@ const create = iterateFunctionNodes((context) => { return; } + // eslint-disable-next-line unicorn/no-array-for-each _.forEach(functionNode.params, (identifierNode) => { const parameterName = getParameterName(identifierNode, context); diff --git a/src/rules/requireReturnType.js b/src/rules/requireReturnType.js index dbce24b8..788ccdf5 100644 --- a/src/rules/requireReturnType.js +++ b/src/rules/requireReturnType.js @@ -48,7 +48,9 @@ const create = (context) => { const annotateUndefined = _.get(context, 'options[1].annotateUndefined') || 'never'; const skipArrows = _.get(context, 'options[1].excludeArrowFunctions') || false; + // eslint-disable-next-line unicorn/no-array-callback-reference const excludeMatching = _.get(context, 'options[1].excludeMatching', []).map(makeRegExp); + // eslint-disable-next-line unicorn/no-array-callback-reference const includeOnlyMatching = _.get(context, 'options[1].includeOnlyMatching', []).map(makeRegExp); const targetNodes = []; diff --git a/src/rules/requireVariableType.js b/src/rules/requireVariableType.js index 9acd64e6..4f42f2ff 100644 --- a/src/rules/requireVariableType.js +++ b/src/rules/requireVariableType.js @@ -49,6 +49,7 @@ const create = (context) => { return; } + // eslint-disable-next-line unicorn/no-array-for-each _.forEach(variableDeclaration.declarations, (variableDeclarator) => { const identifierNode = _.get(variableDeclarator, 'id'); const identifierName = _.get(identifierNode, 'name'); diff --git a/src/rules/sortKeys.js b/src/rules/sortKeys.js index e0b56fb6..787ded22 100644 --- a/src/rules/sortKeys.js +++ b/src/rules/sortKeys.js @@ -101,7 +101,8 @@ const generateOrderedList = (context, sort, properties) => { const itemGroups = [[]]; let itemGroupIndex = 0; - items.forEach((item) => { + + for (const item of items) { if (item[0].type === 'ObjectTypeSpreadProperty') { ++itemGroupIndex; itemGroups[itemGroupIndex] = [item]; @@ -110,10 +111,10 @@ const generateOrderedList = (context, sort, properties) => { } else { itemGroups[itemGroupIndex].push(item); } - }); + } const orderedList = []; - itemGroups.forEach((itemGroup) => { + for (const itemGroup of itemGroups) { if (itemGroup[0] && itemGroup[0].type !== 'ObjectTypeSpreadProperty') { // console.log('itemGroup', itemGroup); @@ -129,7 +130,7 @@ const generateOrderedList = (context, sort, properties) => { return item[2] + ':' + item[3]; })); - }); + } return orderedList; }; @@ -146,7 +147,7 @@ const generateFix = (node, context, sort) => { nodeText = originalSubstring; - node.properties.forEach((property, index) => { + for (const [index, property] of node.properties.entries()) { const nextPunctuator = source.getTokenAfter(property, { filter: (token) => { return token.type === 'Punctuator' || token.value === '|}'; @@ -165,11 +166,11 @@ const generateFix = (node, context, sort) => { ); nodeText = nodeText.replace(subString, '$' + index); - }); + } - newTypes.forEach((item, index) => { + for (const [index, item] of newTypes.entries()) { nodeText = nodeText.replace('$' + index, item); - }); + } return nodeText; }; @@ -181,7 +182,8 @@ const create = (context) => { const checkKeyOrder = (node) => { prev = null; - _.forEach(node.properties, (identifierNode) => { + // eslint-disable-next-line unicorn/no-array-for-each + node.properties.forEach((identifierNode) => { const current = getParameterName(identifierNode, context); const last = prev; diff --git a/src/rules/spreadExactType.js b/src/rules/spreadExactType.js index 03873f11..012ec0c4 100644 --- a/src/rules/spreadExactType.js +++ b/src/rules/spreadExactType.js @@ -10,7 +10,7 @@ const create = (context) => { ObjectTypeAnnotation (node) { const {properties} = node; - properties.forEach((property) => { + for (const property of properties) { const {type} = property; if (type === 'ObjectTypeSpreadProperty') { const {argument: {type: argumentType, id: argumentId}} = property; @@ -22,7 +22,7 @@ const create = (context) => { }); } } - }); + } }, }; }; diff --git a/src/rules/typeColonSpacing/evaluateFunctions.js b/src/rules/typeColonSpacing/evaluateFunctions.js index a06b1cee..db213b02 100644 --- a/src/rules/typeColonSpacing/evaluateFunctions.js +++ b/src/rules/typeColonSpacing/evaluateFunctions.js @@ -1,4 +1,3 @@ -import _ from 'lodash'; import { iterateFunctionNodes, } from '../../utilities'; @@ -10,7 +9,10 @@ export default iterateFunctionNodes((context, report) => { const checkReturnType = evaluateReturnType(context, report); return (functionNode) => { - _.forEach(functionNode.params, checkParam); + for (const param of functionNode.params) { + checkParam(param); + } + checkReturnType(functionNode); }; }); diff --git a/src/rules/typeColonSpacing/evaluateVariables.js b/src/rules/typeColonSpacing/evaluateVariables.js index fc60f408..b6094383 100644 --- a/src/rules/typeColonSpacing/evaluateVariables.js +++ b/src/rules/typeColonSpacing/evaluateVariables.js @@ -9,7 +9,7 @@ export default (context, report) => { return (node) => { const declarations = _.get(node, 'declarations', []); - _.forEach(declarations, (leaf) => { + for (const leaf of declarations) { const typeAnnotation = _.get(leaf, 'id.typeAnnotation'); if (typeAnnotation) { @@ -20,6 +20,6 @@ export default (context, report) => { type: node.kind + ' type annotation', }); } - }); + } }; }; diff --git a/src/rules/typeImportStyle.js b/src/rules/typeImportStyle.js index d8d8ae9b..4a6fccbc 100644 --- a/src/rules/typeImportStyle.js +++ b/src/rules/typeImportStyle.js @@ -19,14 +19,14 @@ const create = (context) => { return { ImportDeclaration (node) { if (node.importKind !== 'type') { - node.specifiers.forEach((specifier) => { + for (const specifier of node.specifiers) { if (specifier.importKind === 'type') { context.report({ message: 'Unexpected type import', node, }); } - }); + } } }, }; diff --git a/src/rules/unionIntersectionSpacing.js b/src/rules/unionIntersectionSpacing.js index e5fab221..4363e5c5 100644 --- a/src/rules/unionIntersectionSpacing.js +++ b/src/rules/unionIntersectionSpacing.js @@ -15,9 +15,9 @@ const create = (context) => { const always = (context.options[0] || 'always') === 'always'; const check = (node) => { - node.types.forEach((type, index) => { + for (const [index, type] of node.types.entries()) { if (index + 1 === node.types.length) { - return; + continue; } const separator = getTokenAfterParens(sourceCode, type); @@ -66,7 +66,7 @@ const create = (context) => { }); } } - }); + } }; return { diff --git a/src/rules/useFlowType.js b/src/rules/useFlowType.js index ce08c072..4c5f78cc 100644 --- a/src/rules/useFlowType.js +++ b/src/rules/useFlowType.js @@ -34,19 +34,19 @@ const create = (context) => { DeclareVariable: markTypeAsUsed, GenericTypeAnnotation: markTypeAsUsedWithGenericType, TypeParameterDeclaration (node) { - node.params.forEach((param) => { + for (const param of node.params) { if (param.default && param.default.typeParameters) { if (param.default.type === 'GenericTypeAnnotation') { markTypeAsUsedWithGenericType(param.default); } - param.default.typeParameters.params.forEach((typeParameterNode) => { + for (const typeParameterNode of param.default.typeParameters.params) { if (typeParameterNode.type === 'GenericTypeAnnotation') { markTypeAsUsedWithGenericType(typeParameterNode); } - }); + } } - }); + } }, }; }; diff --git a/src/rules/validSyntax.js b/src/rules/validSyntax.js index a76a51e8..bd52875f 100644 --- a/src/rules/validSyntax.js +++ b/src/rules/validSyntax.js @@ -9,7 +9,7 @@ const schema = []; const create = iterateFunctionNodes((context) => { return (functionNode) => { - _.forEach(functionNode.params, (identifierNode) => { + for (const identifierNode of functionNode.params) { const nodeType = _.get(identifierNode, 'type'); const isAssignmentPattern = nodeType === 'AssignmentPattern'; const hasTypeAnnotation = Boolean(_.get(identifierNode, 'typeAnnotation')); @@ -24,7 +24,7 @@ const create = iterateFunctionNodes((context) => { node: identifierNode, }); } - }); + } }; });