@pob/eslint-config 67.7.0 → 67.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import tseslint from "typescript-eslint";
4
4
  import oxlintBaseConfig from "../oxlint/base.json" with { type: "json" };
5
+ import oxlintModuleConfig from "../oxlint/module.json" with { type: "json" };
5
6
  import oxlintNodeConfig from "../oxlint/node.json" with { type: "json" };
6
7
  import oxlintTypescriptConfig from "../oxlint/typescript.json" with { type: "json" };
7
8
  import baseConfigs from "./_base.js";
@@ -59,63 +60,104 @@ const oxlintrc = fs.existsSync(oxlintrcPath)
59
60
  ? fs.readFileSync(oxlintrcPath, "utf8")
60
61
  : undefined;
61
62
 
63
+ /**
64
+ * Whether the project's .oxlintrc.json extends a shared oxlint config, like
65
+ * `@pob/eslint-config/oxlint/node.json`.
66
+ *
67
+ * @param {string} configPath
68
+ */
69
+ export const isOxlintConfigExtended = (configPath) =>
70
+ oxlintrc?.includes(configPath) ?? false;
71
+
62
72
  // typescript.json is extended by TypeScript projects only, once regenerated by
63
73
  // pob: until then its rules stay in eslint.
64
- const oxlintTypescript =
65
- oxlintrc?.includes("@pob/eslint-config/oxlint/typescript.json") ?? false;
74
+ const oxlintTypescript = isOxlintConfigExtended(
75
+ "@pob/eslint-config/oxlint/typescript.json",
76
+ );
77
+
78
+ // module.json is extended by ES module projects (not configs.nodeCommonjs),
79
+ // once regenerated by pob: until then its rules stay in eslint.
80
+ const oxlintModule = isOxlintConfigExtended(
81
+ "@pob/eslint-config/oxlint/module.json",
82
+ );
66
83
 
67
84
  // node.json is extended by projects using the node configs, once regenerated
68
85
  // by pob: until then its rules stay in eslint.
69
- const oxlintNode =
70
- oxlintrc?.includes("@pob/eslint-config/oxlint/node.json") ?? false;
86
+ const oxlintNode = isOxlintConfigExtended(
87
+ "@pob/eslint-config/oxlint/node.json",
88
+ );
71
89
 
72
90
  // the shared oxlint configs the project extends
73
91
  const oxlintSharedConfigs = oxlintrc
74
92
  ? [
75
93
  oxlintBaseConfig,
94
+ ...(oxlintModule ? [oxlintModuleConfig] : []),
76
95
  ...(oxlintNode ? [oxlintNodeConfig] : []),
77
96
  ...(oxlintTypescript ? [oxlintTypescriptConfig] : []),
78
97
  ]
79
98
  : [];
80
99
 
81
- const oxlintEslintRuleNames = new Set(
82
- oxlintSharedConfigs
83
- .flatMap((config) => getOxlintConfigRuleNames(config))
84
- .flatMap((ruleName) => oxlintToEslintRuleNames(ruleName)),
85
- );
100
+ /**
101
+ * @param {Parameters<typeof getOxlintConfigRuleNames>[0][]} oxlintConfigs
102
+ */
103
+ const getOxlintEslintRuleNames = (oxlintConfigs) =>
104
+ new Set(
105
+ oxlintConfigs
106
+ .flatMap((config) => getOxlintConfigRuleNames(config))
107
+ .flatMap((ruleName) => oxlintToEslintRuleNames(ruleName)),
108
+ );
86
109
 
