Skip to content

Commit 5f734ea

Browse files
authored
chore: migrate ESLint config (#385)
* chore: replace ESLint standard config by kentcdodds * chore: auto fix new errors * chore: fix new errors reported * chore: forbid imports from @typescript-eslint/experimental-utils/dist * chore: fix last errors related to imports * chore: update prettier * chore: bump eslint plugins * chore: fix prettier version * chore: bump eslint and eslint-config-kentcdodds * chore: remove custom path groups for import/order rule * chore: switch import/no-import-module-exports rule off This rule is causing an ESLint error which prevents the rest of the project to be linted. * chore: update @babel/new-cap rule reference * chore: format files * refactor: update block to avoid unnecessary condition * refactor: get necessary condition back Closes #384
1 parent 3331e72 commit 5f734ea

File tree

65 files changed

+430
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+430
-340
lines changed

.eslintrc.json

+30-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"jest/globals": true
77
},
88
"extends": [
9+
"kentcdodds",
910
"plugin:@typescript-eslint/eslint-recommended",
1011
"plugin:@typescript-eslint/recommended",
1112
"prettier",
@@ -22,8 +23,35 @@
2223
"project": "./tsconfig.eslint.json"
2324
},
2425
"rules": {
25-
"no-var": "error",
26+
// TS
2627
"@typescript-eslint/explicit-function-return-type": "off",
27-
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
28+
"@typescript-eslint/no-unused-vars": [
29+
"warn",
30+
{ "argsIgnorePattern": "^_" }
31+
],
32+
"@typescript-eslint/no-use-before-define": "off",
33+
34+
// ESLint
35+
"max-lines-per-function": "off",
36+
"no-restricted-imports": [
37+
"error",
38+
{
39+
"patterns": ["@typescript-eslint/experimental-utils/dist/*"]
40+
}
41+
],
42+
43+
// Import
44+
"import/no-import-module-exports": "off",
45+
"import/order": [
46+
"warn",
47+
{
48+
"groups": ["builtin", "external", "parent", "sibling", "index"],
49+
"newlines-between": "always",
50+
"alphabetize": {
51+
"order": "asc",
52+
"caseInsensitive": false
53+
}
54+
}
55+
]
2856
}
2957
}

lib/configs/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { join } from 'path';
22

3-
import type { TSESLint } from '@typescript-eslint/experimental-utils';
4-
53
import {
64
importDefault,
75
SUPPORTED_TESTING_FRAMEWORKS,
86
SupportedTestingFramework,
97
} from '../utils';
108

9+
import type { TSESLint } from '@typescript-eslint/experimental-utils';
10+
1111
export type LinterConfigRules = Record<string, TSESLint.Linter.RuleEntry>;
1212

1313
const configsDir = __dirname;

lib/create-testing-library-rule/detect-testing-library-utils.ts

+55-52
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
8989
type CanReportErrorsFn = () => boolean;
9090
type FindImportedTestingLibraryUtilSpecifierFn = (
9191
specifierName: string
92-
) => TSESTree.ImportClause | TSESTree.Identifier | undefined;
92+
) => TSESTree.Identifier | TSESTree.ImportClause | undefined;
9393
type IsNodeComingFromTestingLibraryFn = (
94-
node: TSESTree.MemberExpression | TSESTree.Identifier
94+
node: TSESTree.Identifier | TSESTree.MemberExpression
9595
) => boolean;
9696

