Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): add flat configs by scope and by category to index.d.ts #179

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends `${infer P}Rules` ? P : never;

export function createFlatRulesConfig<
InputConfigs extends Record<string, RuleRecord>,
RuleRecord extends Record<string, string>,
ConfigNameVariable extends keyof InputConfigs,
ConfigName extends WithoutRulesSuffix<ConfigNameVariable>,
OutputConfigs extends Record<
`flat/${KebabCase<ConfigName>}`,
{
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<ConfigName>}`; // 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],
Expand Down