From 9f677ab30968741962ef6892d0ff47994a0aed27 Mon Sep 17 00:00:00 2001 From: "Alexander S." Date: Sat, 12 Oct 2024 18:19:38 +0200 Subject: [PATCH] feat(build): add flat configs by scope and by category to index.d.ts (#179) ![grafik](https://github.com/user-attachments/assets/afe8fb6c-646b-49f7-9e2a-b6625bb81f38) Sadly the output does not includes the rules at the moment: ```ts declare const _default: { configs: { "flat/perf": { name: string; rules: Record; }; "flat/pedantic": { name: string; rules: Record; }; "flat/nursery": { name: string; rules: Record; }; ..... ``` Next plan: Updating generator script to support flat config out of the box. This should also improve the ts definitions for them. My "dirty" fix does not work correctly:
Details

```ts import { kebabCase } from 'scule'; import type { KebabCase } from 'scule'; type WithoutRulesSuffix = T extends `${infer P}Rules` ? P : never; type StringKeys = Array>; function replaceRulesSuffix(val: T): WithoutRulesSuffix { return val.replace('Rules', '') as WithoutRulesSuffix; } export function createFlatRulesConfig< InputConfigs extends Record, ConfigNameVariable extends Extract, RuleRecord extends InputConfigs[ConfigNameVariable], ConfigName extends WithoutRulesSuffix, FloatConfigKey extends `flat/${KebabCase}`, OutputConfigs extends Record< FloatConfigKey, { name: `oxlint/${KebabCase}`; rules: RuleRecord; } >, >(rulesModule: InputConfigs): OutputConfigs { const flatRulesConfig = {} as OutputConfigs; // Iterate over each property in the rules module for (const key of Object.keys(rulesModule) as StringKeys) { if (key.endsWith('Rules')) { // Ensure the property is a rules set const ruleName = kebabCase(replaceRulesSuffix(key)); // we do not care at the moment, we only want our index.d.ts to include the names of the config flatRulesConfig[`flat/${ruleName}`] = { name: `oxlint/${ruleName}`, rules: rulesModule[key], }; // Assign the rules to the new key } } return flatRulesConfig as OutputConfigs; } ```

--- src/utils.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 2d03ac9..61124b1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,13 +1,32 @@ import { kebabCase } from 'scule'; -export function createFlatRulesConfig(rulesModule: { [key: string]: unknown }) { - const flatRulesConfig: { [key: string]: unknown } = {}; // Add index signature to allow indexing with a string +import type { KebabCase } from 'scule'; + +type WithoutRulesSuffix = T extends `${infer P}Rules` ? P : never; + +export function createFlatRulesConfig< + InputConfigs extends Record, + RuleRecord extends Record, + ConfigNameVariable extends keyof InputConfigs, + ConfigName extends WithoutRulesSuffix, + OutputConfigs extends Record< + `flat/${KebabCase}`, + { + name: string; + rules: RuleRecord; + } + >, +>(rulesModule: InputConfigs): OutputConfigs { + const flatRulesConfig = {} as OutputConfigs; // Iterate over each property in the rules module for (const key of Object.keys(rulesModule)) { if (key.endsWith('Rules')) { // Ensure the property is a rules set const ruleName = kebabCase(key.replace('Rules', '')); - const flatKey = `flat/${ruleName}`; // Create the new key + const flatKey = `flat/${ruleName}` as `flat/${KebabCase}`; // Create the new key + + // @ts-ignore TS2322 -- "could be instantiated with a different subtype of constraint". + // we do not care at the moment, we only want our index.d.ts to include the names of the config flatRulesConfig[flatKey] = { name: `oxlint/${ruleName}`, rules: rulesModule[key],