Skip to content

Commit

Permalink
refactor: remove append property for Builder. user can use append m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
zanminkian committed Dec 20, 2024
1 parent 5ce8679 commit d705983
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-kiwis-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fenge/eslint-config": minor
"fenge": minor
---

refactor: remove `append` property for Builder. user can use append method
9 changes: 6 additions & 3 deletions packages/eslint-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ export default new Builder()
.enableJavascript({
omit: ["no-var"], // these rules will not work for js files
})
.enableTypescript({
// apply additional rules or override the built-in rules for ts files
append: {
.enableTypescript()
// apply additional rules or override the built-in rules for ts files
.append({
name: "strictest",
files: ["**/*.{ts,cts,mts,tsx}"],
rules: {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/consistent-type-assertions": [
"error",
Expand Down
26 changes: 14 additions & 12 deletions packages/eslint-config/src/eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ export class Builder {
}

private setup(
[mainConfig, ...otherConfigs]: readonly [
{ plugins: object; rules: object },
...object[],
],
{ pick, omit, append = {} }: Options<string[]>,
configItems: readonly { rules: object }[],
{ pick, omit }: Options<string[]>,
) {
const select = (ruleKey: string) => {
if (!pick && !omit) {
Expand All @@ -60,13 +57,18 @@ export class Builder {
throw new Error("You cannot specify both pick and omit");
}
};
const rules = Object.fromEntries(
Object.entries(mainConfig.rules).filter(([ruleKey]) => select(ruleKey)),
);
this.configs.push(
{ ...mainConfig, rules: { ...rules, ...append } },
...otherConfigs,
);
const result = configItems.map((configItem) => ({
...configItem,
rules: {
...Object.fromEntries(
Object.entries(configItem.rules).filter(([ruleKey]) =>
select(ruleKey),
),
),
},
}));
this.configs.push(...result);

return this;
}

Expand Down
43 changes: 24 additions & 19 deletions packages/fenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,26 +233,31 @@ export default {
},
lint: async () => {
// See https://www.npmjs.com/package/@fenge/eslint-config for eslint-config detail usage
const Builder = (await import("fenge/eslint-config")).Builder;
return new Builder()
.enablePackagejson({
pick: ["packagejson/top-types"], // only these rules will work for package.json files
})
.enableJavascript({
omit: ["no-var"], // these rules will not work for js files
})
.enableTypescript({
const { Builder } = await import("fenge/eslint-config");
return (
new Builder()
.enablePackagejson({
pick: ["packagejson/top-types"], // only these rules will work for package.json files
})
.enableJavascript({
omit: ["no-var"], // these rules will not work for js files
})
.enableTypescript()
// apply additional rules or override the built-in rules for ts files
append: {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/consistent-type-assertions": [
"error",
{ assertionStyle: "never" },
],
"@typescript-eslint/no-non-null-assertion": "error",
},
})
.toConfig();
.append({
name: "strictest",
files: ["**/*.{ts,cts,mts,tsx}"],
rules: {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/consistent-type-assertions": [
"error",
{ assertionStyle: "never" },
],
"@typescript-eslint/no-non-null-assertion": "error",
},
})
.toConfig()
);
},
};
```
Expand Down

0 comments on commit d705983

Please sign in to comment.