Skip to content

Commit 0c5f2d9

Browse files
committed
refactor: rewrite run and createRule
1 parent 8b53954 commit 0c5f2d9

Some content is hidden

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

49 files changed

+278
-202
lines changed

patches/@typescript-eslint+utils+7.4.0.patch

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
diff --git a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
2-
index 5ae7288..3d14a45 100644
3-
--- a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
4-
+++ b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
5-
@@ -11,6 +11,10 @@ export interface RuleRecommendationAcrossConfigs<Options extends readonly unknow
6-
strict: Partial<Options>;
7-
}
8-
export interface RuleMetaDataDocs<Options extends readonly unknown[]> {
9-
+ /**
10-
+ * The category the rule falls under
11-
+ */
12-
+ category?: string;
13-
/**
14-
* Concise description of the rule
15-
*/
16-
@@ -20,7 +24,7 @@ export interface RuleMetaDataDocs<Options extends readonly unknown[]> {
17-
* Used by the build tools to generate the recommended and strict configs.
18-
* Exclude to not include it as a recommendation.
19-
*/
20-
- recommended?: RuleRecommendation | RuleRecommendationAcrossConfigs<Options>;
21-
+ recommended?: RuleRecommendation | RuleRecommendationAcrossConfigs<Options> | boolean;
22-
/**
23-
* The URL of the rule's docs
24-
*/
251
diff --git a/node_modules/@typescript-eslint/utils/dist/ts-eslint/RuleTester.d.ts b/node_modules/@typescript-eslint/utils/dist/ts-eslint/RuleTester.d.ts
262
index 38d1f12..bf897a3 100644
273
--- a/node_modules/@typescript-eslint/utils/dist/ts-eslint/RuleTester.d.ts

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { TSESLint } from '@typescript-eslint/utils'
2-
31
// rules
42
import electron from './config/electron'
53
import errors from './config/errors'
@@ -56,6 +54,7 @@ import preferDefaultExport from './rules/prefer-default-export'
5654
import unambiguous from './rules/unambiguous'
5755
// configs
5856
import type { PluginConfig } from './types'
57+
import type { RuleModuleWithCustomMeta } from './utils'
5958

6059
const configs = {
6160
recommended,
@@ -127,7 +126,7 @@ const rules = {
127126

128127
// deprecated aliases to rules
129128
'imports-first': importsFirst,
130-
} satisfies Record<string, TSESLint.RuleModule<string, readonly unknown[]>>
129+
} satisfies Record<string, RuleModuleWithCustomMeta<string, readonly unknown[]>>
131130

132131
export = {
133132
configs,

src/rules/imports-first.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { ESLintUtils } from '@typescript-eslint/utils'
2-
3-
import { docsUrl } from '../utils/docs-url'
1+
import { createRule } from '../utils'
42

53
import first from './first'
64

7-
const createRule = ESLintUtils.RuleCreator(ruleName =>
8-
docsUrl(ruleName, '7b25c1cb95ee18acc1531002fd343e1e6031f9ed'),
9-
)
10-
115
export = createRule({
126
...first,
7+
commitHash: '7b25c1cb95ee18acc1531002fd343e1e6031f9ed',
138
name: 'imports-first',
149
meta: {
1510
...first.meta,

src/utils/create-rule.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
1+
import type { TSESLint } from '@typescript-eslint/utils'
12
import { ESLintUtils } from '@typescript-eslint/utils'
23

34
import { docsUrl } from './docs-url'
45

5-
export const createRule = ESLintUtils.RuleCreator(docsUrl)
6+
// TSESLint.RuleMetaDataDocs, but with "category" property for eslint-doc-generator
7+
type MetaDataDocsWithCategory<Options extends readonly unknown[]> = {
8+
/**
9+
* The category the rule falls under
10+
*/
11+
category?: string
12+
13+
recommended?:
14+
| TSESLint.RuleRecommendation
15+
| TSESLint.RuleRecommendationAcrossConfigs<Options>
16+
| boolean
17+
} & Omit<TSESLint.RuleMetaDataDocs<Options>, 'recommended'>
18+
19+
// TSESLint.RuleMetaData, but with "docs" property overriden for eslint-doc-generator
20+
type MetadataWithCustomDocs<
21+
MessageIDs extends string,
22+
Options extends readonly unknown[],
23+
> = {
24+
docs: MetaDataDocsWithCategory<Options>
25+
} & Omit<TSESLint.RuleMetaData<MessageIDs, Options>, 'docs'>
26+
27+
// TSESLint.RuleModule, but with "meta" property overriden for eslint-doc-generator
28+
export type RuleModuleWithCustomMeta<
29+
MessageIds extends string,
30+
Options extends readonly unknown[] = [],
31+
ExtendedRuleListener extends TSESLint.RuleListener = TSESLint.RuleListener,
32+
> = {
33+
meta: MetadataWithCustomDocs<MessageIds, Options>
34+
} & Omit<TSESLint.RuleModule<MessageIds, Options, ExtendedRuleListener>, 'meta'>
35+
36+
type RuleCreateOption<
37+
Options extends readonly unknown[],
38+
MessageIds extends string,
39+
> = {
40+
meta: MetadataWithCustomDocs<MessageIds, Options>
41+
commitHash?: string
42+
} & Omit<ESLintUtils.RuleWithMetaAndName<Options, MessageIds>, 'meta'>
43+
44+
export function createRule<
45+
Options extends readonly unknown[],
46+
MessageIds extends string,
47+
>({
48+
name,
49+
meta,
50+
commitHash,
51+
...rules
52+
}: Readonly<RuleCreateOption<Options, MessageIds>>): RuleModuleWithCustomMeta<
53+
MessageIds,
54+
Options
55+
> {
56+
const { docs, ...metaWithoutDocs } = meta
57+
return {
58+
...ESLintUtils.RuleCreator.withoutDocs({
59+
meta: metaWithoutDocs,
60+
...rules,
61+
}),
62+
meta: {
63+
...meta,
64+
docs: {
65+
...docs,
66+
url: docsUrl(name, commitHash),
67+
},
68+
},
69+
}
70+
}

test/rules/consistent-type-specifier-style.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSESLint, TSESTree } from '@typescript-eslint/utils'
22

3-
import { parsers, test } from '../utils'
3+
import { parsers, test, wrapRun } from '../utils'
44

55
import rule from 'eslint-plugin-import-x/rules/consistent-type-specifier-style'
66

@@ -452,7 +452,7 @@ describe('TypeScript', () => {
452452
sourceType: 'module',
453453
},
454454
})
455-
ruleTester.run('consistent-type-specifier-style', rule, {
455+
wrapRun(ruleTester.run)('consistent-type-specifier-style', rule, {
456456
valid: [...COMMON_TESTS.valid, ...TS_ONLY.valid],
457457
invalid: [...COMMON_TESTS.invalid, ...TS_ONLY.invalid],
458458
})
@@ -466,7 +466,7 @@ describe('Babel/Flow', () => {
466466
sourceType: 'module',
467467
},
468468
})
469-
ruleTester.run('consistent-type-specifier-style', rule, {
469+
wrapRun(ruleTester.run)('consistent-type-specifier-style', rule, {
470470
valid: [...COMMON_TESTS.valid, ...FLOW_ONLY.valid],
471471
invalid: [...COMMON_TESTS.invalid, ...FLOW_ONLY.invalid],
472472
})

test/rules/default.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import path from 'node:path'
22

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

5-
import { test, SYNTAX_CASES, parsers } from '../utils'
5+
import { test, SYNTAX_CASES, parsers, wrapRun } from '../utils'
66

77
import rule from 'eslint-plugin-import-x/rules/default'
88
import { CASE_SENSITIVE_FS } from 'eslint-plugin-import-x/utils'
99

1010
const ruleTester = new TSESLint.RuleTester()
1111

12-
ruleTester.run('default', rule, {
12+
wrapRun(ruleTester.run)('default', rule, {
1313
valid: [
1414
test({ code: 'import "./malformed.js"' }),
1515

@@ -161,7 +161,7 @@ ruleTester.run('default', rule, {
161161

162162
// #311: import of mismatched case
163163
if (!CASE_SENSITIVE_FS) {
164-
ruleTester.run('default (path case-insensitivity)', rule, {
164+
wrapRun(ruleTester.run)('default (path case-insensitivity)', rule, {
165165
valid: [
166166
test({
167167
code: 'import foo from "./jsx/MyUncoolComponent.jsx"',
@@ -180,7 +180,7 @@ if (!CASE_SENSITIVE_FS) {
180180

181181
describe('TypeScript', () => {
182182
const parser = parsers.TS
183-
ruleTester.run(`default`, rule, {
183+
wrapRun(ruleTester.run)(`default`, rule, {
184184
valid: [
185185
test({
186186
code: `import foobar from "./typescript-default"`,

test/rules/dynamic-import-chunkname.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TSESLint, TSESTree } from '@typescript-eslint/utils'
22

3-
import { SYNTAX_CASES, parsers } from '../utils'
3+
import { SYNTAX_CASES, parsers, wrapRun } from '../utils'
44

55
import rule from 'eslint-plugin-import-x/rules/dynamic-import-chunkname'
66

@@ -43,7 +43,7 @@ const pickyChunkNameFormatError = {
4343
},
4444
} as const
4545

46-
ruleTester.run('dynamic-import-chunkname', rule, {
46+
wrapRun(ruleTester.run)('dynamic-import-chunkname', rule, {
4747
valid: [
4848
{
4949
code: `dynamicImport(
@@ -971,7 +971,7 @@ describe('TypeScript', () => {
971971
const typescriptParser = parsers.TS
972972
const nodeType = TSESTree.AST_NODE_TYPES.ImportExpression
973973

974-
ruleTester.run('dynamic-import-chunkname', rule, {
974+
wrapRun(ruleTester.run)('dynamic-import-chunkname', rule, {
975975
valid: [
976976
{
977977
code: `import('test')`,

test/rules/export.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TSESLint } from '@typescript-eslint/utils'
22

3-
import { test, testFilePath, SYNTAX_CASES, parsers } from '../utils'
3+
import { test, testFilePath, SYNTAX_CASES, parsers, wrapRun } from '../utils'
44

55
import rule from 'eslint-plugin-import-x/rules/export'
66

77
const ruleTester = new TSESLint.RuleTester()
88

9-
ruleTester.run('export', rule, {
9+
wrapRun(ruleTester.run)('export', rule, {
1010
valid: [
1111
test({ code: 'import "./malformed.js"' }),
1212

@@ -166,7 +166,7 @@ describe('TypeScript', () => {
166166
},
167167
}
168168

169-
ruleTester.run('export', rule, {
169+
wrapRun(ruleTester.run)('export', rule, {
170170
valid: [
171171
// type/value name clash
172172
test({

test/rules/exports-last.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TSESLint } from '@typescript-eslint/utils'
22
import type { TSESTree } from '@typescript-eslint/utils'
33

4-
import { test } from '../utils'
4+
import { test, wrapRun } from '../utils'
55

66
import rule from 'eslint-plugin-import-x/rules/exports-last'
77

@@ -13,7 +13,7 @@ const error = (type: `${TSESTree.AST_NODE_TYPES}`) =>
1313
type,
1414
}) as const
1515

16-
ruleTester.run('exports-last', rule, {
16+
wrapRun(ruleTester.run)('exports-last', rule, {
1717
valid: [
1818
// Empty file
1919
test({

test/rules/extensions.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TSESLint } from '@typescript-eslint/utils'
22

3-
import { test, testFilePath, parsers } from '../utils'
3+
import { test, testFilePath, parsers, wrapRun } from '../utils'
44

55
import rule from 'eslint-plugin-import-x/rules/extensions'
66

77
const ruleTester = new TSESLint.RuleTester()
88

9-
ruleTester.run('extensions', rule, {
9+
wrapRun(ruleTester.run)('extensions', rule, {
1010
valid: [
1111
test({ code: 'import a from "@/a"' }),
1212
test({ code: 'import a from "a"' }),
@@ -662,7 +662,7 @@ ruleTester.run('extensions', rule, {
662662
describe('TypeScript', () => {
663663
const parser = parsers.TS
664664

665-
ruleTester.run(`${parser}: extensions ignore type-only`, rule, {
665+
wrapRun(ruleTester.run)(`${parser}: extensions ignore type-only`, rule, {
666666
valid: [
667667
test({
668668
code: 'import type T from "./typescript-declare";',

0 commit comments

Comments
 (0)