9797
export interface DetectionHelpers {
@@ -181,7 +181,7 @@ export function detectTestingLibraryUtils<
181181
* reporting)
182182
*/
183183
function isPotentialTestingLibraryFunction(
184-
node: TSESTree.Identifier,
184+
node: TSESTree.Identifier | null | undefined,
185185
isPotentialFunctionCallback: (
186186
identifierNodeName: string,
187187
originalNodeName?: string
@@ -472,9 +472,8 @@ export function detectTestingLibraryUtils<
472472
* Determines whether a given node is fireEvent method or not
473473
*/
474474
const isFireEventMethod: IsFireEventMethodFn = (node) => {
475-
const fireEventUtil = findImportedTestingLibraryUtilSpecifier(
476-
FIRE_EVENT_NAME
477-
);
475+
const fireEventUtil =
476+
findImportedTestingLibraryUtilSpecifier(FIRE_EVENT_NAME);
478477
let fireEventUtilName: string | undefined;
479478

480479
if (fireEventUtil) {
@@ -655,9 +654,8 @@ export function detectTestingLibraryUtils<
655654
return false;
656655
}
657656
const referenceNode = getReferenceNode(node);
658-
const referenceNodeIdentifier = getPropertyIdentifierNode(
659-
referenceNode
660-
);
657+
const referenceNodeIdentifier =
658+
getPropertyIdentifierNode(referenceNode);
661659
if (!referenceNodeIdentifier) {
662660
return false;
663661
}
@@ -759,54 +757,58 @@ export function detectTestingLibraryUtils<
759757
/**
760758
* Finds the import util specifier related to Testing Library for a given name.
761759
*/
762-
const findImportedTestingLibraryUtilSpecifier: FindImportedTestingLibraryUtilSpecifierFn = (
763-
specifierName
764-
): TSESTree.ImportClause | TSESTree.Identifier | undefined => {
765-
const node = getCustomModuleImportNode() ?? getTestingLibraryImportNode();
760+
const findImportedTestingLibraryUtilSpecifier: FindImportedTestingLibraryUtilSpecifierFn =
761+
(
762+
specifierName
763+
): TSESTree.Identifier | TSESTree.ImportClause | undefined => {
764+
const node =
765+
getCustomModuleImportNode() ?? getTestingLibraryImportNode();
766+
767+
if (!node) {
768+
return undefined;
769+
}
766770

767-
if (!node) {
768-
return undefined;
769-
}
771+
return findImportSpecifier(specifierName, node);
772+
};
770773

771-
return findImportSpecifier(specifierName, node);
772-
};
774+
const findImportedUserEventSpecifier: () => TSESTree.Identifier | null =
775+
() => {
776+
if (!importedUserEventLibraryNode) {
777+
return null;
778+
}
773779

774-
const findImportedUserEventSpecifier: () => TSESTree.Identifier | null = () => {
775-
if (!importedUserEventLibraryNode) {
776-
return null;
777-
}
780+
if (isImportDeclaration(importedUserEventLibraryNode)) {
781+
const userEventIdentifier =
782+
importedUserEventLibraryNode.specifiers.find((specifier) =>
783+
isImportDefaultSpecifier(specifier)
784+
);
778785

779-
if (isImportDeclaration(importedUserEventLibraryNode)) {
780-
const userEventIdentifier = importedUserEventLibraryNode.specifiers.find(
781-
(specifier) => isImportDefaultSpecifier(specifier)
782-
);
786+
if (userEventIdentifier) {
787+
return userEventIdentifier.local;
788+
}
789+
} else {
790+
if (
791+
!ASTUtils.isVariableDeclarator(importedUserEventLibraryNode.parent)
792+
) {
793+
return null;
794+
}
783795

784-
if (userEventIdentifier) {
785-
return userEventIdentifier.local;
786-
}
787-
} else {
788-
if (
789-
!ASTUtils.isVariableDeclarator(importedUserEventLibraryNode.parent)
790-
) {
791-
return null;
792-
}
796+
const requireNode = importedUserEventLibraryNode.parent;
797+
if (!ASTUtils.isIdentifier(requireNode.id)) {
798+
return null;
799+
}
793800

794-
const requireNode = importedUserEventLibraryNode.parent;
795-
if (!ASTUtils.isIdentifier(requireNode.id)) {
796-
return null;
801+
return requireNode.id;
797802
}
798803

799-
return requireNode.id;
800-
}
801-
802-
return null;
803-
};
804+
return null;
805+
};
804806

805807
const getTestingLibraryImportedUtilSpecifier = (
806-
node: TSESTree.MemberExpression | TSESTree.Identifier
807-
): TSESTree.ImportClause | TSESTree.Identifier | undefined => {
808-
const identifierName: string | undefined = getPropertyIdentifierNode(node)
809-
?.name;
808+
node: TSESTree.Identifier | TSESTree.MemberExpression
809+
): TSESTree.Identifier | TSESTree.ImportClause | undefined => {
810+
const identifierName: string | undefined =
811+
getPropertyIdentifierNode(node)?.name;
810812

811813
if (!identifierName) {
812814
return undefined;
@@ -848,9 +850,8 @@ export function detectTestingLibraryUtils<
848850
return importNode.parent;
849851
}
850852

851-
const variableDeclarator = findClosestVariableDeclaratorNode(
852-
importNode
853-
);
853+
const variableDeclarator =
854+
findClosestVariableDeclaratorNode(importNode);
854855

855856
if (isCallExpression(variableDeclarator?.init)) {
856857
return variableDeclarator?.init;
@@ -868,8 +869,8 @@ export function detectTestingLibraryUtils<
868869
return false;
869870
}
870871

871-
const identifierName: string | undefined = getPropertyIdentifierNode(node)
872-
?.name;
872+
const identifierName: string | undefined =
873+
getPropertyIdentifierNode(node)?.name;
873874

874875
if (!identifierName) {
875876
return false;
@@ -1045,6 +1046,8 @@ export function detectTestingLibraryUtils<
10451046
if (canReportErrors() && ruleInstructions[instruction]) {
10461047
return ruleInstructions[instruction]?.(node);
10471048
}
1049+
1050+
return undefined;
10481051
};
10491052
});
10501053

lib/create-testing-library-rule/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function createTestingLibraryRule<
2424
detectionOptions?: Partial<DetectionOptions>;
2525
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
2626
}>): TSESLint.RuleModule<TMessageIds, TOptions> {
27+
// eslint-disable-next-line @babel/new-cap
2728
return ESLintUtils.RuleCreator(getDocsUrl)({
2829
...remainingConfig,
2930
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>(

0 commit comments

Comments
 (0)