87
- // turns off the rules checked by oxlint, must be applied last. Only when the
88
- // project runs oxlint, so the rules are still checked by eslint otherwise.
89
- const oxlintConfigs = oxlintrc
90
- ? [
91
- {
92
- name: "@pob/eslint-config/oxlint",
93
- rules: Object.fromEntries(
94
- [...oxlintEslintRuleNames].map((ruleName) => [ruleName, "off"]),
95
- ),
96
- },
97
- {
98
- name: "@pob/eslint-config/oxlint-disable-directives",
99
- files: ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"],
100
- processor: createOxlintDisableDirectivesProcessor(
101
- oxlintEslintRuleNames,
102
- ),
103
- },
104
- // oxlint runs every type-aware rule: eslint no longer needs to build the
105
- // TypeScript program, its most expensive part
106
- ...(oxlintTypescript
107
- ? [
108
- {
109
- name: "@pob/eslint-config/oxlint-typescript",
110
- files: [`**/*.${tsExtensions}`],
111
- languageOptions: {
112
- parserOptions: { project: false, projectService: false },
113
- },
114
- },
115
- ]
116
- : []),
117
- ]
118
- : [];
110
+ const oxlintEslintRuleNames = getOxlintEslintRuleNames(oxlintSharedConfigs);
111
+
112
+ /**
113
+ * Turns off the rules checked by oxlint: those of the shared configs of this
114
+ * package the project extends, and those of `additionalOxlintConfigs` (like
115
+ * `@pob/eslint-config-typescript-react/oxlint/react.json`). Must be applied
116
+ * last. Empty when the project does not run oxlint, so the rules are still
117
+ * checked by eslint.
118
+ *
119
+ * @param {Parameters<typeof getOxlintConfigRuleNames>[0][]} [additionalOxlintConfigs]
120
+ * @returns {import("eslint").Linter.Config[]}
121
+ */
122
+ export const createOxlintConfigs = (additionalOxlintConfigs = []) => {
123
+ if (!oxlintrc) return [];
124
+ const ruleNames = getOxlintEslintRuleNames([
125
+ ...oxlintSharedConfigs,
126
+ ...additionalOxlintConfigs,
127
+ ]);
128
+ return [
129
+ {
130
+ name: "@pob/eslint-config/oxlint",
131
+ rules: Object.fromEntries(
132
+ [...ruleNames].map((ruleName) => [ruleName, "off"]),
133
+ ),
134
+ },
135
+ // replaces the processor of previous configs, so it must know every rule
136
+ // checked by oxlint
137
+ {
138
+ name: "@pob/eslint-config/oxlint-disable-directives",
139
+ files: ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"],
140
+ processor: createOxlintDisableDirectivesProcessor(ruleNames),
141
+ },
142
+ ];
143
+ };
144
+
145
+ const oxlintConfigs = [
146
+ ...createOxlintConfigs(),
147
+ // oxlint runs every type-aware rule: eslint no longer needs to build the
148
+ // TypeScript program, its most expensive part
149
+ ...(oxlintTypescript
150
+ ? [
151
+ {
152
+ name: "@pob/eslint-config/oxlint-typescript",
153
+ files: [`**/*.${tsExtensions}`],
154
+ languageOptions: {
155
+ parserOptions: { project: false, projectService: false },
156
+ },
157
+ },
158
+ ]
159
+ : []),
160
+ ];
119
161
 
