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

feat(deps): update to @typescript-eslint/* v8 #955

Merged
merged 1 commit into from
Nov 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions lib/create-testing-library-rule/detect-testing-library-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@ export type TestingLibrarySettings = {
};

export type TestingLibraryContext<
TOptions extends readonly unknown[],
TMessageIds extends string,
TOptions extends readonly unknown[],
> = Readonly<
TSESLint.RuleContext<TMessageIds, TOptions> & {
settings: TestingLibrarySettings;
}
>;

export type EnhancedRuleCreate<
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener,
TOptions extends readonly unknown[],
> = (
context: TestingLibraryContext<TOptions, TMessageIds>,
context: TestingLibraryContext<TMessageIds, TOptions>,
optionsWithDefault: Readonly<TOptions>,
detectionHelpers: Readonly<DetectionHelpers>
) => TRuleListener;
) => TSESLint.RuleListener;

// Helpers methods
type GetTestingLibraryImportNodeFn = () => ImportModuleNode | null;
Expand Down Expand Up @@ -156,15 +155,14 @@ export type DetectionOptions = {
* Enhances a given rule `create` with helpers to detect Testing Library utils.
*/
export function detectTestingLibraryUtils<
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener,
TOptions extends readonly unknown[],
>(
ruleCreate: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>,
ruleCreate: EnhancedRuleCreate<TMessageIds, TOptions>,
{ skipRuleReportingCheck = false }: Partial<DetectionOptions> = {}
) {
return (
context: TestingLibraryContext<TOptions, TMessageIds>,
context: TestingLibraryContext<TMessageIds, TOptions>,
optionsWithDefault: Readonly<TOptions>
): TSESLint.RuleListener => {
const importedTestingLibraryNodes: ImportModuleNode[] = [];
Expand Down Expand Up @@ -214,6 +212,7 @@ export function detectTestingLibraryUtils<

const originalNodeName =
isImportSpecifier(importedUtilSpecifier) &&
ASTUtils.isIdentifier(importedUtilSpecifier.imported) &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra check is needed because of changes from typescript-eslint/typescript-eslint#9972: imported can now be Identifier | StringLiteral, which was only Identifier before

importedUtilSpecifier.local.name !== importedUtilSpecifier.imported.name
? importedUtilSpecifier.imported.name
: undefined;
Expand Down
39 changes: 17 additions & 22 deletions lib/create-testing-library-rule/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ESLintUtils, TSESLint } from '@typescript-eslint/utils';
import { ESLintUtils } from '@typescript-eslint/utils';

import { getDocsUrl, TestingLibraryRuleMeta } from '../utils';
import { getDocsUrl, TestingLibraryPluginDocs } from '../utils';

import {
DetectionOptions,
Expand All @@ -11,32 +11,27 @@ import {
export const createTestingLibraryRule = <
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener,
>({
create,
detectionOptions = {},
meta,
...remainingConfig
}: Readonly<{
name: string;
meta: TestingLibraryRuleMeta<TMessageIds, TOptions>;
defaultOptions: Readonly<TOptions>;
detectionOptions?: Partial<DetectionOptions>;
create: EnhancedRuleCreate<TOptions, TMessageIds, TRuleListener>;
}>): TSESLint.RuleModule<TMessageIds, TOptions> =>
ESLintUtils.RuleCreator(getDocsUrl)({
}: Readonly<
Omit<
ESLintUtils.RuleWithMetaAndName<
TOptions,
TMessageIds,
TestingLibraryPluginDocs<TOptions>
>,
'create'
> & {
create: EnhancedRuleCreate<TMessageIds, TOptions>;
detectionOptions?: Partial<DetectionOptions>;
}
>) =>
ESLintUtils.RuleCreator<TestingLibraryPluginDocs<TOptions>>(getDocsUrl)({
...remainingConfig,
create: detectTestingLibraryUtils<TOptions, TMessageIds, TRuleListener>(
create: detectTestingLibraryUtils<TMessageIds, TOptions>(
create,
detectionOptions
),
meta: {
...meta,
docs: {
...meta.docs,
// We're using our own recommendedConfig meta to tell our build tools
// if the rule is recommended on a config basis
recommended: undefined,
},
},
});
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SupportedTestingFramework } from './utils';
const {
name: packageName,
version: packageVersion,
// eslint-disable-next-line @typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-require-imports
} = require('../package.json') as { name: string; version: string };

const plugin = {
Expand Down
1 change: 1 addition & 0 deletions lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ export function findImportSpecifier(
const namedExport = node.specifiers.find((n) => {
return (
isImportSpecifier(n) &&
ASTUtils.isIdentifier(n.imported) &&
[n.imported.name, n.local.name].includes(specifierName)
);
});
Expand Down
16 changes: 5 additions & 11 deletions lib/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';

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

import { importDefault, TestingLibraryRuleMeta } from '../utils';

type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
meta: TestingLibraryRuleMeta<string, unknown[]> & {
recommended: false;
};
};
import { importDefault, TestingLibraryPluginRuleModule } from '../utils';

const rulesDir = __dirname;
const excludedFiles = ['index'];

export default readdirSync(rulesDir)
.map((rule) => parse(rule).name)
.filter((ruleName) => !excludedFiles.includes(ruleName))
.reduce<Record<string, RuleModule>>(
.reduce<Record<string, TestingLibraryPluginRuleModule<string, unknown[]>>>(
(allRules, ruleName) => ({
...allRules,
[ruleName]: importDefault<RuleModule>(join(rulesDir, ruleName)),
[ruleName]: importDefault<
TestingLibraryPluginRuleModule<string, unknown[]>
>(join(rulesDir, ruleName)),
}),
{}
);
1 change: 1 addition & 0 deletions lib/rules/no-manual-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
const cleanupSpecifier = moduleNode.specifiers.find(
(specifier) =>
isImportSpecifier(specifier) &&
ASTUtils.isIdentifier(specifier.imported) &&
specifier.imported.name === 'cleanup'
);

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/file-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const interopRequireDefault = <T>(obj: any): { default: T } =>
obj?.__esModule ? obj : { default: obj };

export const importDefault = <T>(moduleName: string): T =>
// eslint-disable-next-line @typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-require-imports
interopRequireDefault<T>(require(moduleName)).default;
38 changes: 19 additions & 19 deletions lib/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { type TSESLint } from '@typescript-eslint/utils';
import { TSESLint } from '@typescript-eslint/utils';

type Recommended = 'error' | 'warn' | false;
type RecommendedConfig<TOptions extends readonly unknown[]> =
| Recommended
| [Recommended, ...TOptions];

// These 2 types are copied from `@typescript-eslint/utils`' `CreateRuleMeta`
// and modified to our needs
export type TestingLibraryRuleMetaDocs<TOptions extends readonly unknown[]> =
Omit<TSESLint.RuleMetaDataDocs<TOptions>, 'recommended' | 'url'> & {
/**
* The recommendation level for the rule on a framework basis.
* Used by the build tools to generate the framework config.
* Set to `false` to not include it the config
*/
recommendedConfig: Record<
SupportedTestingFramework,
RecommendedConfig<TOptions>
>;
};
export type TestingLibraryRuleMeta<
export type TestingLibraryPluginDocs<TOptions extends readonly unknown[]> = {
/**
* The recommendation level for the rule on a framework basis.
* Used by the build tools to generate the framework config.
* Set to `false` to not include it the config
*/
recommendedConfig: Record<
SupportedTestingFramework,
RecommendedConfig<TOptions>
>;
};

export type TestingLibraryPluginRuleModule<
TMessageIds extends string,
TOptions extends readonly unknown[],
> = Omit<TSESLint.RuleMetaData<TMessageIds, TOptions>, 'docs'> & {
docs: TestingLibraryRuleMetaDocs<TOptions>;
};
> = TSESLint.RuleModuleWithMetaDocs<
TMessageIds,
TOptions,
TestingLibraryPluginDocs<TOptions>
>;

export const SUPPORTED_TESTING_FRAMEWORKS = [
'dom',
Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//eslint-disable-next-line @typescript-eslint/no-var-requires
//eslint-disable-next-line @typescript-eslint/no-require-imports
const { ESLint } = require('eslint');

const removeIgnoredFiles = async (files) => {
Expand Down
Loading