From e7039845d07e350ccd24388faae880cd11a09e4a Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Tue, 26 Oct 2021 00:41:54 -0500 Subject: [PATCH] fix: apply style fixes --- package.json | 4 +- src/bin/checkDocs.js | 15 +-- src/bin/checkTests.js | 2 +- src/rules/arrayStyle/index.js | 8 +- src/rules/interfaceIdMatch.js | 6 +- src/rules/newlineAfterFlowAnnotation.js | 4 +- src/rules/noFlowFixMeComments.js | 5 +- src/rules/noMutableArray.js | 8 +- src/rules/noTypesMissingFileAnnotation.js | 1 + src/rules/noUnusedExpressions.js | 3 +- src/rules/requireCompoundTypeAlias.js | 4 +- src/rules/requireIndexerName.js | 4 +- src/rules/requireReadonlyReactProps.js | 4 +- src/rules/requireReturnType.js | 7 +- src/rules/requireTypesAtTop.js | 4 +- src/rules/requireValidFileAnnotation.js | 9 +- src/rules/sortKeys.js | 1 + src/rules/sortTypeUnionIntersectionMembers.js | 2 + src/rules/typeColonSpacing/evaluateTypical.js | 4 +- src/rules/typeColonSpacing/reporter.js | 8 +- src/rules/typeIdMatch.js | 6 +- src/rules/typeImportStyle.js | 92 ++++++++++--------- src/rules/useFlowType.js | 1 + src/rules/useReadOnlySpread.js | 1 + src/utilities/getParameterName.js | 9 +- src/utilities/spacingFixers.js | 12 +-- 26 files changed, 123 insertions(+), 101 deletions(-) diff --git a/package.json b/package.json index a6305db7..79870de7 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "ajv": "^8.6.3", "babel-plugin-add-module-exports": "^1.0.4", "eslint": "^8.1.0", - "eslint-config-canonical": "^32.6.0", - "eslint-plugin-eslint-plugin": "^4.0.1", + "eslint-config-canonical": "^32.10.0", + "eslint-plugin-eslint-plugin": "^4.0.2", "gitdown": "^3.1.4", "glob": "^7.2.0", "husky": "^7.0.4", diff --git a/src/bin/checkDocs.js b/src/bin/checkDocs.js index 15c4d9b3..8c1cdc57 100644 --- a/src/bin/checkDocs.js +++ b/src/bin/checkDocs.js @@ -27,12 +27,13 @@ const getDocIndexRules = () => { if (match === null) { return null; - } else { - return match[1].replace('./rules/', '').replace('.md', ''); } - }).filter((rule) => { - return rule !== null; - }); + + return match[1].replace('./rules/', '').replace('.md', ''); + }) + .filter((rule) => { + return rule !== null; + }); if (rules.length === 0) { throw new Error('Docs checker is broken - it could not extract rules from docs index file.'); @@ -48,9 +49,9 @@ const hasCorrectAssertions = (docPath, name) => { if (match === null) { return false; - } else { - return match[1] === name; } + + return match[1] === name; }; /** diff --git a/src/bin/checkTests.js b/src/bin/checkTests.js index cd9d30bf..8665eea7 100644 --- a/src/bin/checkTests.js +++ b/src/bin/checkTests.js @@ -28,7 +28,7 @@ const getTestIndexRules = () => { rules: [], }); - const rules = result.rules; + const {rules} = result; if (rules.length === 0) { throw new Error('Tests checker is broken - it could not extract rules from test index file.'); diff --git a/src/rules/arrayStyle/index.js b/src/rules/arrayStyle/index.js index ba63460c..da37ea93 100644 --- a/src/rules/arrayStyle/index.js +++ b/src/rules/arrayStyle/index.js @@ -13,9 +13,9 @@ const inlineType = (type) => { if (inlined.length <= 50) { return inlined; - } else { - return 'Type'; } + + return 'Type'; }; export default (defaultConfig, simpleType) => { @@ -64,9 +64,9 @@ export default (defaultConfig, simpleType) => { fix (fixer) { if (needWrap(elementTypeNode)) { return fixer.replaceText(node, '(' + rawElementType + ')[]'); - } else { - return fixer.replaceText(node, rawElementType + '[]'); } + + return fixer.replaceText(node, rawElementType + '[]'); }, message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"', node, diff --git a/src/rules/interfaceIdMatch.js b/src/rules/interfaceIdMatch.js index de14d029..54ee6ed4 100644 --- a/src/rules/interfaceIdMatch.js +++ b/src/rules/interfaceIdMatch.js @@ -11,10 +11,12 @@ const create = (context) => { const interfaceIdentifierName = interfaceDeclarationNode.id.name; if (!pattern.test(interfaceIdentifierName)) { - context.report(interfaceDeclarationNode, 'Interface identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', { + context.report({data: { name: interfaceIdentifierName, pattern: pattern.toString(), - }); + }, + message: 'Interface identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', + node: interfaceDeclarationNode}); } }; diff --git a/src/rules/newlineAfterFlowAnnotation.js b/src/rules/newlineAfterFlowAnnotation.js index 2ad4b86d..2d1689da 100644 --- a/src/rules/newlineAfterFlowAnnotation.js +++ b/src/rules/newlineAfterFlowAnnotation.js @@ -22,14 +22,14 @@ const create = (context) => { const sourceCode = context.getSourceCode(); const potentialFlowFileAnnotation = _.find( - context.getAllComments(), + context.getSourceCode().getAllComments(), (comment) => { return looksLikeFlowFileAnnotation(comment.value); }, ); if (potentialFlowFileAnnotation) { - const line = potentialFlowFileAnnotation.loc.end.line; + const {line} = potentialFlowFileAnnotation.loc.end; const nextLineIsEmpty = sourceCode.lines[line] === ''; if (!never && !nextLineIsEmpty) { diff --git a/src/rules/noFlowFixMeComments.js b/src/rules/noFlowFixMeComments.js index 17b0e9b6..255d4174 100644 --- a/src/rules/noFlowFixMeComments.js +++ b/src/rules/noFlowFixMeComments.js @@ -26,7 +26,10 @@ const create = (context) => { const value = comment.value.trim(); if (/\$FlowFixMe/u.test(value) && !passesExtraRegex(value)) { - context.report(comment, message + extraMessage); + context.report({ + message: message + extraMessage, + node: comment, + }); } }; diff --git a/src/rules/noMutableArray.js b/src/rules/noMutableArray.js index 4d8a2d3c..019256fb 100644 --- a/src/rules/noMutableArray.js +++ b/src/rules/noMutableArray.js @@ -11,9 +11,9 @@ const isEmptyArrayLiteral = (node) => { const isEmptyArrayInstance = (node) => { if (_.get(node, 'init.type') === 'NewExpression' || _.get(node, 'init.type') === 'CallExpression') { return _.get(node, 'init.callee.name') === 'Array' && _.get(node, 'init.arguments.length') === 0; - } else { - return false; } + + return false; }; const isAnnotationOfEmptyArrayInit = (node) => { @@ -22,9 +22,9 @@ const isAnnotationOfEmptyArrayInit = (node) => { const isVariableDeclaration = _.get(parent, 'type') === 'VariableDeclarator'; return isVariableDeclaration && (isEmptyArrayLiteral(parent) || isEmptyArrayInstance(parent)); - } else { - return false; } + + return false; }; const create = (context) => { diff --git a/src/rules/noTypesMissingFileAnnotation.js b/src/rules/noTypesMissingFileAnnotation.js index 55bda717..74cfb8b4 100644 --- a/src/rules/noTypesMissingFileAnnotation.js +++ b/src/rules/noTypesMissingFileAnnotation.js @@ -33,6 +33,7 @@ const create = (context) => { if (node.importKind === 'type') { reporter(node, 'imports'); } + if (node.importKind === 'value' && node.specifiers.some((specifier) => { return specifier.importKind === 'type'; diff --git a/src/rules/noUnusedExpressions.js b/src/rules/noUnusedExpressions.js index 9e0b6bdd..96ebd8a7 100644 --- a/src/rules/noUnusedExpressions.js +++ b/src/rules/noUnusedExpressions.js @@ -7,7 +7,7 @@ import { const noUnusedExpressionsRule = getBuiltinRule('no-unused-expressions'); -const meta = noUnusedExpressionsRule.meta; +const {meta} = noUnusedExpressionsRule; const create = (context) => { const coreChecks = noUnusedExpressionsRule.create(context); @@ -20,6 +20,7 @@ const create = (context) => { ) { return; } + // eslint-disable-next-line @babel/new-cap coreChecks.ExpressionStatement(node); }, diff --git a/src/rules/requireCompoundTypeAlias.js b/src/rules/requireCompoundTypeAlias.js index 996ef2fb..8da73195 100644 --- a/src/rules/requireCompoundTypeAlias.js +++ b/src/rules/requireCompoundTypeAlias.js @@ -60,9 +60,9 @@ const create = (context) => { } }, }; - } else { - return {}; } + + return {}; }; export default { diff --git a/src/rules/requireIndexerName.js b/src/rules/requireIndexerName.js index ed1105cb..9d400a72 100644 --- a/src/rules/requireIndexerName.js +++ b/src/rules/requireIndexerName.js @@ -28,9 +28,9 @@ const create = (context) => { } }, }; - } else { - return {}; } + + return {}; }; export default { diff --git a/src/rules/requireReadonlyReactProps.js b/src/rules/requireReadonlyReactProps.js index f396a0e3..ad7bf4e3 100644 --- a/src/rules/requireReadonlyReactProps.js +++ b/src/rules/requireReadonlyReactProps.js @@ -44,7 +44,9 @@ const isReadOnlyObjectType = (node, {useImplicitExactTypes}) => { // we consider `{||}` to be ReadOnly since it's exact AND has no props (when `implicitExactTypes=false`) if (useImplicitExactTypes === true && node.exact === false) { return true; - } else if (node.exact === true) { + } + + if (node.exact === true) { return true; } } diff --git a/src/rules/requireReturnType.js b/src/rules/requireReturnType.js index 788ccdf5..2196df16 100644 --- a/src/rules/requireReturnType.js +++ b/src/rules/requireReturnType.js @@ -89,6 +89,7 @@ const create = (context) => { } else { selector = 'id.name'; } + const identifierName = _.get(functionNode, selector); const checkRegExp = (regex) => { @@ -129,14 +130,14 @@ const create = (context) => { const returnType = functionNode.returnType || isArrow && _.get(functionNode, 'parent.id.typeAnnotation'); if (isFunctionReturnUndefined && isReturnTypeAnnotationUndefined && annotateUndefined === 'never') { - context.report(functionNode, 'Must not annotate undefined return type.'); + context.report({message: 'Must not annotate undefined return type.', node: functionNode}); } else if (isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined && annotateUndefined === 'always') { - context.report(functionNode, 'Must annotate undefined return type.'); + context.report({message: 'Must annotate undefined return type.', node: functionNode}); } else if ( (annotateUndefined === 'always-enforce' || !isFunctionReturnUndefined && !isReturnTypeAnnotationUndefined) && annotateReturn && !returnType && !shouldFilterNode(functionNode) ) { - context.report(functionNode, 'Missing return type annotation.'); + context.report({message: 'Missing return type annotation.', node: functionNode}); } }; diff --git a/src/rules/requireTypesAtTop.js b/src/rules/requireTypesAtTop.js index 084681ed..c55c71bb 100644 --- a/src/rules/requireTypesAtTop.js +++ b/src/rules/requireTypesAtTop.js @@ -73,9 +73,9 @@ const create = (context) => { } }, }; - } else { - return {}; } + + return {}; }; export default { diff --git a/src/rules/requireValidFileAnnotation.js b/src/rules/requireValidFileAnnotation.js index 175fc752..9fe31652 100644 --- a/src/rules/requireValidFileAnnotation.js +++ b/src/rules/requireValidFileAnnotation.js @@ -63,14 +63,15 @@ const create = (context) => { Program (node) { const firstToken = node.tokens[0]; - const potentialFlowFileAnnotation = _.find(context.getAllComments(), (comment) => { + const potentialFlowFileAnnotation = _.find(context.getSourceCode().getAllComments(), (comment) => { return looksLikeFlowFileAnnotation(comment.value); }); if (potentialFlowFileAnnotation) { if (firstToken && firstToken.range[0] < potentialFlowFileAnnotation.range[0]) { - context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.'); + context.report({message: 'Flow file annotation not at the top of the file.', node: potentialFlowFileAnnotation}); } + const annotationValue = potentialFlowFileAnnotation.value.trim(); if (isFlowFileAnnotation(annotationValue)) { @@ -109,9 +110,9 @@ const create = (context) => { }); } } else if (checkAnnotationSpelling(annotationValue)) { - context.report(potentialFlowFileAnnotation, 'Misspelled or malformed Flow file annotation.'); + context.report({message: 'Misspelled or malformed Flow file annotation.', node: potentialFlowFileAnnotation}); } else { - context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.'); + context.report({message: 'Malformed Flow file annotation.', node: potentialFlowFileAnnotation}); } } else if (always && !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation')) { context.report({ diff --git a/src/rules/sortKeys.js b/src/rules/sortKeys.js index 787ded22..4131abad 100644 --- a/src/rules/sortKeys.js +++ b/src/rules/sortKeys.js @@ -123,6 +123,7 @@ const generateOrderedList = (context, sort, properties) => { return sort(first[1], second[1]); }); } + orderedList.push(...itemGroup.map((item) => { if (item.length === 3) { return item[2]; diff --git a/src/rules/sortTypeUnionIntersectionMembers.js b/src/rules/sortTypeUnionIntersectionMembers.js index 4d212d79..5482b077 100644 --- a/src/rules/sortTypeUnionIntersectionMembers.js +++ b/src/rules/sortTypeUnionIntersectionMembers.js @@ -13,6 +13,7 @@ const groups = { // eslint-disable-next-line complexity const getGroup = (node) => { + // eslint-disable-next-line default-case switch (node.type) { case 'FunctionTypeAnnotation': return groups.function; @@ -61,6 +62,7 @@ const fallbackSort = (a, b) => { if (a < b) { return -1; } + if (a > b) { return 1; } diff --git a/src/rules/typeColonSpacing/evaluateTypical.js b/src/rules/typeColonSpacing/evaluateTypical.js index 94710c93..0934eb21 100644 --- a/src/rules/typeColonSpacing/evaluateTypical.js +++ b/src/rules/typeColonSpacing/evaluateTypical.js @@ -9,9 +9,9 @@ export default (context, report, typeForMessage) => { const getColon = (node, typeAnnotation) => { if (node.type === 'FunctionTypeParam') { return sourceCode.getFirstToken(node, node.optional ? 2 : 1); - } else { - return sourceCode.getFirstToken(typeAnnotation); } + + return sourceCode.getFirstToken(typeAnnotation); }; return (node) => { diff --git a/src/rules/typeColonSpacing/reporter.js b/src/rules/typeColonSpacing/reporter.js index 7c7b5459..ce552d84 100644 --- a/src/rules/typeColonSpacing/reporter.js +++ b/src/rules/typeColonSpacing/reporter.js @@ -7,9 +7,9 @@ const hasLineBreak = (direction, colon, context) => { if (direction === 'before') { return colon.loc.start.line !== sourceCode.getTokenBefore(colon).loc.end.line; - } else { - return sourceCode.getTokenAfter(colon).loc.start.line !== colon.loc.end.line; } + + return sourceCode.getTokenAfter(colon).loc.start.line !== colon.loc.end.line; }; const getSpaces = (direction, colon, context) => { @@ -17,9 +17,9 @@ const getSpaces = (direction, colon, context) => { if (direction === 'before') { return colon.range[0] - sourceCode.getTokenBefore(colon).range[1]; - } else { - return sourceCode.getTokenAfter(colon).range[0] - colon.range[1]; } + + return sourceCode.getTokenAfter(colon).range[0] - colon.range[1]; }; export default (direction, context, {always, allowLineBreak}) => { diff --git a/src/rules/typeIdMatch.js b/src/rules/typeIdMatch.js index e3c84984..e00aca4e 100644 --- a/src/rules/typeIdMatch.js +++ b/src/rules/typeIdMatch.js @@ -11,10 +11,12 @@ const create = (context) => { const typeIdentifierName = typeAliasNode.id.name; if (!pattern.test(typeIdentifierName)) { - context.report(typeAliasNode, 'Type identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', { + context.report({data: { name: typeIdentifierName, pattern: pattern.toString(), - }); + }, + message: 'Type identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', + node: typeAliasNode}); } }; diff --git a/src/rules/typeImportStyle.js b/src/rules/typeImportStyle.js index 4a6fccbc..3677e208 100644 --- a/src/rules/typeImportStyle.js +++ b/src/rules/typeImportStyle.js @@ -30,59 +30,61 @@ const create = (context) => { } }, }; - } else { - // Default to 'identifier' - const ignoreTypeDefault = context.options[1] && + } + + // Default to 'identifier' + const ignoreTypeDefault = context.options[1] && context.options[1].ignoreTypeDefault; - let isInsideDeclareModule = false; + let isInsideDeclareModule = false; - return { - DeclareModule () { - isInsideDeclareModule = true; - }, - 'DeclareModule:exit' () { - isInsideDeclareModule = false; - }, - ImportDeclaration (node) { - if (node.importKind !== 'type') { - return; - } + return { + DeclareModule () { + isInsideDeclareModule = true; + }, + 'DeclareModule:exit' () { + isInsideDeclareModule = false; + }, + ImportDeclaration (node) { + if (node.importKind !== 'type') { + return; + } - // type specifiers are not allowed inside module declarations: - // https://github.com/facebook/flow/issues/7609 - if (isInsideDeclareModule) { - return; - } + // type specifiers are not allowed inside module declarations: + // https://github.com/facebook/flow/issues/7609 + if (isInsideDeclareModule) { + return; + } - if ( - ignoreTypeDefault && + if ( + ignoreTypeDefault && node.specifiers[0] && node.specifiers[0].type === 'ImportDefaultSpecifier' - ) { - return; - } + ) { + return; + } - context.report({ - fix (fixer) { - const imports = node.specifiers.map((specifier) => { - if (specifier.type === 'ImportDefaultSpecifier') { - return 'type default as ' + specifier.local.name; - } else if (specifier.imported.name === specifier.local.name) { - return 'type ' + specifier.local.name; - } else { - return 'type ' + specifier.imported.name + ' as ' + specifier.local.name; - } - }); - const source = node.source.value; + context.report({ + fix (fixer) { + const imports = node.specifiers.map((specifier) => { + if (specifier.type === 'ImportDefaultSpecifier') { + return 'type default as ' + specifier.local.name; + } - return fixer.replaceText(node, 'import {' + imports.join(', ') + '} from \'' + source + '\';'); - }, - message: 'Unexpected "import type"', - node, - }); - }, - }; - } + if (specifier.imported.name === specifier.local.name) { + return 'type ' + specifier.local.name; + } + + return 'type ' + specifier.imported.name + ' as ' + specifier.local.name; + }); + const source = node.source.value; + + return fixer.replaceText(node, 'import {' + imports.join(', ') + '} from \'' + source + '\';'); + }, + message: 'Unexpected "import type"', + node, + }); + }, + }; }; export default { diff --git a/src/rules/useFlowType.js b/src/rules/useFlowType.js index 4c5f78cc..c92e6da8 100644 --- a/src/rules/useFlowType.js +++ b/src/rules/useFlowType.js @@ -4,6 +4,7 @@ const create = (context) => { const markTypeAsUsed = (node) => { context.markVariableAsUsed(node.id.name); }; + const markTypeAsUsedWithGenericType = (node) => { let typeId; let scope; diff --git a/src/rules/useReadOnlySpread.js b/src/rules/useReadOnlySpread.js index 6fbc1d97..49751082 100644 --- a/src/rules/useReadOnlySpread.js +++ b/src/rules/useReadOnlySpread.js @@ -27,6 +27,7 @@ const create = (context) => { hasSpread = true; } } + if (hasSpread === true && shouldThrow === true) { context.report({ messageId: 'readonlySpread', diff --git a/src/utilities/getParameterName.js b/src/utilities/getParameterName.js index 436dafc9..ba517af8 100644 --- a/src/utilities/getParameterName.js +++ b/src/utilities/getParameterName.js @@ -56,9 +56,9 @@ export default (identifierNode, context) => { const colonOrBrace = context.getSourceCode().getTokenAfter(id); if (colonOrBrace.value === ':') { return id.value; - } else { - return null; } + + return null; } if (identifierNode.type === 'FunctionTypeParam') { @@ -70,10 +70,11 @@ export default (identifierNode, context) => { if (identifierNode.typeAnnotation) { return text.replace(context.getSourceCode().getText(identifierNode.typeAnnotation), '').trim(); - } else { - return text; } + + return text; } + if (_.get(identifierNode, 'left.type') === 'ObjectPattern') { return context.getSourceCode().getText(identifierNode.left); } diff --git a/src/utilities/spacingFixers.js b/src/utilities/spacingFixers.js index f23d0985..bdb95c45 100644 --- a/src/utilities/spacingFixers.js +++ b/src/utilities/spacingFixers.js @@ -37,23 +37,23 @@ export const replaceWithSpaceAfter = (node, spaces) => { export const stripSpaces = (direction, node, spaces) => { if (direction === 'before') { return stripSpacesBefore(node, spaces); - } else { - return stripSpacesAfter(node, spaces); } + + return stripSpacesAfter(node, spaces); }; export const addSpace = (direction, node) => { if (direction === 'before') { return addSpaceBefore(node); - } else { - return addSpaceAfter(node); } + + return addSpaceAfter(node); }; export const replaceWithSpace = (direction, node, spaces) => { if (direction === 'before') { return replaceWithSpaceBefore(node, spaces); - } else { - return replaceWithSpaceAfter(node, spaces); } + + return replaceWithSpaceAfter(node, spaces); };