@voidh4x0r/slopgate 0.1.0 → 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 +25 -1
- package/README.md +62 -24
- package/dist/cli.js +26 -20
- package/dist/config.js +2 -1
- package/dist/data/python-stdlib.d.ts +5 -3
- package/dist/data/python-stdlib.js +22 -5
- package/dist/deps.js +149 -37
- package/dist/diff.js +46 -8
- package/dist/engine.js +9 -19
- package/dist/files.d.ts +2 -1
- package/dist/files.js +63 -17
- package/dist/parser.js +4 -0
- package/dist/reporters/sarif.js +6 -1
- package/dist/rules/helpers.d.ts +18 -8
- package/dist/rules/helpers.js +37 -22
- package/dist/rules/python.js +61 -20
- package/dist/rules/typescript.js +76 -10
- package/dist/types.d.ts +7 -0
- package/package.json +1 -1
package/dist/rules/typescript.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { builtinModules } from 'node:module';
|
|
2
|
-
import { ancestorOfType,
|
|
2
|
+
import { ancestorOfType, collapseWhitespace, descendantsOfType, endOf, hasCommentBetween, isComment, isTestPath, snippetOf, startOf, truncate, walk, } from "./helpers.js";
|
|
3
3
|
const NODE_BUILTINS = new Set(builtinModules.flatMap((name) => [name, `node:${name}`]));
|
|
4
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
5
|
const VIRTUAL_SPECIFIER = /^(virtual|vite|webpack|bun|deno|astro|parcel|rollup|pnp|data|file|https?|blob):/;
|
|
@@ -65,15 +65,59 @@ function matchesAlias(specifier, aliases) {
|
|
|
65
65
|
return specifier === clean || specifier.startsWith(`${clean}/`);
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
|
-
const
|
|
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
|
+
}
|
|
69
112
|
function hasAssertion(body) {
|
|
70
113
|
let found = false;
|
|
71
114
|
walk(body, (node) => {
|
|
72
115
|
if (found)
|
|
73
116
|
return false;
|
|
74
117
|
if (node.type === 'call_expression') {
|
|
75
|
-
const
|
|
76
|
-
|
|
118
|
+
const fn = node.childForFieldName('function') ?? node.namedChildren[0];
|
|
119
|
+
const text = collapseWhitespace(fn?.text ?? '');
|
|
120
|
+
if (isAssertionCallee(text)) {
|
|
77
121
|
found = true;
|
|
78
122
|
return false;
|
|
79
123
|
}
|
|
@@ -82,7 +126,6 @@ function hasAssertion(body) {
|
|
|
82
126
|
});
|
|
83
127
|
return found;
|
|
84
128
|
}
|
|
85
|
-
const LITERAL = /^(true|false|null|undefined|-?\d[\d_]*(?:\.\d+)?|'[^']*'|"[^"]*"|`[^`]*`)$/;
|
|
86
129
|
const phantomImport = {
|
|
87
130
|
meta: {
|
|
88
131
|
id: 'ts/phantom-import',
|
|
@@ -147,6 +190,33 @@ const phantomImport = {
|
|
|
147
190
|
return drafts;
|
|
148
191
|
},
|
|
149
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
|
+
}
|
|
150
220
|
const noopTest = {
|
|
151
221
|
meta: {
|
|
152
222
|
id: 'ts/noop-test',
|
|
@@ -163,10 +233,7 @@ const noopTest = {
|
|
|
163
233
|
const drafts = [];
|
|
164
234
|
for (const call of descendantsOfType(ctx.root, ['call_expression'])) {
|
|
165
235
|
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')
|
|
236
|
+
if (!fn || !isTestCall(fn))
|
|
170
237
|
continue;
|
|
171
238
|
const args = call.childForFieldName('arguments');
|
|
172
239
|
if (!args)
|
|
@@ -251,7 +318,6 @@ const tautologicalAssert = {
|
|
|
251
318
|
}
|
|
252
319
|
const isSelf = expected !== undefined &&
|
|
253
320
|
expected === actual &&
|
|
254
|
-
(LITERAL.test(actual) || expected === actual) &&
|
|
255
321
|
['toBe', 'toEqual', 'toStrictEqual'].includes(property);
|
|
256
322
|
const isTruthyTrue = property === 'toBeTruthy' && expected === 'true';
|
|
257
323
|
if (!isSelf && !isTruthyTrue)
|
package/dist/types.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ export interface Position {
|
|
|
9
9
|
line: number;
|
|
10
10
|
column: number;
|
|
11
11
|
}
|
|
12
|
+
/** A comment node's line span, used for "a comment explains this" decisions. */
|
|
13
|
+
export interface CommentRange {
|
|
14
|
+
startLine: number;
|
|
15
|
+
endLine: number;
|
|
16
|
+
}
|
|
12
17
|
export interface Finding {
|
|
13
18
|
ruleId: string;
|
|
14
19
|
message: string;
|
|
@@ -74,6 +79,8 @@ export interface RuleContext {
|
|
|
74
79
|
root: AstNode;
|
|
75
80
|
language: LanguageId;
|
|
76
81
|
deps: ProjectDeps;
|
|
82
|
+
/** Real comment nodes in this file; strings and regexes are not comments. */
|
|
83
|
+
comments: readonly CommentRange[];
|
|
77
84
|
}
|
|
78
85
|
export interface Rule {
|
|
79
86
|
meta: RuleMeta;
|