@pob/eslint-config 67.3.0 → 67.5.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/eslint-rules.test.js +16 -0
- package/lib/index.js +37 -3
- package/lib/overrides/tools-project.js +1 -1
- package/lib/oxlint-fixtures.test.js +167 -0
- package/lib/oxlint-rules.test.js +81 -0
- package/lib/oxlint.test.js +109 -0
- package/lib/test-utils/eslintRules.js +69 -0
- package/lib/test-utils/oxlint.js +138 -0
- package/lib/utils/oxlint.js +115 -0
- package/lib/utils/oxlint.test.js +168 -0
- package/oxlint/base.json +8 -0
- package/package.json +9 -6
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getEslintRulesStatus } from "./test-utils/eslintRules.js";
|
|
3
|
+
import pobConfig from "./index.js";
|
|
4
|
+
|
|
5
|
+
describe("eslint rules inventory", () => {
|
|
6
|
+
// New rules and newly deprecated ones of eslint and its plugins show up in
|
|
7
|
+
// the snapshot diff on upgrades.
|
|
8
|
+
it("matches the snapshot", async () => {
|
|
9
|
+
const configs = Object.values(pobConfig.configs)
|
|
10
|
+
.flat()
|
|
11
|
+
.filter((config) => config.name !== "@pob/eslint-config/oxlint");
|
|
12
|
+
await expect(
|
|
13
|
+
`${JSON.stringify(getEslintRulesStatus(configs), null, 2)}\n`,
|
|
14
|
+
).toMatchFileSnapshot("../inventory/eslint-rules.json");
|
|
15
|
+
});
|
|
16
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
1
3
|
import tseslint from "typescript-eslint";
|
|
4
|
+
import oxlintConfig from "../oxlint/base.json" with { type: "json" };
|
|
2
5
|
import baseConfigs from "./_base.js";
|
|
3
6
|
import baseCommonjsConfig from "./base/commonjs.js";
|
|
4
7
|
import baseModuleConfig from "./base/module.js";
|
|
@@ -23,6 +26,10 @@ import typescriptReplaceUnicornConfig from "./plugins/typescript-eslint/typescri
|
|
|
23
26
|
import typescriptPluginRulesConfig from "./plugins/typescript-eslint/typescript-eslint-rules.js";
|
|
24
27
|
import typescriptRulesConfig from "./rules/typescript.js";
|
|
25
28
|
import { apply } from "./utils/apply.js";
|
|
29
|
+
import {
|
|
30
|
+
createOxlintDisableDirectivesProcessor,
|
|
31
|
+
oxlintToEslintRuleNames,
|
|
32
|
+
} from "./utils/oxlint.js";
|
|
26
33
|
|
|
27
34
|
export { apply } from "./utils/apply.js";
|
|
28
35
|
|
|
@@ -44,6 +51,32 @@ if (!tsFiles) {
|
|
|
44
51
|
|
|
45
52
|
const extensions = "{js,cjs,mjs}";
|
|
46
53
|
|
|
54
|
+
const oxlintEslintRuleNames = new Set(
|
|
55
|
+
Object.keys(oxlintConfig.rules).flatMap((ruleName) =>
|
|
56
|
+
oxlintToEslintRuleNames(ruleName),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// turns off the rules checked by oxlint, must be applied last. Only when the
|
|
61
|
+
// project runs oxlint, so the rules are still checked by eslint otherwise.
|
|
62
|
+
const oxlintConfigs = fs.existsSync(path.resolve(".oxlintrc.json"))
|
|
63
|
+
? [
|
|
64
|
+
{
|
|
65
|
+
name: "@pob/eslint-config/oxlint",
|
|
66
|
+
rules: Object.fromEntries(
|
|
67
|
+
[...oxlintEslintRuleNames].map((ruleName) => [ruleName, "off"]),
|
|
68
|
+
),
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "@pob/eslint-config/oxlint-disable-directives",
|
|
72
|
+
files: ["**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx}"],
|
|
73
|
+
processor: createOxlintDisableDirectivesProcessor(
|
|
74
|
+
oxlintEslintRuleNames,
|
|
75
|
+
),
|
|
76
|
+
},
|
|
77
|
+
]
|
|
78
|
+
: [];
|
|
79
|
+
|
|
47
80
|
const testFiles = [
|
|
48
81
|
`**/*.{test,test-e2e,spec}.${extensions}`,
|
|
49
82
|
`**/__tests__/**/*.${extensions}`,
|
|
@@ -169,12 +202,13 @@ export default {
|
|
|
169
202
|
files: ["**/scripts/"],
|
|
170
203
|
configs: [...nodePluginModuleConfigs, scriptsOverrideConfig],
|
|
171
204
|
}),
|
|
205
|
+
...oxlintConfigs,
|
|
172
206
|
],
|
|
173
207
|
/** @deprecated */
|
|
174
|
-
nodeModule,
|
|
208
|
+
nodeModule: [...nodeModule, ...oxlintConfigs],
|
|
175
209
|
/** @deprecated */
|
|
176
|
-
nodeCommonjs,
|
|
177
|
-
node: nodeModule,
|
|
210
|
+
nodeCommonjs: [...nodeCommonjs, ...oxlintConfigs],
|
|
211
|
+
node: [...nodeModule, ...oxlintConfigs],
|
|
178
212
|
|
|
179
213
|
allowImplicitReturnType: applyTs({
|
|
180
214
|
configs: [allowImplicitReturnTypeConfig],
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// (packages/*/scripts, @scope/*/scripts, ...) against the root tools project.
|
|
6
6
|
export default {
|
|
7
7
|
name: "@pob/eslint-config/overrides/tools-project",
|
|
8
|
-
files: ["**/scripts/**/*.{ts,mts,cts}", "*.config.{ts,mts,cts}"],
|
|
8
|
+
files: ["**/{scripts,e2e}/**/*.{ts,mts,cts}", "*.config.{ts,mts,cts}"],
|
|
9
9
|
languageOptions: {
|
|
10
10
|
parserOptions: {
|
|
11
11
|
project: "tsconfig.tools.json",
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mkdir,
|
|
3
|
+
mkdtemp,
|
|
4
|
+
readFile,
|
|
5
|
+
readdir,
|
|
6
|
+
rm,
|
|
7
|
+
writeFile,
|
|
8
|
+
} from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
12
|
+
import { ESLint } from "eslint";
|
|
13
|
+
import {
|
|
14
|
+
getDiagnosticRuleName,
|
|
15
|
+
getEnabledOxlintConfigRules,
|
|
16
|
+
lintWithOxlint,
|
|
17
|
+
oxlintConfigsDir,
|
|
18
|
+
toCanonicalOxlintRuleName,
|
|
19
|
+
} from "./test-utils/oxlint.js";
|
|
20
|
+
import { oxlintToEslintRuleNames } from "./utils/oxlint.js";
|
|
21
|
+
import pobConfig from "./index.js";
|
|
22
|
+
|
|
23
|
+
const packageDir = path.resolve(import.meta.dirname, "..");
|
|
24
|
+
const rootDir = path.resolve(packageDir, "../..");
|
|
25
|
+
const testLintDir = path.join(packageDir, "test-lint");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Rules named by the oxlint-disable directives of `source`, as `plugin/rule`.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} source
|
|
31
|
+
*/
|
|
32
|
+
const getDirectiveRules = (source) =>
|
|
33
|
+
[...source.matchAll(/oxlint-disable(?:-next-line|-line)?([^\n]*)/g)].flatMap(
|
|
34
|
+
([, rest]) =>
|
|
35
|
+
rest
|
|
36
|
+
.split("*/")[0]
|
|
37
|
+
.split("--")[0]
|
|
38
|
+
.split(",")
|
|
39
|
+
.map((rule) => rule.trim())
|
|
40
|
+
.filter(Boolean)
|
|
41
|
+
.map((rule) => toCanonicalOxlintRuleName(rule)),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Removes eslint and oxlint directives, keeping line numbers.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} source
|
|
48
|
+
*/
|
|
49
|
+
const stripDirectives = (source) =>
|
|
50
|
+
source
|
|
51
|
+
.replaceAll(
|
|
52
|
+
/\/\*\s*(?:eslint|oxlint)-(?:disable|enable)[\s\S]*?\*\//g,
|
|
53
|
+
(comment) => comment.replaceAll(/[^\n]/g, ""),
|
|
54
|
+
)
|
|
55
|
+
.replaceAll(/\/\/\s*(?:eslint|oxlint)-(?:disable|enable)[^\n]*/g, "");
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {number[]} lines
|
|
59
|
+
*/
|
|
60
|
+
const sortLines = (lines) => lines.toSorted((a, b) => a - b);
|
|
61
|
+
|
|
62
|
+
const enabledOxlintRules = getEnabledOxlintConfigRules();
|
|
63
|
+
|
|
64
|
+
const testLintDirents = await readdir(testLintDir, {
|
|
65
|
+
recursive: true,
|
|
66
|
+
withFileTypes: true,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/** @type {{ relativePath: string; source: string; oxlintRules: Set<string> }[]} */
|
|
70
|
+
const fixtures = await Promise.all(
|
|
71
|
+
testLintDirents
|
|
72
|
+
.filter((dirent) => dirent.isFile())
|
|
73
|
+
.map(async (dirent) => {
|
|
74
|
+
const filePath = path.join(dirent.parentPath, dirent.name);
|
|
75
|
+
const source = await readFile(filePath, "utf8");
|
|
76
|
+
return {
|
|
77
|
+
relativePath: path.relative(packageDir, filePath),
|
|
78
|
+
source,
|
|
79
|
+
oxlintRules: new Set(getDirectiveRules(source)),
|
|
80
|
+
};
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {string} oxlintRule
|
|
86
|
+
*/
|
|
87
|
+
const getRuleFixtures = (oxlintRule) =>
|
|
88
|
+
fixtures.filter((fixture) => fixture.oxlintRules.has(oxlintRule));
|
|
89
|
+
|
|
90
|
+
describe("oxlint rules fixtures", () => {
|
|
91
|
+
// with P1 directive options, `pnpm lint:oxlint` fails if the rule stops
|
|
92
|
+
// reporting on the fixture
|
|
93
|
+
it.each(enabledOxlintRules)(
|
|
94
|
+
"%s has a test-lint fixture with an oxlint-disable directive",
|
|
95
|
+
(oxlintRule) => {
|
|
96
|
+
expect(getRuleFixtures(oxlintRule)).not.toEqual([]);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("oxlint and eslint parity", () => {
|
|
102
|
+
/** @type {string} */
|
|
103
|
+
let oxlintCwd;
|
|
104
|
+
/** @type {Map<string, { rule: string | undefined; line: number }[]>} */
|
|
105
|
+
const oxlintResults = new Map();
|
|
106
|
+
const eslint = new ESLint({
|
|
107
|
+
cwd: rootDir,
|
|
108
|
+
overrideConfigFile: true,
|
|
109
|
+
overrideConfig: pobConfig.configs.node.filter(
|
|
110
|
+
(config) => config.name !== "@pob/eslint-config/oxlint",
|
|
111
|
+
),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
beforeAll(async () => {
|
|
115
|
+
oxlintCwd = await mkdtemp(path.join(tmpdir(), "pob-oxlint-parity-"));
|
|
116
|
+
await writeFile(
|
|
117
|
+
path.join(oxlintCwd, ".oxlintrc.json"),
|
|
118
|
+
JSON.stringify({ extends: [path.join(oxlintConfigsDir, "base.json")] }),
|
|
119
|
+
);
|
|
120
|
+
for (const fixture of fixtures) {
|
|
121
|
+
if (fixture.oxlintRules.size === 0) continue;
|
|
122
|
+
const filePath = path.join(oxlintCwd, fixture.relativePath);
|
|
123
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
124
|
+
await writeFile(filePath, stripDirectives(fixture.source));
|
|
125
|
+
}
|
|
126
|
+
for (const diagnostic of await lintWithOxlint(oxlintCwd)) {
|
|
127
|
+
const results = oxlintResults.get(diagnostic.filename) ?? [];
|
|
128
|
+
results.push({
|
|
129
|
+
rule: getDiagnosticRuleName(diagnostic),
|
|
130
|
+
line: diagnostic.labels[0].span.line,
|
|
131
|
+
});
|
|
132
|
+
oxlintResults.set(diagnostic.filename, results);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
afterAll(async () => {
|
|
137
|
+
if (oxlintCwd) await rm(oxlintCwd, { recursive: true, force: true });
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it.each(
|
|
141
|
+
enabledOxlintRules.flatMap((oxlintRule) =>
|
|
142
|
+
getRuleFixtures(oxlintRule).map((fixture) => ({ oxlintRule, fixture })),
|
|
143
|
+
),
|
|
144
|
+
)(
|
|
145
|
+
"$oxlintRule reports the same lines as eslint in $fixture.relativePath",
|
|
146
|
+
async ({ oxlintRule, fixture }) => {
|
|
147
|
+
const eslintRuleNames = new Set(oxlintToEslintRuleNames(oxlintRule));
|
|
148
|
+
const [eslintResult] = await eslint.lintText(
|
|
149
|
+
stripDirectives(fixture.source),
|
|
150
|
+
{ filePath: path.join(packageDir, fixture.relativePath) },
|
|
151
|
+
);
|
|
152
|
+
const eslintLines = sortLines(
|
|
153
|
+
eslintResult.messages
|
|
154
|
+
.filter(({ ruleId }) => ruleId && eslintRuleNames.has(ruleId))
|
|
155
|
+
.map(({ line }) => line),
|
|
156
|
+
);
|
|
157
|
+
const oxlintLines = sortLines(
|
|
158
|
+
(oxlintResults.get(fixture.relativePath) ?? [])
|
|
159
|
+
.filter(({ rule }) => rule === oxlintRule)
|
|
160
|
+
.map(({ line }) => line),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
expect(eslintLines).not.toEqual([]);
|
|
164
|
+
expect(oxlintLines).toEqual(eslintLines);
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { beforeAll, describe, expect, it } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
getEnabledOxlintConfigRules,
|
|
5
|
+
listOxlintRules,
|
|
6
|
+
} from "./test-utils/oxlint.js";
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* inventory/oxlint-rules.json records a decision for every oxlint rule of the
|
|
10
|
+
* plugins below, so an oxlint upgrade adding or removing rules fails here.
|
|
11
|
+
* Decisions:
|
|
12
|
+
* - enabled: in oxlint/*.json
|
|
13
|
+
* - migrate: enabled in eslint, to move to oxlint (docs/oxlint-migration.md)
|
|
14
|
+
* - eslint: kept in eslint
|
|
15
|
+
* - evaluate: not used today, to evaluate (Phase 4 of the migration)
|
|
16
|
+
* - rejected: not wanted
|
|
17
|
+
*/
|
|
18
|
+
const plugins = new Set([
|
|
19
|
+
"eslint",
|
|
20
|
+
"typescript",
|
|
21
|
+
"unicorn",
|
|
22
|
+
"import",
|
|
23
|
+
"node",
|
|
24
|
+
"react",
|
|
25
|
+
"jsx_a11y",
|
|
26
|
+
"oxc",
|
|
27
|
+
"promise",
|
|
28
|
+
"vitest",
|
|
29
|
+
]);
|
|
30
|
+
const decisions = new Set([
|
|
31
|
+
"enabled",
|
|
32
|
+
"migrate",
|
|
33
|
+
"eslint",
|
|
34
|
+
"evaluate",
|
|
35
|
+
"rejected",
|
|
36
|
+
]);
|
|
37
|
+
const inventoryPath = new URL(
|
|
38
|
+
"../inventory/oxlint-rules.json",
|
|
39
|
+
import.meta.url,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
describe("oxlint rules inventory", () => {
|
|
43
|
+
/** @type {Record<string, string>} */
|
|
44
|
+
let inventory;
|
|
45
|
+
/** @type {string[]} */
|
|
46
|
+
let oxlintRules;
|
|
47
|
+
|
|
48
|
+
beforeAll(async () => {
|
|
49
|
+
inventory = JSON.parse(await readFile(inventoryPath, "utf8"));
|
|
50
|
+
const rules = await listOxlintRules(import.meta.dirname);
|
|
51
|
+
oxlintRules = rules
|
|
52
|
+
.map(({ name }) => name)
|
|
53
|
+
.filter((name) => plugins.has(name.slice(0, name.indexOf("/"))));
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("has a decision for every oxlint rule", () => {
|
|
57
|
+
const oxlintRuleSet = new Set(oxlintRules);
|
|
58
|
+
expect({
|
|
59
|
+
newRules: oxlintRules.filter((name) => !Object.hasOwn(inventory, name)),
|
|
60
|
+
removedRules: Object.keys(inventory).filter(
|
|
61
|
+
(name) => !oxlintRuleSet.has(name),
|
|
62
|
+
),
|
|
63
|
+
}).toEqual({ newRules: [], removedRules: [] });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("has only known decisions", () => {
|
|
67
|
+
expect(
|
|
68
|
+
Object.entries(inventory).filter(
|
|
69
|
+
([, decision]) => !decisions.has(decision),
|
|
70
|
+
),
|
|
71
|
+
).toEqual([]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("marks as enabled exactly the rules of oxlint/*.json", () => {
|
|
75
|
+
expect(
|
|
76
|
+
Object.keys(inventory)
|
|
77
|
+
.filter((name) => inventory[name] === "enabled")
|
|
78
|
+
.toSorted(),
|
|
79
|
+
).toEqual(getEnabledOxlintConfigRules());
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
5
|
+
import { lintWithOxlint, oxlintConfigsDir } from "./test-utils/oxlint.js";
|
|
6
|
+
|
|
7
|
+
const baseConfigPath = path.join(oxlintConfigsDir, "base.json");
|
|
8
|
+
// this repo's config, the options are not in base.json so projects keep
|
|
9
|
+
// eslint-disable comments working
|
|
10
|
+
const rootConfig = JSON.parse(
|
|
11
|
+
await readFile(
|
|
12
|
+
path.resolve(import.meta.dirname, "../../../.oxlintrc.json"),
|
|
13
|
+
"utf8",
|
|
14
|
+
),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Lints `source` in a project whose .oxlintrc.json extends base.json, like the
|
|
19
|
+
* one written by pob.
|
|
20
|
+
*/
|
|
21
|
+
const lint = async (cwd, source) => {
|
|
22
|
+
await writeFile(path.join(cwd, "file.js"), source);
|
|
23
|
+
return lintWithOxlint(cwd);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const summarize = (diagnostics) =>
|
|
27
|
+
diagnostics.map(({ code, message, labels }) => ({
|
|
28
|
+
line: labels[0].span.line,
|
|
29
|
+
rule: code ?? message,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {Record<string, unknown>} config
|
|
34
|
+
*/
|
|
35
|
+
const useProject = (config) => {
|
|
36
|
+
const project = { cwd: "" };
|
|
37
|
+
|
|
38
|
+
beforeAll(async () => {
|
|
39
|
+
project.cwd = await mkdtemp(path.join(tmpdir(), "pob-oxlint-"));
|
|
40
|
+
await writeFile(
|
|
41
|
+
path.join(project.cwd, ".oxlintrc.json"),
|
|
42
|
+
JSON.stringify({ ...config, extends: [baseConfigPath] }),
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterAll(async () => {
|
|
47
|
+
if (project.cwd) await rm(project.cwd, { recursive: true, force: true });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return project;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
describe("oxlint/base.json", () => {
|
|
54
|
+
const project = useProject({});
|
|
55
|
+
|
|
56
|
+
it("reports the rule", async () => {
|
|
57
|
+
expect(summarize(await lint(project.cwd, "debugger;\n"))).toEqual([
|
|
58
|
+
{ line: 1, rule: "eslint(no-debugger)" },
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("respects oxlint-disable directives", async () => {
|
|
63
|
+
expect(
|
|
64
|
+
await lint(
|
|
65
|
+
project.cwd,
|
|
66
|
+
"// oxlint-disable-next-line no-debugger\ndebugger;\n",
|
|
67
|
+
),
|
|
68
|
+
).toEqual([]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// projects keep their eslint-disable comments when a rule moves to oxlint
|
|
72
|
+
it("respects eslint-disable directives", async () => {
|
|
73
|
+
expect(
|
|
74
|
+
await lint(
|
|
75
|
+
project.cwd,
|
|
76
|
+
"// eslint-disable-next-line no-debugger\ndebugger;\n",
|
|
77
|
+
),
|
|
78
|
+
).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("root .oxlintrc.json options", () => {
|
|
83
|
+
const project = useProject({ options: rootConfig.options });
|
|
84
|
+
|
|
85
|
+
// with these options, test-lint fixtures prove each oxlint rule fires
|
|
86
|
+
it("ignores eslint-disable directives", async () => {
|
|
87
|
+
expect(
|
|
88
|
+
summarize(
|
|
89
|
+
await lint(
|
|
90
|
+
project.cwd,
|
|
91
|
+
"// eslint-disable-next-line no-debugger\ndebugger;\n",
|
|
92
|
+
),
|
|
93
|
+
),
|
|
94
|
+
).toEqual([{ line: 2, rule: "eslint(no-debugger)" }]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("reports unused oxlint-disable directives", async () => {
|
|
98
|
+
const diagnostics = await lint(
|
|
99
|
+
project.cwd,
|
|
100
|
+
"// oxlint-disable-next-line no-debugger\nexport const a = 1;\n",
|
|
101
|
+
);
|
|
102
|
+
expect(diagnostics).toHaveLength(1);
|
|
103
|
+
expect(diagnostics[0]).toMatchObject({
|
|
104
|
+
severity: "error",
|
|
105
|
+
message: expect.stringContaining("Unused oxlint-disable directive"),
|
|
106
|
+
labels: [{ span: { line: 1 } }],
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { builtinRules } from "eslint/use-at-your-own-risk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rules of eslint core and of the plugins loaded by `configs`.
|
|
5
|
+
*
|
|
6
|
+
* @param {import("eslint").Linter.Config[]} configs
|
|
7
|
+
*/
|
|
8
|
+
const getLoadedRules = (configs) => {
|
|
9
|
+
/** @type {Map<string, import("eslint").Rule.RuleModule>} */
|
|
10
|
+
const rules = new Map(builtinRules);
|
|
11
|
+
for (const config of configs) {
|
|
12
|
+
for (const [pluginName, plugin] of Object.entries(config.plugins ?? {})) {
|
|
13
|
+
for (const [ruleName, rule] of Object.entries(plugin.rules ?? {})) {
|
|
14
|
+
rules.set(
|
|
15
|
+
`${pluginName}/${ruleName}`,
|
|
16
|
+
/** @type {import("eslint").Rule.RuleModule} */ (rule),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return rules;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Rules configured in `configs`: true when a config enables it, false when
|
|
26
|
+
* configs only turn it off.
|
|
27
|
+
*
|
|
28
|
+
* @param {import("eslint").Linter.Config[]} configs
|
|
29
|
+
*/
|
|
30
|
+
const getConfiguredRules = (configs) => {
|
|
31
|
+
/** @type {Map<string, boolean>} */
|
|
32
|
+
const configuredRules = new Map();
|
|
33
|
+
for (const config of configs) {
|
|
34
|
+
for (const [ruleName, ruleEntry] of Object.entries(config.rules ?? {})) {
|
|
35
|
+
const severity = Array.isArray(ruleEntry) ? ruleEntry[0] : ruleEntry;
|
|
36
|
+
const enabled = severity !== "off" && severity !== 0;
|
|
37
|
+
configuredRules.set(ruleName, configuredRules.get(ruleName) || enabled);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return configuredRules;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Status of every rule of eslint core and of the plugins loaded by `configs`:
|
|
45
|
+
* `enabled` when a config enables it (for any files), `off` when configs only
|
|
46
|
+
* turn it off, `unset` otherwise. Deprecated rules are listed only when
|
|
47
|
+
* enabled, as `deprecated`.
|
|
48
|
+
*
|
|
49
|
+
* @param {import("eslint").Linter.Config[]} configs
|
|
50
|
+
* @returns {Record<string, "enabled" | "off" | "unset" | "deprecated">}
|
|
51
|
+
*/
|
|
52
|
+
export const getEslintRulesStatus = (configs) => {
|
|
53
|
+
const rules = getLoadedRules(configs);
|
|
54
|
+
const configuredRules = getConfiguredRules(configs);
|
|
55
|
+
|
|
56
|
+
/** @type {Record<string, "enabled" | "off" | "unset" | "deprecated">} */
|
|
57
|
+
const status = {};
|
|
58
|
+
for (const ruleName of [...rules.keys()].toSorted()) {
|
|
59
|
+
const enabled = configuredRules.get(ruleName);
|
|
60
|
+
if (rules.get(ruleName)?.meta?.deprecated) {
|
|
61
|
+
if (enabled) status[ruleName] = "deprecated";
|
|
62
|
+
} else if (enabled === undefined) {
|
|
63
|
+
status[ruleName] = "unset";
|
|
64
|
+
} else {
|
|
65
|
+
status[ruleName] = enabled ? "enabled" : "off";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return status;
|
|
69
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
|
|
9
|
+
const oxlintPackageJsonUrl = import.meta.resolve("oxlint/package.json");
|
|
10
|
+
const { default: oxlintPkg } = await import(oxlintPackageJsonUrl, {
|
|
11
|
+
with: { type: "json" },
|
|
12
|
+
});
|
|
13
|
+
const oxlintBinPath = fileURLToPath(
|
|
14
|
+
new URL(
|
|
15
|
+
typeof oxlintPkg.bin === "string" ? oxlintPkg.bin : oxlintPkg.bin.oxlint,
|
|
16
|
+
oxlintPackageJsonUrl,
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const oxlintConfigsDir = fileURLToPath(
|
|
21
|
+
new URL("../../oxlint/", import.meta.url),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Runs oxlint in `cwd` and parses its JSON output.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} cwd
|
|
28
|
+
* @param {string[]} args
|
|
29
|
+
* @returns {Promise<any>}
|
|
30
|
+
*/
|
|
31
|
+
const execOxlint = async (cwd, args) => {
|
|
32
|
+
try {
|
|
33
|
+
const { stdout } = await execFileAsync(
|
|
34
|
+
process.execPath,
|
|
35
|
+
[oxlintBinPath, ...args],
|
|
36
|
+
{ cwd, maxBuffer: 16 * 1024 * 1024 },
|
|
37
|
+
);
|
|
38
|
+
return JSON.parse(stdout);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const { code, stdout } = /** @type {{ code: unknown; stdout: string }} */ (
|
|
41
|
+
error
|
|
42
|
+
);
|
|
43
|
+
// oxlint exits with code 1 when it reports errors
|
|
44
|
+
if (code !== 1) throw error;
|
|
45
|
+
return JSON.parse(stdout);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {{
|
|
51
|
+
* code?: string;
|
|
52
|
+
* message: string;
|
|
53
|
+
* severity: string;
|
|
54
|
+
* filename: string;
|
|
55
|
+
* labels: { span: { line: number } }[];
|
|
56
|
+
* }} OxlintDiagnostic
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Lints `cwd` with the .oxlintrc.json it contains.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} cwd
|
|
63
|
+
* @returns {Promise<OxlintDiagnostic[]>}
|
|
64
|
+
*/
|
|
65
|
+
export const lintWithOxlint = async (cwd) => {
|
|
66
|
+
const { diagnostics } = await execOxlint(cwd, ["-f", "json"]);
|
|
67
|
+
return diagnostics;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Rules implemented by the installed oxlint, as `plugin/rule`.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} cwd
|
|
74
|
+
* @returns {Promise<{ name: string; typeAware: boolean }[]>}
|
|
75
|
+
*/
|
|
76
|
+
export const listOxlintRules = async (cwd) => {
|
|
77
|
+
/** @type {{ scope: string; value: string; type_aware: boolean }[]} */
|
|
78
|
+
const rules = await execOxlint(cwd, ["--rules", "-f", "json"]);
|
|
79
|
+
return rules.map(({ scope, value, type_aware: typeAware }) => ({
|
|
80
|
+
name: `${scope}/${value}`,
|
|
81
|
+
typeAware,
|
|
82
|
+
}));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `plugin/rule` name, as listed by `oxlint --rules`. Core rules can be written
|
|
87
|
+
* without the `eslint/` prefix in configs and directives.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} ruleName
|
|
90
|
+
*/
|
|
91
|
+
export const toCanonicalOxlintRuleName = (ruleName) =>
|
|
92
|
+
ruleName.includes("/") ? ruleName : `eslint/${ruleName}`;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `plugin/rule` name of a diagnostic, from its code like `jsx-a11y(alt-text)`.
|
|
96
|
+
* Undefined for diagnostics not reported by a rule (unused directives).
|
|
97
|
+
*
|
|
98
|
+
* @param {OxlintDiagnostic} diagnostic
|
|
99
|
+
*/
|
|
100
|
+
export const getDiagnosticRuleName = (diagnostic) => {
|
|
101
|
+
const match = diagnostic.code && /^([^(]+)\(([^)]+)\)$/.exec(diagnostic.code);
|
|
102
|
+
return match ? `${match[1].replaceAll("-", "_")}/${match[2]}` : undefined;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {unknown} ruleEntry
|
|
107
|
+
*/
|
|
108
|
+
const isRuleEnabled = (ruleEntry) => {
|
|
109
|
+
const severity = Array.isArray(ruleEntry) ? ruleEntry[0] : ruleEntry;
|
|
110
|
+
return severity !== "off" && severity !== "allow" && severity !== 0;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Rules enabled in `oxlint/*.json` (including `overrides`), as `plugin/rule`.
|
|
115
|
+
*
|
|
116
|
+
* @returns {string[]}
|
|
117
|
+
*/
|
|
118
|
+
export const getEnabledOxlintConfigRules = () => {
|
|
119
|
+
const ruleNames = new Set();
|
|
120
|
+
for (const fileName of readdirSync(oxlintConfigsDir)) {
|
|
121
|
+
if (!fileName.endsWith(".json")) continue;
|
|
122
|
+
/** @type {{ rules?: Record<string, unknown>; overrides?: { rules?: Record<string, unknown> }[] }} */
|
|
123
|
+
const config = JSON.parse(
|
|
124
|
+
readFileSync(path.join(oxlintConfigsDir, fileName), "utf8"),
|
|
125
|
+
);
|
|
126
|
+
for (const rules of [
|
|
127
|
+
config.rules,
|
|
128
|
+
...(config.overrides ?? []).map((override) => override.rules),
|
|
129
|
+
]) {
|
|
130
|
+
for (const [ruleName, ruleEntry] of Object.entries(rules ?? {})) {
|
|
131
|
+
if (isRuleEnabled(ruleEntry)) {
|
|
132
|
+
ruleNames.add(toCanonicalOxlintRuleName(ruleName));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return [...ruleNames].toSorted();
|
|
138
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import tseslint from "typescript-eslint";
|
|
2
|
+
|
|
3
|
+
/** oxlint plugin name → eslint rule name prefix */
|
|
4
|
+
const eslintRulePrefixes = new Map([
|
|
5
|
+
["eslint", ""],
|
|
6
|
+
["typescript", "@typescript-eslint/"],
|
|
7
|
+
["unicorn", "unicorn/"],
|
|
8
|
+
["import", "import-x/"],
|
|
9
|
+
["node", "n/"],
|
|
10
|
+
["react", "react/"],
|
|
11
|
+
["jsx_a11y", "jsx-a11y/"],
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
/** oxlint plugins without eslint equivalent in @pob/eslint-config */
|
|
15
|
+
const oxlintOnlyPlugins = new Set(["oxc", "promise", "vitest"]);
|
|
16
|
+
|
|
17
|
+
/** oxlint implements eslint-plugin-react-hooks rules in its react plugin */
|
|
18
|
+
const reactHooksRules = new Set(["rules-of-hooks", "exhaustive-deps"]);
|
|
19
|
+
|
|
20
|
+
const typescriptEslintRules =
|
|
21
|
+
/** @type {import("eslint").ESLint.Plugin} */ (tseslint.plugin).rules ?? {};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* eslint core rule → typescript-eslint rules extending it. Type-aware ones are
|
|
25
|
+
* not listed: oxlint's core rule does not replace them (see TA1 in
|
|
26
|
+
* docs/oxlint-migration.md).
|
|
27
|
+
*
|
|
28
|
+
* @type {Map<string, string[]>}
|
|
29
|
+
*/
|
|
30
|
+
const typescriptExtensionRules = new Map();
|
|
31
|
+
for (const [ruleName, rule] of Object.entries(typescriptEslintRules)) {
|
|
32
|
+
const docs =
|
|
33
|
+
/** @type {{ extendsBaseRule?: boolean | string; requiresTypeChecking?: boolean } | undefined} */ (
|
|
34
|
+
rule.meta?.docs
|
|
35
|
+
);
|
|
36
|
+
if (!docs?.extendsBaseRule || docs.requiresTypeChecking) continue;
|
|
37
|
+
const baseRuleName =
|
|
38
|
+
docs.extendsBaseRule === true ? ruleName : docs.extendsBaseRule;
|
|
39
|
+
typescriptExtensionRules.set(baseRuleName, [
|
|
40
|
+
...(typescriptExtensionRules.get(baseRuleName) ?? []),
|
|
41
|
+
`@typescript-eslint/${ruleName}`,
|
|
42
|
+
]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the eslint rule names checked by an oxlint rule, to turn them off in
|
|
47
|
+
* eslint. Rules of plugins other than eslint core must be prefixed with their
|
|
48
|
+
* oxlint plugin name (`typescript/`, `unicorn/`, ...).
|
|
49
|
+
*
|
|
50
|
+
* @param {string} oxlintRuleName
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export const oxlintToEslintRuleNames = (oxlintRuleName) => {
|
|
54
|
+
const slashIndex = oxlintRuleName.indexOf("/");
|
|
55
|
+
const pluginName =
|
|
56
|
+
slashIndex === -1 ? "eslint" : oxlintRuleName.slice(0, slashIndex);
|
|
57
|
+
const ruleName = oxlintRuleName.slice(slashIndex + 1);
|
|
58
|
+
|
|
59
|
+
if (oxlintOnlyPlugins.has(pluginName)) return [];
|
|
60
|
+
if (pluginName === "eslint") {
|
|
61
|
+
return [ruleName, ...(typescriptExtensionRules.get(ruleName) ?? [])];
|
|
62
|
+
}
|
|
63
|
+
if (pluginName === "react" && reactHooksRules.has(ruleName)) {
|
|
64
|
+
return [`react-hooks/${ruleName}`];
|
|
65
|
+
}
|
|
66
|
+
const prefix = eslintRulePrefixes.get(pluginName);
|
|
67
|
+
if (prefix === undefined) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Unknown oxlint plugin "${pluginName}" in rule "${oxlintRuleName}"`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return [`${prefix}${ruleName}`];
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const unusedDisableDirectiveRegExp =
|
|
76
|
+
/^Unused eslint-disable directive \(no problems were reported from (.+)\)\.$/;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Rules named by eslint's "Unused eslint-disable directive" report, undefined
|
|
80
|
+
* for other messages.
|
|
81
|
+
*
|
|
82
|
+
* @param {import("eslint").Linter.LintMessage} message
|
|
83
|
+
* @returns {string[] | undefined}
|
|
84
|
+
*/
|
|
85
|
+
export const getUnusedDisableDirectiveRuleNames = (message) => {
|
|
86
|
+
if (message.ruleId !== null) return undefined;
|
|
87
|
+
const match = unusedDisableDirectiveRegExp.exec(message.message);
|
|
88
|
+
if (!match) return undefined;
|
|
89
|
+
return [...match[1].matchAll(/'([^']+)'/g)].map(([, ruleName]) => ruleName);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Drops eslint's "Unused eslint-disable directive" reports for rules checked
|
|
94
|
+
* by oxlint, so projects keep their `eslint-disable` comments of moved rules:
|
|
95
|
+
* oxlint honors them, and `eslint --fix` no longer deletes them. Unused
|
|
96
|
+
* directives of other rules are still reported and fixed.
|
|
97
|
+
*
|
|
98
|
+
* @param {Set<string>} oxlintEslintRuleNames eslint names of the rules checked
|
|
99
|
+
* by oxlint
|
|
100
|
+
* @returns {import("eslint").Linter.Processor}
|
|
101
|
+
*/
|
|
102
|
+
export const createOxlintDisableDirectivesProcessor = (
|
|
103
|
+
oxlintEslintRuleNames,
|
|
104
|
+
) => ({
|
|
105
|
+
meta: { name: "@pob/eslint-config/oxlint-disable-directives" },
|
|
106
|
+
supportsAutofix: true,
|
|
107
|
+
preprocess: (text) => [text],
|
|
108
|
+
postprocess: (messageLists) =>
|
|
109
|
+
messageLists.flat().filter((message) => {
|
|
110
|
+
const ruleNames = getUnusedDisableDirectiveRuleNames(message);
|
|
111
|
+
return !ruleNames?.every((ruleName) =>
|
|
112
|
+
oxlintEslintRuleNames.has(ruleName),
|
|
113
|
+
);
|
|
114
|
+
}),
|
|
115
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Linter } from "eslint";
|
|
3
|
+
import { builtinRules } from "eslint/use-at-your-own-risk";
|
|
4
|
+
import importPlugin from "eslint-plugin-import-x";
|
|
5
|
+
import nodePlugin from "eslint-plugin-n";
|
|
6
|
+
import eslintPluginUnicorn from "eslint-plugin-unicorn";
|
|
7
|
+
import tseslint from "typescript-eslint";
|
|
8
|
+
import oxlintConfig from "../../oxlint/base.json" with { type: "json" };
|
|
9
|
+
import {
|
|
10
|
+
createOxlintDisableDirectivesProcessor,
|
|
11
|
+
getUnusedDisableDirectiveRuleNames,
|
|
12
|
+
oxlintToEslintRuleNames,
|
|
13
|
+
} from "./oxlint.js";
|
|
14
|
+
|
|
15
|
+
/** @type {import("eslint").Linter.Config} */
|
|
16
|
+
const baseConfig = {
|
|
17
|
+
linterOptions: { reportUnusedDisableDirectives: "error" },
|
|
18
|
+
rules: { "no-debugger": "off", "no-unreachable": "error" },
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
describe("getUnusedDisableDirectiveRuleNames", () => {
|
|
22
|
+
// fails if an eslint upgrade rewords the message the processor relies on
|
|
23
|
+
it.each([
|
|
24
|
+
["no-debugger", ["no-debugger"]],
|
|
25
|
+
["no-debugger, no-console", ["no-debugger", "no-console"]],
|
|
26
|
+
[
|
|
27
|
+
"no-debugger, no-console, no-alert",
|
|
28
|
+
["no-debugger", "no-console", "no-alert"],
|
|
29
|
+
],
|
|
30
|
+
])(
|
|
31
|
+
"parses eslint's report for eslint-disable-next-line %s",
|
|
32
|
+
(ruleNames, expected) => {
|
|
33
|
+
const messages = new Linter().verify(
|
|
34
|
+
`// eslint-disable-next-line ${ruleNames}\nexport const a = 1;\n`,
|
|
35
|
+
baseConfig,
|
|
36
|
+
);
|
|
37
|
+
expect(messages).toHaveLength(1);
|
|
38
|
+
expect(getUnusedDisableDirectiveRuleNames(messages[0])).toEqual(expected);
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
it("ignores other messages", () => {
|
|
43
|
+
const messages = new Linter().verify(
|
|
44
|
+
"export function f() {\n return;\n f();\n}\n",
|
|
45
|
+
baseConfig,
|
|
46
|
+
);
|
|
47
|
+
expect(messages.map(({ ruleId }) => ruleId)).toEqual(["no-unreachable"]);
|
|
48
|
+
expect(getUnusedDisableDirectiveRuleNames(messages[0])).toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("createOxlintDisableDirectivesProcessor", () => {
|
|
53
|
+
/** @type {import("eslint").Linter.Config[]} */
|
|
54
|
+
const configs = [
|
|
55
|
+
baseConfig,
|
|
56
|
+
{
|
|
57
|
+
files: ["**/*.js"],
|
|
58
|
+
processor: createOxlintDisableDirectivesProcessor(
|
|
59
|
+
new Set(["no-debugger"]),
|
|
60
|
+
),
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
const source = [
|
|
64
|
+
"// eslint-disable-next-line no-debugger",
|
|
65
|
+
"export const a = 1;",
|
|
66
|
+
"// eslint-disable-next-line no-unreachable",
|
|
67
|
+
"export const b = 1;",
|
|
68
|
+
"",
|
|
69
|
+
].join("\n");
|
|
70
|
+
|
|
71
|
+
it("drops unused directive reports of rules checked by oxlint only", () => {
|
|
72
|
+
const messages = new Linter().verify(source, configs, "file.js");
|
|
73
|
+
expect(
|
|
74
|
+
messages.map((message) => getUnusedDisableDirectiveRuleNames(message)),
|
|
75
|
+
).toEqual([["no-unreachable"]]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("keeps the comments of rules checked by oxlint on --fix", () => {
|
|
79
|
+
const { output } = new Linter().verifyAndFix(source, configs, "file.js");
|
|
80
|
+
// eslint's fix leaves the indentation of the removed comment
|
|
81
|
+
expect(output.split("\n").map((line) => line.trim())).toEqual([
|
|
82
|
+
"// eslint-disable-next-line no-debugger",
|
|
83
|
+
"export const a = 1;",
|
|
84
|
+
"",
|
|
85
|
+
"export const b = 1;",
|
|
86
|
+
"",
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("oxlintToEslintRuleNames", () => {
|
|
92
|
+
it.each([
|
|
93
|
+
["no-debugger", ["no-debugger"]],
|
|
94
|
+
["eslint/no-debugger", ["no-debugger"]],
|
|
95
|
+
["typescript/no-explicit-any", ["@typescript-eslint/no-explicit-any"]],
|
|
96
|
+
["unicorn/prefer-node-protocol", ["unicorn/prefer-node-protocol"]],
|
|
97
|
+
["import/no-cycle", ["import-x/no-cycle"]],
|
|
98
|
+
["node/no-path-concat", ["n/no-path-concat"]],
|
|
99
|
+
["jsx_a11y/alt-text", ["jsx-a11y/alt-text"]],
|
|
100
|
+
["react/jsx-key", ["react/jsx-key"]],
|
|
101
|
+
["react/rules-of-hooks", ["react-hooks/rules-of-hooks"]],
|
|
102
|
+
["react/exhaustive-deps", ["react-hooks/exhaustive-deps"]],
|
|
103
|
+
["oxc/no-map-spread", []],
|
|
104
|
+
["promise/valid-params", []],
|
|
105
|
+
["vitest/no-focused-tests", []],
|
|
106
|
+
])("maps %s", (oxlintRuleName, eslintRuleNames) => {
|
|
107
|
+
expect(oxlintToEslintRuleNames(oxlintRuleName)).toEqual(eslintRuleNames);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it.each([
|
|
111
|
+
["no-unused-vars", ["no-unused-vars", "@typescript-eslint/no-unused-vars"]],
|
|
112
|
+
[
|
|
113
|
+
"eslint/no-redeclare",
|
|
114
|
+
["no-redeclare", "@typescript-eslint/no-redeclare"],
|
|
115
|
+
],
|
|
116
|
+
[
|
|
117
|
+
"no-useless-constructor",
|
|
118
|
+
["no-useless-constructor", "@typescript-eslint/no-useless-constructor"],
|
|
119
|
+
],
|
|
120
|
+
])(
|
|
121
|
+
"maps extension rule %s to the core and typescript-eslint rules",
|
|
122
|
+
(oxlintRuleName, eslintRuleNames) => {
|
|
123
|
+
expect(oxlintToEslintRuleNames(oxlintRuleName)).toEqual(eslintRuleNames);
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
it.each([
|
|
128
|
+
["require-await"],
|
|
129
|
+
["dot-notation"],
|
|
130
|
+
["no-implied-eval"],
|
|
131
|
+
["no-throw-literal"],
|
|
132
|
+
])("keeps type-aware extension rules of %s in eslint", (oxlintRuleName) => {
|
|
133
|
+
expect(oxlintToEslintRuleNames(oxlintRuleName)).toEqual([oxlintRuleName]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("throws on unknown plugins", () => {
|
|
137
|
+
expect(() => oxlintToEslintRuleNames("jsdoc/check-tag-names")).toThrow(
|
|
138
|
+
'Unknown oxlint plugin "jsdoc" in rule "jsdoc/check-tag-names"',
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("oxlint/base.json", () => {
|
|
144
|
+
/** @type {[string, unknown][]} */
|
|
145
|
+
const plugins = [
|
|
146
|
+
["@typescript-eslint/", tseslint.plugin],
|
|
147
|
+
["unicorn/", eslintPluginUnicorn],
|
|
148
|
+
["import-x/", importPlugin],
|
|
149
|
+
["n/", nodePlugin],
|
|
150
|
+
];
|
|
151
|
+
const eslintRuleNames = new Set([
|
|
152
|
+
...builtinRules.keys(),
|
|
153
|
+
...plugins.flatMap(([prefix, plugin]) =>
|
|
154
|
+
Object.keys(
|
|
155
|
+
/** @type {import("eslint").ESLint.Plugin} */ (plugin).rules ?? {},
|
|
156
|
+
).map((ruleName) => `${prefix}${ruleName}`),
|
|
157
|
+
),
|
|
158
|
+
]);
|
|
159
|
+
|
|
160
|
+
it.each(Object.keys(oxlintConfig.rules))(
|
|
161
|
+
"%s turns off existing eslint rules",
|
|
162
|
+
(oxlintRuleName) => {
|
|
163
|
+
const mapped = oxlintToEslintRuleNames(oxlintRuleName);
|
|
164
|
+
expect(mapped.length).toBeGreaterThan(0);
|
|
165
|
+
expect(mapped.filter((name) => !eslintRuleNames.has(name))).toEqual([]);
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
});
|
package/oxlint/base.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pob/eslint-config",
|
|
3
|
-
"version": "67.
|
|
3
|
+
"version": "67.5.0",
|
|
4
4
|
"description": "eslint config files",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"homepage": "https://github.com/christophehurpeau/pob",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"lib",
|
|
19
|
+
"oxlint/base.json",
|
|
19
20
|
"utils.js"
|
|
20
21
|
],
|
|
21
22
|
"type": "module",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"./lib/rules/code-quality": "./lib/rules/code-quality.js",
|
|
28
29
|
"./lib/rules/errors": "./lib/rules/errors.js",
|
|
29
30
|
"./lib/rules/style": "./lib/rules/style.js",
|
|
31
|
+
"./oxlint/base.json": "./oxlint/base.json",
|
|
30
32
|
"./utils": "./utils.js",
|
|
31
33
|
"./package.json": "./package.json"
|
|
32
34
|
},
|
|
@@ -38,18 +40,18 @@
|
|
|
38
40
|
"@eslint/json": "^1.2.0",
|
|
39
41
|
"@typescript-eslint/eslint-plugin": "8.63.0",
|
|
40
42
|
"@typescript-eslint/parser": "8.63.0",
|
|
41
|
-
"check-package-dependencies": "^13.
|
|
43
|
+
"check-package-dependencies": "^13.2.0",
|
|
42
44
|
"confusing-browser-globals": "^1.0.11",
|
|
43
45
|
"eslint-plugin-import-x": "^4.17.1",
|
|
44
46
|
"eslint-plugin-n": "^17.24.0",
|
|
45
47
|
"eslint-plugin-regexp": "^3.1.0",
|
|
46
48
|
"eslint-plugin-unicorn": "^64.0.0",
|
|
47
49
|
"typescript-eslint": "^8.63.0",
|
|
48
|
-
"@pob/eslint-plugin": "66.
|
|
50
|
+
"@pob/eslint-plugin": "66.2.0"
|
|
49
51
|
},
|
|
50
52
|
"devDependencies": {
|
|
51
53
|
"@types/node": "24.13.3",
|
|
52
|
-
"eslint": "10.8.
|
|
54
|
+
"eslint": "10.8.1",
|
|
53
55
|
"eslint-plugin-import-x": "4.17.1",
|
|
54
56
|
"eslint-plugin-n": "17.24.0",
|
|
55
57
|
"eslint-plugin-regexp": "3.1.0",
|
|
@@ -71,7 +73,8 @@
|
|
|
71
73
|
},
|
|
72
74
|
"pob": {},
|
|
73
75
|
"scripts": {
|
|
74
|
-
"lint": "pnpm run lint:eslint",
|
|
75
|
-
"lint:eslint": "pnpm
|
|
76
|
+
"lint": "pnpm run lint:oxlint && pnpm run lint:eslint",
|
|
77
|
+
"lint:eslint": "pnpm --dir '../..' exec eslint --quiet '@pob/eslint-config'",
|
|
78
|
+
"lint:oxlint": "pnpm --dir '../..' exec oxlint '@pob/eslint-config'"
|
|
76
79
|
}
|
|
77
80
|
}
|