Skip to content

Commit

Permalink
feat: ✨ make each plugin it's own config item (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman authored Nov 26, 2024
1 parent 154fe1b commit 6d8ea49
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 107 deletions.
134 changes: 40 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,128 +21,48 @@ First install the package, by running the following:
pnpm add -D @jimmy.codes/eslint-config
```

Then if you want a simple configuration:
Then all you need to in your `eslint.config.js` is:

```js
// eslint.config.mjs
```mjs
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig();
```

Or if you want to use [TypeScript configuration files](https://eslint.org/docs/latest/use/configure/configuration-files#typescript-configuration-files), you can do the following:

Add `--flag unstable_ts_config` to your eslint script, for example:

```json
{
"scripts": {
"lint": "eslint --flag unstable_ts_config ."
}
}
```

And add the following to your `.vscode/settings.json`:

```json
"eslint.options": {
"flags": ["unstable_ts_config"]
}
```
Which will enable rules based on your project dependencies.

### 🔧 Configuration

> [!NOTE]
> By default all rules are enabled based on the project's dependencies.
This package contains rules that can be enabled or disabled as follows:

```js
```ts
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig({
/**
* Are TypeScript rules enabled?
* @default false
*/
typescript: true,
/**
* Are React rules enabled?
* @default false
*/
react: true,
/**
* Are Astro rules enabled?
* @default false
*/
astro: true,
/**
* Are testing rules enabled?
* @default false
*/
testing: true,
astro: false,
jest: false,
playwright: false,
react: false,
tanstackQuery: false,
testingLibrary: false,
typescript: false,
vitest: false,
});
```

Or you can turn off auto detection to disable rules based on a project's dependencies:

```js
```ts
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig({ autoDetect: false });
```

#### TypeScript

You can also change the project location which can be helpful for monorepos:

> [!WARNING]
> This is [not recommended nor needed since the introduction of `projectService`](https://typescript-eslint.io/getting-started/typed-linting#can-i-customize-the-tsconfig-used-for-typed-linting) which this config uses by default.
```js
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig({
typescript: {
project: ["./tsconfig.eslint.json", "./packages/*/tsconfig.json"],
},
});
```

#### Testing

By default [vitest](https://vitest.dev) is used as the testing framework but you can override and add additional rules for utilities:

```js
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig({
testing: {
framework: "jest",
utilities: ["testing-library"],
},
});
```

#### React

You can add additional rules for utilities:

```js
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig({
react: {
utilities: ["@tanstack/query"],
},
});
```

#### Extending the Configuration

You can also extend the configuration:

```js
```ts
import eslintConfig from "@jimmy.codes/eslint-config";

export default eslintConfig(
Expand All @@ -164,6 +84,8 @@ export default eslintConfig(
);
```

#### Ignores

You can also extend what is ignored:

```ts
Expand All @@ -174,6 +96,30 @@ export default eslintConfig({
});
```

### Typescript Configuration Files

If you want to use [TypeScript configuration files](https://eslint.org/docs/latest/use/configure/configuration-files#typescript-configuration-files), you can do the following:

Add `--flag unstable_ts_config` to your eslint script, for example:

```json
{
"scripts": {
"lint": "eslint --flag unstable_ts_config ."
}
}
```

And add the following to your `.vscode/settings.json`:

```json
{
"eslint.options": {
"flags": ["unstable_ts_config"]
}
}
```

## ❤️ Credits

- [@antfu/eslint-config](https://github.com/antfu/eslint-config) by [Anthony Fu](https://antfu.me)
Expand Down
49 changes: 48 additions & 1 deletion src/factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("eslintConfig", () => {
);
});

it("should create configuration w/ react & @tanstack/query", async () => {
it("should create configuration w/ react & @tanstack/query (deprecated)", async () => {
await expect(
eslintConfig({
autoDetect: false,
Expand All @@ -61,6 +61,12 @@ describe("eslintConfig", () => {
});

it("should create configuration w/ jest", async () => {
const configs = await eslintConfig({ autoDetect: false, jest: true });

expect(configs.at(7)?.name).toBe("jimmy.codes/jest");
});

it("should create configuration w/ jest (deprecated)", async () => {
await expect(
eslintConfig({ autoDetect: false, testing: { framework: "jest" } }),
).resolves.toStrictEqual(
Expand All @@ -72,6 +78,12 @@ describe("eslintConfig", () => {
});

it("should create configuration w/ vitest", async () => {
const configs = await eslintConfig({ autoDetect: false, vitest: true });

expect(configs.at(7)?.name).toBe("jimmy.codes/vitest");
});

it("should create configuration w/ vitest (deprecated)", async () => {
await expect(
eslintConfig({ autoDetect: false, testing: true }),
).resolves.toStrictEqual(
Expand All @@ -83,6 +95,19 @@ describe("eslintConfig", () => {
});

it("should create configuration w/ jest & react & testing library", async () => {
const configs = await eslintConfig({
autoDetect: false,
jest: true,
react: true,
testingLibrary: true,
});

expect(configs.at(7)?.name).toBe("jimmy.codes/react");
expect(configs.at(8)?.name).toBe("jimmy.codes/jest");
expect(configs.at(9)?.name).toBe("jimmy.codes/testing-library");
});

it("should create configuration w/ jest & react & testing library (deprecated)", async () => {
await expect(
eslintConfig({
autoDetect: false,
Expand All @@ -99,6 +124,19 @@ describe("eslintConfig", () => {
});

it("should create configuration w/ vitest & react & testing library", async () => {
const configs = await eslintConfig({
autoDetect: false,
react: true,
testingLibrary: true,
vitest: true,
});

expect(configs.at(7)?.name).toBe("jimmy.codes/react");
expect(configs.at(8)?.name).toBe("jimmy.codes/vitest");
expect(configs.at(9)?.name).toBe("jimmy.codes/testing-library");
});

it("should create configuration w/ vitest & react & testing library (deprecated)", async () => {
await expect(
eslintConfig({
autoDetect: false,
Expand Down Expand Up @@ -142,6 +180,15 @@ describe("eslintConfig", () => {
);
});

it("should create configuration w/ tanstackQuery", async () => {
const configs = await eslintConfig({
autoDetect: false,
tanstackQuery: true,
});

expect(configs.at(7)?.name).toBe("jimmy.codes/react/query");
});

describe("autoDetect", () => {
it("should include typescript when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
Expand Down
14 changes: 12 additions & 2 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,33 @@ export const eslintConfig = async (
autoDetect = true,
configs = [],
ignores = [],
jest = false,
playwright = false,
react = false,
tanstackQuery = false,
testing = false,
testingLibrary = false,
typescript = false,
vitest = false,
}: Options = {},
...moreConfigs: Linter.Config[] | TypedConfigItem[]
) => {
const reactOptions = getReactOptions(react);
const testingOptions = getTestingOptions(testing);
const testingOptions = getTestingOptions(testing, {
jest,
testingLibrary,
vitest,
});
const typescriptOptions = getTypescriptOptions(typescript);
const isTypescriptEnabled =
typescript || !!typescriptOptions || (autoDetect && hasTypescript());
const isReactEnabled = react || (autoDetect && hasReact());
const isTestingEnabled = testing || (autoDetect && hasTesting());
const isTestingEnabled =
testing || jest || vitest || (autoDetect && hasTesting());
const isAstroEnabled = astro || (autoDetect && hasAstro());
const isTanstackQueryEnabled = shouldEnableTanstackQuery(
reactOptions,
tanstackQuery,
autoDetect,
);
const isTestingLibraryEnabled = shouldEnableTestingLibrary(
Expand Down
7 changes: 7 additions & 0 deletions src/rules.gen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9483,12 +9483,19 @@ type PerfectionistSortObjectTypes = []|[{
// ----- perfectionist/sort-objects -----
type PerfectionistSortObjects = []|[{

destructuredObjects?: (boolean | {

groups?: boolean
})

ignorePattern?: string[]

partitionByComment?: (string[] | boolean | string)

destructureOnly?: boolean

objectDeclarations?: boolean

styledComponents?: boolean

partitionByNewLine?: boolean
Expand Down
Loading

0 comments on commit 6d8ea49

Please sign in to comment.