Skip to content

Commit

Permalink
small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnapo committed May 26, 2024
1 parent 712b0c3 commit 2e959ff
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ npm i eslint-config-iamnapo -D
## Usage

Add to your `eslint.config.js` one of the available configs<sup>*</sup> like so:
Add to your `eslint.config.js` one of the available configs (`default`, `react`, `typescript`, `react-typescript`) like so:

```js
import eslintConfigIamnapo from "eslint-config-iamnapo";

const config = [
...eslintConfigIamnapo.configs.default.map(cfg => ({
...cfg,
files: ["**/*.{c,m,}js"], // or "**/*.{c,m,}js{x,}"
// files: ["**/*.{c,m,}ts"], // or "**/*.{c,m,}ts{x,}"
files: [eslintConfigIamnapo.filePatterns.default],
})),
// your overrides
];

export default config;
```

<sup>*</sup>: `default`, `react`, `typescript`, `react-typescript`.
>[!TIP]
> `filePatterns` contains some helpful [patterns](https://eslint.org/docs/latest/use/configure/configuration-files#specifying-files-and-ignores) about each case, so you won’t have to figure them out yourself.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import defaultConfig from "./default.js";
import reactConfig from "./react.js";
import typescriptConfig from "./typescript.js";
import reactTypescriptConfig from "./react-typescript.js";
import defaultConfig from "./configs/default.js";
import reactConfig from "./configs/react.js";
import typescriptConfig from "./configs/typescript.js";
import reactTypescriptConfig from "./configs/react-typescript.js";

const eslintConfigIamnapo = {
configs: {
Expand All @@ -10,6 +10,12 @@ const eslintConfigIamnapo = {
typescript: typescriptConfig,
"react-typescript": reactTypescriptConfig,
},
filePatterns: {
default: "**/*.{c,m,}js",
react: "**/*.{c,m,}js{x,}",
typescript: "**/*.{c,m,}ts",
"react-typescript": "**/*.{c,m,}ts{x,}",
},
};

export default eslintConfigIamnapo;
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
".": "./index.js"
},
"files": [
"index.js",
"default.js",
"typescript.js",
"react.js",
"react-typescript.js"
"configs",
"index.js"
],
"scripts": {
"lint": "eslint . --cache",
Expand Down
7 changes: 6 additions & 1 deletion test/default.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Linter } from "eslint";
import eslintConfigIamnapo from "eslint-config-iamnapo";

const config = eslintConfigIamnapo.configs.default;
const filePattern = eslintConfigIamnapo.filePatterns.default;

test("load config in eslint to validate all rule syntax is correct", async () => {
const linter = new Linter({ configType: "flat" });
Expand All @@ -14,6 +15,10 @@ test("load config in eslint to validate all rule syntax is correct", async () =>
const bar = () => {};
bar(foo);
`.replaceAll("\t", "");
const errorCount = await linter.verify(code, config, fileURLToPath(new URL("test.js", import.meta.url)));
const errorCount = await linter.verify(
code,
config.map((cfg) => ({ ...cfg, files: [filePattern] })),
fileURLToPath(new URL("test.js", import.meta.url)),
);
assert.equal(errorCount.length, 0);
});
19 changes: 11 additions & 8 deletions test/react-typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Linter } from "eslint";
import eslintConfigIamnapo from "eslint-config-iamnapo";

const config = eslintConfigIamnapo.configs["react-typescript"];
const filePattern = eslintConfigIamnapo.filePatterns["react-typescript"];

test("load config in eslint to validate all rule syntax is correct", async () => {
const linter = new Linter({ configType: "flat" });
Expand All @@ -18,15 +19,17 @@ test("load config in eslint to validate all rule syntax is correct", async () =>
`.replaceAll("\t", "");
const errorCount = await linter.verify(
code,
config.map(cfg => ({ ...cfg, files: ["**/*.{c,m,}tsx"] })),
config.map((cfg) => ({ ...cfg, files: [filePattern] })),
fileURLToPath(new URL("test.tsx", import.meta.url)),
);
assert.equal(errorCount.length, 6);
assert.ok(errorCount.every(({ ruleId }) =>
[
"@typescript-eslint/prefer-nullish-coalescing",
"@typescript-eslint/no-unsafe-assignment",
"@typescript-eslint/no-unsafe-call",
"@typescript-eslint/no-unsafe-member-access",
].includes(ruleId)));
assert.ok(
errorCount.every(({ ruleId }) =>
[
"@typescript-eslint/prefer-nullish-coalescing",
"@typescript-eslint/no-unsafe-assignment",
"@typescript-eslint/no-unsafe-call",
"@typescript-eslint/no-unsafe-member-access",
].includes(ruleId)),
);
});
3 changes: 2 additions & 1 deletion test/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Linter } from "eslint";
import eslintConfigIamnapo from "eslint-config-iamnapo";

const config = eslintConfigIamnapo.configs.react;
const filePattern = eslintConfigIamnapo.filePatterns.react;

test("load config in eslint to validate all rule syntax is correct", async () => {
const linter = new Linter({ configType: "flat" });
Expand All @@ -18,7 +19,7 @@ test("load config in eslint to validate all rule syntax is correct", async () =>
`.replaceAll("\t", "");
const errorCount = await linter.verify(
code,
[...config.map(cfg => ({ ...cfg, files: ["**/*.{c,m,}jsx"] })), { rules: { "import/no-unresolved": "off" } }],
[...config.map((cfg) => ({ ...cfg, files: [filePattern] })), { rules: { "import/no-unresolved": "off" } }],
fileURLToPath(new URL("test.jsx", import.meta.url)),
);
assert.equal(errorCount.length, 0);
Expand Down
3 changes: 2 additions & 1 deletion test/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Linter } from "eslint";
import eslintConfigIamnapo from "eslint-config-iamnapo";

const config = eslintConfigIamnapo.configs.typescript;
const filePattern = eslintConfigIamnapo.filePatterns.typescript;

test("load config in eslint to validate all rule syntax is correct", async () => {
const linter = new Linter({ configType: "flat" });
Expand All @@ -16,7 +17,7 @@ test("load config in eslint to validate all rule syntax is correct", async () =>
`.replaceAll("\t", "");
const errorCount = await linter.verify(
code,
config.map(cfg => ({ ...cfg, files: ["**/*.{c,m,}ts"] })),
config.map((cfg) => ({ ...cfg, files: [filePattern] })),
fileURLToPath(new URL("test.ts", import.meta.url)),
);
assert.equal(errorCount.length, 1);
Expand Down

0 comments on commit 2e959ff

Please sign in to comment.