@voidh4x0r/slopgate 0.0.0-stage → 0.1.1
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/CHANGELOG.md +82 -0
- package/LICENSE +21 -0
- package/README.md +304 -2
- package/dist/ast.d.ts +29 -0
- package/dist/ast.js +3 -0
- package/dist/baseline.d.ts +33 -0
- package/dist/baseline.js +104 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +487 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +91 -0
- package/dist/data/python-stdlib.d.ts +21 -0
- package/dist/data/python-stdlib.js +92 -0
- package/dist/deps.d.ts +8 -0
- package/dist/deps.js +491 -0
- package/dist/diff.d.ts +30 -0
- package/dist/diff.js +217 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.js +169 -0
- package/dist/files.d.ts +33 -0
- package/dist/files.js +214 -0
- package/dist/mcp.d.ts +41 -0
- package/dist/mcp.js +226 -0
- package/dist/parser.d.ts +3 -0
- package/dist/parser.js +45 -0
- package/dist/reporters/github.d.ts +2 -0
- package/dist/reporters/github.js +44 -0
- package/dist/reporters/json.d.ts +10 -0
- package/dist/reporters/json.js +36 -0
- package/dist/reporters/pretty.d.ts +12 -0
- package/dist/reporters/pretty.js +103 -0
- package/dist/reporters/sarif.d.ts +2 -0
- package/dist/reporters/sarif.js +75 -0
- package/dist/rules/helpers.d.ts +53 -0
- package/dist/rules/helpers.js +178 -0
- package/dist/rules/index.d.ts +3 -0
- package/dist/rules/index.js +4 -0
- package/dist/rules/python.d.ts +2 -0
- package/dist/rules/python.js +632 -0
- package/dist/rules/typescript.d.ts +2 -0
- package/dist/rules/typescript.js +563 -0
- package/dist/suppress.d.ts +12 -0
- package/dist/suppress.js +52 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.js +1 -0
- package/grammars/README.md +28 -0
- package/grammars/tree-sitter-python.wasm +0 -0
- package/grammars/tree-sitter-tsx.wasm +0 -0
- package/grammars/tree-sitter-typescript.wasm +0 -0
- package/package.json +61 -4
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
import { builtinModules } from 'node:module';
|
|
2
|
+
import { ancestorOfType, collapseWhitespace, descendantsOfType, endOf, hasCommentBetween, isComment, isTestPath, snippetOf, startOf, truncate, walk, } from "./helpers.js";
|
|
3
|
+
const NODE_BUILTINS = new Set(builtinModules.flatMap((name) => [name, `node:${name}`]));
|
|
4
|
+
const ASSET_SPECIFIER = /\.(css|scss|sass|less|styl|svg|png|jpe?g|gif|webp|avif|ico|json|wasm|mdx?|txt|graphql|gql|vue|svelte|astro|html?)(\?.*)?$/i;
|
|
5
|
+
const VIRTUAL_SPECIFIER = /^(virtual|vite|webpack|bun|deno|astro|parcel|rollup|pnp|data|file|https?|blob):/;
|
|
6
|
+
const TERMINATORS = new Set([
|
|
7
|
+
'return_statement',
|
|
8
|
+
'throw_statement',
|
|
9
|
+
'break_statement',
|
|
10
|
+
'continue_statement',
|
|
11
|
+
]);
|
|
12
|
+
function stringLiteralValue(node) {
|
|
13
|
+
if (!node || node.type !== 'string')
|
|
14
|
+
return undefined;
|
|
15
|
+
const raw = node.text;
|
|
16
|
+
if (raw.length < 2)
|
|
17
|
+
return undefined;
|
|
18
|
+
return raw.slice(1, -1);
|
|
19
|
+
}
|
|
20
|
+
function sourceString(node) {
|
|
21
|
+
const field = node.childForFieldName('source');
|
|
22
|
+
const fromField = stringLiteralValue(field);
|
|
23
|
+
if (fromField !== undefined)
|
|
24
|
+
return fromField;
|
|
25
|
+
for (let i = node.namedChildren.length - 1; i >= 0; i -= 1) {
|
|
26
|
+
const child = node.namedChildren[i];
|
|
27
|
+
const value = stringLiteralValue(child);
|
|
28
|
+
if (value !== undefined)
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
function moduleSpecifiers(root) {
|
|
34
|
+
const refs = [];
|
|
35
|
+
walk(root, (node) => {
|
|
36
|
+
if (node.type === 'import_statement' || node.type === 'export_statement') {
|
|
37
|
+
const specifier = sourceString(node);
|
|
38
|
+
if (specifier)
|
|
39
|
+
refs.push({ node, specifier });
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (node.type === 'call_expression') {
|
|
43
|
+
const fn = node.childForFieldName('function');
|
|
44
|
+
const fnName = fn ? collapseWhitespace(fn.text) : '';
|
|
45
|
+
if (fnName === 'require' || fnName === 'import') {
|
|
46
|
+
const args = node.childForFieldName('arguments');
|
|
47
|
+
const specifier = stringLiteralValue(args?.namedChildren[0]);
|
|
48
|
+
if (specifier)
|
|
49
|
+
refs.push({ node, specifier });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
});
|
|
54
|
+
return refs;
|
|
55
|
+
}
|
|
56
|
+
function packageRootOf(specifier) {
|
|
57
|
+
const parts = specifier.split('/');
|
|
58
|
+
if (specifier.startsWith('@'))
|
|
59
|
+
return parts.slice(0, 2).join('/');
|
|
60
|
+
return parts[0] ?? specifier;
|
|
61
|
+
}
|
|
62
|
+
function matchesAlias(specifier, aliases) {
|
|
63
|
+
return aliases.some((alias) => {
|
|
64
|
+
const clean = alias.replace(/\/+$/, '');
|
|
65
|
+
return specifier === clean || specifier.startsWith(`${clean}/`);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const ASSERTION_ROOTS = new Set(['assert', 'expect', 'expectTypeOf', 'should', 'chai']);
|
|
69
|
+
const T_ASSERTION_MEMBERS = new Set([
|
|
70
|
+
'ok',
|
|
71
|
+
'is',
|
|
72
|
+
'not',
|
|
73
|
+
'same',
|
|
74
|
+
'throws',
|
|
75
|
+
'rejects',
|
|
76
|
+
'match',
|
|
77
|
+
'snapshot',
|
|
78
|
+
'pass',
|
|
79
|
+
'fail',
|
|
80
|
+
'assert',
|
|
81
|
+
]);
|
|
82
|
+
/**
|
|
83
|
+
* Test whether a call is an assertion. The full callee path matters:
|
|
84
|
+
* `assert.strictEqual` ends in `strictEqual`, which the old last-segment
|
|
85
|
+
* regex missed, so node:assert-based tests were reported as assertion-free.
|
|
86
|
+
*/
|
|
87
|
+
function isAssertionCallee(text) {
|
|
88
|
+
if (text.length === 0)
|
|
89
|
+
return false;
|
|
90
|
+
// node:assert and node:assert/strict: assert(...) plus every method.
|
|
91
|
+
if (text === 'assert' || text.startsWith('assert.'))
|
|
92
|
+
return true;
|
|
93
|
+
// Jest/Vitest/Chai: expect(x).toBe(y), expect(x).not.toEqual(y), expect.soft(...)
|
|
94
|
+
if (text === 'expect' || text === 'expectTypeOf')
|
|
95
|
+
return true;
|
|
96
|
+
if (text.startsWith('expect(') || text.startsWith('expect.') || text.startsWith('expectTypeOf(')) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (text === 'should' || text.startsWith('should.') || text.startsWith('should('))
|
|
100
|
+
return true;
|
|
101
|
+
if (text.startsWith('chai.'))
|
|
102
|
+
return true;
|
|
103
|
+
// node:test context: t.ok, t.is, t.throws, t.assert.strictEqual, ...
|
|
104
|
+
if (/^t\.assert(\.[A-Za-z_$][\w$]*)*$/.test(text))
|
|
105
|
+
return true;
|
|
106
|
+
if (/^t\.[A-Za-z_$][\w$]*$/.test(text)) {
|
|
107
|
+
const member = text.slice(2);
|
|
108
|
+
return T_ASSERTION_MEMBERS.has(member);
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
function hasAssertion(body) {
|
|
113
|
+
let found = false;
|
|
114
|
+
walk(body, (node) => {
|
|
115
|
+
if (found)
|
|
116
|
+
return false;
|
|
117
|
+
if (node.type === 'call_expression') {
|
|
118
|
+
const fn = node.childForFieldName('function') ?? node.namedChildren[0];
|
|
119
|
+
const text = collapseWhitespace(fn?.text ?? '');
|
|
120
|
+
if (isAssertionCallee(text)) {
|
|
121
|
+
found = true;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
});
|
|
127
|
+
return found;
|
|
128
|
+
}
|
|
129
|
+
const phantomImport = {
|
|
130
|
+
meta: {
|
|
131
|
+
id: 'ts/phantom-import',
|
|
132
|
+
title: 'Import is not a dependency',
|
|
133
|
+
summary: 'Finds imports of packages that package.json never declares (and, worse, that are not even installed). The most common way AI code fails to run anywhere but the machine it was written on.',
|
|
134
|
+
severity: 'error',
|
|
135
|
+
confidence: 'high',
|
|
136
|
+
languages: ['typescript', 'tsx'],
|
|
137
|
+
tags: ['dependencies', 'hallucination'],
|
|
138
|
+
},
|
|
139
|
+
run(ctx) {
|
|
140
|
+
if (ctx.language === 'python')
|
|
141
|
+
return [];
|
|
142
|
+
const drafts = [];
|
|
143
|
+
if (!ctx.deps.node.hasPackageJson)
|
|
144
|
+
return drafts;
|
|
145
|
+
const { declared, installed, workspaces, aliases } = ctx.deps.node;
|
|
146
|
+
for (const { node, specifier } of moduleSpecifiers(ctx.root)) {
|
|
147
|
+
if (specifier.startsWith('.') || specifier.startsWith('/'))
|
|
148
|
+
continue;
|
|
149
|
+
if (NODE_BUILTINS.has(specifier))
|
|
150
|
+
continue;
|
|
151
|
+
if (specifier.startsWith('#'))
|
|
152
|
+
continue;
|
|
153
|
+
if (ASSET_SPECIFIER.test(specifier))
|
|
154
|
+
continue;
|
|
155
|
+
if (VIRTUAL_SPECIFIER.test(specifier))
|
|
156
|
+
continue;
|
|
157
|
+
if (matchesAlias(specifier, aliases))
|
|
158
|
+
continue;
|
|
159
|
+
// `try { require('optional-dep') } catch {}` is the optional-dependency idiom.
|
|
160
|
+
if (ancestorOfType(node, ['try_statement']))
|
|
161
|
+
continue;
|
|
162
|
+
const pkg = packageRootOf(specifier);
|
|
163
|
+
if (NODE_BUILTINS.has(pkg))
|
|
164
|
+
continue;
|
|
165
|
+
if (matchesAlias(pkg, aliases))
|
|
166
|
+
continue;
|
|
167
|
+
if (declared.has(pkg) || workspaces.has(pkg))
|
|
168
|
+
continue;
|
|
169
|
+
if (ctx.deps.ignoredModules.has(pkg))
|
|
170
|
+
continue;
|
|
171
|
+
const isInstalled = installed.has(pkg);
|
|
172
|
+
drafts.push({
|
|
173
|
+
message: isInstalled
|
|
174
|
+
? `\`${pkg}\` is imported but not listed in package.json`
|
|
175
|
+
: `\`${pkg}\` is imported but is not a dependency and is not installed`,
|
|
176
|
+
start: startOf(node),
|
|
177
|
+
end: endOf(node),
|
|
178
|
+
snippet: snippetOf(ctx, node),
|
|
179
|
+
confidence: isInstalled || isTestPath(ctx.file) ? 'medium' : 'high',
|
|
180
|
+
evidence: [
|
|
181
|
+
`specifier: ${specifier}`,
|
|
182
|
+
isInstalled ? `found in node_modules/${pkg}` : `not found in node_modules/${pkg}`,
|
|
183
|
+
'not listed in dependencies, devDependencies, peerDependencies or optionalDependencies',
|
|
184
|
+
],
|
|
185
|
+
help: isInstalled
|
|
186
|
+
? 'Add it to package.json so a fresh install reproduces your build.'
|
|
187
|
+
: 'A package that is not installed cannot be run. Check the name on npm: this is the signature of a hallucinated package.',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return drafts;
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
const TEST_MODIFIERS = new Set([
|
|
194
|
+
'only',
|
|
195
|
+
'skip',
|
|
196
|
+
'todo',
|
|
197
|
+
'concurrent',
|
|
198
|
+
'sequential',
|
|
199
|
+
'each',
|
|
200
|
+
'fails',
|
|
201
|
+
'failing',
|
|
202
|
+
'serial',
|
|
203
|
+
]);
|
|
204
|
+
/**
|
|
205
|
+
* `test(...)` / `it(...)` and their modifiers (`test.only`, `test.each(...)`).
|
|
206
|
+
* The root segment alone is not enough: `test.valid.forEach(...)` inside
|
|
207
|
+
* ESLint's rule tester is a data loop, not a test.
|
|
208
|
+
*/
|
|
209
|
+
function isTestCall(fn) {
|
|
210
|
+
let node = fn;
|
|
211
|
+
if (node.type === 'call_expression')
|
|
212
|
+
node = node.childForFieldName('function') ?? node;
|
|
213
|
+
const text = collapseWhitespace(node.text);
|
|
214
|
+
const root = text.split(/[.(]/)[0] ?? '';
|
|
215
|
+
if (root !== 'test' && root !== 'it')
|
|
216
|
+
return false;
|
|
217
|
+
const modifiers = [...text.matchAll(/\.([A-Za-z_$][\w$]*)/g)].map((match) => match[1] ?? '');
|
|
218
|
+
return modifiers.every((name) => TEST_MODIFIERS.has(name));
|
|
219
|
+
}
|
|
220
|
+
const noopTest = {
|
|
221
|
+
meta: {
|
|
222
|
+
id: 'ts/noop-test',
|
|
223
|
+
title: 'Test does not assert anything',
|
|
224
|
+
summary: 'Finds `test()` / `it()` callbacks with an empty body or no assertion, so they pass regardless of the code.',
|
|
225
|
+
severity: 'warning',
|
|
226
|
+
confidence: 'high',
|
|
227
|
+
languages: ['typescript', 'tsx'],
|
|
228
|
+
tags: ['tests', 'theater'],
|
|
229
|
+
},
|
|
230
|
+
run(ctx) {
|
|
231
|
+
if (ctx.language === 'python')
|
|
232
|
+
return [];
|
|
233
|
+
const drafts = [];
|
|
234
|
+
for (const call of descendantsOfType(ctx.root, ['call_expression'])) {
|
|
235
|
+
const fn = call.childForFieldName('function');
|
|
236
|
+
if (!fn || !isTestCall(fn))
|
|
237
|
+
continue;
|
|
238
|
+
const args = call.childForFieldName('arguments');
|
|
239
|
+
if (!args)
|
|
240
|
+
continue;
|
|
241
|
+
const callback = [...args.namedChildren]
|
|
242
|
+
.reverse()
|
|
243
|
+
.find((child) => child.type === 'arrow_function' || child.type === 'function_expression' || child.type === 'function');
|
|
244
|
+
if (!callback)
|
|
245
|
+
continue;
|
|
246
|
+
const body = callback.childForFieldName('body');
|
|
247
|
+
if (!body)
|
|
248
|
+
continue;
|
|
249
|
+
const label = args.namedChildren[0]?.type === 'string' ? truncate(args.namedChildren[0].text, 60) : undefined;
|
|
250
|
+
if (body.type !== 'statement_block') {
|
|
251
|
+
if (!hasAssertion(body)) {
|
|
252
|
+
drafts.push({
|
|
253
|
+
message: 'test makes no assertions',
|
|
254
|
+
start: startOf(call),
|
|
255
|
+
end: endOf(body),
|
|
256
|
+
snippet: snippetOf(ctx, call),
|
|
257
|
+
evidence: [`test: ${label ?? '(anonymous)'}`, 'the test body only calls code without checking a result'],
|
|
258
|
+
help: 'Every test should be able to fail. Add an assertion that depends on the behaviour under test.',
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
264
|
+
if (statements.length === 0) {
|
|
265
|
+
drafts.push({
|
|
266
|
+
message: 'test has an empty body',
|
|
267
|
+
start: startOf(call),
|
|
268
|
+
end: endOf(body),
|
|
269
|
+
snippet: snippetOf(ctx, call),
|
|
270
|
+
evidence: [`test: ${label ?? '(anonymous)'}`],
|
|
271
|
+
help: 'Add an assertion, or delete the test.',
|
|
272
|
+
});
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (!hasAssertion(body)) {
|
|
276
|
+
drafts.push({
|
|
277
|
+
message: 'test makes no assertions',
|
|
278
|
+
start: startOf(call),
|
|
279
|
+
end: endOf(body),
|
|
280
|
+
snippet: snippetOf(ctx, call),
|
|
281
|
+
evidence: [`test: ${label ?? '(anonymous)'}`, `statements: ${statements.length}`, 'no expect() / assert() found'],
|
|
282
|
+
help: 'Every test should be able to fail. Add an assertion that depends on the behaviour under test.',
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return drafts;
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
const tautologicalAssert = {
|
|
290
|
+
meta: {
|
|
291
|
+
id: 'ts/tautological-assert',
|
|
292
|
+
title: 'Assertion compares a value to itself',
|
|
293
|
+
summary: 'Finds `expect(true).toBe(true)`, `expect(x).toEqual(x)` and similar assertions that cannot fail.',
|
|
294
|
+
severity: 'warning',
|
|
295
|
+
confidence: 'high',
|
|
296
|
+
languages: ['typescript', 'tsx'],
|
|
297
|
+
tags: ['tests', 'theater'],
|
|
298
|
+
},
|
|
299
|
+
run(ctx) {
|
|
300
|
+
if (ctx.language === 'python')
|
|
301
|
+
return [];
|
|
302
|
+
const drafts = [];
|
|
303
|
+
for (const call of descendantsOfType(ctx.root, ['call_expression'])) {
|
|
304
|
+
const fn = call.childForFieldName('function');
|
|
305
|
+
if (!fn || fn.type !== 'member_expression')
|
|
306
|
+
continue;
|
|
307
|
+
const property = fn.childForFieldName('property')?.text;
|
|
308
|
+
if (!property)
|
|
309
|
+
continue;
|
|
310
|
+
const object = fn.childForFieldName('object');
|
|
311
|
+
const args = call.childForFieldName('arguments');
|
|
312
|
+
const actual = args?.namedChildren[0]?.text;
|
|
313
|
+
if (actual === undefined)
|
|
314
|
+
continue;
|
|
315
|
+
let expected;
|
|
316
|
+
if (object?.type === 'call_expression' && collapseWhitespace(object.childForFieldName('function')?.text ?? '') === 'expect') {
|
|
317
|
+
expected = object.childForFieldName('arguments')?.namedChildren[0]?.text;
|
|
318
|
+
}
|
|
319
|
+
const isSelf = expected !== undefined &&
|
|
320
|
+
expected === actual &&
|
|
321
|
+
['toBe', 'toEqual', 'toStrictEqual'].includes(property);
|
|
322
|
+
const isTruthyTrue = property === 'toBeTruthy' && expected === 'true';
|
|
323
|
+
if (!isSelf && !isTruthyTrue)
|
|
324
|
+
continue;
|
|
325
|
+
drafts.push({
|
|
326
|
+
message: `assertion compares a value to itself: \`${truncate(collapseWhitespace(call.text), 80)}\``,
|
|
327
|
+
start: startOf(call),
|
|
328
|
+
end: endOf(call),
|
|
329
|
+
snippet: snippetOf(ctx, call),
|
|
330
|
+
evidence: ['expected and actual expressions are identical'],
|
|
331
|
+
help: 'Assert on the behaviour under test: a return value, a thrown error, or a call.',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
return drafts;
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
function stubForm(body) {
|
|
338
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
339
|
+
if (statements.length === 0)
|
|
340
|
+
return 'empty body';
|
|
341
|
+
if (statements.length === 1 && statements[0]?.type === 'throw_statement') {
|
|
342
|
+
if (/not\s*implemented|notimplemented|todo/i.test(statements[0].text))
|
|
343
|
+
return 'throws "not implemented"';
|
|
344
|
+
}
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
const NOOP_NAMES = /^(noop|no_?op|nop|ignore|identity|pass|doNothing|noopFn|noOpFn)$/i;
|
|
348
|
+
/** JSDoc that marks a deliberate abstract/no-op member. */
|
|
349
|
+
function hasAbstractDoc(ctx, node) {
|
|
350
|
+
const start = node.startPosition.row + 1;
|
|
351
|
+
for (let line = Math.max(1, start - 8); line < start; line += 1) {
|
|
352
|
+
const text = ctx.lines[line - 1] ?? '';
|
|
353
|
+
if (/@abstract|@virtual|@noop/i.test(text))
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
function declaredName(node) {
|
|
359
|
+
const name = node.childForFieldName('name')?.text;
|
|
360
|
+
return name;
|
|
361
|
+
}
|
|
362
|
+
const stubFunction = {
|
|
363
|
+
meta: {
|
|
364
|
+
id: 'ts/stub-function',
|
|
365
|
+
title: 'Function body is a placeholder',
|
|
366
|
+
summary: 'Finds exported functions and class methods whose body is empty or only throws "not implemented". Common when a model writes the signature and forgets the implementation.',
|
|
367
|
+
severity: 'warning',
|
|
368
|
+
confidence: 'high',
|
|
369
|
+
languages: ['typescript', 'tsx'],
|
|
370
|
+
tags: ['stubs', 'completeness'],
|
|
371
|
+
},
|
|
372
|
+
run(ctx) {
|
|
373
|
+
if (ctx.language === 'python')
|
|
374
|
+
return [];
|
|
375
|
+
if (isTestPath(ctx.file))
|
|
376
|
+
return [];
|
|
377
|
+
const drafts = [];
|
|
378
|
+
for (const node of descendantsOfType(ctx.root, ['function_declaration', 'method_definition'])) {
|
|
379
|
+
if (ancestorOfType(node, ['ambient_declaration']))
|
|
380
|
+
continue;
|
|
381
|
+
if (node.children.some((child) => child.type === 'abstract'))
|
|
382
|
+
continue;
|
|
383
|
+
// Methods in object literals are often deliberate no-op dispatch entries.
|
|
384
|
+
if (node.parent?.type === 'object')
|
|
385
|
+
continue;
|
|
386
|
+
const name = declaredName(node);
|
|
387
|
+
if (name === 'constructor')
|
|
388
|
+
continue;
|
|
389
|
+
if (name !== undefined && NOOP_NAMES.test(name))
|
|
390
|
+
continue;
|
|
391
|
+
if (hasAbstractDoc(ctx, node))
|
|
392
|
+
continue;
|
|
393
|
+
const body = node.childForFieldName('body');
|
|
394
|
+
if (!body || body.type !== 'statement_block')
|
|
395
|
+
continue;
|
|
396
|
+
const form = stubForm(body);
|
|
397
|
+
if (!form)
|
|
398
|
+
continue;
|
|
399
|
+
drafts.push({
|
|
400
|
+
message: `\`${name ?? '(anonymous)'}\` has a placeholder body (${form})`,
|
|
401
|
+
start: startOf(node),
|
|
402
|
+
end: endOf(body),
|
|
403
|
+
snippet: snippetOf(ctx, node),
|
|
404
|
+
// An empty body is a weak signal in JavaScript: callbacks and dispatch
|
|
405
|
+
// tables legitimately do nothing. Only an explicit "not implemented"
|
|
406
|
+
// throw is evidence of an abandoned implementation.
|
|
407
|
+
confidence: form === 'empty body' ? 'medium' : 'high',
|
|
408
|
+
evidence: [`body: ${form}`],
|
|
409
|
+
help: 'Implement it, or throw an error with a message so callers fail loudly instead of silently doing nothing.',
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
for (const node of descendantsOfType(ctx.root, ['arrow_function'])) {
|
|
413
|
+
const body = node.childForFieldName('body');
|
|
414
|
+
if (!body || body.type !== 'statement_block')
|
|
415
|
+
continue;
|
|
416
|
+
const form = stubForm(body);
|
|
417
|
+
if (form !== 'throws "not implemented"')
|
|
418
|
+
continue;
|
|
419
|
+
drafts.push({
|
|
420
|
+
message: 'arrow function has a placeholder body (throws "not implemented")',
|
|
421
|
+
start: startOf(node),
|
|
422
|
+
end: endOf(body),
|
|
423
|
+
snippet: snippetOf(ctx, node),
|
|
424
|
+
evidence: ['body: throws "not implemented"'],
|
|
425
|
+
help: 'Implement it, or throw an error with a message so callers fail loudly.',
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
return drafts;
|
|
429
|
+
},
|
|
430
|
+
};
|
|
431
|
+
const swallowedException = {
|
|
432
|
+
meta: {
|
|
433
|
+
id: 'ts/swallowed-exception',
|
|
434
|
+
title: 'Exception is swallowed silently',
|
|
435
|
+
summary: 'Finds `catch {}` blocks that discard the error. Confidence is medium by default: empty catch is idiomatic in JavaScript for best-effort cleanup.',
|
|
436
|
+
severity: 'warning',
|
|
437
|
+
confidence: 'medium',
|
|
438
|
+
languages: ['typescript', 'tsx'],
|
|
439
|
+
tags: ['errors', 'reliability'],
|
|
440
|
+
},
|
|
441
|
+
run(ctx) {
|
|
442
|
+
if (ctx.language === 'python')
|
|
443
|
+
return [];
|
|
444
|
+
if (isTestPath(ctx.file))
|
|
445
|
+
return [];
|
|
446
|
+
const drafts = [];
|
|
447
|
+
for (const clause of descendantsOfType(ctx.root, ['catch_clause'])) {
|
|
448
|
+
const body = clause.namedChildren.filter((child) => child.type === 'statement_block').pop();
|
|
449
|
+
if (!body)
|
|
450
|
+
continue;
|
|
451
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
452
|
+
if (statements.length > 0)
|
|
453
|
+
continue;
|
|
454
|
+
const commented = hasCommentBetween(ctx, clause.startPosition.row + 1, body.endPosition.row + 1);
|
|
455
|
+
drafts.push({
|
|
456
|
+
message: commented ? '`catch` swallows the error (a comment explains why)' : '`catch` swallows the error silently',
|
|
457
|
+
start: startOf(clause),
|
|
458
|
+
end: endOf(body),
|
|
459
|
+
snippet: snippetOf(ctx, clause),
|
|
460
|
+
confidence: 'medium',
|
|
461
|
+
evidence: [
|
|
462
|
+
`handler: ${truncate(collapseWhitespace(clause.text.split(/\r?\n/)[0] ?? ''), 100)}`,
|
|
463
|
+
...(commented ? ['a comment explains the suppression'] : []),
|
|
464
|
+
],
|
|
465
|
+
help: 'Log it, rethrow it, or add a comment explaining why it is safe to ignore.',
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return drafts;
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
const deadBranch = {
|
|
472
|
+
meta: {
|
|
473
|
+
id: 'ts/dead-branch',
|
|
474
|
+
title: 'Constant condition',
|
|
475
|
+
summary: 'Finds `if (false)`, `while (false)` and `if (true)` branches that can never behave as written.',
|
|
476
|
+
severity: 'warning',
|
|
477
|
+
confidence: 'medium',
|
|
478
|
+
languages: ['typescript', 'tsx'],
|
|
479
|
+
tags: ['control-flow', 'dead-code'],
|
|
480
|
+
},
|
|
481
|
+
run(ctx) {
|
|
482
|
+
if (ctx.language === 'python')
|
|
483
|
+
return [];
|
|
484
|
+
if (isTestPath(ctx.file))
|
|
485
|
+
return [];
|
|
486
|
+
const drafts = [];
|
|
487
|
+
for (const node of descendantsOfType(ctx.root, ['if_statement', 'while_statement'])) {
|
|
488
|
+
const condition = node.childForFieldName('condition');
|
|
489
|
+
if (!condition)
|
|
490
|
+
continue;
|
|
491
|
+
let text = collapseWhitespace(condition.text);
|
|
492
|
+
while (text.startsWith('(') && text.endsWith(')')) {
|
|
493
|
+
text = text.slice(1, -1).trim();
|
|
494
|
+
}
|
|
495
|
+
const isFalse = text === 'false' || text === '0';
|
|
496
|
+
const isTrue = text === 'true' || text === '1';
|
|
497
|
+
if (!isFalse && !isTrue)
|
|
498
|
+
continue;
|
|
499
|
+
if (isTrue && node.type === 'while_statement')
|
|
500
|
+
continue;
|
|
501
|
+
drafts.push({
|
|
502
|
+
message: isFalse
|
|
503
|
+
? `\`${node.type === 'while_statement' ? 'while' : 'if'} (${text})\` is dead code`
|
|
504
|
+
: `\`if (${text})\` makes the else-branch unreachable`,
|
|
505
|
+
start: startOf(node),
|
|
506
|
+
end: endOf(condition),
|
|
507
|
+
snippet: snippetOf(ctx, node),
|
|
508
|
+
confidence: isFalse ? 'high' : 'medium',
|
|
509
|
+
evidence: [`condition: ${text}`],
|
|
510
|
+
help: 'Replace the constant with the flag or configuration value it is standing in for.',
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
return drafts;
|
|
514
|
+
},
|
|
515
|
+
};
|
|
516
|
+
const unreachable = {
|
|
517
|
+
meta: {
|
|
518
|
+
id: 'ts/unreachable',
|
|
519
|
+
title: 'Statement after an unconditional exit',
|
|
520
|
+
summary: 'Finds statements placed after `return`, `throw`, `break` or `continue` in the same block.',
|
|
521
|
+
severity: 'error',
|
|
522
|
+
confidence: 'high',
|
|
523
|
+
languages: ['typescript', 'tsx'],
|
|
524
|
+
tags: ['control-flow', 'dead-code'],
|
|
525
|
+
},
|
|
526
|
+
run(ctx) {
|
|
527
|
+
if (ctx.language === 'python')
|
|
528
|
+
return [];
|
|
529
|
+
if (isTestPath(ctx.file))
|
|
530
|
+
return [];
|
|
531
|
+
const drafts = [];
|
|
532
|
+
for (const block of descendantsOfType(ctx.root, ['statement_block'])) {
|
|
533
|
+
const statements = block.namedChildren.filter((child) => !isComment(child));
|
|
534
|
+
const index = statements.findIndex((statement) => TERMINATORS.has(statement.type));
|
|
535
|
+
if (index === -1)
|
|
536
|
+
continue;
|
|
537
|
+
const terminator = statements[index];
|
|
538
|
+
const firstUnreachable = statements[index + 1];
|
|
539
|
+
if (!terminator || !firstUnreachable)
|
|
540
|
+
continue;
|
|
541
|
+
const count = statements.length - index - 1;
|
|
542
|
+
const keyword = terminator.type.replace('_statement', '');
|
|
543
|
+
drafts.push({
|
|
544
|
+
message: `code after \`${keyword}\` is unreachable`,
|
|
545
|
+
start: startOf(firstUnreachable),
|
|
546
|
+
end: endOf(firstUnreachable),
|
|
547
|
+
snippet: snippetOf(ctx, firstUnreachable),
|
|
548
|
+
evidence: [`${count} statement(s) follow the \`${keyword}\` at line ${terminator.startPosition.row + 1}`],
|
|
549
|
+
help: 'Delete the unreachable code, or move it before the exit if it was meant to run.',
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
return drafts;
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
export const typescriptRules = [
|
|
556
|
+
phantomImport,
|
|
557
|
+
noopTest,
|
|
558
|
+
tautologicalAssert,
|
|
559
|
+
stubFunction,
|
|
560
|
+
swallowedException,
|
|
561
|
+
deadBranch,
|
|
562
|
+
unreachable,
|
|
563
|
+
];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Finding } from './types.ts';
|
|
2
|
+
export interface Suppressions {
|
|
3
|
+
fileDisabled: boolean;
|
|
4
|
+
/** 1-based line -> rule ids, or 'all'. */
|
|
5
|
+
lineRules: Map<number, Set<string> | 'all'>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @param commentLines 1-based lines that actually contain a comment node.
|
|
9
|
+
* When supplied, directives inside string literals or docs are ignored.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readSuppressions(source: string, commentLines?: ReadonlySet<number>): Suppressions;
|
|
12
|
+
export declare function isSuppressed(finding: Finding, suppressions: Suppressions): boolean;
|
package/dist/suppress.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const NEXT_LINE = /slopgate-ignore-next-line\b(.*)$/;
|
|
2
|
+
const SAME_LINE = /slopgate-ignore-line\b(.*)$/;
|
|
3
|
+
const FILE = /slopgate-disable-file\b/;
|
|
4
|
+
function ruleList(raw) {
|
|
5
|
+
const cleaned = raw.replace(/^[:\s]+/, '').replace(/[*/-]+\/?\s*$/, '').trim();
|
|
6
|
+
if (!cleaned || cleaned === '*' || cleaned === 'all')
|
|
7
|
+
return 'all';
|
|
8
|
+
const ids = cleaned
|
|
9
|
+
.split(/[,\s]+/)
|
|
10
|
+
.map((part) => part.trim())
|
|
11
|
+
.filter((part) => /^[a-z]{2}\/[a-z0-9-]+$/.test(part));
|
|
12
|
+
return ids.length > 0 ? new Set(ids) : 'all';
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @param commentLines 1-based lines that actually contain a comment node.
|
|
16
|
+
* When supplied, directives inside string literals or docs are ignored.
|
|
17
|
+
*/
|
|
18
|
+
export function readSuppressions(source, commentLines) {
|
|
19
|
+
const suppressions = { fileDisabled: false, lineRules: new Map() };
|
|
20
|
+
const lines = source.split(/\r?\n/);
|
|
21
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
22
|
+
const lineNumber = index + 1;
|
|
23
|
+
if (commentLines && !commentLines.has(lineNumber))
|
|
24
|
+
continue;
|
|
25
|
+
const line = lines[index] ?? '';
|
|
26
|
+
if (FILE.test(line)) {
|
|
27
|
+
suppressions.fileDisabled = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const next = NEXT_LINE.exec(line);
|
|
31
|
+
if (next) {
|
|
32
|
+
const target = index + 2;
|
|
33
|
+
suppressions.lineRules.set(target, ruleList(next[1] ?? ''));
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const same = SAME_LINE.exec(line);
|
|
37
|
+
if (same && !next) {
|
|
38
|
+
suppressions.lineRules.set(index + 1, ruleList(same[1] ?? ''));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return suppressions;
|
|
42
|
+
}
|
|
43
|
+
export function isSuppressed(finding, suppressions) {
|
|
44
|
+
if (suppressions.fileDisabled)
|
|
45
|
+
return true;
|
|
46
|
+
const rules = suppressions.lineRules.get(finding.start.line);
|
|
47
|
+
if (!rules)
|
|
48
|
+
return false;
|
|
49
|
+
if (rules === 'all')
|
|
50
|
+
return true;
|
|
51
|
+
return rules.has(finding.ruleId);
|
|
52
|
+
}
|