@jandragsbaek/oxlint-plugin 0.1.0 → 0.2.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/README.md +58 -33
- package/dist/index.mjs +369 -327
- package/dist/presets.d.mts +0 -15
- package/dist/presets.mjs +10 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @jandragsbaek/oxlint-plugin
|
|
2
2
|
|
|
3
3
|
Oxlint rules and presets, learned in real projects and shared between them. The rules keep
|
|
4
|
-
tests deterministic, Effect code idiomatic, React code honest about its effects,
|
|
5
|
-
and
|
|
4
|
+
tests deterministic, Effect code idiomatic, React code honest about its effects, files short,
|
|
5
|
+
and code free of comments that explain it.
|
|
6
6
|
|
|
7
7
|
The rules started in [Glose](https://github.com/jandragsbaek/glose) and are adapted from
|
|
8
8
|
[T3 Code](https://github.com/pingdotgg/t3code) under its MIT license.
|
|
@@ -45,17 +45,18 @@ them all.
|
|
|
45
45
|
|
|
46
46
|
### Presets
|
|
47
47
|
|
|
48
|
-
| Preset | For
|
|
49
|
-
| ---------------------------- |
|
|
50
|
-
| `recommended` | Any TypeScript project. Built-in rule choices, plus `namespace-node-imports`, `no-
|
|
51
|
-
| `tests` | Projects with Vitest tests. The rules read the filename and check only test files.
|
|
52
|
-
| `effect` | Projects on [Effect](https://effect.website). `prefer-effect-fn` is off in test files.
|
|
53
|
-
| `react` | React projects. Strict hook and render rules, and `no-unreasoned-state-sync`.
|
|
54
|
-
| `reactCompiler({ files })` | Files the React Compiler compiles. The compiler owns memoization. Use with `react`.
|
|
55
|
-
| `productCopy({ files })` | Code that holds text users or agents read: no em dashes.
|
|
56
|
-
| `uiText({ files, message })` | Interface code with i18n catalogs: no inline JSX text. `message` tells the reader where the catalogs are.
|
|
48
|
+
| Preset | For |
|
|
49
|
+
| ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
50
|
+
| `recommended` | Any TypeScript project. Built-in rule choices, plus `max-file-lines`, `max-file-lines-hard`, `namespace-node-imports`, `no-explanatory-comments`, `no-unscoped-has`, `require-suppression-reason`. |
|
|
51
|
+
| `tests` | Projects with Vitest tests. The rules read the filename and check only test files. |
|
|
52
|
+
| `effect` | Projects on [Effect](https://effect.website). `prefer-effect-fn` is off in test files. |
|
|
53
|
+
| `react` | React projects. Strict hook and render rules, and `no-unreasoned-state-sync`. |
|
|
54
|
+
| `reactCompiler({ files })` | Files the React Compiler compiles. The compiler owns memoization. Use with `react`. |
|
|
55
|
+
| `productCopy({ files })` | Code that holds text users or agents read: no em dashes. |
|
|
56
|
+
| `uiText({ files, message })` | Interface code with i18n catalogs: no inline JSX text. `message` tells the reader where the catalogs are. |
|
|
57
57
|
|
|
58
|
-
The presets that take `files` skip test files. `excludeFiles` skips more.
|
|
58
|
+
The presets that take `files` skip test files. `excludeFiles` skips more. Globs in `files` and
|
|
59
|
+
`excludeFiles` are relative to the config file. Glose's setup:
|
|
59
60
|
|
|
60
61
|
```ts
|
|
61
62
|
extends: [
|
|
@@ -76,27 +77,51 @@ extends: [
|
|
|
76
77
|
|
|
77
78
|
All rules have the `jandragsbaek/` prefix.
|
|
78
79
|
|
|
79
|
-
| Rule | Rejects
|
|
80
|
-
| ------------------------------------ |
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `no-
|
|
85
|
-
| `no-
|
|
86
|
-
| `no-
|
|
87
|
-
| `no-
|
|
88
|
-
| `no-
|
|
89
|
-
| `no-
|
|
90
|
-
| `no-
|
|
91
|
-
| `no-
|
|
92
|
-
| `no-
|
|
93
|
-
| `no-
|
|
94
|
-
| `no-
|
|
95
|
-
| `no-
|
|
96
|
-
| `no-
|
|
97
|
-
| `
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
80
|
+
| Rule | Rejects |
|
|
81
|
+
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
82
|
+
| `max-file-lines` | Files longer than 500 lines (option `max`). A file that must stay long says why: `/* oxlint-disable jandragsbaek/max-file-lines -- <why> */`. |
|
|
83
|
+
| `max-file-lines-hard` | Files longer than 750 lines (option `max`). No disable directive silences it: split the file. |
|
|
84
|
+
| `namespace-node-imports` | Node built-ins imported other than as a namespace with the canonical name (`import * as NodeFS from "node:fs"`). |
|
|
85
|
+
| `no-dom-test-environment` | Per-file Vitest environment pragmas and DOM emulator imports (jsdom, happy-dom). |
|
|
86
|
+
| `no-em-dash-copy` | Em dashes in strings and JSX text. |
|
|
87
|
+
| `no-explanatory-comments` | Every comment except a license header at the top of the file and tool directives. See [Comments](#comments). |
|
|
88
|
+
| `no-hookless-use-function` | `use*` functions that call no hook; the React Compiler skips them. |
|
|
89
|
+
| `no-inline-schema-compile` | Effect Schema decoders and encoders compiled inside function bodies. |
|
|
90
|
+
| `no-manual-effect-runtime-in-tests` | Effect runtimes made or run by hand in tests; use `@effect/vitest`. |
|
|
91
|
+
| `no-manual-memo` | `useMemo`, `useCallback` and memo directives where the React Compiler memoizes. |
|
|
92
|
+
| `no-polling-loop-in-tests` | Test loops that spin on ticks until a condition holds. |
|
|
93
|
+
| `no-real-time-wait-in-tests` | Real-clock waits in tests. |
|
|
94
|
+
| `no-test-in-loop` | Tests declared in a `for` loop; use `.each`. |
|
|
95
|
+
| `no-test-timeout-or-unreasoned-skip` | Per-test timeouts and retries, and skipped or focused tests with no tracking issue. |
|
|
96
|
+
| `no-unreasoned-state-sync` | Every React effect hook, until a suppression above it says what it keeps in step. |
|
|
97
|
+
| `no-unscoped-has` | Tailwind `has-[...]` variants not anchored to a class, attribute, id or tag. |
|
|
98
|
+
| `no-untranslated-ui-text` | JSX text and user-facing attributes that are not read from a catalog. |
|
|
99
|
+
| `no-wall-clock-in-tests` | `Date.now()`, `new Date()`, `performance.now()` and `process.hrtime` in tests. |
|
|
100
|
+
| `prefer-catch-tags` | `Effect.catchTag`; use `Effect.catchTags`. |
|
|
101
|
+
| `prefer-effect-fn` | Functions that return `Effect.gen`; use `Effect.fn`. |
|
|
102
|
+
| `require-suppression-reason` | Lint and type-checker suppressions with no reason in the directive itself. |
|
|
103
|
+
|
|
104
|
+
### Comments
|
|
105
|
+
|
|
106
|
+
The code explains itself: names, types, small functions and tests carry what a comment would.
|
|
107
|
+
`no-explanatory-comments` allows only:
|
|
108
|
+
|
|
109
|
+
- A license header: a comment block before any code that carries a license or copyright notice
|
|
110
|
+
(`SPDX-License-Identifier`, `Copyright`, `@license`, `/*!`, an MIT or "Licensed under" notice).
|
|
111
|
+
- Tool directives, which a tool reads:
|
|
112
|
+
- lint: `oxlint-disable*`, `oxlint-enable`, `eslint-disable*`, `eslint-enable`, `biome-ignore`
|
|
113
|
+
- type checker: `@ts-expect-error`, `@ts-ignore`, `@ts-nocheck`, `@ts-check`,
|
|
114
|
+
`/// <reference …>`, `/// <amd-module …>`, `/// <amd-dependency …>`, `@effect-diagnostics`
|
|
115
|
+
- bundler and JSX: `@__PURE__`, `@__NO_SIDE_EFFECTS__` (and their `#__` forms), `@jsx`, `@jsxFrag`,
|
|
116
|
+
`@jsxImportSource`, `@jsxRuntime`, `@vite-ignore`, `webpack*:` and `turbopack*:` magic
|
|
117
|
+
comments, `# sourceMappingURL=`, `# sourceURL=`
|
|
118
|
+
- formatter: `oxfmt-ignore`, `prettier-ignore`, `dprint-ignore`
|
|
119
|
+
- coverage: `v8 ignore`, `c8 ignore`, `istanbul ignore`, `node:coverage`
|
|
120
|
+
- tests and editors: `@vitest-environment`, `@jest-environment`, `@refresh reset`, `cspell:`,
|
|
121
|
+
and `quarantine: #<issue>` above a skipped test (read by `no-test-timeout-or-unreasoned-skip`)
|
|
122
|
+
|
|
123
|
+
Everything else fails: JSDoc, TODOs, section headers and "why" comments. A suppression's reason
|
|
124
|
+
goes in the directive, after `--`.
|
|
100
125
|
|
|
101
126
|
## Add a rule
|
|
102
127
|
|
package/dist/index.mjs
CHANGED
|
@@ -62,168 +62,6 @@ Object.freeze({
|
|
|
62
62
|
throw Error("Cannot access `context.parserPath` in `createOnce`");
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
-
// src/rules/namespace-node-imports.ts
|
|
66
|
-
const NODE_MODULE_ALIASES = /* @__PURE__ */ new Map([["assert/strict", "Assert"], ["fs/promises", "FSP"]]);
|
|
67
|
-
const NODE_SEGMENT_ALIASES = /* @__PURE__ */ new Map([
|
|
68
|
-
["fs", "FS"],
|
|
69
|
-
["os", "OS"],
|
|
70
|
-
["url", "URL"],
|
|
71
|
-
["vm", "VM"]
|
|
72
|
-
]);
|
|
73
|
-
const toPascalCase = (value) => value.split(/[_-]/u).filter((segment) => segment.length > 0).map((segment) => segment[0]?.toUpperCase() + segment.slice(1)).join("");
|
|
74
|
-
const expectedNamespaceAlias = (source) => {
|
|
75
|
-
const moduleName = source.slice(5);
|
|
76
|
-
const knownAlias = NODE_MODULE_ALIASES.get(moduleName);
|
|
77
|
-
if (knownAlias !== void 0) return `Node${knownAlias}`;
|
|
78
|
-
return `Node${moduleName.split("/").map((segment) => NODE_SEGMENT_ALIASES.get(segment) ?? toPascalCase(segment)).join("")}`;
|
|
79
|
-
};
|
|
80
|
-
const literalStringValue = (node) => {
|
|
81
|
-
if (typeof node !== "object" || node === null) return void 0;
|
|
82
|
-
if (!("type" in node) || node.type !== "Literal") return void 0;
|
|
83
|
-
if (!("value" in node) || typeof node.value !== "string") return void 0;
|
|
84
|
-
return node.value;
|
|
85
|
-
};
|
|
86
|
-
const identifierName = (node) => {
|
|
87
|
-
if (typeof node !== "object" || node === null) return void 0;
|
|
88
|
-
if (!("type" in node) || node.type !== "Identifier") return void 0;
|
|
89
|
-
if (!("name" in node) || typeof node.name !== "string") return void 0;
|
|
90
|
-
return node.name;
|
|
91
|
-
};
|
|
92
|
-
var namespace_node_imports_default = defineRule({
|
|
93
|
-
meta: {
|
|
94
|
-
type: "problem",
|
|
95
|
-
docs: { description: "Require canonical namespace imports for Node.js built-in modules." }
|
|
96
|
-
},
|
|
97
|
-
create(context) {
|
|
98
|
-
return { ImportDeclaration(node) {
|
|
99
|
-
const source = literalStringValue(node.source);
|
|
100
|
-
if (source === void 0 || !source.startsWith("node:")) return;
|
|
101
|
-
const expectedAlias = expectedNamespaceAlias(source);
|
|
102
|
-
const namespaceImport = node.specifiers.length === 1 && node.specifiers[0]?.type === "ImportNamespaceSpecifier" ? node.specifiers[0] : void 0;
|
|
103
|
-
if (identifierName(namespaceImport?.local) === expectedAlias) return;
|
|
104
|
-
context.report({
|
|
105
|
-
node,
|
|
106
|
-
message: `Import ${source} as a namespace named ${expectedAlias}.`
|
|
107
|
-
});
|
|
108
|
-
} };
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
// src/rules/no-dom-test-environment.ts
|
|
112
|
-
const DOM_ENVIRONMENT_PACKAGE = /^(?:jsdom|happy-dom)(?:\/|$)/u;
|
|
113
|
-
const PRAGMA = ["@vitest", "environment"].join("-");
|
|
114
|
-
const MESSAGE$5 = "Select test environments in Vitest project configuration instead of a per-file pragma or a DOM emulator import. Web DOM tests belong in *.browser.test.{ts,tsx}.";
|
|
115
|
-
var no_dom_test_environment_default = defineRule({
|
|
116
|
-
meta: {
|
|
117
|
-
type: "problem",
|
|
118
|
-
docs: { description: "Reject per-file Vitest environments and DOM emulator imports." }
|
|
119
|
-
},
|
|
120
|
-
create(context) {
|
|
121
|
-
const checkImport = (source) => {
|
|
122
|
-
if (source === null) return;
|
|
123
|
-
const value = source.type === "Literal" ? source.value : source.type === "TemplateLiteral" && source.expressions.length === 0 ? source.quasis[0]?.value.cooked : void 0;
|
|
124
|
-
if (typeof value === "string" && DOM_ENVIRONMENT_PACKAGE.test(value)) context.report({
|
|
125
|
-
node: source,
|
|
126
|
-
message: MESSAGE$5
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
return {
|
|
130
|
-
Program() {
|
|
131
|
-
const index = context.sourceCode.text.indexOf(PRAGMA);
|
|
132
|
-
if (index !== -1) context.report({
|
|
133
|
-
loc: context.sourceCode.getLocFromIndex(index),
|
|
134
|
-
message: MESSAGE$5
|
|
135
|
-
});
|
|
136
|
-
},
|
|
137
|
-
ImportDeclaration: (node) => checkImport(node.source),
|
|
138
|
-
ImportExpression: (node) => checkImport(node.source),
|
|
139
|
-
ExportNamedDeclaration: (node) => checkImport(node.source),
|
|
140
|
-
ExportAllDeclaration: (node) => checkImport(node.source),
|
|
141
|
-
TSExternalModuleReference: (node) => checkImport(node.expression),
|
|
142
|
-
CallExpression(node) {
|
|
143
|
-
if (node.callee.type === "Identifier" && node.callee.name === "require") checkImport(node.arguments[0] ?? null);
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
// src/rules/no-em-dash-copy.ts
|
|
149
|
-
const EM_DASH = "—";
|
|
150
|
-
const MESSAGE$4 = "No em dashes in product copy. Use a period, comma, colon, or parentheses instead.";
|
|
151
|
-
var no_em_dash_copy_default = defineRule({
|
|
152
|
-
meta: {
|
|
153
|
-
type: "suggestion",
|
|
154
|
-
docs: { description: "Disallow em dashes in strings and JSX text that users or agents read." }
|
|
155
|
-
},
|
|
156
|
-
create(context) {
|
|
157
|
-
const check = (node, text) => {
|
|
158
|
-
if (text?.includes(EM_DASH)) context.report({
|
|
159
|
-
node,
|
|
160
|
-
message: MESSAGE$4
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
return {
|
|
164
|
-
Literal(node) {
|
|
165
|
-
if (typeof node.value === "string") check(node, node.value);
|
|
166
|
-
},
|
|
167
|
-
TemplateElement(node) {
|
|
168
|
-
check(node, node.value.cooked);
|
|
169
|
-
},
|
|
170
|
-
JSXText(node) {
|
|
171
|
-
check(node, node.value);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
// src/rules/no-hookless-use-function.ts
|
|
177
|
-
const HOOK_NAME = /^use[A-Z]/;
|
|
178
|
-
const HOOK_CALLEE = /^use([A-Z]|$)/;
|
|
179
|
-
function isFunctionNode(node) {
|
|
180
|
-
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
181
|
-
}
|
|
182
|
-
function isHookCallee(callee) {
|
|
183
|
-
if (callee.type === "Identifier") return HOOK_CALLEE.test(callee.name);
|
|
184
|
-
if (callee.type === "MemberExpression" && !callee.computed) return callee.property.type === "Identifier" && HOOK_CALLEE.test(callee.property.name);
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
function callsHook(node, root) {
|
|
188
|
-
if (typeof node !== "object" || node === null) return false;
|
|
189
|
-
if (Array.isArray(node)) return node.some((child) => callsHook(child, root));
|
|
190
|
-
if (!("type" in node) || typeof node.type !== "string") return false;
|
|
191
|
-
const typed = node;
|
|
192
|
-
if (typed !== root && isFunctionNode(typed)) return false;
|
|
193
|
-
if (typed.type === "CallExpression" && isHookCallee(typed.callee)) return true;
|
|
194
|
-
for (const [key, value] of Object.entries(typed)) {
|
|
195
|
-
if (key === "parent") continue;
|
|
196
|
-
if (callsHook(value, root)) return true;
|
|
197
|
-
}
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
var no_hookless_use_function_default = defineRule({
|
|
201
|
-
meta: {
|
|
202
|
-
type: "problem",
|
|
203
|
-
docs: { description: "Disallow use* functions that call no hooks; the React Compiler only compiles functions that do. Mirrors eslint-react's no-unnecessary-use-prefix, which oxlint does not ship." },
|
|
204
|
-
schema: []
|
|
205
|
-
},
|
|
206
|
-
create(context) {
|
|
207
|
-
const check = (name, fn, reportNode) => {
|
|
208
|
-
if (!HOOK_NAME.test(name)) return;
|
|
209
|
-
if (callsHook(fn.body, fn)) return;
|
|
210
|
-
context.report({
|
|
211
|
-
node: reportNode,
|
|
212
|
-
message: `"${name}" calls no hooks, so the React Compiler skips it and whatever it returns is rebuilt on every render. Rename it to a plain function, or read the hook it depends on inside it.`
|
|
213
|
-
});
|
|
214
|
-
};
|
|
215
|
-
return {
|
|
216
|
-
FunctionDeclaration(node) {
|
|
217
|
-
if (node.id) check(node.id.name, node, node.id);
|
|
218
|
-
},
|
|
219
|
-
VariableDeclarator(node) {
|
|
220
|
-
if (node.id.type !== "Identifier" || node.init == null) return;
|
|
221
|
-
if (node.init.type !== "ArrowFunctionExpression" && node.init.type !== "FunctionExpression") return;
|
|
222
|
-
check(node.id.name, node.init, node.id);
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
65
|
// node_modules/.pnpm/effect@4.1.2/node_modules/effect/dist/Pipeable.js
|
|
228
66
|
/**
|
|
229
67
|
* The `Pipeable` module defines the shared interface and implementation helpers
|
|
@@ -1996,6 +1834,302 @@ function isIdentifier(node, name) {
|
|
|
1996
1834
|
const expression = node.value;
|
|
1997
1835
|
return expression.type === "Identifier" && typeof expression.name === "string" && (name === void 0 || expression.name === name);
|
|
1998
1836
|
}
|
|
1837
|
+
function countFileLines(lines) {
|
|
1838
|
+
return lines.at(-1) === "" ? lines.length - 1 : lines.length;
|
|
1839
|
+
}
|
|
1840
|
+
function readMaxLines(options, defaultMax) {
|
|
1841
|
+
const [first] = options;
|
|
1842
|
+
return typeof first === "object" && first !== null && "max" in first && typeof first.max === "number" && Number.isInteger(first.max) && first.max > 0 ? first.max : defaultMax;
|
|
1843
|
+
}
|
|
1844
|
+
const maxLinesSchema = (defaultMax) => [{
|
|
1845
|
+
type: "object",
|
|
1846
|
+
properties: { max: {
|
|
1847
|
+
type: "integer",
|
|
1848
|
+
minimum: 1,
|
|
1849
|
+
default: defaultMax
|
|
1850
|
+
} },
|
|
1851
|
+
additionalProperties: false
|
|
1852
|
+
}];
|
|
1853
|
+
var max_file_lines_default = defineRule({
|
|
1854
|
+
meta: {
|
|
1855
|
+
type: "suggestion",
|
|
1856
|
+
docs: { description: "Report a file longer than `max` lines (default 500); a reasoned disable can keep it." },
|
|
1857
|
+
schema: maxLinesSchema(500)
|
|
1858
|
+
},
|
|
1859
|
+
create(context) {
|
|
1860
|
+
const max = readMaxLines(context.options, 500);
|
|
1861
|
+
const { lines } = context.sourceCode;
|
|
1862
|
+
const count = countFileLines(lines);
|
|
1863
|
+
if (count <= max) return {};
|
|
1864
|
+
return { Program() {
|
|
1865
|
+
context.report({
|
|
1866
|
+
loc: {
|
|
1867
|
+
start: {
|
|
1868
|
+
line: max + 1,
|
|
1869
|
+
column: 0
|
|
1870
|
+
},
|
|
1871
|
+
end: {
|
|
1872
|
+
line: lines.length,
|
|
1873
|
+
column: lines.at(-1).length
|
|
1874
|
+
}
|
|
1875
|
+
},
|
|
1876
|
+
message: `This file has ${count} lines; the cap is ${max}. Split it by responsibility. If this file must stay long, say why at its top: \`/* oxlint-disable jandragsbaek/max-file-lines -- <why this file is long> */\`.`
|
|
1877
|
+
});
|
|
1878
|
+
} };
|
|
1879
|
+
}
|
|
1880
|
+
});
|
|
1881
|
+
const BEFORE_EVERY_DIRECTIVE = {
|
|
1882
|
+
line: 1,
|
|
1883
|
+
column: 0
|
|
1884
|
+
};
|
|
1885
|
+
var max_file_lines_hard_default = defineRule({
|
|
1886
|
+
meta: {
|
|
1887
|
+
type: "problem",
|
|
1888
|
+
docs: { description: "Report a file longer than `max` lines (default 750); no disable directive can silence it." },
|
|
1889
|
+
schema: maxLinesSchema(750)
|
|
1890
|
+
},
|
|
1891
|
+
create(context) {
|
|
1892
|
+
const max = readMaxLines(context.options, 750);
|
|
1893
|
+
const count = countFileLines(context.sourceCode.lines);
|
|
1894
|
+
if (count <= max) return {};
|
|
1895
|
+
return { Program() {
|
|
1896
|
+
context.report({
|
|
1897
|
+
loc: {
|
|
1898
|
+
start: BEFORE_EVERY_DIRECTIVE,
|
|
1899
|
+
end: BEFORE_EVERY_DIRECTIVE
|
|
1900
|
+
},
|
|
1901
|
+
message: `This file has ${count} lines; the hard cap is ${max}, and no directive silences it. Split the file by responsibility.`
|
|
1902
|
+
});
|
|
1903
|
+
} };
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
// src/rules/namespace-node-imports.ts
|
|
1907
|
+
const NODE_MODULE_ALIASES = /* @__PURE__ */ new Map([["assert/strict", "Assert"], ["fs/promises", "FSP"]]);
|
|
1908
|
+
const NODE_SEGMENT_ALIASES = /* @__PURE__ */ new Map([
|
|
1909
|
+
["fs", "FS"],
|
|
1910
|
+
["os", "OS"],
|
|
1911
|
+
["url", "URL"],
|
|
1912
|
+
["vm", "VM"]
|
|
1913
|
+
]);
|
|
1914
|
+
const toPascalCase = (value) => value.split(/[_-]/u).filter((segment) => segment.length > 0).map((segment) => segment[0]?.toUpperCase() + segment.slice(1)).join("");
|
|
1915
|
+
const expectedNamespaceAlias = (source) => {
|
|
1916
|
+
const moduleName = source.slice(5);
|
|
1917
|
+
const knownAlias = NODE_MODULE_ALIASES.get(moduleName);
|
|
1918
|
+
if (knownAlias !== void 0) return `Node${knownAlias}`;
|
|
1919
|
+
return `Node${moduleName.split("/").map((segment) => NODE_SEGMENT_ALIASES.get(segment) ?? toPascalCase(segment)).join("")}`;
|
|
1920
|
+
};
|
|
1921
|
+
const literalStringValue = (node) => {
|
|
1922
|
+
if (typeof node !== "object" || node === null) return void 0;
|
|
1923
|
+
if (!("type" in node) || node.type !== "Literal") return void 0;
|
|
1924
|
+
if (!("value" in node) || typeof node.value !== "string") return void 0;
|
|
1925
|
+
return node.value;
|
|
1926
|
+
};
|
|
1927
|
+
const identifierName = (node) => {
|
|
1928
|
+
if (typeof node !== "object" || node === null) return void 0;
|
|
1929
|
+
if (!("type" in node) || node.type !== "Identifier") return void 0;
|
|
1930
|
+
if (!("name" in node) || typeof node.name !== "string") return void 0;
|
|
1931
|
+
return node.name;
|
|
1932
|
+
};
|
|
1933
|
+
var namespace_node_imports_default = defineRule({
|
|
1934
|
+
meta: {
|
|
1935
|
+
type: "problem",
|
|
1936
|
+
docs: { description: "Require canonical namespace imports for Node.js built-in modules." }
|
|
1937
|
+
},
|
|
1938
|
+
create(context) {
|
|
1939
|
+
return { ImportDeclaration(node) {
|
|
1940
|
+
const source = literalStringValue(node.source);
|
|
1941
|
+
if (source === void 0 || !source.startsWith("node:")) return;
|
|
1942
|
+
const expectedAlias = expectedNamespaceAlias(source);
|
|
1943
|
+
const namespaceImport = node.specifiers.length === 1 && node.specifiers[0]?.type === "ImportNamespaceSpecifier" ? node.specifiers[0] : void 0;
|
|
1944
|
+
if (identifierName(namespaceImport?.local) === expectedAlias) return;
|
|
1945
|
+
context.report({
|
|
1946
|
+
node,
|
|
1947
|
+
message: `Import ${source} as a namespace named ${expectedAlias}.`
|
|
1948
|
+
});
|
|
1949
|
+
} };
|
|
1950
|
+
}
|
|
1951
|
+
});
|
|
1952
|
+
// src/rules/no-dom-test-environment.ts
|
|
1953
|
+
const DOM_ENVIRONMENT_PACKAGE = /^(?:jsdom|happy-dom)(?:\/|$)/u;
|
|
1954
|
+
const PRAGMA_SPLIT_TO_HIDE_IT_FROM_THIS_RULE = ["@vitest", "environment"].join("-");
|
|
1955
|
+
const MESSAGE$5 = "Select test environments in Vitest project configuration instead of a per-file pragma or a DOM emulator import. Web DOM tests belong in *.browser.test.{ts,tsx}.";
|
|
1956
|
+
var no_dom_test_environment_default = defineRule({
|
|
1957
|
+
meta: {
|
|
1958
|
+
type: "problem",
|
|
1959
|
+
docs: { description: "Reject per-file Vitest environments and DOM emulator imports." }
|
|
1960
|
+
},
|
|
1961
|
+
create(context) {
|
|
1962
|
+
const checkImport = (source) => {
|
|
1963
|
+
if (source === null) return;
|
|
1964
|
+
const value = source.type === "Literal" ? source.value : source.type === "TemplateLiteral" && source.expressions.length === 0 ? source.quasis[0]?.value.cooked : void 0;
|
|
1965
|
+
if (typeof value === "string" && DOM_ENVIRONMENT_PACKAGE.test(value)) context.report({
|
|
1966
|
+
node: source,
|
|
1967
|
+
message: MESSAGE$5
|
|
1968
|
+
});
|
|
1969
|
+
};
|
|
1970
|
+
return {
|
|
1971
|
+
Program() {
|
|
1972
|
+
const index = context.sourceCode.text.indexOf(PRAGMA_SPLIT_TO_HIDE_IT_FROM_THIS_RULE);
|
|
1973
|
+
if (index !== -1) context.report({
|
|
1974
|
+
loc: context.sourceCode.getLocFromIndex(index),
|
|
1975
|
+
message: MESSAGE$5
|
|
1976
|
+
});
|
|
1977
|
+
},
|
|
1978
|
+
ImportDeclaration: (node) => checkImport(node.source),
|
|
1979
|
+
ImportExpression: (node) => checkImport(node.source),
|
|
1980
|
+
ExportNamedDeclaration: (node) => checkImport(node.source),
|
|
1981
|
+
ExportAllDeclaration: (node) => checkImport(node.source),
|
|
1982
|
+
TSExternalModuleReference: (node) => checkImport(node.expression),
|
|
1983
|
+
CallExpression(node) {
|
|
1984
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require") checkImport(node.arguments[0] ?? null);
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
}
|
|
1988
|
+
});
|
|
1989
|
+
// src/rules/no-em-dash-copy.ts
|
|
1990
|
+
const EM_DASH = "—";
|
|
1991
|
+
const MESSAGE$4 = "No em dashes in product copy. Use a period, comma, colon, or parentheses instead.";
|
|
1992
|
+
var no_em_dash_copy_default = defineRule({
|
|
1993
|
+
meta: {
|
|
1994
|
+
type: "suggestion",
|
|
1995
|
+
docs: { description: "Disallow em dashes in strings and JSX text that users or agents read." }
|
|
1996
|
+
},
|
|
1997
|
+
create(context) {
|
|
1998
|
+
const check = (node, text) => {
|
|
1999
|
+
if (text?.includes(EM_DASH)) context.report({
|
|
2000
|
+
node,
|
|
2001
|
+
message: MESSAGE$4
|
|
2002
|
+
});
|
|
2003
|
+
};
|
|
2004
|
+
return {
|
|
2005
|
+
Literal(node) {
|
|
2006
|
+
if (typeof node.value === "string") check(node, node.value);
|
|
2007
|
+
},
|
|
2008
|
+
TemplateElement(node) {
|
|
2009
|
+
check(node, node.value.cooked);
|
|
2010
|
+
},
|
|
2011
|
+
JSXText(node) {
|
|
2012
|
+
check(node, node.value);
|
|
2013
|
+
}
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
});
|
|
2017
|
+
// src/rules/no-explanatory-comments.ts
|
|
2018
|
+
const END = String.raw`(?![\w-])`;
|
|
2019
|
+
const TOOL_DIRECTIVES = [
|
|
2020
|
+
new RegExp(String.raw`^(?:oxlint|eslint)-(?:disable(?:-next-line|-line)?|enable)${END}`, "u"),
|
|
2021
|
+
new RegExp(String.raw`^@ts-(?:expect-error|ignore|nocheck|check)${END}`, "u"),
|
|
2022
|
+
/^\/\s*<(?:reference|amd-module|amd-dependency)\s/u,
|
|
2023
|
+
new RegExp(String.raw`^[@#]__(?:PURE|NO_SIDE_EFFECTS)__${END}`, "u"),
|
|
2024
|
+
new RegExp(String.raw`^@jsx(?:Frag|ImportSource|Runtime)?${END}`, "u"),
|
|
2025
|
+
new RegExp(String.raw`^(?:oxfmt|prettier|biome|dprint)-ignore(?:-start|-end)?${END}`, "u"),
|
|
2026
|
+
new RegExp(String.raw`^(?:v8|c8|istanbul)\s+ignore${END}`, "u"),
|
|
2027
|
+
new RegExp(String.raw`^node:coverage\s+(?:ignore|disable|enable)${END}`, "u"),
|
|
2028
|
+
new RegExp(String.raw`^@vite-ignore${END}`, "u"),
|
|
2029
|
+
/^(?:webpack|turbopack)[A-Z]\w*\s*:/u,
|
|
2030
|
+
new RegExp(String.raw`^@effect-diagnostics(?:-next-line)?${END}`, "u"),
|
|
2031
|
+
new RegExp(String.raw`^@(?:vitest|jest)-environment${END}`, "u"),
|
|
2032
|
+
/^[#@]\s*source(?:Mapping)?URL=/u,
|
|
2033
|
+
new RegExp(String.raw`^@refresh\s+reset${END}`, "u"),
|
|
2034
|
+
new RegExp(String.raw`^cspell:\s*(?:words|ignore|ignoreRegExp|includeRegExp|flagWords|dictionaries|language|locale|enable|disable(?:-next-line|-line)?)${END}`, "u"),
|
|
2035
|
+
/^quarantine:\s*#\d+(?!\w)/u
|
|
2036
|
+
];
|
|
2037
|
+
const LICENSE_NOTICE_LINE = /^[\s*]*(?:copyright\b|©|\(c\)\s|SPDX-License-Identifier:|@license\b|Permission is hereby granted\b|Licensed under\b)/imu;
|
|
2038
|
+
const isLicenseNotice = (comment) => comment.type === "Block" && comment.value.startsWith("!") || LICENSE_NOTICE_LINE.test(comment.value);
|
|
2039
|
+
const LEADING_COMMENT_DECORATION = /^[\s*]*/u;
|
|
2040
|
+
const MESSAGE$3 = "Delete this comment. Make the code say it: a name, a type, a smaller function or a test. Only a license header at the top of the file and tool directives (lint, type-checker, formatter, coverage and bundler comments) stay.";
|
|
2041
|
+
const isToolDirective = (comment) => {
|
|
2042
|
+
const text = comment.value.replace(LEADING_COMMENT_DECORATION, "");
|
|
2043
|
+
return TOOL_DIRECTIVES.some((directive) => directive.test(text));
|
|
2044
|
+
};
|
|
2045
|
+
const continuesLineComment = (previous, comment) => comment.type === "Line" && previous?.type === "Line" && previous.loc.end.line === comment.loc.start.line - 1 && previous.loc.start.column === comment.loc.start.column;
|
|
2046
|
+
var no_explanatory_comments_default = defineRule({
|
|
2047
|
+
meta: {
|
|
2048
|
+
type: "suggestion",
|
|
2049
|
+
docs: { description: "Reject every comment except a license header at the top of the file and tool directives." }
|
|
2050
|
+
},
|
|
2051
|
+
create(context) {
|
|
2052
|
+
const { sourceCode } = context;
|
|
2053
|
+
return { Program(program) {
|
|
2054
|
+
const firstCodeStart = program.body[0]?.range[0] ?? Number.POSITIVE_INFINITY;
|
|
2055
|
+
const comments = sourceCode.getAllComments().filter((comment) => comment.type !== "Shebang");
|
|
2056
|
+
const licenseHeader = /* @__PURE__ */ new Set();
|
|
2057
|
+
const headerBlocks = [];
|
|
2058
|
+
for (const comment of comments) {
|
|
2059
|
+
if (comment.range[0] >= firstCodeStart) break;
|
|
2060
|
+
const block = headerBlocks.at(-1);
|
|
2061
|
+
if (block !== void 0 && continuesLineComment(block.at(-1), comment)) block.push(comment);
|
|
2062
|
+
else headerBlocks.push([comment]);
|
|
2063
|
+
}
|
|
2064
|
+
for (const block of headerBlocks) if (block.some(isLicenseNotice)) for (const comment of block) licenseHeader.add(comment);
|
|
2065
|
+
const reported = [];
|
|
2066
|
+
for (const comment of comments) {
|
|
2067
|
+
if (licenseHeader.has(comment) || isToolDirective(comment)) continue;
|
|
2068
|
+
const group = reported.at(-1);
|
|
2069
|
+
if (group !== void 0 && continuesLineComment(group.at(-1), comment)) group.push(comment);
|
|
2070
|
+
else reported.push([comment]);
|
|
2071
|
+
}
|
|
2072
|
+
for (const group of reported) context.report({
|
|
2073
|
+
loc: {
|
|
2074
|
+
start: group[0].loc.start,
|
|
2075
|
+
end: group.at(-1).loc.end
|
|
2076
|
+
},
|
|
2077
|
+
message: MESSAGE$3
|
|
2078
|
+
});
|
|
2079
|
+
} };
|
|
2080
|
+
}
|
|
2081
|
+
});
|
|
2082
|
+
// src/rules/no-hookless-use-function.ts
|
|
2083
|
+
const HOOK_NAME = /^use[A-Z]/;
|
|
2084
|
+
const HOOK_CALLEE_OR_REACT_19_USE = /^use([A-Z]|$)/;
|
|
2085
|
+
function isFunctionNode(node) {
|
|
2086
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
2087
|
+
}
|
|
2088
|
+
function isHookCallee(callee) {
|
|
2089
|
+
if (callee.type === "Identifier") return HOOK_CALLEE_OR_REACT_19_USE.test(callee.name);
|
|
2090
|
+
if (callee.type === "MemberExpression" && !callee.computed) return callee.property.type === "Identifier" && HOOK_CALLEE_OR_REACT_19_USE.test(callee.property.name);
|
|
2091
|
+
return false;
|
|
2092
|
+
}
|
|
2093
|
+
function callsHookOutsideNestedFunctions(node, root) {
|
|
2094
|
+
if (typeof node !== "object" || node === null) return false;
|
|
2095
|
+
if (Array.isArray(node)) return node.some((child) => callsHookOutsideNestedFunctions(child, root));
|
|
2096
|
+
if (!("type" in node) || typeof node.type !== "string") return false;
|
|
2097
|
+
const typed = node;
|
|
2098
|
+
if (typed !== root && isFunctionNode(typed)) return false;
|
|
2099
|
+
if (typed.type === "CallExpression" && isHookCallee(typed.callee)) return true;
|
|
2100
|
+
for (const [key, value] of Object.entries(typed)) {
|
|
2101
|
+
if (key === "parent") continue;
|
|
2102
|
+
if (callsHookOutsideNestedFunctions(value, root)) return true;
|
|
2103
|
+
}
|
|
2104
|
+
return false;
|
|
2105
|
+
}
|
|
2106
|
+
var no_hookless_use_function_default = defineRule({
|
|
2107
|
+
meta: {
|
|
2108
|
+
type: "problem",
|
|
2109
|
+
docs: { description: "Disallow use* functions that call no hooks; the React Compiler only compiles functions that do. Mirrors eslint-react's no-unnecessary-use-prefix, which oxlint does not ship." },
|
|
2110
|
+
schema: []
|
|
2111
|
+
},
|
|
2112
|
+
create(context) {
|
|
2113
|
+
const check = (name, fn, reportNode) => {
|
|
2114
|
+
if (!HOOK_NAME.test(name)) return;
|
|
2115
|
+
if (callsHookOutsideNestedFunctions(fn.body, fn)) return;
|
|
2116
|
+
context.report({
|
|
2117
|
+
node: reportNode,
|
|
2118
|
+
message: `"${name}" calls no hooks, so the React Compiler skips it and whatever it returns is rebuilt on every render. Rename it to a plain function, or read the hook it depends on inside it.`
|
|
2119
|
+
});
|
|
2120
|
+
};
|
|
2121
|
+
return {
|
|
2122
|
+
FunctionDeclaration(node) {
|
|
2123
|
+
if (node.id) check(node.id.name, node, node.id);
|
|
2124
|
+
},
|
|
2125
|
+
VariableDeclarator(node) {
|
|
2126
|
+
if (node.id.type !== "Identifier" || node.init == null) return;
|
|
2127
|
+
if (node.init.type !== "ArrowFunctionExpression" && node.init.type !== "FunctionExpression") return;
|
|
2128
|
+
check(node.id.name, node.init, node.id);
|
|
2129
|
+
}
|
|
2130
|
+
};
|
|
2131
|
+
}
|
|
2132
|
+
});
|
|
1999
2133
|
// src/rules/no-inline-schema-compile.ts
|
|
2000
2134
|
const COMPILER_METHODS = /* @__PURE__ */ new Set([
|
|
2001
2135
|
"is",
|
|
@@ -2101,86 +2235,6 @@ var no_inline_schema_compile_default = defineRule({
|
|
|
2101
2235
|
};
|
|
2102
2236
|
}
|
|
2103
2237
|
});
|
|
2104
|
-
// src/rules/no-low-value-comments.ts
|
|
2105
|
-
const TS_FILE_PATTERN = /\.[cm]?tsx?$/u;
|
|
2106
|
-
const DIRECTIVE_PATTERN$1 = /^\s*(?:(?:oxlint|eslint)-|@ts-|@effect-diagnostics|prettier-ignore|biome-ignore|c8 |istanbul |#(?:end)?region)/u;
|
|
2107
|
-
const DIVIDER_LINE_PATTERN = /^(?:[-=*#~_/+]{3,}.*|.*[-=*#~_]{3,})$/u;
|
|
2108
|
-
const TEST_STEP_PATTERN = /^(?:arrange|act|assert|given|when|then)\s*[:.]?$/iu;
|
|
2109
|
-
const TASK_MARKER_PATTERN = /\b(?:TODO|FIXME|XXX|HACK)\b/u;
|
|
2110
|
-
const TEST_MARKER_PATTERN = /^\s*(?:regression|quarantine):/u;
|
|
2111
|
-
const TASK_LINK_PATTERN = /#\d+|https?:\/\//u;
|
|
2112
|
-
const BARE_ISSUE_REF_PATTERN = /(?<![\w/&#-])#[1-9]\d{2,4}(?![\w-])/u;
|
|
2113
|
-
const CODE_SPAN_PATTERN = /`[^`]*`/gu;
|
|
2114
|
-
const PUBLIC_BOILERPLATE_PATTERN = /^@public\s+Service construction is part of the canonical/u;
|
|
2115
|
-
const MAX_COMMENT_LINES = 4;
|
|
2116
|
-
const LICENSE_PATTERN = /^!|@license|\bcopyright\b|\bSPDX-License-Identifier\b/iu;
|
|
2117
|
-
const JSDOC_TYPE_PATTERN = /^@(?:param|returns?|type|typedef)\s+\{/u;
|
|
2118
|
-
const EMPTY_JSDOC_TAG_PATTERN = /^@(?:param(?:\s+\[?[\w$.]+\]?)?|returns?)$/u;
|
|
2119
|
-
const COMMENTED_CODE_PATTERN = /^(?:(?:(?:const|let|var)\s+[\w${}[\], ]+=.*|import\s.+\sfrom\s+["'].+["']|(?:await\s+|return\s+)?[\w$.]+\(.*\)|[\w$.[\]]+\s*=\s*.+);|(?:if|for|while|switch)\s*\(.*\)\s*\{|\}(?:\s*else\s*\{)?)$/u;
|
|
2120
|
-
const DECLARATION_PATTERN = /^\s*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(?:async\s+)?(?:static\s+)?(?:readonly\s+)?(?:(?:const|let|var|function\*?|class|interface|type|enum)\s+)?([A-Za-z_$][\w$]*)\s*[?!]?\s*[:=(<{]/u;
|
|
2121
|
-
const FILLER_WORDS = new Set("a an the of for to in on at by with from and or is are be this that it its as get set return create new current value given whether".split(" "));
|
|
2122
|
-
const stem = (word) => word.length > 3 ? word.replace(/([^s])s$/u, "$1") : word;
|
|
2123
|
-
const words = (text) => (text.toLowerCase().match(/[a-z0-9]+/gu) ?? []).filter((word) => !FILLER_WORDS.has(word)).map(stem).filter((word) => !FILLER_WORDS.has(word));
|
|
2124
|
-
const identifierWords = (name) => words(name.replace(/([a-z0-9])([A-Z])/gu, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/gu, "$1 $2"));
|
|
2125
|
-
const commentLines = (comment) => comment.value.split("\n").map((line) => line.replace(/^\s*\*?\s?/u, "").trim()).filter((line) => line !== "");
|
|
2126
|
-
var no_low_value_comments_default = defineRule({
|
|
2127
|
-
meta: {
|
|
2128
|
-
type: "suggestion",
|
|
2129
|
-
docs: { description: "Reject comments that only repeat the code, divide sections, or narrate tests." }
|
|
2130
|
-
},
|
|
2131
|
-
create(context) {
|
|
2132
|
-
const { sourceCode } = context;
|
|
2133
|
-
const isTypeScript = TS_FILE_PATTERN.test(context.filename);
|
|
2134
|
-
const groups = [];
|
|
2135
|
-
for (const comment of sourceCode.getAllComments()) {
|
|
2136
|
-
const previous = groups.at(-1)?.comments.at(-1);
|
|
2137
|
-
if (comment.type === "Line" && previous?.type === "Line" && previous.loc.end.line === comment.loc.start.line - 1 && previous.loc.start.column === comment.loc.start.column) {
|
|
2138
|
-
groups.at(-1).comments.push(comment);
|
|
2139
|
-
groups.at(-1).lines.push(...commentLines(comment));
|
|
2140
|
-
} else groups.push({
|
|
2141
|
-
comments: [comment],
|
|
2142
|
-
lines: commentLines(comment)
|
|
2143
|
-
});
|
|
2144
|
-
}
|
|
2145
|
-
const restatesNextName = (lastLine, lines) => {
|
|
2146
|
-
if (lines.some((line) => line.startsWith("@"))) return false;
|
|
2147
|
-
const name = DECLARATION_PATTERN.exec(sourceCode.lines[lastLine] ?? "")?.[1];
|
|
2148
|
-
if (name === void 0) return false;
|
|
2149
|
-
const content = words(lines.join(" "));
|
|
2150
|
-
if (content.length === 0 || content.length > 6) return false;
|
|
2151
|
-
const nameWords = new Set(identifierWords(name));
|
|
2152
|
-
return content.every((word) => nameWords.has(word));
|
|
2153
|
-
};
|
|
2154
|
-
const problem = (comments, lines) => {
|
|
2155
|
-
const text = comments.map((comment) => comment.value).join("\n");
|
|
2156
|
-
if (lines.length === 0 || comments.some((comment) => DIRECTIVE_PATTERN$1.test(comment.value))) return;
|
|
2157
|
-
const isLine = comments[0].type === "Line";
|
|
2158
|
-
if (lines.some((line) => DIVIDER_LINE_PATTERN.test(line))) return "Drop the section divider; split the file or let the code's structure show the grouping.";
|
|
2159
|
-
if (isLine && lines.length === 1 && TEST_STEP_PATTERN.test(lines[0])) return "Drop the test step label.";
|
|
2160
|
-
if (TASK_MARKER_PATTERN.test(text) && !TASK_LINK_PATTERN.test(text)) return "Link the issue that tracks this, or drop the marker.";
|
|
2161
|
-
if (!TASK_MARKER_PATTERN.test(text) && !TEST_MARKER_PATTERN.test(text) && BARE_ISSUE_REF_PATTERN.test(text.replace(CODE_SPAN_PATTERN, ""))) return "Drop the PR or issue number; git history records it. Keep the reason.";
|
|
2162
|
-
if (isTypeScript && lines.some((line) => JSDOC_TYPE_PATTERN.test(line))) return "Drop the JSDoc type; TypeScript already has it.";
|
|
2163
|
-
if (lines.some((line) => PUBLIC_BOILERPLATE_PATTERN.test(line))) return "Keep `@public` alone; the boilerplate after it says nothing about this export.";
|
|
2164
|
-
if (lines.some((line) => EMPTY_JSDOC_TAG_PATTERN.test(line))) return "Drop the JSDoc tag that has no description.";
|
|
2165
|
-
if (isLine && lines.every((line) => COMMENTED_CODE_PATTERN.test(line))) return "Delete commented-out code; git keeps the history.";
|
|
2166
|
-
if (restatesNextName(comments.at(-1).loc.end.line, lines)) return "Drop the comment; it only repeats the name below it. Say why, or say nothing.";
|
|
2167
|
-
if (lines.length > MAX_COMMENT_LINES && !LICENSE_PATTERN.test(text)) return `Cut this to ${MAX_COMMENT_LINES} lines or fewer: keep the constraint, drop the background.`;
|
|
2168
|
-
};
|
|
2169
|
-
return { Program() {
|
|
2170
|
-
for (const { comments, lines } of groups) {
|
|
2171
|
-
const message = problem(comments, lines);
|
|
2172
|
-
if (message === void 0) continue;
|
|
2173
|
-
context.report({
|
|
2174
|
-
loc: {
|
|
2175
|
-
start: comments[0].loc.start,
|
|
2176
|
-
end: comments.at(-1).loc.end
|
|
2177
|
-
},
|
|
2178
|
-
message
|
|
2179
|
-
});
|
|
2180
|
-
}
|
|
2181
|
-
} };
|
|
2182
|
-
}
|
|
2183
|
-
});
|
|
2184
2238
|
// src/rules/no-manual-effect-runtime-in-tests.ts
|
|
2185
2239
|
const TEST_FILE_PATTERN$5 = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
2186
2240
|
const EFFECT_RUNTIME_METHODS = /* @__PURE__ */ new Set([
|
|
@@ -2244,7 +2298,7 @@ var no_manual_effect_runtime_in_tests_default = defineRule({
|
|
|
2244
2298
|
});
|
|
2245
2299
|
// src/rules/no-manual-memo.ts
|
|
2246
2300
|
const MANUAL_MEMO_HOOKS = /* @__PURE__ */ new Set(["useCallback", "useMemo"]);
|
|
2247
|
-
const
|
|
2301
|
+
const DIRECTIVES_THAT_HIDE_COMPILER_BAILOUTS = /* @__PURE__ */ new Set([
|
|
2248
2302
|
"use memo",
|
|
2249
2303
|
"use no memo",
|
|
2250
2304
|
"use no forget"
|
|
@@ -2276,7 +2330,7 @@ var no_manual_memo_default = defineRule({
|
|
|
2276
2330
|
const allowMemoHooks = readAllowMemoHooks(context.options);
|
|
2277
2331
|
return {
|
|
2278
2332
|
ExpressionStatement(node) {
|
|
2279
|
-
if (typeof node.directive !== "string" || !
|
|
2333
|
+
if (typeof node.directive !== "string" || !DIRECTIVES_THAT_HIDE_COMPILER_BAILOUTS.has(node.directive)) return;
|
|
2280
2334
|
context.report({
|
|
2281
2335
|
node,
|
|
2282
2336
|
message: node.directive === "use memo" ? "Drop \"use memo\": restructure so the compiler caches this on its own, e.g. move plain uses of a value before the hook call that splits its scope, or inline the helper." : `Drop "${node.directive}": fix the code the compiler cannot prove safe instead of skipping it.`
|
|
@@ -2308,16 +2362,16 @@ const COUNTER_OPERATORS = /* @__PURE__ */ new Set([
|
|
|
2308
2362
|
">="
|
|
2309
2363
|
]);
|
|
2310
2364
|
const TIMERS = /* @__PURE__ */ new Set(["setImmediate", "setTimeout"]);
|
|
2311
|
-
const MESSAGE$
|
|
2365
|
+
const MESSAGE$2 = "Polling loop in a test. Spinning on `yieldNow`/`sleep` is scheduler- and load-dependent. Await the `Deferred`, `Queue.take`, `Stream` element, `drain()`, or `expect.poll` that marks the milestone.";
|
|
2312
2366
|
const isNode = (value) => typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
2313
|
-
const
|
|
2314
|
-
if (Array.isArray(node)) return node.some((child) =>
|
|
2367
|
+
const someDescendantOutsideNestedFunctions = (node, match) => {
|
|
2368
|
+
if (Array.isArray(node)) return node.some((child) => someDescendantOutsideNestedFunctions(child, match));
|
|
2315
2369
|
if (!isNode(node)) return false;
|
|
2316
2370
|
if (match(node)) return true;
|
|
2317
2371
|
if (FUNCTIONS$2.has(node.type)) return false;
|
|
2318
2372
|
for (const [key, value] of Object.entries(node)) {
|
|
2319
2373
|
if (key === "parent") continue;
|
|
2320
|
-
if (
|
|
2374
|
+
if (someDescendantOutsideNestedFunctions(value, match)) return true;
|
|
2321
2375
|
}
|
|
2322
2376
|
return false;
|
|
2323
2377
|
};
|
|
@@ -2337,7 +2391,7 @@ const isTimerReference = (node) => {
|
|
|
2337
2391
|
const expression = unwrapExpression(node);
|
|
2338
2392
|
return isSome(expression) && expression.value.type === "Identifier" && TIMERS.has(expression.value.name);
|
|
2339
2393
|
};
|
|
2340
|
-
const
|
|
2394
|
+
const isPromiseResolveOrTimerPromise = (node) => {
|
|
2341
2395
|
const expression = unwrapExpression(node);
|
|
2342
2396
|
if (isNone(expression)) return false;
|
|
2343
2397
|
const value = expression.value;
|
|
@@ -2346,34 +2400,34 @@ const isTickPromise = (node) => {
|
|
|
2346
2400
|
const [executor] = value.arguments;
|
|
2347
2401
|
if (isTimerReference(executor)) return true;
|
|
2348
2402
|
if (!isNode(executor) || !FUNCTIONS$2.has(executor.type)) return false;
|
|
2349
|
-
return
|
|
2403
|
+
return someDescendantOutsideNestedFunctions(executor.body, (child) => child.type === "CallExpression" && isTimerReference(child.callee));
|
|
2350
2404
|
};
|
|
2351
2405
|
const thunkReturnsTick = (node) => {
|
|
2352
2406
|
if (!isNode(node) || node.type !== "ArrowFunctionExpression") return false;
|
|
2353
2407
|
const body = node.body;
|
|
2354
|
-
if (body.type !== "BlockStatement") return
|
|
2408
|
+
if (body.type !== "BlockStatement") return isPromiseResolveOrTimerPromise(body);
|
|
2355
2409
|
const last = body.body.at(-1);
|
|
2356
|
-
return last?.type === "ReturnStatement" &&
|
|
2410
|
+
return last?.type === "ReturnStatement" && isPromiseResolveOrTimerPromise(last.argument);
|
|
2357
2411
|
};
|
|
2358
2412
|
const isWait = (node) => {
|
|
2359
2413
|
if (node.type === "MemberExpression") return isMember(node, "Effect", "yieldNow");
|
|
2360
|
-
if (node.type === "AwaitExpression") return
|
|
2361
|
-
if (node.type === "NewExpression") return
|
|
2414
|
+
if (node.type === "AwaitExpression") return isPromiseResolveOrTimerPromise(node.argument);
|
|
2415
|
+
if (node.type === "NewExpression") return isPromiseResolveOrTimerPromise(node);
|
|
2362
2416
|
if (node.type !== "CallExpression") return false;
|
|
2363
2417
|
if (isMember(node.callee, "Effect", "sleep")) return true;
|
|
2364
2418
|
if (isIdentifier(unwrapExpression(node.callee), "setImmediate")) return true;
|
|
2365
2419
|
if (isMember(node.callee, "Effect", "promise")) return thunkReturnsTick(node.arguments[0]);
|
|
2366
2420
|
return false;
|
|
2367
2421
|
};
|
|
2368
|
-
const hasConditionalExit = (body) =>
|
|
2422
|
+
const hasConditionalExit = (body) => someDescendantOutsideNestedFunctions(body, (node) => node.type === "IfStatement" && someDescendantOutsideNestedFunctions([node.consequent, node.alternate], (child) => child.type === "ReturnStatement" || child.type === "BreakStatement" || child.type === "ThrowStatement"));
|
|
2369
2423
|
const isNumericBound = (node) => {
|
|
2370
2424
|
const expression = unwrapExpression(node);
|
|
2371
2425
|
if (isNone(expression)) return false;
|
|
2372
2426
|
const value = expression.value;
|
|
2373
2427
|
return value.type === "Identifier" || value.type === "Literal" && typeof value.value === "number";
|
|
2374
2428
|
};
|
|
2375
|
-
const updatesCounter = (node, name) =>
|
|
2376
|
-
const
|
|
2429
|
+
const updatesCounter = (node, name) => someDescendantOutsideNestedFunctions(node, (child) => child.type === "UpdateExpression" && isIdentifier(unwrapExpression(child.argument), name) || child.type === "AssignmentExpression" && (child.operator === "+=" || child.operator === "-=") && isIdentifier(unwrapExpression(child.left), name));
|
|
2430
|
+
const isBoundByCounterTheLoopAdvances = (loop) => {
|
|
2377
2431
|
if (loop.test == null) return false;
|
|
2378
2432
|
const expression = unwrapExpression(loop.test);
|
|
2379
2433
|
if (isNone(expression) || expression.value.type !== "BinaryExpression") return false;
|
|
@@ -2389,7 +2443,7 @@ const isTrueLiteral = (test) => {
|
|
|
2389
2443
|
const expression = unwrapExpression(test);
|
|
2390
2444
|
return isSome(expression) && expression.value.type === "Literal" && expression.value.value === true;
|
|
2391
2445
|
};
|
|
2392
|
-
const hasPredicateExit = (loop) => hasConditionalExit(loop.body) || loop.test != null && !isTrueLiteral(loop.test) && !
|
|
2446
|
+
const hasPredicateExit = (loop) => hasConditionalExit(loop.body) || loop.test != null && !isTrueLiteral(loop.test) && !isBoundByCounterTheLoopAdvances(loop);
|
|
2393
2447
|
var no_polling_loop_in_tests_default = defineRule({
|
|
2394
2448
|
meta: {
|
|
2395
2449
|
type: "problem",
|
|
@@ -2399,11 +2453,11 @@ var no_polling_loop_in_tests_default = defineRule({
|
|
|
2399
2453
|
create(context) {
|
|
2400
2454
|
if (!TEST_FILE_PATTERN$4.test(context.filename)) return {};
|
|
2401
2455
|
const check = (loop) => {
|
|
2402
|
-
if (!
|
|
2456
|
+
if (!someDescendantOutsideNestedFunctions(loop.body, isWait)) return;
|
|
2403
2457
|
if (!hasPredicateExit(loop)) return;
|
|
2404
2458
|
context.report({
|
|
2405
2459
|
node: loop,
|
|
2406
|
-
message: MESSAGE$
|
|
2460
|
+
message: MESSAGE$2
|
|
2407
2461
|
});
|
|
2408
2462
|
};
|
|
2409
2463
|
return {
|
|
@@ -2436,7 +2490,7 @@ const FUNCTIONS$1 = /* @__PURE__ */ new Set([
|
|
|
2436
2490
|
const REAL_CLOCK_MESSAGE = "Real-clock wait in a test. Await the `Deferred`/`Queue`/event that marks the milestone, or run under `it.effect` and `TestClock.adjust`. `it.live` is for subprocess/network fixtures, not for sleeping.";
|
|
2437
2491
|
const UNDRIVEN_TEST_CLOCK_MESSAGE = "Sleeps on the TestClock never fire without `TestClock.adjust`; this test can only time out.";
|
|
2438
2492
|
const MIXED_CLOCK_MESSAGE = "`setTimeout` is wall-clock while `Effect.sleep` here is TestClock; they never meet.";
|
|
2439
|
-
const
|
|
2493
|
+
const flattenCalleeChain = (callee) => {
|
|
2440
2494
|
const members = [];
|
|
2441
2495
|
const calls = [];
|
|
2442
2496
|
let current = unwrapExpression(callee);
|
|
@@ -2480,13 +2534,13 @@ const excludesTestServices = (chain) => chain.calls.some((call) => call.argument
|
|
|
2480
2534
|
return isSome(key) && key.value === "excludeTestServices" && isSome(value) && value.value.type === "Literal" && value.value.value === true;
|
|
2481
2535
|
});
|
|
2482
2536
|
}));
|
|
2483
|
-
const
|
|
2537
|
+
const clockOfNearestEnclosingTest = (node) => {
|
|
2484
2538
|
let child = node;
|
|
2485
2539
|
let current = node.parent;
|
|
2486
2540
|
let tester = none();
|
|
2487
2541
|
while (current) {
|
|
2488
2542
|
if (current.type === "CallExpression" && current.arguments.some((argument) => argument === child)) {
|
|
2489
|
-
const chain =
|
|
2543
|
+
const chain = flattenCalleeChain(current.callee);
|
|
2490
2544
|
if (isSome(chain)) {
|
|
2491
2545
|
if (isNone(tester)) {
|
|
2492
2546
|
tester = testerKind(chain.value);
|
|
@@ -2514,7 +2568,7 @@ const LOOPS$1 = /* @__PURE__ */ new Set([
|
|
|
2514
2568
|
"ForStatement",
|
|
2515
2569
|
"WhileStatement"
|
|
2516
2570
|
]);
|
|
2517
|
-
const
|
|
2571
|
+
const isUnconditionalStatementOfLoopBody = (node) => {
|
|
2518
2572
|
let statement = node.parent;
|
|
2519
2573
|
while (statement && statement.type !== "ExpressionStatement") {
|
|
2520
2574
|
if (FUNCTIONS$1.has(statement.type)) return false;
|
|
@@ -2545,7 +2599,7 @@ var no_real_time_wait_in_tests_default = defineRule({
|
|
|
2545
2599
|
create(context) {
|
|
2546
2600
|
if (!TEST_FILE_PATTERN$3.test(context.filename)) return {};
|
|
2547
2601
|
let drivesTestClock = false;
|
|
2548
|
-
const
|
|
2602
|
+
const reportsUnlessFileDrivesTestClock = [];
|
|
2549
2603
|
const timerPromiseLocals = /* @__PURE__ */ new Set();
|
|
2550
2604
|
const timerPromiseNamespaces = /* @__PURE__ */ new Set();
|
|
2551
2605
|
const isTimerPromiseCall = (callee) => {
|
|
@@ -2576,7 +2630,7 @@ var no_real_time_wait_in_tests_default = defineRule({
|
|
|
2576
2630
|
message
|
|
2577
2631
|
});
|
|
2578
2632
|
if (isTimerPromiseCall(node.callee) || isCallbackTimer(node.callee) && isInsidePromiseExecutor(node)) {
|
|
2579
|
-
const tester =
|
|
2633
|
+
const tester = clockOfNearestEnclosingTest(node);
|
|
2580
2634
|
reportFor(isSome(tester) && tester.value === "effect" ? `${REAL_CLOCK_MESSAGE} ${MIXED_CLOCK_MESSAGE}` : REAL_CLOCK_MESSAGE);
|
|
2581
2635
|
return;
|
|
2582
2636
|
}
|
|
@@ -2589,31 +2643,31 @@ var no_real_time_wait_in_tests_default = defineRule({
|
|
|
2589
2643
|
}
|
|
2590
2644
|
const isSleep = object === "Effect" && property === "sleep";
|
|
2591
2645
|
if (!(isSleep || object === "Effect" && property === "delay" || object === "Schedule" && SCHEDULE_WAITS.has(property))) return;
|
|
2592
|
-
const tester =
|
|
2646
|
+
const tester = clockOfNearestEnclosingTest(node);
|
|
2593
2647
|
if (isNone(tester)) {
|
|
2594
|
-
if (
|
|
2595
|
-
else
|
|
2648
|
+
if (isUnconditionalStatementOfLoopBody(node)) reportFor(REAL_CLOCK_MESSAGE);
|
|
2649
|
+
else reportsUnlessFileDrivesTestClock.push({
|
|
2596
2650
|
node: node.callee,
|
|
2597
2651
|
message: REAL_CLOCK_MESSAGE
|
|
2598
2652
|
});
|
|
2599
2653
|
return;
|
|
2600
2654
|
}
|
|
2601
2655
|
if (tester.value !== "effect") reportFor(REAL_CLOCK_MESSAGE);
|
|
2602
|
-
else if (isSleep)
|
|
2656
|
+
else if (isSleep) reportsUnlessFileDrivesTestClock.push({
|
|
2603
2657
|
node: node.callee,
|
|
2604
2658
|
message: UNDRIVEN_TEST_CLOCK_MESSAGE
|
|
2605
2659
|
});
|
|
2606
2660
|
},
|
|
2607
2661
|
"Program:exit"() {
|
|
2608
2662
|
if (drivesTestClock) return;
|
|
2609
|
-
for (const report of
|
|
2663
|
+
for (const report of reportsUnlessFileDrivesTestClock) context.report(report);
|
|
2610
2664
|
}
|
|
2611
2665
|
};
|
|
2612
2666
|
}
|
|
2613
2667
|
});
|
|
2614
2668
|
// src/rules/no-test-in-loop.ts
|
|
2615
2669
|
const TEST_FILE_PATTERN$2 = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
2616
|
-
const
|
|
2670
|
+
const NODE_TEST_IMPORT_WHICH_HAS_NO_EACH = /["']node:test["']/u;
|
|
2617
2671
|
const TEST_FUNCTIONS$1 = /* @__PURE__ */ new Set([
|
|
2618
2672
|
"describe",
|
|
2619
2673
|
"it",
|
|
@@ -2659,7 +2713,7 @@ var no_test_in_loop_default = defineRule({
|
|
|
2659
2713
|
},
|
|
2660
2714
|
create(context) {
|
|
2661
2715
|
if (!TEST_FILE_PATTERN$2.test(context.filename)) return {};
|
|
2662
|
-
if (
|
|
2716
|
+
if (NODE_TEST_IMPORT_WHICH_HAS_NO_EACH.test(context.sourceCode.text)) return {};
|
|
2663
2717
|
return { CallExpression(node) {
|
|
2664
2718
|
const name = testCallName(node.callee);
|
|
2665
2719
|
if (isNone(name)) return;
|
|
@@ -2673,7 +2727,7 @@ var no_test_in_loop_default = defineRule({
|
|
|
2673
2727
|
});
|
|
2674
2728
|
// src/rules/no-test-timeout-or-unreasoned-skip.ts
|
|
2675
2729
|
const TEST_FILE_PATTERN$1 = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
2676
|
-
const
|
|
2730
|
+
const OPT_IN_LIVE_AGENT_SUITE_PATTERN = /\.live\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
2677
2731
|
const TEST_FUNCTIONS = /* @__PURE__ */ new Set([
|
|
2678
2732
|
"describe",
|
|
2679
2733
|
"it",
|
|
@@ -2707,16 +2761,16 @@ const SET_CONFIG_OPTIONS = /* @__PURE__ */ new Set([
|
|
|
2707
2761
|
const QUARANTINE_PATTERN = /quarantine:\s*#\d+|https:\/\/github\.com\/[^\s/]+\/[^\s/]+\/issues\/\d+/u;
|
|
2708
2762
|
const TIMEOUT_MESSAGE = "Per-test timeouts and retries hide waits that should be event-driven; the project timeout is the ceiling. Remove the override and await the milestone.";
|
|
2709
2763
|
const SKIP_MESSAGE = "A skipped test needs a tracking issue in the comment above it: `// quarantine: #1234 <why>`.";
|
|
2710
|
-
const
|
|
2764
|
+
const identifierThatRegistersTheTest = (node) => {
|
|
2711
2765
|
const expression = unwrapExpression(node);
|
|
2712
2766
|
if (isNone(expression)) return none();
|
|
2713
2767
|
const value = expression.value;
|
|
2714
2768
|
if (value.type === "Identifier") return some(value.name);
|
|
2715
|
-
if (value.type === "MemberExpression") return
|
|
2716
|
-
if (value.type === "CallExpression") return
|
|
2769
|
+
if (value.type === "MemberExpression") return identifierThatRegistersTheTest(value.object);
|
|
2770
|
+
if (value.type === "CallExpression") return identifierThatRegistersTheTest(value.callee);
|
|
2717
2771
|
return none();
|
|
2718
2772
|
};
|
|
2719
|
-
const isTestChain = (node) => exists(
|
|
2773
|
+
const isTestChain = (node) => exists(identifierThatRegistersTheTest(node), (name) => TEST_FUNCTIONS.has(name));
|
|
2720
2774
|
const hasEnabledOption = (node, names) => {
|
|
2721
2775
|
const expression = unwrapExpression(node);
|
|
2722
2776
|
if (isNone(expression) || expression.value.type !== "ObjectExpression") return false;
|
|
@@ -2741,7 +2795,7 @@ var no_test_timeout_or_unreasoned_skip_default = defineRule({
|
|
|
2741
2795
|
},
|
|
2742
2796
|
create(context) {
|
|
2743
2797
|
if (!TEST_FILE_PATTERN$1.test(context.filename)) return {};
|
|
2744
|
-
const checkTimeouts = !
|
|
2798
|
+
const checkTimeouts = !OPT_IN_LIVE_AGENT_SUITE_PATTERN.test(context.filename);
|
|
2745
2799
|
const reportUnlessQuarantined = (node) => {
|
|
2746
2800
|
if (context.sourceCode.getCommentsBefore(enclosingStatement(node)).some((comment) => QUARANTINE_PATTERN.test(comment.value))) return;
|
|
2747
2801
|
context.report({
|
|
@@ -2753,7 +2807,7 @@ var no_test_timeout_or_unreasoned_skip_default = defineRule({
|
|
|
2753
2807
|
CallExpression(node) {
|
|
2754
2808
|
const callee = unwrapExpression(node.callee);
|
|
2755
2809
|
if (isNone(callee)) return;
|
|
2756
|
-
if (callee.value.type === "MemberExpression" && exists(
|
|
2810
|
+
if (callee.value.type === "MemberExpression" && exists(identifierThatRegistersTheTest(callee.value.object), (name) => name === "vi") && exists(getPropertyName(callee.value.property), (name) => name === "setConfig")) {
|
|
2757
2811
|
if (checkTimeouts && hasOption(node.arguments[0], SET_CONFIG_OPTIONS)) context.report({
|
|
2758
2812
|
node,
|
|
2759
2813
|
message: TIMEOUT_MESSAGE
|
|
@@ -2806,7 +2860,7 @@ const isEffectHookCall = (node) => {
|
|
|
2806
2860
|
if (expression.type === "MemberExpression" && !expression.computed) return exists(getPropertyName(expression.property), (name) => EFFECT_HOOKS.has(name));
|
|
2807
2861
|
return false;
|
|
2808
2862
|
};
|
|
2809
|
-
const
|
|
2863
|
+
const MESSAGE_FOR_AGENTS_ALTERNATIVES_FIRST = "Stop and reconsider before keeping this effect. Is it computable from props or state? Compute it during render; the compiler memoizes it. Does it respond to a user action? Do it in that handler. Does it reset state when a prop changes? Key the component on that prop. Does it tell a parent or store that a value changed? Do it where the change happens. Does it load app data? Read it from the data layer (an atom, query or loader). Does it read a value from outside React? Use a store subscription or useSyncExternalStore. An effect that sets state another effect reads is one handler or one derivation. An effect is right only when it keeps something outside React in step with this component while mounted: a DOM node, listener, timer, native module, connection, router, imperative toast, or a registry entry or resource held until unmount. Moving the same logic into a ref or store to dodge this rule is not a fix. If the effect does that, name what it keeps in step on the line above: `// oxlint-disable-next-line jandragsbaek/no-unreasoned-state-sync -- <what it syncs with and why>`.";
|
|
2810
2864
|
var no_unreasoned_state_sync_default = defineRule({
|
|
2811
2865
|
meta: {
|
|
2812
2866
|
type: "suggestion",
|
|
@@ -2818,18 +2872,20 @@ var no_unreasoned_state_sync_default = defineRule({
|
|
|
2818
2872
|
if (!isEffectHookCall(node)) return;
|
|
2819
2873
|
context.report({
|
|
2820
2874
|
node,
|
|
2821
|
-
message:
|
|
2875
|
+
message: MESSAGE_FOR_AGENTS_ALTERNATIVES_FIRST
|
|
2822
2876
|
});
|
|
2823
2877
|
} };
|
|
2824
2878
|
}
|
|
2825
2879
|
});
|
|
2826
2880
|
// src/rules/no-unscoped-has.ts
|
|
2827
2881
|
const COMBINATOR_PATTERN = /[\s>+~]/u;
|
|
2828
|
-
const
|
|
2882
|
+
const CLASS_ID_ATTRIBUTE_OR_LEADING_TAG = /[.#[]|^[a-z]/iu;
|
|
2829
2883
|
const ROOT_COMPOUND_PATTERN = /^(?:html|body|:root)(?![\w-])/iu;
|
|
2830
|
-
const
|
|
2831
|
-
const
|
|
2832
|
-
const
|
|
2884
|
+
const GROUP_OR_PEER_ELEMENT_PREFIX = /(?:^|:)(?:group|peer)-$/u;
|
|
2885
|
+
const ANCESTOR_PREFIX = /(?:^|:)in-$/u;
|
|
2886
|
+
const ATTRIBUTE_VALUE_NOT_SELECTOR_PREFIX = /(?:^|:)(?:data|aria)-$/u;
|
|
2887
|
+
const TAILWIND_SPACE = "_";
|
|
2888
|
+
const TAILWIND_ELEMENT_WITH_THE_CLASS = "&";
|
|
2833
2889
|
const QUOTED_PATTERN = /(["'])(?:\\.|(?!\1).)*\1/gu;
|
|
2834
2890
|
const VARIANT_END_PATTERN = /^(?:\/[\w-]+)?:/u;
|
|
2835
2891
|
function variantGroups(token) {
|
|
@@ -2857,7 +2913,7 @@ function closingParen(selector, open) {
|
|
|
2857
2913
|
else if (selector[index] === ")" && --depth === 0) return index;
|
|
2858
2914
|
return selector.length;
|
|
2859
2915
|
}
|
|
2860
|
-
function
|
|
2916
|
+
function hasIsInSubjectPositionOfNotIsOrWhere(selector, from) {
|
|
2861
2917
|
let depth = 0;
|
|
2862
2918
|
for (let index = from; index < selector.length; index++) {
|
|
2863
2919
|
const char = selector[index] ?? "";
|
|
@@ -2882,22 +2938,22 @@ function hasCompounds(selector) {
|
|
|
2882
2938
|
let compound = "";
|
|
2883
2939
|
let depth = 0;
|
|
2884
2940
|
let subjectEnd = closingParen(selector, index + 4) + 1;
|
|
2885
|
-
let
|
|
2941
|
+
let skippingOtherSelectorListBranch = false;
|
|
2886
2942
|
for (let position = index - 1; position >= 0; position--) {
|
|
2887
2943
|
const char = selector[position] ?? "";
|
|
2888
2944
|
if (char === ")") depth++;
|
|
2889
2945
|
else if (char === "(") {
|
|
2890
2946
|
if (depth > 0) depth--;
|
|
2891
2947
|
else {
|
|
2892
|
-
if (!
|
|
2893
|
-
|
|
2948
|
+
if (!hasIsInSubjectPositionOfNotIsOrWhere(selector, subjectEnd)) break;
|
|
2949
|
+
skippingOtherSelectorListBranch = false;
|
|
2894
2950
|
subjectEnd = closingParen(selector, position) + 1;
|
|
2895
2951
|
}
|
|
2896
2952
|
} else if (depth === 0 && char === ",") {
|
|
2897
|
-
|
|
2953
|
+
skippingOtherSelectorListBranch = true;
|
|
2898
2954
|
continue;
|
|
2899
|
-
} else if (depth === 0 && !
|
|
2900
|
-
if (!
|
|
2955
|
+
} else if (depth === 0 && !skippingOtherSelectorListBranch && COMBINATOR_PATTERN.test(char)) break;
|
|
2956
|
+
if (!skippingOtherSelectorListBranch) compound = char + compound;
|
|
2901
2957
|
}
|
|
2902
2958
|
compounds.push(compound);
|
|
2903
2959
|
index = selector.indexOf(":has(", index + 1);
|
|
@@ -2924,12 +2980,12 @@ function findUnscopedHasVariants(text) {
|
|
|
2924
2980
|
if (!text.toLowerCase().includes(":has(")) return [];
|
|
2925
2981
|
const offenders = [];
|
|
2926
2982
|
for (const token of text.split(/\s+/u)) for (const { prefix, group } of variantGroups(token)) {
|
|
2927
|
-
if (
|
|
2928
|
-
const relative = group.toLowerCase().replace(QUOTED_PATTERN, "\"\"").replaceAll(
|
|
2929
|
-
const owner =
|
|
2930
|
-
if (hasCompounds(
|
|
2983
|
+
if (ATTRIBUTE_VALUE_NOT_SELECTOR_PREFIX.test(prefix)) continue;
|
|
2984
|
+
const relative = group.toLowerCase().replace(QUOTED_PATTERN, "\"\"").replaceAll(TAILWIND_SPACE, " ");
|
|
2985
|
+
const owner = GROUP_OR_PEER_ELEMENT_PREFIX.test(prefix) ? ".group" : ".self";
|
|
2986
|
+
if (hasCompounds(ANCESTOR_PREFIX.test(prefix) ? `:is(${relative.replaceAll(TAILWIND_ELEMENT_WITH_THE_CLASS, "*")}) .self` : relative.includes(TAILWIND_ELEMENT_WITH_THE_CLASS) ? relative.replaceAll(TAILWIND_ELEMENT_WITH_THE_CLASS, owner) : `${owner}:is(${relative})`).some((compound) => {
|
|
2931
2987
|
const anchor = withoutNegations(compound);
|
|
2932
|
-
return !
|
|
2988
|
+
return !CLASS_ID_ATTRIBUTE_OR_LEADING_TAG.test(anchor) || ROOT_COMPOUND_PATTERN.test(anchor);
|
|
2933
2989
|
})) offenders.push(`[${group}]`);
|
|
2934
2990
|
}
|
|
2935
2991
|
return offenders;
|
|
@@ -2964,8 +3020,7 @@ const readMessage = (options) => {
|
|
|
2964
3020
|
const [first] = options;
|
|
2965
3021
|
return typeof first === "object" && first !== null && "message" in first && typeof first.message === "string" ? first.message : DEFAULT_MESSAGE;
|
|
2966
3022
|
};
|
|
2967
|
-
|
|
2968
|
-
const USER_FACING_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
3023
|
+
const ATTRIBUTES_USERS_READ_OR_HEAR = /* @__PURE__ */ new Set([
|
|
2969
3024
|
"alt",
|
|
2970
3025
|
"aria-description",
|
|
2971
3026
|
"aria-label",
|
|
@@ -2976,7 +3031,7 @@ const USER_FACING_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
|
2976
3031
|
"placeholder",
|
|
2977
3032
|
"title"
|
|
2978
3033
|
]);
|
|
2979
|
-
const
|
|
3034
|
+
const HAS_LETTER_NOT_ONLY_SEPARATORS_OR_MARKS = /\p{L}/u;
|
|
2980
3035
|
var no_untranslated_ui_text_default = defineRule({
|
|
2981
3036
|
meta: {
|
|
2982
3037
|
type: "problem",
|
|
@@ -2993,7 +3048,7 @@ var no_untranslated_ui_text_default = defineRule({
|
|
|
2993
3048
|
create(context) {
|
|
2994
3049
|
const message = readMessage(context.options);
|
|
2995
3050
|
const check = (node, text) => {
|
|
2996
|
-
if (
|
|
3051
|
+
if (HAS_LETTER_NOT_ONLY_SEPARATORS_OR_MARKS.test(text)) context.report({
|
|
2997
3052
|
node,
|
|
2998
3053
|
message
|
|
2999
3054
|
});
|
|
@@ -3003,7 +3058,7 @@ var no_untranslated_ui_text_default = defineRule({
|
|
|
3003
3058
|
check(node, node.value);
|
|
3004
3059
|
},
|
|
3005
3060
|
JSXAttribute(node) {
|
|
3006
|
-
if (node.name.type !== "JSXIdentifier" || !
|
|
3061
|
+
if (node.name.type !== "JSXIdentifier" || !ATTRIBUTES_USERS_READ_OR_HEAR.has(node.name.name)) return;
|
|
3007
3062
|
const value = node.value;
|
|
3008
3063
|
if (value?.type === "Literal" && typeof value.value === "string") check(value, value.value);
|
|
3009
3064
|
if (value?.type === "JSXExpressionContainer" && value.expression.type === "Literal" && typeof value.expression.value === "string") check(value.expression, value.expression.value);
|
|
@@ -3057,7 +3112,7 @@ const isMockingCall = (callee) => {
|
|
|
3057
3112
|
if (MOCK_METHODS.has(property.value)) return true;
|
|
3058
3113
|
return VI_MOCKING_FUNCTIONS.has(property.value) && isIdentifier(unwrapExpression(expression.value.object), "vi");
|
|
3059
3114
|
};
|
|
3060
|
-
const
|
|
3115
|
+
const seedsFakeTimeThroughAMock = (node) => {
|
|
3061
3116
|
let child = node;
|
|
3062
3117
|
let current = node.parent;
|
|
3063
3118
|
while (current) {
|
|
@@ -3077,7 +3132,7 @@ var no_wall_clock_in_tests_default = defineRule({
|
|
|
3077
3132
|
return {
|
|
3078
3133
|
CallExpression(node) {
|
|
3079
3134
|
if (!isWallClockCall(node.callee)) return;
|
|
3080
|
-
if (
|
|
3135
|
+
if (seedsFakeTimeThroughAMock(node)) return;
|
|
3081
3136
|
context.report({
|
|
3082
3137
|
node,
|
|
3083
3138
|
message: MESSAGE$1
|
|
@@ -3086,7 +3141,7 @@ var no_wall_clock_in_tests_default = defineRule({
|
|
|
3086
3141
|
NewExpression(node) {
|
|
3087
3142
|
if (node.arguments.length > 0) return;
|
|
3088
3143
|
if (!isIdentifier(unwrapExpression(node.callee), "Date")) return;
|
|
3089
|
-
if (
|
|
3144
|
+
if (seedsFakeTimeThroughAMock(node)) return;
|
|
3090
3145
|
context.report({
|
|
3091
3146
|
node,
|
|
3092
3147
|
message: MESSAGE$1
|
|
@@ -3207,19 +3262,19 @@ var prefer_effect_fn_default = defineRule({
|
|
|
3207
3262
|
}
|
|
3208
3263
|
});
|
|
3209
3264
|
// src/rules/require-suppression-reason.ts
|
|
3210
|
-
const DIRECTIVE_PATTERN = /^\s*\*?\s*(?:(?:oxlint|eslint)-(?:disable|enable)|@ts-(?:expect-error|ignore|nocheck)|@effect-diagnostics)/u;
|
|
3211
3265
|
const TS_DIRECTIVE_PATTERN = /^\s*\*?\s*@ts-(?:expect-error|ignore|nocheck)(?![\w-])(.*)$/su;
|
|
3212
|
-
const EMPTY_COMMENT_PATTERN = /^[\s*]*$/u;
|
|
3213
3266
|
// src/index.ts
|
|
3214
3267
|
var src_default = definePlugin({
|
|
3215
3268
|
meta: { name: "jandragsbaek" },
|
|
3216
3269
|
rules: {
|
|
3270
|
+
"max-file-lines": max_file_lines_default,
|
|
3271
|
+
"max-file-lines-hard": max_file_lines_hard_default,
|
|
3217
3272
|
"namespace-node-imports": namespace_node_imports_default,
|
|
3218
3273
|
"no-dom-test-environment": no_dom_test_environment_default,
|
|
3219
3274
|
"no-em-dash-copy": no_em_dash_copy_default,
|
|
3275
|
+
"no-explanatory-comments": no_explanatory_comments_default,
|
|
3220
3276
|
"no-hookless-use-function": no_hookless_use_function_default,
|
|
3221
3277
|
"no-inline-schema-compile": no_inline_schema_compile_default,
|
|
3222
|
-
"no-low-value-comments": no_low_value_comments_default,
|
|
3223
3278
|
"no-manual-effect-runtime-in-tests": no_manual_effect_runtime_in_tests_default,
|
|
3224
3279
|
"no-manual-memo": no_manual_memo_default,
|
|
3225
3280
|
"no-polling-loop-in-tests": no_polling_loop_in_tests_default,
|
|
@@ -3235,37 +3290,24 @@ var src_default = definePlugin({
|
|
|
3235
3290
|
"require-suppression-reason": defineRule({
|
|
3236
3291
|
meta: {
|
|
3237
3292
|
type: "suggestion",
|
|
3238
|
-
docs: { description: "Require every lint or type-checker suppression to say why." }
|
|
3293
|
+
docs: { description: "Require every lint or type-checker suppression to say why, in the directive." }
|
|
3239
3294
|
},
|
|
3240
3295
|
create(context) {
|
|
3241
3296
|
const { sourceCode } = context;
|
|
3242
3297
|
return { Program() {
|
|
3243
|
-
const comments = sourceCode.getAllComments();
|
|
3244
|
-
const ownLineCommentsByEndLine = /* @__PURE__ */ new Map();
|
|
3245
|
-
for (const comment of comments) {
|
|
3246
|
-
const { line, column } = comment.loc.start;
|
|
3247
|
-
if ((sourceCode.lines[line - 1] ?? "").slice(0, column).trim() === "") ownLineCommentsByEndLine.set(comment.loc.end.line, comment);
|
|
3248
|
-
}
|
|
3249
|
-
const explainedAbove = (directive) => {
|
|
3250
|
-
let above = ownLineCommentsByEndLine.get(directive.loc.start.line - 1);
|
|
3251
|
-
while (above && (DIRECTIVE_PATTERN.test(above.value) || EMPTY_COMMENT_PATTERN.test(above.value))) above = ownLineCommentsByEndLine.get(above.loc.start.line - 1);
|
|
3252
|
-
return above !== void 0;
|
|
3253
|
-
};
|
|
3254
3298
|
for (const directive of sourceCode.getDisableDirectives().directives) {
|
|
3255
|
-
if (directive.type === "enable" || directive.justification
|
|
3256
|
-
if (explainedAbove(directive.node)) continue;
|
|
3299
|
+
if (directive.type === "enable" || directive.justification.trim()) continue;
|
|
3257
3300
|
context.report({
|
|
3258
3301
|
loc: directive.node.loc,
|
|
3259
|
-
message: "Say why: end the directive with `-- reason
|
|
3302
|
+
message: "Say why: end the directive with `-- <reason>`."
|
|
3260
3303
|
});
|
|
3261
3304
|
}
|
|
3262
|
-
for (const comment of
|
|
3305
|
+
for (const comment of sourceCode.getAllComments()) {
|
|
3263
3306
|
const after = TS_DIRECTIVE_PATTERN.exec(comment.value)?.[1];
|
|
3264
3307
|
if (after === void 0 || after.replace(/^[\s:-]+/u, "").trim()) continue;
|
|
3265
|
-
if (explainedAbove(comment)) continue;
|
|
3266
3308
|
context.report({
|
|
3267
3309
|
loc: comment.loc,
|
|
3268
|
-
message: "Say why: write the reason after the directive
|
|
3310
|
+
message: "Say why: write the reason after the directive, on the same comment."
|
|
3269
3311
|
});
|
|
3270
3312
|
}
|
|
3271
3313
|
} };
|
package/dist/presets.d.mts
CHANGED
|
@@ -1,30 +1,15 @@
|
|
|
1
1
|
import { OxlintConfig } from "oxlint";
|
|
2
2
|
export declare const TEST_FILES: string[];
|
|
3
3
|
interface FileScope {
|
|
4
|
-
/** Globs the rules apply to, relative to the config file. */
|
|
5
4
|
readonly files: ReadonlyArray<string>;
|
|
6
|
-
/** Globs inside `files` the rules skip. Test files are always skipped. */
|
|
7
5
|
readonly excludeFiles?: ReadonlyArray<string>;
|
|
8
6
|
}
|
|
9
|
-
/** Built-in rule choices and the plugin rules that fit any TypeScript project. */
|
|
10
7
|
export declare const recommended: OxlintConfig;
|
|
11
|
-
/** Deterministic tests: no real time, no polling, no timeouts or unreasoned skips. */
|
|
12
8
|
export declare const tests: OxlintConfig;
|
|
13
|
-
/** Effect code: Effect.fn, catchTags, hoisted Schema compilers, @effect/vitest. */
|
|
14
9
|
export declare const effect: OxlintConfig;
|
|
15
|
-
/** React: strict hook and render rules, and a reason on every effect that stays. */
|
|
16
10
|
export declare const react: OxlintConfig;
|
|
17
|
-
/**
|
|
18
|
-
* Files the React Compiler compiles. The compiler owns memoization, and it caches `use*`
|
|
19
|
-
* functions only if they call a hook. Use with `react`.
|
|
20
|
-
*/
|
|
21
11
|
export declare const reactCompiler: (scope: FileScope) => OxlintConfig;
|
|
22
|
-
/** Product copy that users or agents read: no em dashes. Test files are skipped. */
|
|
23
12
|
export declare const productCopy: (scope: FileScope) => OxlintConfig;
|
|
24
|
-
/**
|
|
25
|
-
* Interface text lives in message catalogs, never inline in JSX. `message` names the
|
|
26
|
-
* project's catalogs and how to read them. Test files are skipped.
|
|
27
|
-
*/
|
|
28
13
|
export declare const uiText: (scope: FileScope & {
|
|
29
14
|
readonly message?: string;
|
|
30
15
|
}) => OxlintConfig;
|
package/dist/presets.mjs
CHANGED
|
@@ -2,17 +2,17 @@ import * as NodePath from "node:path";
|
|
|
2
2
|
import * as NodeURL from "node:url";
|
|
3
3
|
// src/presets.ts
|
|
4
4
|
const here = NodeURL.fileURLToPath(import.meta.url);
|
|
5
|
-
const
|
|
5
|
+
const siblingIndexTsInSourceOrIndexMjsOnceBuilt = `index${NodePath.extname(here)}`;
|
|
6
|
+
const jsPlugins = [NodePath.join(NodePath.dirname(here), siblingIndexTsInSourceOrIndexMjsOnceBuilt)];
|
|
6
7
|
const TEST_FILES = ["**/*.{test,spec,testkit}.{ts,tsx,js,jsx,mts,cts,mjs,cjs}", "**/{test,tests,__tests__,testkit,testing,fixtures,integration}/**"];
|
|
7
|
-
const scoped = (scope, rules,
|
|
8
|
-
const excludeFiles = [...
|
|
8
|
+
const scoped = (scope, rules, testFiles = "skip") => {
|
|
9
|
+
const excludeFiles = [...testFiles === "skip" ? TEST_FILES : [], ...scope.excludeFiles ?? []];
|
|
9
10
|
return {
|
|
10
11
|
files: [...scope.files],
|
|
11
12
|
...excludeFiles.length > 0 ? { excludeFiles } : {},
|
|
12
13
|
rules
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
|
-
/** Built-in rule choices and the plugin rules that fit any TypeScript project. */
|
|
16
16
|
const recommended = {
|
|
17
17
|
plugins: [
|
|
18
18
|
"eslint",
|
|
@@ -55,14 +55,15 @@ const recommended = {
|
|
|
55
55
|
"unicorn/prefer-set-has": "off",
|
|
56
56
|
"unicorn/prefer-string-starts-ends-with": "off",
|
|
57
57
|
"unicorn/require-module-specifiers": "error",
|
|
58
|
+
"jandragsbaek/max-file-lines": "error",
|
|
59
|
+
"jandragsbaek/max-file-lines-hard": "error",
|
|
58
60
|
"jandragsbaek/namespace-node-imports": "error",
|
|
59
|
-
"jandragsbaek/no-
|
|
61
|
+
"jandragsbaek/no-explanatory-comments": "error",
|
|
60
62
|
"jandragsbaek/no-unscoped-has": "error",
|
|
61
63
|
"jandragsbaek/require-suppression-reason": "error"
|
|
62
64
|
},
|
|
63
65
|
options: { reportUnusedDisableDirectives: "error" }
|
|
64
66
|
};
|
|
65
|
-
/** Deterministic tests: no real time, no polling, no timeouts or unreasoned skips. */
|
|
66
67
|
const tests = {
|
|
67
68
|
jsPlugins,
|
|
68
69
|
rules: {
|
|
@@ -74,7 +75,6 @@ const tests = {
|
|
|
74
75
|
"jandragsbaek/no-wall-clock-in-tests": "error"
|
|
75
76
|
}
|
|
76
77
|
};
|
|
77
|
-
/** Effect code: Effect.fn, catchTags, hoisted Schema compilers, @effect/vitest. */
|
|
78
78
|
const effect = {
|
|
79
79
|
jsPlugins,
|
|
80
80
|
rules: {
|
|
@@ -88,7 +88,6 @@ const effect = {
|
|
|
88
88
|
rules: { "jandragsbaek/prefer-effect-fn": "off" }
|
|
89
89
|
}]
|
|
90
90
|
};
|
|
91
|
-
/** React: strict hook and render rules, and a reason on every effect that stays. */
|
|
92
91
|
const react = {
|
|
93
92
|
plugins: ["react"],
|
|
94
93
|
jsPlugins,
|
|
@@ -109,31 +108,23 @@ const react = {
|
|
|
109
108
|
"jandragsbaek/no-unreasoned-state-sync": "error"
|
|
110
109
|
}
|
|
111
110
|
};
|
|
112
|
-
|
|
113
|
-
* Files the React Compiler compiles. The compiler owns memoization, and it caches `use*`
|
|
114
|
-
* functions only if they call a hook. Use with `react`.
|
|
115
|
-
*/
|
|
111
|
+
const compilerMemoizesContextValues = { "react/jsx-no-constructed-context-values": "off" };
|
|
116
112
|
const reactCompiler = (scope) => ({
|
|
117
113
|
jsPlugins,
|
|
118
114
|
overrides: [scoped(scope, {
|
|
119
115
|
"react/globals": "error",
|
|
120
116
|
"react/hooks": "error",
|
|
121
|
-
|
|
117
|
+
...compilerMemoizesContextValues,
|
|
122
118
|
"react/preserve-manual-memoization": "error",
|
|
123
119
|
"react/set-state-in-render": "error",
|
|
124
120
|
"react/unsupported-syntax": "error",
|
|
125
121
|
"jandragsbaek/no-manual-memo": "error"
|
|
126
|
-
},
|
|
122
|
+
}, "check"), scoped(scope, { "jandragsbaek/no-hookless-use-function": "error" })]
|
|
127
123
|
});
|
|
128
|
-
/** Product copy that users or agents read: no em dashes. Test files are skipped. */
|
|
129
124
|
const productCopy = (scope) => ({
|
|
130
125
|
jsPlugins,
|
|
131
126
|
overrides: [scoped(scope, { "jandragsbaek/no-em-dash-copy": "error" })]
|
|
132
127
|
});
|
|
133
|
-
/**
|
|
134
|
-
* Interface text lives in message catalogs, never inline in JSX. `message` names the
|
|
135
|
-
* project's catalogs and how to read them. Test files are skipped.
|
|
136
|
-
*/
|
|
137
128
|
const uiText = (scope) => ({
|
|
138
129
|
jsPlugins,
|
|
139
130
|
overrides: [scoped(scope, { "jandragsbaek/no-untranslated-ui-text": scope.message === void 0 ? "error" : ["error", { message: scope.message }] })]
|