Skip to content

Commit 9f677ab

Browse files
authored
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<string, string>; }; "flat/pedantic": { name: string; rules: Record<string, string>; }; "flat/nursery": { name: string; rules: Record<string, string>; }; ..... ``` 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><summary>Details</summary> <p> ```ts import { kebabCase } from 'scule'; import type { KebabCase } from 'scule'; type WithoutRulesSuffix<T> = T extends `${infer P}Rules` ? P : never; type StringKeys<objType extends {}> = Array<Extract<keyof objType, string>>; function replaceRulesSuffix<T extends string>(val: T): WithoutRulesSuffix<T> { return val.replace('Rules', '') as WithoutRulesSuffix<T>; } export function createFlatRulesConfig< InputConfigs extends Record<string, unknown>, ConfigNameVariable extends Extract<keyof InputConfigs, string>, RuleRecord extends InputConfigs[ConfigNameVariable], ConfigName extends WithoutRulesSuffix<ConfigNameVariable>, FloatConfigKey extends `flat/${KebabCase<ConfigName>}`, OutputConfigs extends Record< FloatConfigKey, { name: `oxlint/${KebabCase<ConfigName>}`; 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<InputConfigs>) { 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; } ``` </p> </details>
1 parent 5533225 commit 9f677ab

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/utils.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
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;
420

521
// Iterate over each property in the rules module
622
for (const key of Object.keys(rulesModule)) {
723
if (key.endsWith('Rules')) {
824
// Ensure the property is a rules set
925
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
1130
flatRulesConfig[flatKey] = {
1231
name: `oxlint/${ruleName}`,
1332
rules: rulesModule[key],

0 commit comments

Comments
 (0)