Skip to content

Commit

Permalink
style: fix code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 14, 2021
1 parent 6f70f61 commit 09246b5
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 85 deletions.
4 changes: 2 additions & 2 deletions src/bin/addAssertions.js
Expand Up @@ -4,8 +4,8 @@
* @file This script is used to inline assertions into the README.md documents.
*/

import path from 'path';
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import _ from 'lodash';

Expand Down Expand Up @@ -43,7 +43,7 @@ const getAssertions = () => {
});

const assertionCodes = _.map(assertionFiles, (filePath) => {
// eslint-disable-next-line global-require, import/no-dynamic-require
// eslint-disable-next-line import/no-dynamic-require
const codes = require(filePath);

return {
Expand Down
1 change: 0 additions & 1 deletion src/bin/checkTests.js
Expand Up @@ -10,7 +10,6 @@ import {
const getTestIndexRules = () => {
const content = fs.readFileSync(path.resolve(__dirname, '../../tests/rules/index.js'), 'utf-8');

// eslint-disable-next-line unicorn/no-reduce
const result = content.split('\n').reduce((acc, line) => {
if (acc.inRulesArray) {
if (line === '];') {
Expand Down
17 changes: 9 additions & 8 deletions src/index.js
@@ -1,8 +1,8 @@
/* eslint-disable import/max-dependencies */
import _ from 'lodash';
import recommended from './configs/recommended.json';
import arrayStyleComplexType from './rules/arrayStyleComplexType';
import arrayStyleSimpleType from './rules/arrayStyleSimpleType';
import arrowParens from './rules/arrowParens';
import booleanStyle from './rules/booleanStyle';
import defineFlowType from './rules/defineFlowType';
import delimiterDangle from './rules/delimiterDangle';
Expand All @@ -13,20 +13,20 @@ import newlineAfterFlowAnnotation from './rules/newlineAfterFlowAnnotation';
import noDupeKeys from './rules/noDupeKeys';
import noExistentialType from './rules/noExistentialType';
import noFlowFixMeComments from './rules/noFlowFixMeComments';
import noInternalFlowType from './rules/noInternalFlowType';
import noMixed from './rules/noMixed';
import noMutableArray from './rules/noMutableArray';
import noPrimitiveConstructorTypes from './rules/noPrimitiveConstructorTypes';
import noTypesMissingFileAnnotation from './rules/noTypesMissingFileAnnotation';
import noUnusedExpressions from './rules/noUnusedExpressions';
import noWeakTypes from './rules/noWeakTypes';
import noInternalFlowType from './rules/noInternalFlowType';
import noMixed from './rules/noMixed';
import objectTypeCurlySpacing from './rules/objectTypeCurlySpacing';
import objectTypeDelimiter from './rules/objectTypeDelimiter';
import quotes from './rules/quotes';
import requireIndexerName from './rules/requireIndexerName';
import requireCompoundTypeAlias from './rules/requireCompoundTypeAlias';
import requireInexactType from './rules/requireInexactType';
import requireExactType from './rules/requireExactType';
import requireIndexerName from './rules/requireIndexerName';
import requireInexactType from './rules/requireInexactType';
import requireParameterType from './rules/requireParameterType';
import requireReadonlyReactProps from './rules/requireReadonlyReactProps';
import requireReturnType from './rules/requireReturnType';
Expand All @@ -39,15 +39,16 @@ import sortTypeUnionIntersectionMembers from './rules/sortTypeUnionIntersectionM
import spaceAfterTypeColon from './rules/spaceAfterTypeColon';
import spaceBeforeGenericBracket from './rules/spaceBeforeGenericBracket';
import spaceBeforeTypeColon from './rules/spaceBeforeTypeColon';
import spreadExactType from './rules/spreadExactType';
import typeIdMatch from './rules/typeIdMatch';
import typeImportStyle from './rules/typeImportStyle';
import unionIntersectionSpacing from './rules/unionIntersectionSpacing';
import useFlowType from './rules/useFlowType';
import useReadOnlySpread from './rules/useReadOnlySpread';
import validSyntax from './rules/validSyntax';
import spreadExactType from './rules/spreadExactType';
import arrowParens from './rules/arrowParens';
import {checkFlowFileAnnotation} from './utilities';
import {
checkFlowFileAnnotation,
} from './utilities';

const rules = {
'array-style-complex-type': arrayStyleComplexType,
Expand Down
49 changes: 24 additions & 25 deletions src/rules/arrayStyle/index.js
Expand Up @@ -46,32 +46,31 @@ export default (defaultConfig, simpleType) => {

// verbose
GenericTypeAnnotation (node) {
if (node.id.name === 'Array') {
// Don't report on un-parameterized Array annotations. There are valid cases for this,
// but regardless, we must NOT crash when encountering them.
if (node.typeParameters && node.typeParameters.params.length === 1) {
const elementTypeNode = node.typeParameters.params[0];
const rawElementType = context.getSourceCode().getText(elementTypeNode);
const inlinedType = inlineType(rawElementType);
const wrappedInlinedType = needWrap(elementTypeNode) ? '(' + inlinedType + ')' : inlinedType;
// Don't report on un-parameterized Array annotations. There are valid cases for this,
// but regardless, we must NOT crash when encountering them.
if (node.id.name === 'Array' &&
node.typeParameters && node.typeParameters.params.length === 1) {
const elementTypeNode = node.typeParameters.params[0];
const rawElementType = context.getSourceCode().getText(elementTypeNode);
const inlinedType = inlineType(rawElementType);
const wrappedInlinedType = needWrap(elementTypeNode) ? '(' + inlinedType + ')' : inlinedType;

if (isSimpleType(elementTypeNode) === simpleType && !verbose) {
context.report({
data: {
type: inlinedType,
wrappedType: wrappedInlinedType,
},
fix (fixer) {
if (needWrap(elementTypeNode)) {
return fixer.replaceText(node, '(' + rawElementType + ')[]');
} else {
return fixer.replaceText(node, rawElementType + '[]');
}
},
message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"',
node,
});
}
if (isSimpleType(elementTypeNode) === simpleType && !verbose) {
context.report({
data: {
type: inlinedType,
wrappedType: wrappedInlinedType,
},
fix (fixer) {
if (needWrap(elementTypeNode)) {
return fixer.replaceText(node, '(' + rawElementType + ')[]');
} else {
return fixer.replaceText(node, rawElementType + '[]');
}
},
message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"',
node,
});
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/rules/defineFlowType.js
Expand Up @@ -14,7 +14,7 @@ const create = (context) => {

if (ref.identifier.name === ident.name) {
// use "__defineGeneric" since we don't have a reference to "escope.Variable"
// eslint-disable-next-line no-underscore-dangle

globalScope.__defineGeneric(
ident.name,
globalScope.set,
Expand Down
4 changes: 3 additions & 1 deletion src/rules/genericSpacing.js
@@ -1,4 +1,6 @@
import {spacingFixers} from '../utilities';
import {
spacingFixers,
} from '../utilities';

const schema = [
{
Expand Down
4 changes: 3 additions & 1 deletion src/rules/noTypesMissingFileAnnotation.js
@@ -1,4 +1,6 @@
import {isFlowFile} from '../utilities';
import {
isFlowFile,
} from '../utilities';

/**
* Disallows the use for flow types without a valid file annotation.
Expand Down
4 changes: 3 additions & 1 deletion src/rules/objectTypeCurlySpacing.js
@@ -1,4 +1,6 @@
import {spacingFixers} from '../utilities';
import {
spacingFixers,
} from '../utilities';

const schema = [
{
Expand Down
18 changes: 8 additions & 10 deletions src/rules/objectTypeDelimiter.js
Expand Up @@ -37,16 +37,14 @@ const create = (context) => {
lastToken = parentTokens[parentTokens.indexOf(lastToken) + 1];
}

if (lastToken.type === 'Punctuator') {
if (lastToken.value === BAD.char) {
context.report({
fix (fixer) {
return fixer.replaceText(lastToken, GOOD.char);
},
message: 'Prefer ' + GOOD.name + 's to ' + BAD.name + 's in object and class types',
node: lastToken,
});
}
if (lastToken.type === 'Punctuator' && lastToken.value === BAD.char) {
context.report({
fix (fixer) {
return fixer.replaceText(lastToken, GOOD.char);
},
message: 'Prefer ' + GOOD.name + 's to ' + BAD.name + 's in object and class types',
node: lastToken,
});
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/rules/requireIndexerName.js
@@ -1,4 +1,6 @@
import {getParameterName} from '../utilities';
import {
getParameterName,
} from '../utilities';

const schema = [
{
Expand Down
4 changes: 3 additions & 1 deletion src/rules/spaceBeforeGenericBracket.js
@@ -1,4 +1,6 @@
import {spacingFixers} from '../utilities';
import {
spacingFixers,
} from '../utilities';

const schema = [
{
Expand Down
6 changes: 4 additions & 2 deletions src/rules/typeColonSpacing/evaluateFunctions.js
@@ -1,7 +1,9 @@
import _ from 'lodash';
import {iterateFunctionNodes} from '../../utilities';
import evaluateTypical from './evaluateTypical';
import {
iterateFunctionNodes,
} from '../../utilities';
import evaluateReturnType from './evaluateReturnType';
import evaluateTypical from './evaluateTypical';

export default iterateFunctionNodes((context, report) => {
const checkParam = evaluateTypical(context, report, 'parameter');
Expand Down
4 changes: 3 additions & 1 deletion src/rules/typeColonSpacing/evaluateObjectTypeIndexer.js
@@ -1,4 +1,6 @@
import {getTokenAfterParens, getTokenBeforeParens} from '../../utilities';
import {
getTokenAfterParens, getTokenBeforeParens,
} from '../../utilities';

export default (context, report) => {
const sourceCode = context.getSourceCode();
Expand Down
5 changes: 3 additions & 2 deletions src/rules/typeColonSpacing/evaluateObjectTypeProperty.js
@@ -1,7 +1,8 @@
import {getParameterName, quoteName} from '../../utilities';
import {
getParameterName, quoteName,
} from '../../utilities';

const getColon = (context, objectTypeProperty) => {
// eslint-disable-next-line init-declarations
let tokenIndex = 1;

if (objectTypeProperty.optional) {
Expand Down
4 changes: 3 additions & 1 deletion src/rules/typeColonSpacing/evaluateTypical.js
@@ -1,5 +1,7 @@
import _ from 'lodash';
import {getParameterName, quoteName} from '../../utilities';
import {
getParameterName, quoteName,
} from '../../utilities';

export default (context, report, typeForMessage) => {
const sourceCode = context.getSourceCode();
Expand Down
4 changes: 3 additions & 1 deletion src/rules/typeColonSpacing/evaluateVariables.js
@@ -1,5 +1,7 @@
import _ from 'lodash';
import {getParameterName, quoteName} from '../../utilities';
import {
getParameterName, quoteName,
} from '../../utilities';

export default (context, report) => {
const sourceCode = context.getSourceCode();
Expand Down
4 changes: 2 additions & 2 deletions src/rules/typeColonSpacing/index.js
@@ -1,10 +1,10 @@
import reporter from './reporter';
import evaluateFunctions from './evaluateFunctions';
import evaluateObjectTypeIndexer from './evaluateObjectTypeIndexer';
import evaluateObjectTypeProperty from './evaluateObjectTypeProperty';
import evaluateTypeCastExpression from './evaluateTypeCastExpression';
import evaluateTypical from './evaluateTypical';
import evaluateFunctions from './evaluateFunctions';
import evaluateVariables from './evaluateVariables';
import reporter from './reporter';

export default (direction, context, options) => {
const report = reporter(direction, context, options);
Expand Down
4 changes: 3 additions & 1 deletion src/rules/typeColonSpacing/reporter.js
@@ -1,4 +1,6 @@
import {spacingFixers} from '../../utilities';
import {
spacingFixers,
} from '../../utilities';

const hasLineBreak = (direction, colon, context) => {
const sourceCode = context.getSourceCode();
Expand Down
4 changes: 3 additions & 1 deletion src/rules/unionIntersectionSpacing.js
@@ -1,4 +1,6 @@
import {spacingFixers, getTokenAfterParens} from '../utilities';
import {
spacingFixers, getTokenAfterParens,
} from '../utilities';

const schema = [
{
Expand Down
40 changes: 30 additions & 10 deletions src/utilities/index.js
Expand Up @@ -3,16 +3,36 @@
// eslint-disable-next-line import/no-namespace
import * as spacingFixers from './spacingFixers';

export {default as checkFlowFileAnnotation} from './checkFlowFileAnnotation';
export {default as fuzzyStringMatch} from './fuzzyStringMatch';
export {default as getParameterName} from './getParameterName';
export {default as getTokenAfterParens} from './getTokenAfterParens';
export {default as getTokenBeforeParens} from './getTokenBeforeParens';
export {default as isFlowFile} from './isFlowFile';
export {default as isNoFlowFile} from './isNoFlowFile';
export {default as isFlowFileAnnotation} from './isFlowFileAnnotation';
export {default as iterateFunctionNodes} from './iterateFunctionNodes';
export {default as quoteName} from './quoteName';
export {
default as checkFlowFileAnnotation,
} from './checkFlowFileAnnotation';
export {
default as fuzzyStringMatch,
} from './fuzzyStringMatch';
export {
default as getParameterName,
} from './getParameterName';
export {
default as getTokenAfterParens,
} from './getTokenAfterParens';
export {
default as getTokenBeforeParens,
} from './getTokenBeforeParens';
export {
default as isFlowFile,
} from './isFlowFile';
export {
default as isNoFlowFile,
} from './isNoFlowFile';
export {
default as isFlowFileAnnotation,
} from './isFlowFileAnnotation';
export {
default as iterateFunctionNodes,
} from './iterateFunctionNodes';
export {
default as quoteName,
} from './quoteName';

export {
spacingFixers,
Expand Down
7 changes: 2 additions & 5 deletions tests/rules/assertions/arrowParens.js
@@ -1,4 +1,3 @@
/* eslint-disable sort-keys */
const type = 'ArrowFunctionExpression';

export default {
Expand Down Expand Up @@ -285,11 +284,9 @@ export default {
options: ['as-needed'],
parserOptions: {ecmaVersion: 8}},
{code: '(a: T) => a',
options: ['as-needed'],
},
options: ['as-needed']},
{code: '(a): T => a',
options: ['as-needed'],
},
options: ['as-needed']},

// "as-needed", { "requireForBlockBody": true }
{code: '() => {}',
Expand Down
1 change: 0 additions & 1 deletion tests/rules/assertions/defineFlowType.js
Expand Up @@ -131,7 +131,6 @@ const VALID_WITH_DEFINE_FLOW_TYPE = [

// This tests to ensure we have a robust handling of @flow comments
{
// eslint-disable-next-line no-restricted-syntax
code: `
/**
* Copyright 2019 no corp
Expand Down
2 changes: 0 additions & 2 deletions tests/rules/assertions/sortKeys.js
Expand Up @@ -40,7 +40,6 @@ export default {
errors: [{message: 'Expected type annotations to be in ascending order. "b" must be before "c".'}],
output: 'type FooType = { a: number, b: string, c: number }',
},
/* eslint-disable no-restricted-syntax */
{
code: `
type FooType = {
Expand Down Expand Up @@ -438,7 +437,6 @@ export default {
|}
`,
},
/* eslint-enable no-restricted-syntax */

// https://github.com/gajus/eslint-plugin-flowtype/issues/455
{
Expand Down

0 comments on commit 09246b5

Please sign in to comment.