Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate codebase ESLint config #385

Merged
merged 15 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions .eslintrc.json
Expand Up @@ -6,6 +6,7 @@
"jest/globals": true
},
"extends": [
"kentcdodds",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
Expand All @@ -22,8 +23,35 @@
"project": "./tsconfig.eslint.json"
},
"rules": {
"no-var": "error",
// TS
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
"@typescript-eslint/no-unused-vars": [
"warn",
{ "argsIgnorePattern": "^_" }
],
"@typescript-eslint/no-use-before-define": "off",

// ESLint
"max-lines-per-function": "off",
"no-restricted-imports": [
"error",
{
"patterns": ["@typescript-eslint/experimental-utils/dist/*"]
}
],

// Import
"import/no-import-module-exports": "off",
"import/order": [
"warn",
{
"groups": ["builtin", "external", "parent", "sibling", "index"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": false
}
}
]
}
}
4 changes: 2 additions & 2 deletions lib/configs/index.ts
@@ -1,13 +1,13 @@
import { join } from 'path';

import type { TSESLint } from '@typescript-eslint/experimental-utils';

import {
importDefault,
SUPPORTED_TESTING_FRAMEWORKS,
SupportedTestingFramework,
} from '../utils';

import type { TSESLint } from '@typescript-eslint/experimental-utils';

export type LinterConfigRules = Record<string, TSESLint.Linter.RuleEntry>;

const configsDir = __dirname;
Expand Down
107 changes: 55 additions & 52 deletions lib/create-testing-library-rule/detect-testing-library-utils.ts
Expand Up @@ -89,9 +89,9 @@ type IsAbsenceAssertFn = (node: TSESTree.MemberExpression) => boolean;
type CanReportErrorsFn = () => boolean;
type FindImportedTestingLibraryUtilSpecifierFn = (
specifierName: string
) => TSESTree.ImportClause | TSESTree.Identifier | undefined;
) => TSESTree.Identifier | TSESTree.ImportClause | undefined;
type IsNodeComingFromTestingLibraryFn = (
node: TSESTree.MemberExpression | TSESTree.Identifier
node: TSESTree.Identifier | TSESTree.MemberExpression
) => boolean;

