@siim-m/config 0.1.0 → 0.3.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 +82 -5
- package/dist/ai-guard.d.ts +5 -0
- package/dist/ai-guard.js +5 -0
- package/dist/anti-slop/effect/LICENSE-enfect +21 -0
- package/dist/anti-slop/effect/index.js +8 -0
- package/dist/anti-slop/effect/rules/no-ordie-in-service-construction.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-ordie-in-service-construction.js +117 -0
- package/dist/anti-slop/effect/rules/no-unbounded-retry.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-unbounded-retry.js +307 -0
- package/dist/anti-slop/effect/rules/require-cause-when-narrowing.d.ts +2 -0
- package/dist/anti-slop/effect/rules/require-cause-when-narrowing.js +250 -0
- package/dist/anti-slop/effect/rules/throw-in-effect-gen.d.ts +2 -0
- package/dist/anti-slop/effect/rules/throw-in-effect-gen.js +87 -0
- package/dist/anti-slop/effect/shared/effect-modules.d.ts +79 -0
- package/dist/anti-slop/effect/shared/effect-modules.js +336 -0
- package/dist/anti-slop/index.js +6 -0
- package/dist/anti-slop/rules/comments.d.ts +7 -0
- package/dist/anti-slop/rules/comments.js +376 -0
- package/dist/anti-slop/rules/no-disabled-type-assertion-rules.d.ts +2 -0
- package/dist/anti-slop/rules/no-disabled-type-assertion-rules.js +94 -0
- package/dist/anti-slop/rules/require-suppression-reason.d.ts +6 -0
- package/dist/anti-slop/rules/require-suppression-reason.js +61 -0
- package/dist/anti-slop-react/LICENSE +21 -0
- package/dist/anti-slop-react/index.d.ts +3 -0
- package/dist/anti-slop-react/index.js +14 -0
- package/dist/anti-slop-react/rules/no-hookless-use-function.d.ts +8 -0
- package/dist/anti-slop-react/rules/no-hookless-use-function.js +67 -0
- package/dist/anti-slop-react/rules/no-manual-memo.d.ts +7 -0
- package/dist/anti-slop-react/rules/no-manual-memo.js +58 -0
- package/dist/anti-slop-react/rules/require-sync-comment-for-effect.d.ts +8 -0
- package/dist/anti-slop-react/rules/require-sync-comment-for-effect.js +64 -0
- package/dist/landmines.d.ts +5 -0
- package/dist/landmines.js +5 -0
- package/dist/oxlint-react.d.ts +62 -0
- package/dist/oxlint-react.js +62 -0
- package/dist/oxlint.d.ts +41 -0
- package/dist/oxlint.js +48 -1
- package/package.json +20 -6
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
const DIRECTIVE = /^(?:(?:oxlint|eslint)-(?:disable|enable|env)\b|eslint\s+[\w@/-]+\s*:\s*(?:off|warn|error|[012]|\[)|@ts-(?:expect-error|ignore|nocheck|check)\b|@effect-diagnostics|(?:prettier|oxfmt)-ignore|biome-ignore|svelte-ignore|(?:c8|v8|istanbul)\s+ignore|@(?:vitest|jest)-environment|@vite-ignore|webpack[A-Z]\w*:|@jsx|[@#]__(?:PURE|NO_SIDE_EFFECTS|INLINE|NOINLINE)__|\/\s*<(?:reference|amd-module|amd-dependency)\s|#\s*source(?:Mapping)?URL=|#(?:end)?region\b|nosemgrep\b)/u;
|
|
3
|
+
const LICENSE = /@license\b|\bSPDX-License-Identifier\b/u;
|
|
4
|
+
const COPYRIGHT = /\bcopyright\b/iu;
|
|
5
|
+
const SUPPRESSION = /^\s*\*?\s*(?:(?:oxlint|eslint)-disable|@ts-(?:expect-error|ignore|nocheck))/u;
|
|
6
|
+
const URL = /\bhttps?:\/\/\S+/gu;
|
|
7
|
+
const CODE_SPAN = /`[^`]*`/gu;
|
|
8
|
+
const ISSUE = /(?<![\w&/])#\d+\b(?![a-f])/giu;
|
|
9
|
+
const DIVIDER = /^(?:[-=*#~_/+\u2500-\u257F]{3,}(?:\s.*)?|.*[-=*#~_\u2500-\u257F]{3,})$/u;
|
|
10
|
+
const STEP_LABEL = /^(?:arrange|act|assert|given|when|then)\s*[:.]?$/iu;
|
|
11
|
+
const TASK_MARKER = /\b(?:TODO|FIXME|HACK)\b|(?<!-)\bXXX\b(?!-)/u;
|
|
12
|
+
/** A markdown heading, which a doc comment may use: `### Example`. */
|
|
13
|
+
const HEADING = /^#{1,6}\s+\S/u;
|
|
14
|
+
const TASK_LINK = /#\d+|\b[A-Z][A-Z\d]*-\d+\b|https?:\/\//u;
|
|
15
|
+
const JSDOC_TYPE = /^@(?:param|returns?|type|typedef)\s+\{/u;
|
|
16
|
+
const EMPTY_JSDOC_TAG = /^@(?:param(?:\s+\[?[\w$.]+\]?)?|returns?)$/u;
|
|
17
|
+
const COMMENTED_CODE = /^(?:(?:(?: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;
|
|
18
|
+
const TYPESCRIPT_FILE = /\.[cm]?tsx?$/u;
|
|
19
|
+
const FILLER = new Set('a an the of for to in on at by with from and or is are be this that it its as'.split(' '));
|
|
20
|
+
/** The words a comment or name carries once filler words go and plurals fold. */
|
|
21
|
+
const contentWords = (text) => (text.toLowerCase().match(/[a-z0-9]+/gu) ?? [])
|
|
22
|
+
.filter((word) => !FILLER.has(word))
|
|
23
|
+
.map((word) => (word.length > 3 ? word.replace(/([^s])s$/u, '$1') : word))
|
|
24
|
+
.filter((word) => !FILLER.has(word));
|
|
25
|
+
const nameWords = (name) => new Set(contentWords(name.replace(/([a-z0-9])([A-Z])/gu, '$1 $2').replace(/([A-Z]+)([A-Z][a-z])/gu, '$1 $2')));
|
|
26
|
+
const isDoc = (comment) => comment.type === 'Block' && comment.value.startsWith('*');
|
|
27
|
+
/** A comment's lines of prose: no doc-comment stars, blank lines or directives. */
|
|
28
|
+
const textLines = (comment) => {
|
|
29
|
+
const lines = comment.value
|
|
30
|
+
.split('\n')
|
|
31
|
+
.map((line) => line.replace(/^\s*\*?\s?/u, '').trim())
|
|
32
|
+
.filter((line) => line !== '' && line !== '*');
|
|
33
|
+
// A block that opens with a directive, such as a multi-line `oxlint-disable`, is all directive.
|
|
34
|
+
if (comment.type === 'Block' && DIRECTIVE.test(lines[0] ?? ''))
|
|
35
|
+
return [];
|
|
36
|
+
return lines.filter((line) => !DIRECTIVE.test(line));
|
|
37
|
+
};
|
|
38
|
+
/** A comment that holds only directives, such as `// oxlint-disable-next-line rule -- reason`. */
|
|
39
|
+
const isDirective = (comment) => comment.value.trim() !== '' && textLines(comment).length === 0;
|
|
40
|
+
/** Adjacent `//` lines at one indent read as one comment; a directive stands alone. */
|
|
41
|
+
function commentGroups(comments, text) {
|
|
42
|
+
const groups = [];
|
|
43
|
+
for (const comment of comments) {
|
|
44
|
+
if (comment.type === 'Shebang')
|
|
45
|
+
continue;
|
|
46
|
+
const group = groups.at(-1);
|
|
47
|
+
const previous = group?.at(-1);
|
|
48
|
+
const continues = group !== undefined &&
|
|
49
|
+
previous !== undefined &&
|
|
50
|
+
!isDirective(previous) &&
|
|
51
|
+
!isDirective(comment) &&
|
|
52
|
+
previous?.type === 'Line' &&
|
|
53
|
+
comment.type === 'Line' &&
|
|
54
|
+
previous.loc.start.column === comment.loc.start.column &&
|
|
55
|
+
/^[ \t]*\r?\n[ \t]*$/u.test(text.slice(previous.end, comment.start));
|
|
56
|
+
if (continues)
|
|
57
|
+
group.push(comment);
|
|
58
|
+
else
|
|
59
|
+
groups.push([comment]);
|
|
60
|
+
}
|
|
61
|
+
return groups;
|
|
62
|
+
}
|
|
63
|
+
/** The doc comment directly above a node, looking past directives such as `oxlint-disable-next-line`. */
|
|
64
|
+
function docBefore(node, sourceCode) {
|
|
65
|
+
const comments = sourceCode.getCommentsBefore(node);
|
|
66
|
+
let comment = comments.pop();
|
|
67
|
+
while (comment !== undefined && !isDoc(comment) && textLines(comment).length === 0)
|
|
68
|
+
comment = comments.pop();
|
|
69
|
+
return comment !== undefined && isDoc(comment) ? comment : undefined;
|
|
70
|
+
}
|
|
71
|
+
const isExport = (node) => node.type === 'ExportNamedDeclaration' ||
|
|
72
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
73
|
+
node.type === 'ExportAllDeclaration';
|
|
74
|
+
/** Top-level statements that export, and declarations of a binding that `export { name }` or `export default name` exports. */
|
|
75
|
+
function exportedStatements(program, sourceCode) {
|
|
76
|
+
const names = new Set();
|
|
77
|
+
for (const statement of program.body) {
|
|
78
|
+
if (statement.type === 'ExportNamedDeclaration' &&
|
|
79
|
+
statement.declaration === null &&
|
|
80
|
+
statement.source === null) {
|
|
81
|
+
for (const specifier of statement.specifiers) {
|
|
82
|
+
if (specifier.local.type === 'Identifier')
|
|
83
|
+
names.add(specifier.local.name);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else if (statement.type === 'ExportDefaultDeclaration' &&
|
|
87
|
+
statement.declaration.type === 'Identifier') {
|
|
88
|
+
names.add(statement.declaration.name);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const exported = new Set();
|
|
92
|
+
for (const statement of program.body) {
|
|
93
|
+
if (isExport(statement)) {
|
|
94
|
+
exported.add(statement);
|
|
95
|
+
}
|
|
96
|
+
else if (statement.type === 'VariableDeclaration') {
|
|
97
|
+
for (const declarator of statement.declarations) {
|
|
98
|
+
const declared = sourceCode.getDeclaredVariables(declarator);
|
|
99
|
+
if (declared.some((variable) => names.has(variable.name)))
|
|
100
|
+
exported.add(declarator);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if ('id' in statement &&
|
|
104
|
+
statement.id?.type === 'Identifier' &&
|
|
105
|
+
names.has(statement.id.name)) {
|
|
106
|
+
exported.add(statement);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return exported;
|
|
110
|
+
}
|
|
111
|
+
/** The statement a doc comment for an exported node sits above. */
|
|
112
|
+
const docTarget = (node) => node.type === 'VariableDeclarator' && node.parent !== null ? node.parent : node;
|
|
113
|
+
/**
|
|
114
|
+
* Whether a suppression directive follows a comment group on the next lines, past other
|
|
115
|
+
* directives, so the group is its reason, as `require-suppression-reason` reads it.
|
|
116
|
+
*/
|
|
117
|
+
function suppressionFollows(groups, index, ownLine) {
|
|
118
|
+
let line = groups[index]?.at(-1)?.loc.end.line ?? 0;
|
|
119
|
+
for (let next = index + 1; next < groups.length; next++) {
|
|
120
|
+
const comment = groups[next]?.[0];
|
|
121
|
+
if (comment === undefined || comment.loc.start.line !== line + 1)
|
|
122
|
+
return false;
|
|
123
|
+
// A reason may sit above a line that ends in `// oxlint-disable-line rule`.
|
|
124
|
+
if (!ownLine(comment))
|
|
125
|
+
return SUPPRESSION.test(comment.value);
|
|
126
|
+
if (SUPPRESSION.test(comment.value))
|
|
127
|
+
return true;
|
|
128
|
+
if (!isDirective(comment) && comment.value.replace(/[\s*]/gu, '') !== '')
|
|
129
|
+
return false;
|
|
130
|
+
line = comment.loc.end.line;
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
const FUNCTIONS = new Set([
|
|
135
|
+
'ArrowFunctionExpression',
|
|
136
|
+
'FunctionExpression',
|
|
137
|
+
'FunctionDeclaration',
|
|
138
|
+
]);
|
|
139
|
+
/** Whether a member belongs to an exported declaration and is public: not in a function body, not private. */
|
|
140
|
+
function isExposedMember(member, exported) {
|
|
141
|
+
let node = member;
|
|
142
|
+
for (;;) {
|
|
143
|
+
if (node.type === 'BlockStatement' || node.type === 'StaticBlock')
|
|
144
|
+
return false;
|
|
145
|
+
if ('accessibility' in node &&
|
|
146
|
+
(node.accessibility === 'private' || node.accessibility === 'protected')) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
if ('key' in node && node.key.type === 'PrivateIdentifier')
|
|
150
|
+
return false;
|
|
151
|
+
if (exported.has(node))
|
|
152
|
+
return true;
|
|
153
|
+
const parent = node.parent;
|
|
154
|
+
if (parent === null || parent.type === 'Program')
|
|
155
|
+
return false;
|
|
156
|
+
if (FUNCTIONS.has(parent.type) && 'body' in parent && parent.body === node)
|
|
157
|
+
return false;
|
|
158
|
+
node = parent;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Hold comments to a short why: a word budget, at most one cited issue, configured patterns, and
|
|
163
|
+
* no dividers, step labels, commented-out code, untracked TODOs, JSDoc types or comments that only
|
|
164
|
+
* repeat the name below them. `tagged-only` keeps just directives, licence headers, docs on exports
|
|
165
|
+
* and comments that start with an allowed prefix such as `SAFETY:`.
|
|
166
|
+
*/
|
|
167
|
+
export const commentsRule = defineRule({
|
|
168
|
+
meta: {
|
|
169
|
+
type: 'suggestion',
|
|
170
|
+
docs: {
|
|
171
|
+
description: 'Hold comments to a short why, or with the tagged-only policy, to tagged comments and docs on exports.',
|
|
172
|
+
},
|
|
173
|
+
schema: [
|
|
174
|
+
{
|
|
175
|
+
type: 'object',
|
|
176
|
+
properties: {
|
|
177
|
+
policy: { enum: ['budget', 'tagged-only'] },
|
|
178
|
+
maxWords: { type: 'integer', minimum: 1 },
|
|
179
|
+
maxExportDocWords: { type: 'integer', minimum: 1 },
|
|
180
|
+
allow: { type: 'array', items: { type: 'string', minLength: 1 } },
|
|
181
|
+
forbidden: {
|
|
182
|
+
type: 'array',
|
|
183
|
+
items: {
|
|
184
|
+
type: 'object',
|
|
185
|
+
properties: { pattern: { type: 'string' }, message: { type: 'string' } },
|
|
186
|
+
required: ['pattern', 'message'],
|
|
187
|
+
additionalProperties: false,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
additionalProperties: false,
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
defaultOptions: [
|
|
195
|
+
{
|
|
196
|
+
policy: 'budget',
|
|
197
|
+
maxWords: 30,
|
|
198
|
+
maxExportDocWords: 45,
|
|
199
|
+
allow: ['SAFETY', 'SYNC', 'WARNING'],
|
|
200
|
+
forbidden: [],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
messages: {
|
|
204
|
+
tooLong: "This comment has {{words}} words; the limit is {{max}}. Keep the why the code can't say, and leave the history to git and the PR.",
|
|
205
|
+
issueChain: 'This comment cites {{count}} issues. Cite the one that decided the code; git has the rest.',
|
|
206
|
+
forbidden: 'This comment has `{{match}}` in it. {{message}}',
|
|
207
|
+
divider: "Drop the section divider. Split the file, or let the code's structure show the grouping.",
|
|
208
|
+
stepLabel: 'Drop the test step label.',
|
|
209
|
+
commentedOutCode: 'Delete the commented-out code; git keeps it.',
|
|
210
|
+
restatesName: 'This comment only repeats the name below it. Say why, or say nothing.',
|
|
211
|
+
untrackedTask: 'Link the issue that tracks this, or drop the marker.',
|
|
212
|
+
jsdocType: 'Drop the JSDoc type; TypeScript has it.',
|
|
213
|
+
emptyJsdocTag: 'Drop the JSDoc tag that has no description.',
|
|
214
|
+
untagged: 'Remove this comment: put the meaning in names and the rationale in the PR. Comments that start with {{prefixes}}, directives and docs on exports stay.',
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
create(context) {
|
|
218
|
+
// SAFETY: oxlint validates the options against the schema above and fills in defaultOptions.
|
|
219
|
+
const [options] = context.options;
|
|
220
|
+
const { sourceCode } = context;
|
|
221
|
+
const typescript = TYPESCRIPT_FILE.test(context.filename);
|
|
222
|
+
const forbidden = options.forbidden.map(({ pattern, message }) => ({
|
|
223
|
+
pattern: new RegExp(pattern, 'u'),
|
|
224
|
+
message,
|
|
225
|
+
}));
|
|
226
|
+
const isTagged = (line) => options.allow.some((prefix) => line.startsWith(`${prefix}:`) && line.slice(prefix.length + 1).trim() !== '');
|
|
227
|
+
const prefixes = options.allow.map((prefix) => `\`${prefix}:\``).join(', ');
|
|
228
|
+
const members = [];
|
|
229
|
+
/** Declarations and members by where they start, for the name a comment sits above. */
|
|
230
|
+
const named = new Map();
|
|
231
|
+
const name = (node, id) => {
|
|
232
|
+
if (id?.type === 'Identifier')
|
|
233
|
+
named.set(node.start, id.name);
|
|
234
|
+
else if (id?.type === 'Literal' && typeof id.value === 'string')
|
|
235
|
+
named.set(node.start, id.value);
|
|
236
|
+
};
|
|
237
|
+
const collect = (node) => {
|
|
238
|
+
members.push(node);
|
|
239
|
+
if ('key' in node)
|
|
240
|
+
name(node, node.key);
|
|
241
|
+
else if ('id' in node)
|
|
242
|
+
name(node, node.id);
|
|
243
|
+
};
|
|
244
|
+
const declaration = (node) => {
|
|
245
|
+
if (node.type === 'VariableDeclaration')
|
|
246
|
+
name(node, node.declarations[0]?.id);
|
|
247
|
+
else if ('id' in node)
|
|
248
|
+
name(node, node.id);
|
|
249
|
+
};
|
|
250
|
+
const exported = (node) => {
|
|
251
|
+
const inner = node.declaration;
|
|
252
|
+
if (inner === null)
|
|
253
|
+
return;
|
|
254
|
+
if (inner.type === 'VariableDeclaration')
|
|
255
|
+
name(node, inner.declarations[0]?.id);
|
|
256
|
+
else if ('id' in inner)
|
|
257
|
+
name(node, inner.id);
|
|
258
|
+
};
|
|
259
|
+
const restatesName = (last, lines) => {
|
|
260
|
+
if (lines.some((line) => line.startsWith('@')))
|
|
261
|
+
return false;
|
|
262
|
+
// A trailing comment belongs to the code before it on its line.
|
|
263
|
+
const before = (sourceCode.lines[last.loc.start.line - 1] ?? '').slice(0, last.loc.start.column);
|
|
264
|
+
if (before.trim() !== '')
|
|
265
|
+
return false;
|
|
266
|
+
const next = sourceCode.getTokenAfter(last);
|
|
267
|
+
const name = next === null ? undefined : named.get(next.start);
|
|
268
|
+
if (name === undefined)
|
|
269
|
+
return false;
|
|
270
|
+
const words = contentWords(lines.join(' '));
|
|
271
|
+
const fromName = nameWords(name);
|
|
272
|
+
return words.length > 0 && words.length <= 6 && words.every((word) => fromName.has(word));
|
|
273
|
+
};
|
|
274
|
+
/** The first problem with a comment, as a message id and its data. */
|
|
275
|
+
const problem = (group, lines, exportDoc, explainsSuppression) => {
|
|
276
|
+
const isLine = group[0]?.type === 'Line';
|
|
277
|
+
const body = lines.join('\n');
|
|
278
|
+
const prose = body.replace(URL, '');
|
|
279
|
+
if (options.policy === 'tagged-only' &&
|
|
280
|
+
!exportDoc &&
|
|
281
|
+
!explainsSuppression &&
|
|
282
|
+
!isTagged(lines[0] ?? '')) {
|
|
283
|
+
return ['untagged', { prefixes }];
|
|
284
|
+
}
|
|
285
|
+
const isDoc = group[0]?.type === 'Block' && group[0].value.startsWith('*');
|
|
286
|
+
if (lines.some((line) => DIVIDER.test(line) && !(isDoc && HEADING.test(line))))
|
|
287
|
+
return ['divider'];
|
|
288
|
+
if (isLine && lines.length === 1 && STEP_LABEL.test(body))
|
|
289
|
+
return ['stepLabel'];
|
|
290
|
+
if (isLine && lines.every((line) => COMMENTED_CODE.test(line)))
|
|
291
|
+
return ['commentedOutCode'];
|
|
292
|
+
if (TASK_MARKER.test(body) && !TASK_LINK.test(body))
|
|
293
|
+
return ['untrackedTask'];
|
|
294
|
+
const issues = new Set(prose.replace(CODE_SPAN, '').match(ISSUE) ?? []);
|
|
295
|
+
if (issues.size > 1)
|
|
296
|
+
return ['issueChain', { count: issues.size }];
|
|
297
|
+
if (typescript && lines.some((line) => JSDOC_TYPE.test(line)))
|
|
298
|
+
return ['jsdocType'];
|
|
299
|
+
const describedLater = (index) => (lines[index + 1] ?? '@').startsWith('@') === false;
|
|
300
|
+
if (lines.some((line, index) => EMPTY_JSDOC_TAG.test(line) && !describedLater(index)))
|
|
301
|
+
return ['emptyJsdocTag'];
|
|
302
|
+
const last = group.at(-1);
|
|
303
|
+
if (last !== undefined && restatesName(last, lines))
|
|
304
|
+
return ['restatesName'];
|
|
305
|
+
const words = body.split(/\s+/u).filter((word) => word !== '').length;
|
|
306
|
+
const max = exportDoc ? options.maxExportDocWords : options.maxWords;
|
|
307
|
+
if (words > max)
|
|
308
|
+
return ['tooLong', { words, max }];
|
|
309
|
+
for (const { pattern, message } of forbidden) {
|
|
310
|
+
const match = pattern.exec(prose)?.[0];
|
|
311
|
+
if (match !== undefined)
|
|
312
|
+
return ['forbidden', { match, message }];
|
|
313
|
+
}
|
|
314
|
+
return undefined;
|
|
315
|
+
};
|
|
316
|
+
return {
|
|
317
|
+
TSPropertySignature: collect,
|
|
318
|
+
TSMethodSignature: collect,
|
|
319
|
+
TSIndexSignature: collect,
|
|
320
|
+
TSCallSignatureDeclaration: collect,
|
|
321
|
+
TSConstructSignatureDeclaration: collect,
|
|
322
|
+
TSEnumMember: collect,
|
|
323
|
+
PropertyDefinition: collect,
|
|
324
|
+
MethodDefinition: collect,
|
|
325
|
+
Property: collect,
|
|
326
|
+
VariableDeclaration: declaration,
|
|
327
|
+
FunctionDeclaration: declaration,
|
|
328
|
+
ClassDeclaration: declaration,
|
|
329
|
+
TSDeclareFunction: declaration,
|
|
330
|
+
TSInterfaceDeclaration: declaration,
|
|
331
|
+
TSTypeAliasDeclaration: declaration,
|
|
332
|
+
TSEnumDeclaration: declaration,
|
|
333
|
+
ExportNamedDeclaration: exported,
|
|
334
|
+
ExportDefaultDeclaration: exported,
|
|
335
|
+
'Program:exit'(program) {
|
|
336
|
+
const exportedNodes = exportedStatements(program, sourceCode);
|
|
337
|
+
const exportDocs = new Set();
|
|
338
|
+
const addDoc = (node) => {
|
|
339
|
+
const doc = docBefore(node, sourceCode);
|
|
340
|
+
if (doc !== undefined)
|
|
341
|
+
exportDocs.add(doc.start);
|
|
342
|
+
};
|
|
343
|
+
for (const node of exportedNodes)
|
|
344
|
+
addDoc(docTarget(node));
|
|
345
|
+
for (const member of members)
|
|
346
|
+
if (isExposedMember(member, exportedNodes))
|
|
347
|
+
addDoc(member);
|
|
348
|
+
const codeStart = program.body[0]?.start ?? sourceCode.text.length;
|
|
349
|
+
const groups = commentGroups(sourceCode.getAllComments(), sourceCode.text);
|
|
350
|
+
const ownLine = (comment) => (sourceCode.lines[comment.loc.start.line - 1] ?? '')
|
|
351
|
+
.slice(0, comment.loc.start.column)
|
|
352
|
+
.trim() === '';
|
|
353
|
+
for (const [index, group] of groups.entries()) {
|
|
354
|
+
const first = group[0];
|
|
355
|
+
const last = group.at(-1);
|
|
356
|
+
if (first === undefined || last === undefined)
|
|
357
|
+
continue;
|
|
358
|
+
const licence = first.value.startsWith('!') ||
|
|
359
|
+
group.some((comment) => LICENSE.test(comment.value)) ||
|
|
360
|
+
(last.end <= codeStart && group.some((comment) => COPYRIGHT.test(comment.value)));
|
|
361
|
+
const lines = group.flatMap(textLines);
|
|
362
|
+
if (licence || lines.length === 0)
|
|
363
|
+
continue;
|
|
364
|
+
const explainsSuppression = options.policy === 'tagged-only' &&
|
|
365
|
+
ownLine(first) &&
|
|
366
|
+
suppressionFollows(groups, index, ownLine);
|
|
367
|
+
const found = problem(group, lines, group.length === 1 && exportDocs.has(first.start), explainsSuppression);
|
|
368
|
+
if (found === undefined)
|
|
369
|
+
continue;
|
|
370
|
+
const [messageId, data] = found;
|
|
371
|
+
context.report({ loc: { start: first.loc.start, end: last.loc.end }, messageId, data });
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
},
|
|
376
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
/** The rules that keep type assertions out. An inline disable of any of them lets a cast through. */
|
|
3
|
+
const defaultTypeAssertionRules = [
|
|
4
|
+
"anti-slop/no-chained-type-assertions",
|
|
5
|
+
"anti-slop/no-widen-then-assert",
|
|
6
|
+
"anti-slop/require-safety-comment-for-type-assertion",
|
|
7
|
+
"effecttsgo/unsafe-effect-type-assertion",
|
|
8
|
+
"typescript/consistent-type-assertions",
|
|
9
|
+
"typescript/no-unsafe-type-assertion",
|
|
10
|
+
];
|
|
11
|
+
const directivePattern = /^\s*(?:oxlint|eslint)-disable(?:-next-line|-line)?(?=\s|$)(.*)$/su;
|
|
12
|
+
/** Rule names are compared with `@typescript-eslint/` read as `typescript/`. */
|
|
13
|
+
function normalizeRuleName(rule) {
|
|
14
|
+
return rule.trim().replace(/^@?typescript-eslint\//u, "typescript/");
|
|
15
|
+
}
|
|
16
|
+
/** The rule names a disable directive lists, or null when the comment is not a disable directive. */
|
|
17
|
+
function directiveRules(value) {
|
|
18
|
+
const match = directivePattern.exec(value);
|
|
19
|
+
if (match === null)
|
|
20
|
+
return null;
|
|
21
|
+
// Oxlint ends the rule list at ` --`, with or without a space before the reason.
|
|
22
|
+
const list = (match[1] ?? "").split(/\s--/u)[0] ?? "";
|
|
23
|
+
// Commas, spaces and newlines all separate rule names.
|
|
24
|
+
return list
|
|
25
|
+
.split(/[\s,]+/u)
|
|
26
|
+
.map(normalizeRuleName)
|
|
27
|
+
.filter((rule) => rule.length > 0);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Oxlint applies a disable to a built-in rule by the name after the slash, whatever the prefix, and
|
|
31
|
+
* a disable with no prefix to that built-in rule too. A bare name doesn't reach a JS plugin rule, so
|
|
32
|
+
* reporting one only flags a disable that does nothing.
|
|
33
|
+
*/
|
|
34
|
+
function isTypeAssertionRule(typeAssertionRules, rule) {
|
|
35
|
+
if (typeAssertionRules.has(rule))
|
|
36
|
+
return true;
|
|
37
|
+
const name = rule.slice(rule.lastIndexOf("/") + 1);
|
|
38
|
+
return [...typeAssertionRules].some((known) => known === `typescript/${name}` || (!rule.includes("/") && known.endsWith(`/${name}`)));
|
|
39
|
+
}
|
|
40
|
+
function configuredAdditionalRules(option) {
|
|
41
|
+
if (typeof option !== "object" || option === null || !("additionalRules" in option))
|
|
42
|
+
return [];
|
|
43
|
+
const { additionalRules } = option;
|
|
44
|
+
if (!Array.isArray(additionalRules))
|
|
45
|
+
return [];
|
|
46
|
+
return additionalRules.filter((rule) => typeof rule === "string").map(normalizeRuleName);
|
|
47
|
+
}
|
|
48
|
+
/** Disallow inline disables of the type-assertion rules, and disables that name no rule at all. */
|
|
49
|
+
export const noDisabledTypeAssertionRulesRule = defineRule({
|
|
50
|
+
meta: {
|
|
51
|
+
type: "problem",
|
|
52
|
+
docs: {
|
|
53
|
+
description: "Disallow inline disable comments for the type-assertion rules, and disable comments that name no rule.",
|
|
54
|
+
},
|
|
55
|
+
messages: {
|
|
56
|
+
disabledRule: "Don't disable `{{rule}}` inline. Remove the cast, or add a file override with its reason in the lint config.",
|
|
57
|
+
disabledEverything: "This disable comment names no rule, so it turns off the type-assertion rules too. Name the rules it disables.",
|
|
58
|
+
},
|
|
59
|
+
schema: [
|
|
60
|
+
{
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: {
|
|
63
|
+
additionalRules: { type: "array", items: { type: "string", minLength: 1 }, uniqueItems: true },
|
|
64
|
+
},
|
|
65
|
+
additionalProperties: false,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
defaultOptions: [{ additionalRules: [] }],
|
|
69
|
+
},
|
|
70
|
+
createOnce(context) {
|
|
71
|
+
return {
|
|
72
|
+
Program() {
|
|
73
|
+
const typeAssertionRules = new Set([
|
|
74
|
+
...defaultTypeAssertionRules,
|
|
75
|
+
...configuredAdditionalRules(context.options?.[0]),
|
|
76
|
+
]);
|
|
77
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
78
|
+
const rules = directiveRules(comment.value);
|
|
79
|
+
if (rules === null)
|
|
80
|
+
continue;
|
|
81
|
+
if (rules.length === 0) {
|
|
82
|
+
context.report({ node: comment, messageId: "disabledEverything" });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
for (const rule of rules) {
|
|
86
|
+
if (!isTypeAssertionRule(typeAssertionRules, rule))
|
|
87
|
+
continue;
|
|
88
|
+
context.report({ node: comment, messageId: "disabledRule", data: { rule } });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Require every lint or type-checker suppression to say why: after ` -- ` (or after the
|
|
3
|
+
* `@ts-` directive), or in a comment on the line above. Adapted from T3 Code's
|
|
4
|
+
* `require-suppression-reason` (MIT, T3 Tools Inc.).
|
|
5
|
+
*/
|
|
6
|
+
export declare const requireSuppressionReasonRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
const DIRECTIVE = /^\s*\*?\s*(?:(?:oxlint|eslint)-(?:disable|enable)|@ts-(?:expect-error|ignore|nocheck|check)|@effect-diagnostics|(?:prettier|oxfmt)-ignore|biome-ignore|svelte-ignore|(?:c8|v8|istanbul)\s+ignore|@(?:vitest|jest)-environment|@vite-ignore|@jsx|nosemgrep\b)/u;
|
|
3
|
+
const TYPESCRIPT_DIRECTIVE = /^\s*\*?\s*@ts-(?:expect-error|ignore|nocheck)(?![\w-])(.*)$/su;
|
|
4
|
+
const EMPTY = /^[\s*]*$/u;
|
|
5
|
+
/**
|
|
6
|
+
* Require every lint or type-checker suppression to say why: after ` -- ` (or after the
|
|
7
|
+
* `@ts-` directive), or in a comment on the line above. Adapted from T3 Code's
|
|
8
|
+
* `require-suppression-reason` (MIT, T3 Tools Inc.).
|
|
9
|
+
*/
|
|
10
|
+
export const requireSuppressionReasonRule = defineRule({
|
|
11
|
+
meta: {
|
|
12
|
+
type: 'suggestion',
|
|
13
|
+
docs: { description: 'Require every lint or type-checker suppression to say why.' },
|
|
14
|
+
messages: {
|
|
15
|
+
lintDirective: 'Say why: end the directive with `-- reason`, or put a comment on the line above.',
|
|
16
|
+
typescriptDirective: 'Say why: write the reason after the directive, or in a comment on the line above.',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
createOnce(context) {
|
|
20
|
+
return {
|
|
21
|
+
Program() {
|
|
22
|
+
const { sourceCode } = context;
|
|
23
|
+
const comments = sourceCode.getAllComments();
|
|
24
|
+
const ownLineByEndLine = new Map();
|
|
25
|
+
for (const comment of comments) {
|
|
26
|
+
if (comment.type === 'Shebang')
|
|
27
|
+
continue;
|
|
28
|
+
const { line, column } = comment.loc.start;
|
|
29
|
+
if ((sourceCode.lines[line - 1] ?? '').slice(0, column).trim() === '') {
|
|
30
|
+
ownLineByEndLine.set(comment.loc.end.line, comment);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Whether a prose comment sits directly above, past other directives and empty comments. */
|
|
34
|
+
const explainedAbove = (directive) => {
|
|
35
|
+
let above = ownLineByEndLine.get(directive.loc.start.line - 1);
|
|
36
|
+
while (above !== undefined && (DIRECTIVE.test(above.value) || EMPTY.test(above.value))) {
|
|
37
|
+
above = ownLineByEndLine.get(above.loc.start.line - 1);
|
|
38
|
+
}
|
|
39
|
+
return above !== undefined;
|
|
40
|
+
};
|
|
41
|
+
for (const directive of sourceCode.getDisableDirectives().directives) {
|
|
42
|
+
if (directive.type === 'enable' || directive.justification?.trim())
|
|
43
|
+
continue;
|
|
44
|
+
if (explainedAbove(directive.node))
|
|
45
|
+
continue;
|
|
46
|
+
context.report({ loc: directive.node.loc, messageId: 'lintDirective' });
|
|
47
|
+
}
|
|
48
|
+
for (const comment of comments) {
|
|
49
|
+
if (comment.type === 'Shebang')
|
|
50
|
+
continue;
|
|
51
|
+
const after = TYPESCRIPT_DIRECTIVE.exec(comment.value)?.[1];
|
|
52
|
+
if (after === undefined || after.replace(/^[\s:-]+/u, '').trim() !== '')
|
|
53
|
+
continue;
|
|
54
|
+
if (explainedAbove(comment))
|
|
55
|
+
continue;
|
|
56
|
+
context.report({ loc: comment.loc, messageId: 'typescriptDirective' });
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 T3 Tools Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { eslintCompatPlugin } from '@oxlint/plugins';
|
|
2
|
+
import { noHooklessUseFunctionRule } from './rules/no-hookless-use-function.js';
|
|
3
|
+
import { noManualMemoRule } from './rules/no-manual-memo.js';
|
|
4
|
+
import { requireSyncCommentForEffectRule } from './rules/require-sync-comment-for-effect.js';
|
|
5
|
+
/** Oxlint rules for React code that the React Compiler compiles. */
|
|
6
|
+
const antiSlopReactPlugin = eslintCompatPlugin({
|
|
7
|
+
meta: { name: 'anti-slop-react' },
|
|
8
|
+
rules: {
|
|
9
|
+
'no-hookless-use-function': noHooklessUseFunctionRule,
|
|
10
|
+
'no-manual-memo': noManualMemoRule,
|
|
11
|
+
'require-sync-comment-for-effect': requireSyncCommentForEffectRule,
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
export default antiSlopReactPlugin;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disallow `use*` functions that call no hook during their own body. React treats the name as
|
|
3
|
+
* a hook, and the React Compiler only memoizes functions that call one, so whatever a hook-less
|
|
4
|
+
* `use*` function returns is rebuilt on every render. From T3 Code's rule of the same name,
|
|
5
|
+
* which mirrors eslint-react's `no-unnecessary-use-prefix`; this one tracks hook calls through
|
|
6
|
+
* oxlint's traversal instead of walking each function's body.
|
|
7
|
+
*/
|
|
8
|
+
export declare const noHooklessUseFunctionRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { defineRule } from '@oxlint/plugins';
|
|
2
|
+
const HOOK_NAME = /^use[A-Z]/;
|
|
3
|
+
// Bare `use` is React 19's `use(promise | context)`, which is a hook call too.
|
|
4
|
+
const HOOK_CALLEE = /^use([A-Z]|$)/;
|
|
5
|
+
function isHookCall(callee) {
|
|
6
|
+
if (callee.type === 'Identifier')
|
|
7
|
+
return HOOK_CALLEE.test(callee.name);
|
|
8
|
+
return (callee.type === 'MemberExpression' && !callee.computed && HOOK_CALLEE.test(callee.property.name));
|
|
9
|
+
}
|
|
10
|
+
/** The name a function is declared or assigned under, as `function useX` or `const useX = () =>`. */
|
|
11
|
+
function functionName(node) {
|
|
12
|
+
if (node.type === 'FunctionDeclaration')
|
|
13
|
+
return node.id ?? undefined;
|
|
14
|
+
const parent = node.parent;
|
|
15
|
+
if (parent.type === 'VariableDeclarator' &&
|
|
16
|
+
parent.init === node &&
|
|
17
|
+
parent.id.type === 'Identifier') {
|
|
18
|
+
return parent.id;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Disallow `use*` functions that call no hook during their own body. React treats the name as
|
|
24
|
+
* a hook, and the React Compiler only memoizes functions that call one, so whatever a hook-less
|
|
25
|
+
* `use*` function returns is rebuilt on every render. From T3 Code's rule of the same name,
|
|
26
|
+
* which mirrors eslint-react's `no-unnecessary-use-prefix`; this one tracks hook calls through
|
|
27
|
+
* oxlint's traversal instead of walking each function's body.
|
|
28
|
+
*/
|
|
29
|
+
export const noHooklessUseFunctionRule = defineRule({
|
|
30
|
+
meta: {
|
|
31
|
+
type: 'problem',
|
|
32
|
+
docs: {
|
|
33
|
+
description: 'Disallow use* functions that call no hooks.',
|
|
34
|
+
},
|
|
35
|
+
messages: {
|
|
36
|
+
noHook: '"{{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.',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
createOnce(context) {
|
|
40
|
+
// One entry per function being visited: whether its own body, outside nested functions,
|
|
41
|
+
// calls a hook.
|
|
42
|
+
const callsHook = [];
|
|
43
|
+
const enter = () => {
|
|
44
|
+
callsHook.push(false);
|
|
45
|
+
};
|
|
46
|
+
const exit = (node) => {
|
|
47
|
+
const called = callsHook.pop();
|
|
48
|
+
const name = functionName(node);
|
|
49
|
+
if (called === true || name === undefined || !HOOK_NAME.test(name.name))
|
|
50
|
+
return;
|
|
51
|
+
context.report({ node: name, messageId: 'noHook', data: { name: name.name } });
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
FunctionDeclaration: enter,
|
|
55
|
+
FunctionExpression: enter,
|
|
56
|
+
ArrowFunctionExpression: enter,
|
|
57
|
+
'FunctionDeclaration:exit': exit,
|
|
58
|
+
'FunctionExpression:exit': exit,
|
|
59
|
+
'ArrowFunctionExpression:exit': exit,
|
|
60
|
+
CallExpression(node) {
|
|
61
|
+
if (callsHook.length > 0 && isHookCall(node.callee)) {
|
|
62
|
+
callsHook[callsHook.length - 1] = true;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
});
|