@voidh4x0r/slopgate 0.0.0-stage → 0.1.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/CHANGELOG.md +58 -0
- package/LICENSE +21 -0
- package/README.md +266 -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 +481 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +90 -0
- package/dist/data/python-stdlib.d.ts +19 -0
- package/dist/data/python-stdlib.js +75 -0
- package/dist/deps.d.ts +8 -0
- package/dist/deps.js +379 -0
- package/dist/diff.d.ts +30 -0
- package/dist/diff.js +179 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.js +179 -0
- package/dist/files.d.ts +32 -0
- package/dist/files.js +168 -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 +41 -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 +70 -0
- package/dist/rules/helpers.d.ts +43 -0
- package/dist/rules/helpers.js +163 -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 +591 -0
- package/dist/rules/typescript.d.ts +2 -0
- package/dist/rules/typescript.js +497 -0
- package/dist/suppress.d.ts +12 -0
- package/dist/suppress.js +52 -0
- package/dist/types.d.ts +88 -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,497 @@
|
|
|
1
|
+
import { builtinModules } from 'node:module';
|
|
2
|
+
import { ancestorOfType, calleeName, 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_CALLEE = /^(expect|assert\w*|assert|expectTypeOf|should|equal|deepEqual|throws|rejects|to[A-Z]\w*|pass)\b/;
|
|
69
|
+
function hasAssertion(body) {
|
|
70
|
+
let found = false;
|
|
71
|
+
walk(body, (node) => {
|
|
72
|
+
if (found)
|
|
73
|
+
return false;
|
|
74
|
+
if (node.type === 'call_expression') {
|
|
75
|
+
const name = calleeName(node) ?? '';
|
|
76
|
+
if (ASSERTION_CALLEE.test(name)) {
|
|
77
|
+
found = true;
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
});
|
|
83
|
+
return found;
|
|
84
|
+
}
|
|
85
|
+
const LITERAL = /^(true|false|null|undefined|-?\d[\d_]*(?:\.\d+)?|'[^']*'|"[^"]*"|`[^`]*`)$/;
|
|
86
|
+
const phantomImport = {
|
|
87
|
+
meta: {
|
|
88
|
+
id: 'ts/phantom-import',
|
|
89
|
+
title: 'Import is not a dependency',
|
|
90
|
+
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.',
|
|
91
|
+
severity: 'error',
|
|
92
|
+
confidence: 'high',
|
|
93
|
+
languages: ['typescript', 'tsx'],
|
|
94
|
+
tags: ['dependencies', 'hallucination'],
|
|
95
|
+
},
|
|
96
|
+
run(ctx) {
|
|
97
|
+
if (ctx.language === 'python')
|
|
98
|
+
return [];
|
|
99
|
+
const drafts = [];
|
|
100
|
+
if (!ctx.deps.node.hasPackageJson)
|
|
101
|
+
return drafts;
|
|
102
|
+
const { declared, installed, workspaces, aliases } = ctx.deps.node;
|
|
103
|
+
for (const { node, specifier } of moduleSpecifiers(ctx.root)) {
|
|
104
|
+
if (specifier.startsWith('.') || specifier.startsWith('/'))
|
|
105
|
+
continue;
|
|
106
|
+
if (NODE_BUILTINS.has(specifier))
|
|
107
|
+
continue;
|
|
108
|
+
if (specifier.startsWith('#'))
|
|
109
|
+
continue;
|
|
110
|
+
if (ASSET_SPECIFIER.test(specifier))
|
|
111
|
+
continue;
|
|
112
|
+
if (VIRTUAL_SPECIFIER.test(specifier))
|
|
113
|
+
continue;
|
|
114
|
+
if (matchesAlias(specifier, aliases))
|
|
115
|
+
continue;
|
|
116
|
+
// `try { require('optional-dep') } catch {}` is the optional-dependency idiom.
|
|
117
|
+
if (ancestorOfType(node, ['try_statement']))
|
|
118
|
+
continue;
|
|
119
|
+
const pkg = packageRootOf(specifier);
|
|
120
|
+
if (NODE_BUILTINS.has(pkg))
|
|
121
|
+
continue;
|
|
122
|
+
if (matchesAlias(pkg, aliases))
|
|
123
|
+
continue;
|
|
124
|
+
if (declared.has(pkg) || workspaces.has(pkg))
|
|
125
|
+
continue;
|
|
126
|
+
if (ctx.deps.ignoredModules.has(pkg))
|
|
127
|
+
continue;
|
|
128
|
+
const isInstalled = installed.has(pkg);
|
|
129
|
+
drafts.push({
|
|
130
|
+
message: isInstalled
|
|
131
|
+
? `\`${pkg}\` is imported but not listed in package.json`
|
|
132
|
+
: `\`${pkg}\` is imported but is not a dependency and is not installed`,
|
|
133
|
+
start: startOf(node),
|
|
134
|
+
end: endOf(node),
|
|
135
|
+
snippet: snippetOf(ctx, node),
|
|
136
|
+
confidence: isInstalled || isTestPath(ctx.file) ? 'medium' : 'high',
|
|
137
|
+
evidence: [
|
|
138
|
+
`specifier: ${specifier}`,
|
|
139
|
+
isInstalled ? `found in node_modules/${pkg}` : `not found in node_modules/${pkg}`,
|
|
140
|
+
'not listed in dependencies, devDependencies, peerDependencies or optionalDependencies',
|
|
141
|
+
],
|
|
142
|
+
help: isInstalled
|
|
143
|
+
? 'Add it to package.json so a fresh install reproduces your build.'
|
|
144
|
+
: 'A package that is not installed cannot be run. Check the name on npm: this is the signature of a hallucinated package.',
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return drafts;
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
const noopTest = {
|
|
151
|
+
meta: {
|
|
152
|
+
id: 'ts/noop-test',
|
|
153
|
+
title: 'Test does not assert anything',
|
|
154
|
+
summary: 'Finds `test()` / `it()` callbacks with an empty body or no assertion, so they pass regardless of the code.',
|
|
155
|
+
severity: 'warning',
|
|
156
|
+
confidence: 'high',
|
|
157
|
+
languages: ['typescript', 'tsx'],
|
|
158
|
+
tags: ['tests', 'theater'],
|
|
159
|
+
},
|
|
160
|
+
run(ctx) {
|
|
161
|
+
if (ctx.language === 'python')
|
|
162
|
+
return [];
|
|
163
|
+
const drafts = [];
|
|
164
|
+
for (const call of descendantsOfType(ctx.root, ['call_expression'])) {
|
|
165
|
+
const fn = call.childForFieldName('function');
|
|
166
|
+
if (!fn)
|
|
167
|
+
continue;
|
|
168
|
+
const head = collapseWhitespace(fn.text).split('.')[0]?.replace(/\(.*$/, '');
|
|
169
|
+
if (head !== 'test' && head !== 'it')
|
|
170
|
+
continue;
|
|
171
|
+
const args = call.childForFieldName('arguments');
|
|
172
|
+
if (!args)
|
|
173
|
+
continue;
|
|
174
|
+
const callback = [...args.namedChildren]
|
|
175
|
+
.reverse()
|
|
176
|
+
.find((child) => child.type === 'arrow_function' || child.type === 'function_expression' || child.type === 'function');
|
|
177
|
+
if (!callback)
|
|
178
|
+
continue;
|
|
179
|
+
const body = callback.childForFieldName('body');
|
|
180
|
+
if (!body)
|
|
181
|
+
continue;
|
|
182
|
+
const label = args.namedChildren[0]?.type === 'string' ? truncate(args.namedChildren[0].text, 60) : undefined;
|
|
183
|
+
if (body.type !== 'statement_block') {
|
|
184
|
+
if (!hasAssertion(body)) {
|
|
185
|
+
drafts.push({
|
|
186
|
+
message: 'test makes no assertions',
|
|
187
|
+
start: startOf(call),
|
|
188
|
+
end: endOf(body),
|
|
189
|
+
snippet: snippetOf(ctx, call),
|
|
190
|
+
evidence: [`test: ${label ?? '(anonymous)'}`, 'the test body only calls code without checking a result'],
|
|
191
|
+
help: 'Every test should be able to fail. Add an assertion that depends on the behaviour under test.',
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
197
|
+
if (statements.length === 0) {
|
|
198
|
+
drafts.push({
|
|
199
|
+
message: 'test has an empty body',
|
|
200
|
+
start: startOf(call),
|
|
201
|
+
end: endOf(body),
|
|
202
|
+
snippet: snippetOf(ctx, call),
|
|
203
|
+
evidence: [`test: ${label ?? '(anonymous)'}`],
|
|
204
|
+
help: 'Add an assertion, or delete the test.',
|
|
205
|
+
});
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!hasAssertion(body)) {
|
|
209
|
+
drafts.push({
|
|
210
|
+
message: 'test makes no assertions',
|
|
211
|
+
start: startOf(call),
|
|
212
|
+
end: endOf(body),
|
|
213
|
+
snippet: snippetOf(ctx, call),
|
|
214
|
+
evidence: [`test: ${label ?? '(anonymous)'}`, `statements: ${statements.length}`, 'no expect() / assert() found'],
|
|
215
|
+
help: 'Every test should be able to fail. Add an assertion that depends on the behaviour under test.',
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return drafts;
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
const tautologicalAssert = {
|
|
223
|
+
meta: {
|
|
224
|
+
id: 'ts/tautological-assert',
|
|
225
|
+
title: 'Assertion compares a value to itself',
|
|
226
|
+
summary: 'Finds `expect(true).toBe(true)`, `expect(x).toEqual(x)` and similar assertions that cannot fail.',
|
|
227
|
+
severity: 'warning',
|
|
228
|
+
confidence: 'high',
|
|
229
|
+
languages: ['typescript', 'tsx'],
|
|
230
|
+
tags: ['tests', 'theater'],
|
|
231
|
+
},
|
|
232
|
+
run(ctx) {
|
|
233
|
+
if (ctx.language === 'python')
|
|
234
|
+
return [];
|
|
235
|
+
const drafts = [];
|
|
236
|
+
for (const call of descendantsOfType(ctx.root, ['call_expression'])) {
|
|
237
|
+
const fn = call.childForFieldName('function');
|
|
238
|
+
if (!fn || fn.type !== 'member_expression')
|
|
239
|
+
continue;
|
|
240
|
+
const property = fn.childForFieldName('property')?.text;
|
|
241
|
+
if (!property)
|
|
242
|
+
continue;
|
|
243
|
+
const object = fn.childForFieldName('object');
|
|
244
|
+
const args = call.childForFieldName('arguments');
|
|
245
|
+
const actual = args?.namedChildren[0]?.text;
|
|
246
|
+
if (actual === undefined)
|
|
247
|
+
continue;
|
|
248
|
+
let expected;
|
|
249
|
+
if (object?.type === 'call_expression' && collapseWhitespace(object.childForFieldName('function')?.text ?? '') === 'expect') {
|
|
250
|
+
expected = object.childForFieldName('arguments')?.namedChildren[0]?.text;
|
|
251
|
+
}
|
|
252
|
+
const isSelf = expected !== undefined &&
|
|
253
|
+
expected === actual &&
|
|
254
|
+
(LITERAL.test(actual) || expected === actual) &&
|
|
255
|
+
['toBe', 'toEqual', 'toStrictEqual'].includes(property);
|
|
256
|
+
const isTruthyTrue = property === 'toBeTruthy' && expected === 'true';
|
|
257
|
+
if (!isSelf && !isTruthyTrue)
|
|
258
|
+
continue;
|
|
259
|
+
drafts.push({
|
|
260
|
+
message: `assertion compares a value to itself: \`${truncate(collapseWhitespace(call.text), 80)}\``,
|
|
261
|
+
start: startOf(call),
|
|
262
|
+
end: endOf(call),
|
|
263
|
+
snippet: snippetOf(ctx, call),
|
|
264
|
+
evidence: ['expected and actual expressions are identical'],
|
|
265
|
+
help: 'Assert on the behaviour under test: a return value, a thrown error, or a call.',
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
return drafts;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
function stubForm(body) {
|
|
272
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
273
|
+
if (statements.length === 0)
|
|
274
|
+
return 'empty body';
|
|
275
|
+
if (statements.length === 1 && statements[0]?.type === 'throw_statement') {
|
|
276
|
+
if (/not\s*implemented|notimplemented|todo/i.test(statements[0].text))
|
|
277
|
+
return 'throws "not implemented"';
|
|
278
|
+
}
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
const NOOP_NAMES = /^(noop|no_?op|nop|ignore|identity|pass|doNothing|noopFn|noOpFn)$/i;
|
|
282
|
+
/** JSDoc that marks a deliberate abstract/no-op member. */
|
|
283
|
+
function hasAbstractDoc(ctx, node) {
|
|
284
|
+
const start = node.startPosition.row + 1;
|
|
285
|
+
for (let line = Math.max(1, start - 8); line < start; line += 1) {
|
|
286
|
+
const text = ctx.lines[line - 1] ?? '';
|
|
287
|
+
if (/@abstract|@virtual|@noop/i.test(text))
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
function declaredName(node) {
|
|
293
|
+
const name = node.childForFieldName('name')?.text;
|
|
294
|
+
return name;
|
|
295
|
+
}
|
|
296
|
+
const stubFunction = {
|
|
297
|
+
meta: {
|
|
298
|
+
id: 'ts/stub-function',
|
|
299
|
+
title: 'Function body is a placeholder',
|
|
300
|
+
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.',
|
|
301
|
+
severity: 'warning',
|
|
302
|
+
confidence: 'high',
|
|
303
|
+
languages: ['typescript', 'tsx'],
|
|
304
|
+
tags: ['stubs', 'completeness'],
|
|
305
|
+
},
|
|
306
|
+
run(ctx) {
|
|
307
|
+
if (ctx.language === 'python')
|
|
308
|
+
return [];
|
|
309
|
+
if (isTestPath(ctx.file))
|
|
310
|
+
return [];
|
|
311
|
+
const drafts = [];
|
|
312
|
+
for (const node of descendantsOfType(ctx.root, ['function_declaration', 'method_definition'])) {
|
|
313
|
+
if (ancestorOfType(node, ['ambient_declaration']))
|
|
314
|
+
continue;
|
|
315
|
+
if (node.children.some((child) => child.type === 'abstract'))
|
|
316
|
+
continue;
|
|
317
|
+
// Methods in object literals are often deliberate no-op dispatch entries.
|
|
318
|
+
if (node.parent?.type === 'object')
|
|
319
|
+
continue;
|
|
320
|
+
const name = declaredName(node);
|
|
321
|
+
if (name === 'constructor')
|
|
322
|
+
continue;
|
|
323
|
+
if (name !== undefined && NOOP_NAMES.test(name))
|
|
324
|
+
continue;
|
|
325
|
+
if (hasAbstractDoc(ctx, node))
|
|
326
|
+
continue;
|
|
327
|
+
const body = node.childForFieldName('body');
|
|
328
|
+
if (!body || body.type !== 'statement_block')
|
|
329
|
+
continue;
|
|
330
|
+
const form = stubForm(body);
|
|
331
|
+
if (!form)
|
|
332
|
+
continue;
|
|
333
|
+
drafts.push({
|
|
334
|
+
message: `\`${name ?? '(anonymous)'}\` has a placeholder body (${form})`,
|
|
335
|
+
start: startOf(node),
|
|
336
|
+
end: endOf(body),
|
|
337
|
+
snippet: snippetOf(ctx, node),
|
|
338
|
+
// An empty body is a weak signal in JavaScript: callbacks and dispatch
|
|
339
|
+
// tables legitimately do nothing. Only an explicit "not implemented"
|
|
340
|
+
// throw is evidence of an abandoned implementation.
|
|
341
|
+
confidence: form === 'empty body' ? 'medium' : 'high',
|
|
342
|
+
evidence: [`body: ${form}`],
|
|
343
|
+
help: 'Implement it, or throw an error with a message so callers fail loudly instead of silently doing nothing.',
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
for (const node of descendantsOfType(ctx.root, ['arrow_function'])) {
|
|
347
|
+
const body = node.childForFieldName('body');
|
|
348
|
+
if (!body || body.type !== 'statement_block')
|
|
349
|
+
continue;
|
|
350
|
+
const form = stubForm(body);
|
|
351
|
+
if (form !== 'throws "not implemented"')
|
|
352
|
+
continue;
|
|
353
|
+
drafts.push({
|
|
354
|
+
message: 'arrow function has a placeholder body (throws "not implemented")',
|
|
355
|
+
start: startOf(node),
|
|
356
|
+
end: endOf(body),
|
|
357
|
+
snippet: snippetOf(ctx, node),
|
|
358
|
+
evidence: ['body: throws "not implemented"'],
|
|
359
|
+
help: 'Implement it, or throw an error with a message so callers fail loudly.',
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return drafts;
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
const swallowedException = {
|
|
366
|
+
meta: {
|
|
367
|
+
id: 'ts/swallowed-exception',
|
|
368
|
+
title: 'Exception is swallowed silently',
|
|
369
|
+
summary: 'Finds `catch {}` blocks that discard the error. Confidence is medium by default: empty catch is idiomatic in JavaScript for best-effort cleanup.',
|
|
370
|
+
severity: 'warning',
|
|
371
|
+
confidence: 'medium',
|
|
372
|
+
languages: ['typescript', 'tsx'],
|
|
373
|
+
tags: ['errors', 'reliability'],
|
|
374
|
+
},
|
|
375
|
+
run(ctx) {
|
|
376
|
+
if (ctx.language === 'python')
|
|
377
|
+
return [];
|
|
378
|
+
if (isTestPath(ctx.file))
|
|
379
|
+
return [];
|
|
380
|
+
const drafts = [];
|
|
381
|
+
for (const clause of descendantsOfType(ctx.root, ['catch_clause'])) {
|
|
382
|
+
const body = clause.namedChildren.filter((child) => child.type === 'statement_block').pop();
|
|
383
|
+
if (!body)
|
|
384
|
+
continue;
|
|
385
|
+
const statements = body.namedChildren.filter((child) => !isComment(child));
|
|
386
|
+
if (statements.length > 0)
|
|
387
|
+
continue;
|
|
388
|
+
const commented = hasCommentBetween(ctx, clause.startPosition.row + 1, body.endPosition.row + 1);
|
|
389
|
+
drafts.push({
|
|
390
|
+
message: commented ? '`catch` swallows the error (a comment explains why)' : '`catch` swallows the error silently',
|
|
391
|
+
start: startOf(clause),
|
|
392
|
+
end: endOf(body),
|
|
393
|
+
snippet: snippetOf(ctx, clause),
|
|
394
|
+
confidence: 'medium',
|
|
395
|
+
evidence: [
|
|
396
|
+
`handler: ${truncate(collapseWhitespace(clause.text.split(/\r?\n/)[0] ?? ''), 100)}`,
|
|
397
|
+
...(commented ? ['a comment explains the suppression'] : []),
|
|
398
|
+
],
|
|
399
|
+
help: 'Log it, rethrow it, or add a comment explaining why it is safe to ignore.',
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
return drafts;
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
const deadBranch = {
|
|
406
|
+
meta: {
|
|
407
|
+
id: 'ts/dead-branch',
|
|
408
|
+
title: 'Constant condition',
|
|
409
|
+
summary: 'Finds `if (false)`, `while (false)` and `if (true)` branches that can never behave as written.',
|
|
410
|
+
severity: 'warning',
|
|
411
|
+
confidence: 'medium',
|
|
412
|
+
languages: ['typescript', 'tsx'],
|
|
413
|
+
tags: ['control-flow', 'dead-code'],
|
|
414
|
+
},
|
|
415
|
+
run(ctx) {
|
|
416
|
+
if (ctx.language === 'python')
|
|
417
|
+
return [];
|
|
418
|
+
if (isTestPath(ctx.file))
|
|
419
|
+
return [];
|
|
420
|
+
const drafts = [];
|
|
421
|
+
for (const node of descendantsOfType(ctx.root, ['if_statement', 'while_statement'])) {
|
|
422
|
+
const condition = node.childForFieldName('condition');
|
|
423
|
+
if (!condition)
|
|
424
|
+
continue;
|
|
425
|
+
let text = collapseWhitespace(condition.text);
|
|
426
|
+
while (text.startsWith('(') && text.endsWith(')')) {
|
|
427
|
+
text = text.slice(1, -1).trim();
|
|
428
|
+
}
|
|
429
|
+
const isFalse = text === 'false' || text === '0';
|
|
430
|
+
const isTrue = text === 'true' || text === '1';
|
|
431
|
+
if (!isFalse && !isTrue)
|
|
432
|
+
continue;
|
|
433
|
+
if (isTrue && node.type === 'while_statement')
|
|
434
|
+
continue;
|
|
435
|
+
drafts.push({
|
|
436
|
+
message: isFalse
|
|
437
|
+
? `\`${node.type === 'while_statement' ? 'while' : 'if'} (${text})\` is dead code`
|
|
438
|
+
: `\`if (${text})\` makes the else-branch unreachable`,
|
|
439
|
+
start: startOf(node),
|
|
440
|
+
end: endOf(condition),
|
|
441
|
+
snippet: snippetOf(ctx, node),
|
|
442
|
+
confidence: isFalse ? 'high' : 'medium',
|
|
443
|
+
evidence: [`condition: ${text}`],
|
|
444
|
+
help: 'Replace the constant with the flag or configuration value it is standing in for.',
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return drafts;
|
|
448
|
+
},
|
|
449
|
+
};
|
|
450
|
+
const unreachable = {
|
|
451
|
+
meta: {
|
|
452
|
+
id: 'ts/unreachable',
|
|
453
|
+
title: 'Statement after an unconditional exit',
|
|
454
|
+
summary: 'Finds statements placed after `return`, `throw`, `break` or `continue` in the same block.',
|
|
455
|
+
severity: 'error',
|
|
456
|
+
confidence: 'high',
|
|
457
|
+
languages: ['typescript', 'tsx'],
|
|
458
|
+
tags: ['control-flow', 'dead-code'],
|
|
459
|
+
},
|
|
460
|
+
run(ctx) {
|
|
461
|
+
if (ctx.language === 'python')
|
|
462
|
+
return [];
|
|
463
|
+
if (isTestPath(ctx.file))
|
|
464
|
+
return [];
|
|
465
|
+
const drafts = [];
|
|
466
|
+
for (const block of descendantsOfType(ctx.root, ['statement_block'])) {
|
|
467
|
+
const statements = block.namedChildren.filter((child) => !isComment(child));
|
|
468
|
+
const index = statements.findIndex((statement) => TERMINATORS.has(statement.type));
|
|
469
|
+
if (index === -1)
|
|
470
|
+
continue;
|
|
471
|
+
const terminator = statements[index];
|
|
472
|
+
const firstUnreachable = statements[index + 1];
|
|
473
|
+
if (!terminator || !firstUnreachable)
|
|
474
|
+
continue;
|
|
475
|
+
const count = statements.length - index - 1;
|
|
476
|
+
const keyword = terminator.type.replace('_statement', '');
|
|
477
|
+
drafts.push({
|
|
478
|
+
message: `code after \`${keyword}\` is unreachable`,
|
|
479
|
+
start: startOf(firstUnreachable),
|
|
480
|
+
end: endOf(firstUnreachable),
|
|
481
|
+
snippet: snippetOf(ctx, firstUnreachable),
|
|
482
|
+
evidence: [`${count} statement(s) follow the \`${keyword}\` at line ${terminator.startPosition.row + 1}`],
|
|
483
|
+
help: 'Delete the unreachable code, or move it before the exit if it was meant to run.',
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
return drafts;
|
|
487
|
+
},
|
|
488
|
+
};
|
|
489
|
+
export const typescriptRules = [
|
|
490
|
+
phantomImport,
|
|
491
|
+
noopTest,
|
|
492
|
+
tautologicalAssert,
|
|
493
|
+
stubFunction,
|
|
494
|
+
swallowedException,
|
|
495
|
+
deadBranch,
|
|
496
|
+
unreachable,
|
|
497
|
+
];
|
|
@@ -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
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { AstNode } from './ast.ts';
|
|
2
|
+
/** Tree-sitter grammars we ship. */
|
|
3
|
+
export type LanguageId = 'python' | 'typescript' | 'tsx';
|
|
4
|
+
/** How sure we are that a finding is worth a human's time. */
|
|
5
|
+
export type Confidence = 'high' | 'medium' | 'low';
|
|
6
|
+
export type Severity = 'error' | 'warning' | 'info';
|
|
7
|
+
/** 1-based line, 1-based column. */
|
|
8
|
+
export interface Position {
|
|
9
|
+
line: number;
|
|
10
|
+
column: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Finding {
|
|
13
|
+
ruleId: string;
|
|
14
|
+
message: string;
|
|
15
|
+
severity: Severity;
|
|
16
|
+
confidence: Confidence;
|
|
17
|
+
/** Repository-relative path, always with forward slashes. */
|
|
18
|
+
file: string;
|
|
19
|
+
start: Position;
|
|
20
|
+
end: Position;
|
|
21
|
+
/** Single-line excerpt of the offending source, trimmed and capped. */
|
|
22
|
+
snippet: string;
|
|
23
|
+
/** Facts a reviewer can check for themselves. */
|
|
24
|
+
evidence: string[];
|
|
25
|
+
help?: string;
|
|
26
|
+
}
|
|
27
|
+
/** What a rule returns; the engine fills in rule id, file and defaults. */
|
|
28
|
+
export interface RuleDraft {
|
|
29
|
+
message: string;
|
|
30
|
+
start: Position;
|
|
31
|
+
end: Position;
|
|
32
|
+
snippet?: string;
|
|
33
|
+
evidence?: string[];
|
|
34
|
+
help?: string;
|
|
35
|
+
confidence?: Confidence;
|
|
36
|
+
severity?: Severity;
|
|
37
|
+
}
|
|
38
|
+
export interface RuleMeta {
|
|
39
|
+
/** Namespaced id, e.g. `py/noop-test`. */
|
|
40
|
+
id: string;
|
|
41
|
+
title: string;
|
|
42
|
+
summary: string;
|
|
43
|
+
severity: Severity;
|
|
44
|
+
confidence: Confidence;
|
|
45
|
+
languages: LanguageId[];
|
|
46
|
+
tags: string[];
|
|
47
|
+
}
|
|
48
|
+
export interface PythonDeps {
|
|
49
|
+
declared: Set<string>;
|
|
50
|
+
manifests: string[];
|
|
51
|
+
/** Top-level module names that exist inside the repository. */
|
|
52
|
+
localModules: Set<string>;
|
|
53
|
+
}
|
|
54
|
+
export interface NodeDeps {
|
|
55
|
+
declared: Set<string>;
|
|
56
|
+
workspaces: Set<string>;
|
|
57
|
+
/** Import aliases declared in tsconfig `compilerOptions.paths`. */
|
|
58
|
+
aliases: string[];
|
|
59
|
+
hasPackageJson: boolean;
|
|
60
|
+
/** Resolvable packages discovered in node_modules (top level and scoped). */
|
|
61
|
+
installed: Set<string>;
|
|
62
|
+
}
|
|
63
|
+
export interface ProjectDeps {
|
|
64
|
+
root: string;
|
|
65
|
+
python: PythonDeps;
|
|
66
|
+
node: NodeDeps;
|
|
67
|
+
/** Module/package names the user told us to trust (`ignoreModules`). */
|
|
68
|
+
ignoredModules: Set<string>;
|
|
69
|
+
}
|
|
70
|
+
export interface RuleContext {
|
|
71
|
+
/** Repository-relative POSIX path. */
|
|
72
|
+
file: string;
|
|
73
|
+
lines: string[];
|
|
74
|
+
root: AstNode;
|
|
75
|
+
language: LanguageId;
|
|
76
|
+
deps: ProjectDeps;
|
|
77
|
+
}
|
|
78
|
+
export interface Rule {
|
|
79
|
+
meta: RuleMeta;
|
|
80
|
+
run(ctx: RuleContext): RuleDraft[];
|
|
81
|
+
}
|
|
82
|
+
export interface ScanStats {
|
|
83
|
+
files: number;
|
|
84
|
+
byLanguage: Record<LanguageId, number>;
|
|
85
|
+
skipped: number;
|
|
86
|
+
parseErrors: number;
|
|
87
|
+
durationMs: number;
|
|
88
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Bundled grammars
|
|
2
|
+
|
|
3
|
+
These `.wasm` files are tree-sitter parsers compiled for the [web-tree-sitter](https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web) runtime, vendored so that `slopgate` works offline and without a build step.
|
|
4
|
+
|
|
5
|
+
| File | Upstream | License |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| `tree-sitter-python.wasm` | [tree-sitter/tree-sitter-python](https://github.com/tree-sitter/tree-sitter-python) | MIT |
|
|
8
|
+
| `tree-sitter-typescript.wasm` | [tree-sitter/tree-sitter-typescript](https://github.com/tree-sitter/tree-sitter-typescript) | MIT |
|
|
9
|
+
| `tree-sitter-tsx.wasm` | [tree-sitter/tree-sitter-typescript](https://github.com/tree-sitter/tree-sitter-typescript) | MIT |
|
|
10
|
+
|
|
11
|
+
(Tree-sitter itself is MIT licensed; the grammars carry their own MIT licenses, reproduced by their upstream repositories.)
|
|
12
|
+
|
|
13
|
+
We use the prebuilt artifacts from [`tree-sitter-wasms`](https://www.npmjs.com/package/tree-sitter-wasms). The TypeScript grammar is also used for plain JavaScript, and the TSX grammar for `.jsx` / `.tsx`.
|
|
14
|
+
|
|
15
|
+
## Compatibility
|
|
16
|
+
|
|
17
|
+
`web-tree-sitter` is pinned to `0.25.x` in `package.json`. Newer runtimes (0.26+) load grammars built with a different dynamic-linking convention and currently reject these files — that is why the dependency is pinned rather than ranged. If you upgrade the runtime, re-verify grammar loading before publishing.
|
|
18
|
+
|
|
19
|
+
## Updating a grammar
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm pack tree-sitter-wasms
|
|
23
|
+
tar -xzf tree-sitter-wasms-*.tgz
|
|
24
|
+
cp package/out/tree-sitter-python.wasm grammars/
|
|
25
|
+
cp package/out/tree-sitter-typescript.wasm grammars/
|
|
26
|
+
cp package/out/tree-sitter-tsx.wasm grammars/
|
|
27
|
+
npm test
|
|
28
|
+
```
|
|
Binary file
|
|
Binary file
|