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 974ac53
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 99 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
92 changes: 1 addition & 91 deletions packages/deprecated-eslint-config/README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,3 @@
# @git-validator/eslint-config

[![](https://img.shields.io/npm/l/@git-validator/eslint-config.svg)](https://github.com/zanminkian/git-validator/blob/main/LICENSE)
[![](https://img.shields.io/npm/v/@git-validator/eslint-config.svg)](https://www.npmjs.com/package/@git-validator/eslint-config)
[![](https://img.shields.io/npm/dm/@git-validator/eslint-config.svg)](https://www.npmjs.com/package/@git-validator/eslint-config)
[![](https://packagephobia.com/badge?p=@git-validator/eslint-config)](https://packagephobia.com/result?p=@git-validator/eslint-config)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://makeapullrequest.com)

A strict eslint config for linting `js` / `ts` / `package.json` files. Based on [standard.js](https://github.com/standard/standard) without any stylistic opinions.

## Feature

- Lint `js` / `mjs` / `cjs` / `jsx` / `ts` / `mts` / `cts` / `tsx` / `package.json` files only.
- One-line of config.
- Type safe. TypeScript friendly.
- Respect `.gitignore`.
- Based on [standard.js](https://github.com/standard/standard).
- Have no stylistic opinions. Prettier friendly.
- [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
- Strict, but progressive.
- Modern. ESM first.
- React friendly.
- NestJS friendly.

## Usage

Install it in the root of js / ts project.

```sh
npm install -D eslint @git-validator/eslint-config
```

Config `eslint.config.js` (for ESM).

```js
import config from "@git-validator/eslint-config";

export default config;
```

If you are in CommonJS, config `eslint.config.js` bellow:

```js
module.exports = import("@git-validator/eslint-config");
```

Config `package.json`

```json
{
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
}
}
```

> Note: TypeScript project is required a `tsconfig.json` file in the root.
## Advanced Usage

### Config Builder

The default config is very strict. If you don't like the default config, use `Builder` to customize your own.

```ts
import { Builder } from "@git-validator/eslint-config";

export default 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({
project: "tsconfig.json", // tsconfig.json path
extend: {
// apply additional rules for ts files
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/consistent-type-assertions": [
"error",
{ assertionStyle: "never" },
],
"@typescript-eslint/no-non-null-assertion": "error",
},
})
.toConfig();
```

## License

MIT
This package re-export from [@fenge/eslint-config](https://www.npmjs.com/package/@fenge/eslint-config) since v0.10.0. Use it instead.
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 974ac53

Please sign in to comment.