120
162
  /**
121
163
  * Removes the rules checked by oxlint from configs applied after
@@ -6,7 +6,14 @@ import {
6
6
  toCanonicalOxlintRuleName,
7
7
  } from "./test-utils/oxlint.js";
8
8
 
9
- const testLintDir = path.resolve(import.meta.dirname, "../test-lint");
9
+ const testLintDirs = [
10
+ path.resolve(import.meta.dirname, "../test-lint"),
11
+ // rules of @pob/eslint-config-typescript-react/oxlint/*.json
12
+ path.resolve(
13
+ import.meta.dirname,
14
+ "../../eslint-config-typescript-react/test-lint",
15
+ ),
16
+ ];
10
17
 
11
18
  /**
12
19
  * Rules named by the oxlint-disable directives of `source`, as `plugin/rule`.
@@ -25,10 +32,12 @@ const getDirectiveRules = (source) =>
25
32
  .map((rule) => toCanonicalOxlintRuleName(rule)),
26
33
  );
27
34
 
28
- const testLintDirents = await readdir(testLintDir, {
29
- recursive: true,
30
- withFileTypes: true,
31
- });
35
+ const testLintDirsDirents = await Promise.all(
36
+ testLintDirs.map((testLintDir) =>
37
+ readdir(testLintDir, { recursive: true, withFileTypes: true }),
38
+ ),
39
+ );
40
+ const testLintDirents = testLintDirsDirents.flat();
32
41
 
33
42
  const fixturesDirectiveRules = await Promise.all(
34
43
  testLintDirents
@@ -2,9 +2,14 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2
2
  import { tmpdir } from "node:os";
3
3
  import path from "node:path";
4
4
  import { afterAll, beforeAll, describe, expect, it } from "vitest";
5
- import { lintWithOxlint, oxlintConfigsDir } from "./test-utils/oxlint.js";
5
+ import {
6
+ lintWithOxlint,
7
+ oxlintConfigsDir,
8
+ reactOxlintConfigsDir,
9
+ } from "./test-utils/oxlint.js";
6
10
 
7
11
  const baseConfigPath = path.join(oxlintConfigsDir, "base.json");
12
+ const moduleConfigPath = path.join(oxlintConfigsDir, "module.json");
8
13
  const nodeConfigPath = path.join(oxlintConfigsDir, "node.json");
9
14
  const typescriptConfigPath = path.join(oxlintConfigsDir, "typescript.json");
10
15
  // this repo's config, the options are not in base.json so projects keep
@@ -86,6 +91,23 @@ describe("oxlint/base.json", () => {
86
91
  });
87
92
  });
88
93
 
94
+ describe("oxlint/module.json", () => {
95
+ const project = useProject({}, [baseConfigPath, moduleConfigPath]);
96
+ const commonjs = "exports.a = 1;\n";
97
+
98
+ it("reports CommonJS in ES modules", async () => {
99
+ expect(summarize(await lint(project.cwd, commonjs, "file.js"))).toEqual([
100
+ { line: 1, rule: "import(no-commonjs)" },
101
+ ]);
102
+ });
103
+
104
+ // like the eslint configs, which apply commonjs rules to .cjs files
105
+ it("allows CommonJS in .cjs files", async () => {
106
+ await rm(path.join(project.cwd, "file.js"));
107
+ expect(await lint(project.cwd, commonjs, "file.cjs")).toEqual([]);
108
+ });
109
+ });
110
+
89
111
  describe("oxlint/node.json", () => {
90
112
  const project = useProject({}, [baseConfigPath, nodeConfigPath]);
91
113
  const commonjs = '"use strict";\n';
@@ -101,6 +123,23 @@ describe("oxlint/node.json", () => {
101
123
  await rm(path.join(project.cwd, "file.js"));
102
124
  expect(await lint(project.cwd, commonjs, "file.cjs")).toEqual([]);
103
125
  });
126
+
127
+ // plugins are merged through extends: node.json does not replace the import
128
+ // plugin of base.json
129
+ it("keeps the plugins of base.json", async () => {
130
+ expect(
131
+ summarize(
132
+ await lint(
133
+ project.cwd,
134
+ '"use strict";\n\nexports = require(process.env.NAME);\n',
135
+ "file.cjs",
136
+ ),
137
+ ),
138
+ ).toEqual([
139
+ { line: 3, rule: "node(no-exports-assign)" },
140
+ { line: 3, rule: "import(no-dynamic-require)" },
141
+ ]);
142
+ });
104
143
  });
105
144
 
106
145
  describe("oxlint/typescript.json", () => {
@@ -187,6 +226,41 @@ describe.each([
187
226
  });
188
227
  });
189
228
 
229
+ // React projects extend them after node.json: their import/extensions options
230
+ // win, and their plugins do not replace the import plugin of base.json
231
+ describe.each([
232
+ { fileNames: [], expected: [2] },
233
+ { fileNames: ["react.json"], expected: [1] },
234
+ { fileNames: ["react.json", "react-native-web.json"], expected: [1, 3] },
235
+ ])(
236
+ "@pob/eslint-config-typescript-react/oxlint/$fileNames",
237
+ ({ fileNames, expected }) => {
238
+ const project = useProject({}, [
239
+ baseConfigPath,
240
+ moduleConfigPath,
241
+ nodeConfigPath,
242
+ ...fileNames.map((fileName) =>
243
+ path.join(reactOxlintConfigsDir, fileName),
244
+ ),
245
+ ]);
246
+
247
+ it("overrides import/extensions options", async () => {
248
+ await writeFile(path.join(project.cwd, "t.ts"), "export const t = 1;\n");
249
+ await writeFile(path.join(project.cwd, "m.mts"), "export const m = 1;\n");
250
+ const diagnostics = await lint(
251
+ project.cwd,
252
+ 'import { t } from "./t.ts";\nimport { t as t2 } from "./t";\nimport { m } from "./m.mts";\n\nexport const a = [t, t2, m];\n',
253
+ "file.ts",
254
+ );
255
+ expect(
256
+ summarize(diagnostics).filter(
257
+ ({ rule }) => rule === "import(extensions)",
258
+ ),
259
+ ).toEqual(expected.map((line) => ({ line, rule: "import(extensions)" })));
260
+ });
261
+ },
262
+ );
263
+
190
264
  describe("root .oxlintrc.json options", () => {
191
265
  const project = useProject({ options: rootConfig.options });
192
266
 
@@ -1,7 +1,7 @@
1
- import importPlugin, { createNodeResolver } from "eslint-plugin-import-x";
1
+ import { createNodeResolver, flatConfigs } from "eslint-plugin-import-x";
2
2
 
3
3
  export default [
4
- importPlugin.flatConfigs.recommended,
4
+ flatConfigs.recommended,
5
5
  {
6
6
  name: "@pob/eslint-config/import/base",
7
7
  // override recommended language options
@@ -1,8 +1,8 @@
1
- import eslintPluginRegexp from "eslint-plugin-regexp";
1
+ import { configs } from "eslint-plugin-regexp";
2
2
 
3
3
  export default [
4
4
  {
5
- ...eslintPluginRegexp.configs["flat/recommended"],
5
+ ...configs["flat/recommended"],
6
6
  name: "eslint-plugin-regexp/flat/recommended",
7
7
  },
8
8
  // {
@@ -15,6 +15,11 @@ export const oxlintConfigsDir = fileURLToPath(
15
15
  new URL("../../oxlint/", import.meta.url),
16
16
  );
17
17
 
18
+ // @pob/eslint-config-typescript-react shares the inventory of this package
19
+ export const reactOxlintConfigsDir = fileURLToPath(
20
+ new URL("../../../eslint-config-typescript-react/oxlint/", import.meta.url),
21
+ );
22
+
18
23
  /**
19
24
  * Runs oxlint in `cwd` and parses its JSON output.
20
25
  *
@@ -94,18 +99,22 @@ const isRuleEnabled = (ruleEntry) => {
94
99
  };
95
100
 
96
101
  /**
97
- * Rules enabled in `oxlint/*.json` (including `overrides`), as `plugin/rule`.
102
+ * Rules enabled in `oxlint/*.json` of this package and of
103
+ * @pob/eslint-config-typescript-react (including `overrides`), as
104
+ * `plugin/rule`.
98
105
  *
99
106
  * @returns {string[]}
100
107
  */
