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

Conversation

Sysix
Copy link
Collaborator

@Sysix Sysix commented Oct 12, 2024

grafik

Sadly the output does not includes the rules at the moment:

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

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;
}

@Sysix Sysix changed the title feat(build): add flats config by scope and by category to index.d.ts feat(build): add flat configs by scope and by category to index.d.ts Oct 12, 2024
@Sysix Sysix marked this pull request as ready for review October 12, 2024 14:56
@Boshen Boshen merged commit 9f677ab into oxc-project:main Oct 12, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants