@pob/eslint-config 67.8.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
@@ -60,20 +60,32 @@ const oxlintrc = fs.existsSync(oxlintrcPath)
60
60
  ? fs.readFileSync(oxlintrcPath, "utf8")
61
61
  : undefined;
62
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
+
63
72
  // typescript.json is extended by TypeScript projects only, once regenerated by
64
73
  // pob: until then its rules stay in eslint.
65
- const oxlintTypescript =
66
- oxlintrc?.includes("@pob/eslint-config/oxlint/typescript.json") ?? false;
74
+ const oxlintTypescript = isOxlintConfigExtended(
75
+ "@pob/eslint-config/oxlint/typescript.json",
76
+ );
67
77
 
68
78
  // module.json is extended by ES module projects (not configs.nodeCommonjs),
69
79
  // once regenerated by pob: until then its rules stay in eslint.
70
- const oxlintModule =
71
- oxlintrc?.includes("@pob/eslint-config/oxlint/module.json") ?? false;
80
+ const oxlintModule = isOxlintConfigExtended(
81
+ "@pob/eslint-config/oxlint/module.json",
82
+ );
72
83
 
73
84
  // node.json is extended by projects using the node configs, once regenerated
74
85
  // by pob: until then its rules stay in eslint.
75
- const oxlintNode =
76
- oxlintrc?.includes("@pob/eslint-config/oxlint/node.json") ?? false;
86
+ const oxlintNode = isOxlintConfigExtended(
87
+ "@pob/eslint-config/oxlint/node.json",
88
+ );
77
89
 
78
90
  // the shared oxlint configs the project extends
79
91
  const oxlintSharedConfigs = oxlintrc
@@ -85,44 +97,67 @@ const oxlintSharedConfigs = oxlintrc
85
97
  ]
86
98
  : [];
87
99
 
88
- const oxlintEslintRuleNames = new Set(
89
- oxlintSharedConfigs
90
- .flatMap((config) => getOxlintConfigRuleNames(config))
91
- .flatMap((ruleName) => oxlintToEslintRuleNames(ruleName)),
92
- );
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
+ );
93
109
 
94
- // turns off the rules checked by oxlint, must be applied last. Only when the
95
- // project runs oxlint, so the rules are still checked by eslint otherwise.
96
- const oxlintConfigs = oxlintrc
97
- ? [
98
- {
99
- name: "@pob/eslint-config/oxlint",
100
- rules: Object.fromEntries(
101
- [...oxlintEslintRuleNames].map((ruleName) => [ruleName, "off"]),
102
- ),
103
- },
104
- {
105
- name: "@pob/eslint-config/oxlint-disable-directives",
106
- files: ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"],
107
- processor: createOxlintDisableDirectivesProcessor(
108
- oxlintEslintRuleNames,
109
- ),
110
- },
111
- // oxlint runs every type-aware rule: eslint no longer needs to build the
112
- // TypeScript program, its most expensive part
113
- ...(oxlintTypescript
114
- ? [
115
- {
116
- name: "@pob/eslint-config/oxlint-typescript",
117
- files: [`**/*.${tsExtensions}`],
118
- languageOptions: {
119
- parserOptions: { project: false, projectService: false },
120
- },
121
- },
122
- ]
123
- : []),
124
- ]
125
- : [];
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
+ ];
126
161
 
127
162
  /**
128
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,7 +2,11 @@ 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");
8
12
  const moduleConfigPath = path.join(oxlintConfigsDir, "module.json");
@@ -119,6 +123,23 @@ describe("oxlint/node.json", () => {
119
123
  await rm(path.join(project.cwd, "file.js"));
120
124
  expect(await lint(project.cwd, commonjs, "file.cjs")).toEqual([]);
121
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
+ });
122
143
  });
123
144
 
124
145
  describe("oxlint/typescript.json", () => {
@@ -205,6 +226,41 @@ describe.each([
205
226
  });
206
227
  });
207
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
+
208
264
  describe("root .oxlintrc.json options", () => {
209
265
  const project = useProject({ options: rootConfig.options });
210
266
 
@@ -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),
@@ -6,6 +6,12 @@
6
6
  "import/no-amd": "error",
7
7
  "import/no-commonjs": "error"
8
8
  }
9
+ },
10
+ {
11
+ "files": ["**/*.{ts,cts,mts,tsx}", "**/scripts/**/*.{js,cjs,mjs}"],
12
+ "rules": {
13
+ "import/extensions": ["error", "ignorePackages"]
14
+ }
9
15
  }
10
16
  ]
11
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.8.0",
3
+ "version": "67.9.0",
4
4
  "description": "eslint config files",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/christophehurpeau/pob",