export interface DetectionHelpers {
Expand Down Expand Up @@ -181,7 +181,7 @@ export function detectTestingLibraryUtils<
* reporting)
*/
function isPotentialTestingLibraryFunction(
node: TSESTree.Identifier,
node: TSESTree.Identifier | null | undefined,
isPotentialFunctionCallback: (
identifierNodeName: string,
originalNodeName?: string
Expand Down Expand Up @@ -472,9 +472,8 @@ export function detectTestingLibraryUtils<
* Determines whether a given node is fireEvent method or not
*/
const isFireEventMethod: IsFireEventMethodFn = (node) => {
const fireEventUtil = findImportedTestingLibraryUtilSpecifier(
FIRE_EVENT_NAME
);
const fireEventUtil =
findImportedTestingLibraryUtilSpecifier(FIRE_EVENT_NAME);
let fireEventUtilName: string | undefined;

if (fireEventUtil) {
Expand Down Expand Up @@ -655,9 +654,8 @@ export function detectTestingLibraryUtils<
return false;
}
const referenceNode = getReferenceNode(node);
const referenceNodeIdentifier = getPropertyIdentifierNode(
referenceNode
);
const referenceNodeIdentifier =
getPropertyIdentifierNode(referenceNode);
if (!referenceNodeIdentifier) {
return false;
}
Expand Down Expand Up @@ -759,54 +757,58 @@ export function detectTestingLibraryUtils<
/**
* Finds the import util specifier related to Testing Library for a given name.
*/
const findImportedTestingLibraryUtilSpecifier: FindImportedTestingLibraryUtilSpecifierFn = (
specifierName
): TSESTree.ImportClause | TSESTree.Identifier | undefined => {
const node = getCustomModuleImportNode() ?? getTestingLibraryImportNode();
const findImportedTestingLibraryUtilSpecifier: FindImportedTestingLibraryUtilSpecifierFn =
(
specifierName
): TSESTree.Identifier | TSESTree.ImportClause | undefined => {
const node =
getCustomModuleImportNode() ?? getTestingLibraryImportNode();

if (!node) {
return undefined;
}

if (!node) {
return undefined;
}
return findImportSpecifier(specifierName, node);
};

return findImportSpecifier(specifierName, node);
};
const findImportedUserEventSpecifier: () => TSESTree.Identifier | null =
() => {
if (!importedUserEventLibraryNode) {
return null;
}

const findImportedUserEventSpecifier: () => TSESTree.Identifier | null = () => {
if (!importedUserEventLibraryNode) {
return null;
}
if (isImportDeclaration(importedUserEventLibraryNode)) {
const userEventIdentifier =
importedUserEventLibraryNode.specifiers.find((specifier) =>
isImportDefaultSpecifier(specifier)
);

if (isImportDeclaration(importedUserEventLibraryNode)) {
const userEventIdentifier = importedUserEventLibraryNode.specifiers.find(
(specifier) => isImportDefaultSpecifier(specifier)
);
if (userEventIdentifier) {
return userEventIdentifier.local;
}
} else {
if (
!ASTUtils.isVariableDeclarator(importedUserEventLibraryNode.parent)
) {
return null;
}

if (userEventIdentifier) {
return userEventIdentifier.local;
}
} else {
if (
!ASTUtils.isVariableDeclarator(importedUserEventLibraryNode.parent)
) {
return null;
}
const requireNode = importedUserEventLibraryNode.parent;
if (!ASTUtils.isIdentifier(requireNode.id)) {
return null;
}

const requireNode = importedUserEventLibraryNode.parent;
if (!ASTUtils.isIdentifier(requireNode.id)) {
return null;
return requireNode.id;
}

return requireNode.id;
}

return null;
};
return null;
};

const getTestingLibraryImportedUtilSpecifier = (
node: TSESTree.MemberExpression | TSESTree.Identifier
): TSESTree.ImportClause | TSESTree.Identifier | undefined => {
const identifierName: string | undefined = getPropertyIdentifierNode(node)
?.name;
node: TSESTree.Identifier | TSESTree.MemberExpression
): TSESTree.Identifier | TSESTree.ImportClause | undefined => {
const identifierName: string | undefined =
getPropertyIdentifierNode(node)?.name;

if (!identifierName) {
return undefined;
Expand Down Expand Up @@ -848,9 +850,8 @@ export function detectTestingLibraryUtils<
return importNode.parent;
}

const variableDeclarator = findClosestVariableDeclaratorNode(
importNode
);
const variableDeclarator =
findClosestVariableDeclaratorNode(importNode);

if (isCallExpression(variableDeclarator?.init)) {
return variableDeclarator?.init;
Expand All @@ -868,8 +869,8 @@ export function detectTestingLibraryUtils<
return false;
}

const identifierName: string | undefined = getPropertyIdentifierNode(node)
?.name;
const identifierName: string | undefined =
getPropertyIdentifierNode(node)?.name;

if (!identifierName) {
return false;
Expand Down Expand Up @@ -1045,6 +1046,8 @@ export function detectTestingLibraryUtils<
if (canReportErrors() && ruleInstructions[instruction]) {
return ruleInstructions[instruction]?.(node);
}

return undefined;
};
});

Expand Down
1 change: 1 addition & 0 deletions lib/create-testing-library-rule/index.ts
Expand Up @@ -24,6 +24,7 @@ export function createTestingLibraryRule<
detectionOptions?: Partial<DetectionOptions>;
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
}>): TSESLint.RuleModule<TMessageIds, TOptions> {
// eslint-disable-next-line @babel/new-cap
return ESLintUtils.RuleCreator(getDocsUrl)({
...remainingConfig,
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>(
Expand Down