|
1 | 1 | import { kebabCase } from 'scule';
|
2 |
| -export function createFlatRulesConfig(rulesModule: { [key: string]: unknown }) { |
3 |
| - const flatRulesConfig: { [key: string]: unknown } = {}; // Add index signature to allow indexing with a string |
| 2 | +import type { KebabCase } from 'scule'; |
| 3 | + |
| 4 | +type WithoutRulesSuffix<T> = T extends `${infer P}Rules` ? P : never; |
| 5 | + |
| 6 | +export function createFlatRulesConfig< |
| 7 | + InputConfigs extends Record<string, RuleRecord>, |
| 8 | + RuleRecord extends Record<string, string>, |
| 9 | + ConfigNameVariable extends keyof InputConfigs, |
| 10 | + ConfigName extends WithoutRulesSuffix<ConfigNameVariable>, |
| 11 | + OutputConfigs extends Record< |
| 12 | + `flat/${KebabCase<ConfigName>}`, |
| 13 | + { |
| 14 | + name: string; |
| 15 | + rules: RuleRecord; |
| 16 | + } |
| 17 | + >, |
| 18 | +>(rulesModule: InputConfigs): OutputConfigs { |
| 19 | + const flatRulesConfig = {} as OutputConfigs; |
4 | 20 |
|
5 | 21 | // Iterate over each property in the rules module
|
6 | 22 | for (const key of Object.keys(rulesModule)) {
|
7 | 23 | if (key.endsWith('Rules')) {
|
8 | 24 | // Ensure the property is a rules set
|
9 | 25 | const ruleName = kebabCase(key.replace('Rules', ''));
|
10 |
| - const flatKey = `flat/${ruleName}`; // Create the new key |
| 26 | + const flatKey = `flat/${ruleName}` as `flat/${KebabCase<ConfigName>}`; // Create the new key |
| 27 | + |
| 28 | + // @ts-ignore TS2322 -- "could be instantiated with a different subtype of constraint". |
| 29 | + // we do not care at the moment, we only want our index.d.ts to include the names of the config |
11 | 30 | flatRulesConfig[flatKey] = {
|
12 | 31 | name: `oxlint/${ruleName}`,
|
13 | 32 | rules: rulesModule[key],
|
|
0 commit comments