Skip to content

Commit

Permalink
feat(eslint-config): update function type signature. pick and `omit…
Browse files Browse the repository at this point in the history
…` properties disallow duplicated rule names
  • Loading branch information
zanminkian committed Nov 20, 2024
1 parent a90e61e commit 9760697
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-buttons-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fenge/eslint-config": patch
---

feat(eslint-config): update function type signature. `pick` and `omit` properties disallow duplicated rule names
29 changes: 21 additions & 8 deletions packages/eslint-config/src/eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@ import { javascript } from "./config/javascript.js";
import { packagejson } from "./config/packagejson.js";
import { typescript } from "./config/typescript.js";

type NoDuplicate<A extends unknown[]> = {
[I in keyof A]: true extends {
[J in keyof A]: J extends I ? false : A[J] extends A[I] ? true : false;
}[number]
? never
: A[I];
};

type JsRuleKey = keyof ReturnType<typeof javascript>[0]["rules"];
type TsRuleKey = keyof ReturnType<typeof typescript>[0]["rules"];
type PkgRuleKey = keyof ReturnType<typeof packagejson>[0]["rules"];

interface Options<T extends string> {
pick?: T[];
omit?: T[];
interface Options<T extends string[]> {
pick?: NoDuplicate<T>;
omit?: NoDuplicate<T>;
extend?: Record<
string,
"error" | "warn" | "off" | ["error" | "warn", ...unknown[]]
>;
override?: Partial<
Record<T, "error" | "warn" | "off" | ["error" | "warn", ...unknown[]]>
Record<
T[number],
"error" | "warn" | "off" | ["error" | "warn", ...unknown[]]
>
>;
}

Expand All @@ -32,7 +43,7 @@ export class Builder {
{ plugins: object; rules: object },
...object[],
],
{ pick, omit, extend = {}, override = {} }: Options<string>,
{ pick, omit, extend = {}, override = {} }: Options<string[]>,
) {
const select = (ruleKey: string) => {
if (!pick && !omit) {
Expand Down Expand Up @@ -80,15 +91,17 @@ export class Builder {
return this;
}

enableTypescript(options: Options<TsRuleKey> & { project?: string } = {}) {
enableTypescript<T extends TsRuleKey[]>(
options: Options<T> & { project?: string } = {},
) {
return this.setup(typescript(options.project), options);
}

enableJavascript(options: Options<JsRuleKey> = {}) {
enableJavascript<T extends JsRuleKey[]>(options: Options<T> = {}) {
return this.setup(javascript(), options);
}

enablePackagejson(options: Options<PkgRuleKey> = {}) {
enablePackagejson<T extends PkgRuleKey[]>(options: Options<T> = {}) {
return this.setup(packagejson(), options);
}
}
Expand Down

0 comments on commit 9760697

Please sign in to comment.