101
108
  export const getEnabledOxlintConfigRules = () => {
102
109
  const ruleNames = new Set();
103
- for (const fileName of readdirSync(oxlintConfigsDir)) {
104
- if (!fileName.endsWith(".json")) continue;
110
+ const configPaths = [oxlintConfigsDir, reactOxlintConfigsDir].flatMap((dir) =>
111
+ readdirSync(dir)
112
+ .filter((fileName) => fileName.endsWith(".json"))
113
+ .map((fileName) => path.join(dir, fileName)),
114
+ );
115
+ for (const configPath of configPaths) {
105
116
  /** @type {{ rules?: Record<string, unknown>; overrides?: { rules?: Record<string, unknown> }[] }} */
106
- const config = JSON.parse(
107
- readFileSync(path.join(oxlintConfigsDir, fileName), "utf8"),
108
- );
117
+ const config = JSON.parse(readFileSync(configPath, "utf8"));
109
118
  for (const rules of [
110
119
  config.rules,
111
120
  ...(config.overrides ?? []).map((override) => override.rules),
package/oxlint/base.json CHANGED
@@ -2,6 +2,7 @@
2
2
  "categories": {
3
3
  "correctness": "off"
4
4
  },
5
+ "plugins": ["import"],
5
6
  "rules": {
6
7
  "array-callback-return": [
7
8
  "error",
@@ -686,6 +687,26 @@
686
687
  }
687
688
  ],
688
689
  "symbol-description": "error",
690
+ "import/first": "error",
691
+ "import/named": "error",
692
+ "import/namespace": "error",
693
+ "import/newline-after-import": "error",
694
+ "import/no-absolute-path": "error",
695
+ "import/no-cycle": [
696
+ "error",
697
+ {
698
+ "ignoreTypes": true
699
+ }
700
+ ],
701
+ "import/no-duplicates": "error",
702
+ "import/no-dynamic-require": "error",
703
+ "import/no-empty-named-blocks": "error",
704
+ "import/no-mutable-exports": "error",
705
+ "import/no-named-as-default": "error",
706
+ "import/no-named-as-default-member": "error",
707
+ "import/no-named-default": "error",
708
+ "import/no-self-import": "error",
709
+ "import/no-webpack-loader-syntax": "error",
689
710
  "unicorn/catch-error-name": [
690
711
  "error",
691
712
  {
@@ -918,6 +939,19 @@
918
939
  "no-with": "off",
919
940
  "prefer-promise-reject-errors": "off",
920
941
  "require-await": "off",
942
+ "import/named": "off",
943
+ "import/no-anonymous-default-export": [
944
+ "error",
945
+ {
946
+ "allowArray": true,
947
+ "allowArrowFunction": false,
948
+ "allowAnonymousClass": false,
949
+ "allowAnonymousFunction": false,
950
+ "allowCallExpression": true,
951
+ "allowLiteral": true,
952
+ "allowObject": true
953
+ }
954
+ ],
921
955
  "unicorn/prefer-array-find": "off",
922
956
  "unicorn/prefer-string-slice": "off",
923
957
  "unicorn/prefer-string-starts-ends-with": "off"
@@ -0,0 +1,17 @@
1
+ {
2
+ "overrides": [
3
+ {
4
+ "files": ["**/*.{js,mjs,mts,ts,tsx}"],
5
+ "rules": {
6
+ "import/no-amd": "error",
7
+ "import/no-commonjs": "error"
8
+ }
9
+ },
10
+ {
11
+ "files": ["**/*.{ts,cts,mts,tsx}", "**/scripts/**/*.{js,cjs,mjs}"],
12
+ "rules": {
13
+ "import/extensions": ["error", "ignorePackages"]
14
+ }
15
+ }
16
+ ]
17
+ }
package/oxlint/node.json CHANGED
@@ -1,10 +1,33 @@
1
1
  {
2
+ "plugins": ["node"],
3
+ "rules": {
4
+ "node/no-exports-assign": "error"
5
+ },
2
6
  "overrides": [
3
7
  {
4
8
  "files": ["**/*.{js,mjs,mts,ts,tsx}"],
5
9
  "rules": {
6
10
  "unicorn/prefer-module": "error"
7
11
  }
12
+ },
13
+ {
14
+ "files": ["**/*.{cjs,cts}"],
15
+ "rules": {
16
+ "node/no-new-require": "error",
17
+ "node/no-path-concat": "error"
18
+ }
19
+ },
20
+ {
21
+ "files": ["**/*.{js,mjs,cts,mts,ts,tsx}"],
22
+ "rules": {
23
+ "import/extensions": ["error", "ignorePackages"]
24
+ }
25
+ },
26
+ {
27
+ "files": ["**/scripts/**/*.cjs"],
28
+ "rules": {
29
+ "import/extensions": "off"
30
+ }
8
31
  }
9
32
  ]
10
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pob/eslint-config",
3
- "version": "67.7.0",
3
+ "version": "67.9.0",
4
4
  "description": "eslint config files",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/christophehurpeau/pob",
@@ -21,6 +21,7 @@
21
21
  "oxlint/allow-unsafe.json",
22
22
  "oxlint/app.json",
23
23
  "oxlint/base.json",
24
+ "oxlint/module.json",
24
25
  "oxlint/node.json",
25
26
  "oxlint/typescript.json",
26
27
  "utils.js"
@@ -39,6 +40,7 @@
39
40
  "./oxlint/allow-unsafe-as-warn.json": "./oxlint/allow-unsafe-as-warn.json",
40
41
  "./oxlint/app.json": "./oxlint/app.json",
41
42
  "./oxlint/base.json": "./oxlint/base.json",
43
+ "./oxlint/module.json": "./oxlint/module.json",
42
44
  "./oxlint/node.json": "./oxlint/node.json",
43
45
  "./oxlint/typescript.json": "./oxlint/typescript.json",
44
46
  "./utils": "./utils.js",