|
| 1 | +import type { TSESLint } from '@typescript-eslint/utils' |
1 | 2 | import { ESLintUtils } from '@typescript-eslint/utils'
|
2 | 3 |
|
3 | 4 | import { docsUrl } from './docs-url'
|
4 | 5 |
|
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 | +} |
0 commit comments