@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
package/dist/diff.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
const exec = promisify(execFile);
|
|
5
|
+
const DIFF_HEADER = /^diff --git (.+)$/;
|
|
6
|
+
const NEW_FILE = /^\+\+\+ (.+)$/;
|
|
7
|
+
const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
8
|
+
/**
|
|
9
|
+
* Git quotes paths that contain spaces, quotes or non-ASCII bytes, and appends
|
|
10
|
+
* a tab when the path contains a space. `core.quotePath=false` avoids the octal
|
|
11
|
+
* escaping, but quote/backslash escaping still happens, so normalise both.
|
|
12
|
+
*/
|
|
13
|
+
export function normalizeGitPath(raw) {
|
|
14
|
+
let value = raw;
|
|
15
|
+
const tab = value.indexOf('\t');
|
|
16
|
+
if (tab !== -1)
|
|
17
|
+
value = value.slice(0, tab);
|
|
18
|
+
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
|
|
19
|
+
const inner = value.slice(1, -1);
|
|
20
|
+
const bytes = [];
|
|
21
|
+
for (let index = 0; index < inner.length; index += 1) {
|
|
22
|
+
const ch = inner[index] ?? '';
|
|
23
|
+
if (ch !== '\\') {
|
|
24
|
+
bytes.push(...Buffer.from(ch, 'utf8'));
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const next = inner[index + 1];
|
|
28
|
+
index += 1;
|
|
29
|
+
switch (next) {
|
|
30
|
+
case 'n':
|
|
31
|
+
bytes.push(0x0a);
|
|
32
|
+
break;
|
|
33
|
+
case 'r':
|
|
34
|
+
bytes.push(0x0d);
|
|
35
|
+
break;
|
|
36
|
+
case 't':
|
|
37
|
+
bytes.push(0x09);
|
|
38
|
+
break;
|
|
39
|
+
case '"':
|
|
40
|
+
case '\\':
|
|
41
|
+
bytes.push(next.charCodeAt(0));
|
|
42
|
+
break;
|
|
43
|
+
case undefined:
|
|
44
|
+
break;
|
|
45
|
+
default:
|
|
46
|
+
if (/[0-7]/.test(next)) {
|
|
47
|
+
let octal = next;
|
|
48
|
+
while (octal.length < 3 && /[0-7]/.test(inner[index + 1] ?? '')) {
|
|
49
|
+
index += 1;
|
|
50
|
+
octal += inner[index];
|
|
51
|
+
}
|
|
52
|
+
bytes.push(Number.parseInt(octal, 8));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
bytes.push(next.charCodeAt(0));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
value = Buffer.from(bytes).toString('utf8');
|
|
60
|
+
}
|
|
61
|
+
if (value.startsWith('b/'))
|
|
62
|
+
value = value.slice(2);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
async function git(root, args) {
|
|
66
|
+
try {
|
|
67
|
+
const { stdout } = await exec('git', ['-c', 'core.quotePath=false', ...args], {
|
|
68
|
+
cwd: root,
|
|
69
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
70
|
+
});
|
|
71
|
+
return stdout;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
const message = error.stderr?.trim() || error.message;
|
|
75
|
+
throw new Error(`git ${args.join(' ')} failed: ${message}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* `git diff` parses options anywhere before `--`, so a ref beginning with `-`
|
|
80
|
+
* (`--diff=--output=file`) would be interpreted as a git flag. Reject it.
|
|
81
|
+
*/
|
|
82
|
+
export function assertSafeRef(ref) {
|
|
83
|
+
if (ref.length === 0)
|
|
84
|
+
throw new Error('empty git ref');
|
|
85
|
+
if (ref.startsWith('-'))
|
|
86
|
+
throw new Error(`invalid git ref: ${ref}`);
|
|
87
|
+
return ref;
|
|
88
|
+
}
|
|
89
|
+
export function parseUnifiedDiff(stdout) {
|
|
90
|
+
const set = { lines: new Map(), full: new Set() };
|
|
91
|
+
let currentFile;
|
|
92
|
+
let previousWasDevNull = false;
|
|
93
|
+
/** Content lines left in the current hunk, tracked per side. */
|
|
94
|
+
let oldRemaining = 0;
|
|
95
|
+
let newRemaining = 0;
|
|
96
|
+
/** Next line number in the new file. */
|
|
97
|
+
let newLine = 0;
|
|
98
|
+
let store;
|
|
99
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
100
|
+
// Inside a hunk every line is content, even one that looks like a header:
|
|
101
|
+
// JavaScript `++ x;` produces a `+++ x;` line. Consume by hunk counts.
|
|
102
|
+
if (oldRemaining > 0 || newRemaining > 0) {
|
|
103
|
+
if (line.startsWith('\\'))
|
|
104
|
+
continue; // ""
|
|
105
|
+
if (line.startsWith('+')) {
|
|
106
|
+
if (newRemaining > 0) {
|
|
107
|
+
store?.add(newLine);
|
|
108
|
+
newLine += 1;
|
|
109
|
+
newRemaining -= 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (line.startsWith('-')) {
|
|
113
|
+
if (oldRemaining > 0)
|
|
114
|
+
oldRemaining -= 1;
|
|
115
|
+
}
|
|
116
|
+
else if (line.startsWith(' ')) {
|
|
117
|
+
if (oldRemaining > 0)
|
|
118
|
+
oldRemaining -= 1;
|
|
119
|
+
if (newRemaining > 0) {
|
|
120
|
+
newLine += 1;
|
|
121
|
+
newRemaining -= 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// Malformed hunk: consume defensively rather than parsing it as a header.
|
|
126
|
+
if (oldRemaining > 0)
|
|
127
|
+
oldRemaining -= 1;
|
|
128
|
+
if (newRemaining > 0)
|
|
129
|
+
newRemaining -= 1;
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (line.startsWith('--- ')) {
|
|
134
|
+
previousWasDevNull = line.trim() === '--- /dev/null';
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const newFile = NEW_FILE.exec(line);
|
|
138
|
+
if (newFile?.[1] && newFile[1] !== '/dev/null') {
|
|
139
|
+
currentFile = normalizeGitPath(newFile[1]);
|
|
140
|
+
if (previousWasDevNull)
|
|
141
|
+
set.full.add(currentFile);
|
|
142
|
+
previousWasDevNull = false;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
previousWasDevNull = false;
|
|
146
|
+
const header = DIFF_HEADER.exec(line);
|
|
147
|
+
if (header?.[1] && currentFile === undefined) {
|
|
148
|
+
// Fallback for binary diffs, which have no `+++` line.
|
|
149
|
+
const match = /^"?a\/(.+?)"? "?b\/(.+?)"?$/.exec(header[1]);
|
|
150
|
+
if (match?.[2])
|
|
151
|
+
currentFile = normalizeGitPath(match[2]);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (!currentFile)
|
|
155
|
+
continue;
|
|
156
|
+
const hunk = HUNK.exec(line);
|
|
157
|
+
if (!hunk?.[1] || !hunk[3])
|
|
158
|
+
continue;
|
|
159
|
+
oldRemaining = hunk[2] === undefined ? 1 : Number.parseInt(hunk[2], 10);
|
|
160
|
+
newRemaining = hunk[4] === undefined ? 1 : Number.parseInt(hunk[4], 10);
|
|
161
|
+
newLine = Number.parseInt(hunk[3], 10);
|
|
162
|
+
if (!Number.isFinite(newLine))
|
|
163
|
+
continue;
|
|
164
|
+
store = set.lines.get(currentFile);
|
|
165
|
+
if (!store) {
|
|
166
|
+
store = new Set();
|
|
167
|
+
set.lines.set(currentFile, store);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return set;
|
|
171
|
+
}
|
|
172
|
+
/** Lines that differ from `ref`, plus every line of untracked files. */
|
|
173
|
+
export async function diffSince(root, ref) {
|
|
174
|
+
const safeRef = assertSafeRef(ref);
|
|
175
|
+
const stdout = await git(root, ['diff', '--unified=0', '--no-color', '--no-ext-diff', safeRef, '--']);
|
|
176
|
+
const set = parseUnifiedDiff(stdout);
|
|
177
|
+
// `-z` gives NUL-separated raw paths, immune to quoting and newlines in names.
|
|
178
|
+
const untracked = await git(root, ['ls-files', '--others', '--exclude-standard', '-z']).catch(() => '');
|
|
179
|
+
for (const file of untracked.split('\u0000')) {
|
|
180
|
+
if (file.length > 0)
|
|
181
|
+
set.full.add(file);
|
|
182
|
+
}
|
|
183
|
+
return set;
|
|
184
|
+
}
|
|
185
|
+
export function isInDiff(finding, set) {
|
|
186
|
+
if (set.full.has(finding.file))
|
|
187
|
+
return true;
|
|
188
|
+
const lines = set.lines.get(finding.file);
|
|
189
|
+
if (!lines)
|
|
190
|
+
return false;
|
|
191
|
+
for (let line = finding.start.line; line <= Math.max(finding.start.line, finding.end.line); line += 1) {
|
|
192
|
+
if (lines.has(line))
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Findings carry paths relative to the scan target. When the target is a
|
|
199
|
+
* subdirectory (or a single file) of the repository, prepend the relative
|
|
200
|
+
* prefix before comparing against git's repository-root paths.
|
|
201
|
+
*/
|
|
202
|
+
export function isInDiffWithPrefix(finding, set, prefix) {
|
|
203
|
+
if (isInDiff(finding, set))
|
|
204
|
+
return true;
|
|
205
|
+
const clean = prefix.replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/+$/, '');
|
|
206
|
+
if (clean.length === 0 || clean.startsWith('..'))
|
|
207
|
+
return false;
|
|
208
|
+
return isInDiff({ ...finding, file: `${clean}/${finding.file}` }, set);
|
|
209
|
+
}
|
|
210
|
+
/** Repository root for a path inside a git working tree. */
|
|
211
|
+
export async function findRepoRoot(start) {
|
|
212
|
+
const stdout = await git(start, ['rev-parse', '--show-toplevel']);
|
|
213
|
+
const root = stdout.trim();
|
|
214
|
+
if (root.length === 0)
|
|
215
|
+
throw new Error('not inside a git repository');
|
|
216
|
+
return path.resolve(root);
|
|
217
|
+
}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Config } from './config.ts';
|
|
2
|
+
import type { Finding, ScanStats } from './types.ts';
|
|
3
|
+
export interface ScanOptions {
|
|
4
|
+
root: string;
|
|
5
|
+
config: Config;
|
|
6
|
+
/** When set, only these rule ids run (CLI `--rule`). */
|
|
7
|
+
onlyRules?: ReadonlySet<string>;
|
|
8
|
+
}
|
|
9
|
+
export interface ScanResult {
|
|
10
|
+
findings: Finding[];
|
|
11
|
+
suppressedCount: number;
|
|
12
|
+
stats: ScanStats;
|
|
13
|
+
warnings: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function scanProject(options: ScanOptions): Promise<ScanResult>;
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { asAst } from "./ast.js";
|
|
4
|
+
import { loadProjectDeps } from "./deps.js";
|
|
5
|
+
import { discoverFiles } from "./files.js";
|
|
6
|
+
import { getParser } from "./parser.js";
|
|
7
|
+
import { ALL_RULES, RULE_BY_ID } from "./rules/index.js";
|
|
8
|
+
import { collectCommentRanges } from "./rules/helpers.js";
|
|
9
|
+
import { isSuppressed, readSuppressions } from "./suppress.js";
|
|
10
|
+
function collectLocalPythonModules(files, deps) {
|
|
11
|
+
const valid = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
12
|
+
for (const file of files) {
|
|
13
|
+
if (file.language !== 'python')
|
|
14
|
+
continue;
|
|
15
|
+
const parts = file.rel.split('/');
|
|
16
|
+
const fileName = parts.pop() ?? '';
|
|
17
|
+
for (const dir of parts) {
|
|
18
|
+
if (valid.test(dir))
|
|
19
|
+
deps.python.localModules.add(dir);
|
|
20
|
+
}
|
|
21
|
+
// Every module stem, not just top-level ones: packages import their
|
|
22
|
+
// siblings flat (`from util import x` inside `idlelib/`).
|
|
23
|
+
const stem = fileName.replace(/\.pyi?$/, '');
|
|
24
|
+
if (valid.test(stem))
|
|
25
|
+
deps.python.localModules.add(stem);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function toFinding(ruleId, draft, defaults, file) {
|
|
29
|
+
return {
|
|
30
|
+
ruleId,
|
|
31
|
+
message: draft.message,
|
|
32
|
+
severity: draft.severity ?? defaults.severity,
|
|
33
|
+
confidence: draft.confidence ?? defaults.confidence,
|
|
34
|
+
file,
|
|
35
|
+
start: draft.start,
|
|
36
|
+
end: draft.end,
|
|
37
|
+
snippet: draft.snippet ?? '',
|
|
38
|
+
evidence: draft.evidence ?? [],
|
|
39
|
+
...(draft.help ? { help: draft.help } : {}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export async function scanProject(options) {
|
|
43
|
+
const started = performance.now();
|
|
44
|
+
const target = path.resolve(options.root);
|
|
45
|
+
const targetInfo = await stat(target).catch(() => undefined);
|
|
46
|
+
// Scanning a single file still resolves manifests and local modules from the
|
|
47
|
+
// directory that contains it.
|
|
48
|
+
const root = targetInfo?.isFile() ? path.dirname(target) : target;
|
|
49
|
+
const warnings = [];
|
|
50
|
+
const { config } = options;
|
|
51
|
+
for (const ruleId of Object.keys(config.rules)) {
|
|
52
|
+
if (!RULE_BY_ID.has(ruleId))
|
|
53
|
+
warnings.push(`unknown rule in config: ${ruleId}`);
|
|
54
|
+
}
|
|
55
|
+
const deps = await loadProjectDeps(root);
|
|
56
|
+
deps.ignoredModules = new Set(config.ignoreModules);
|
|
57
|
+
const { files, skipped: skippedByDiscovery } = await discoverFiles({
|
|
58
|
+
root: target,
|
|
59
|
+
include: config.include,
|
|
60
|
+
exclude: config.exclude,
|
|
61
|
+
});
|
|
62
|
+
collectLocalPythonModules(files, deps);
|
|
63
|
+
const enabled = ALL_RULES.filter((rule) => {
|
|
64
|
+
const setting = config.rules[rule.meta.id];
|
|
65
|
+
if (setting === 'off')
|
|
66
|
+
return false;
|
|
67
|
+
if (options.onlyRules && !options.onlyRules.has(rule.meta.id))
|
|
68
|
+
return false;
|
|
69
|
+
return true;
|
|
70
|
+
});
|
|
71
|
+
const byLanguage = { python: 0, typescript: 0, tsx: 0 };
|
|
72
|
+
const findings = [];
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
let suppressedCount = 0;
|
|
75
|
+
let parseErrors = 0;
|
|
76
|
+
let skipped = skippedByDiscovery;
|
|
77
|
+
for (const file of files) {
|
|
78
|
+
let buffer;
|
|
79
|
+
try {
|
|
80
|
+
buffer = await readFile(file.abs);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
skipped += 1;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (buffer.includes(0)) {
|
|
87
|
+
skipped += 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
// Editors on Windows write a UTF-8 BOM; it must not shift line/column
|
|
91
|
+
// numbers in findings.
|
|
92
|
+
const source = buffer.toString('utf8').replace(/^\uFEFF/, '');
|
|
93
|
+
byLanguage[file.language] += 1;
|
|
94
|
+
const parser = await getParser(file.language);
|
|
95
|
+
const tree = parser.parse(source);
|
|
96
|
+
if (!tree) {
|
|
97
|
+
parseErrors += 1;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const hadParseError = tree.rootNode.hasError;
|
|
101
|
+
if (hadParseError)
|
|
102
|
+
parseErrors += 1;
|
|
103
|
+
const root = asAst(tree.rootNode);
|
|
104
|
+
const comments = collectCommentRanges(root);
|
|
105
|
+
const ctx = {
|
|
106
|
+
file: file.rel,
|
|
107
|
+
lines: source.split(/\r?\n/),
|
|
108
|
+
root,
|
|
109
|
+
language: file.language,
|
|
110
|
+
deps,
|
|
111
|
+
comments,
|
|
112
|
+
};
|
|
113
|
+
const suppressions = readSuppressions(source, new Set(comments.map((comment) => comment.startLine)));
|
|
114
|
+
for (const rule of enabled) {
|
|
115
|
+
if (!rule.meta.languages.includes(file.language))
|
|
116
|
+
continue;
|
|
117
|
+
let drafts;
|
|
118
|
+
try {
|
|
119
|
+
drafts = rule.run(ctx);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
warnings.push(`${rule.meta.id} crashed on ${file.rel}: ${error.message}`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const configured = config.rules[rule.meta.id];
|
|
126
|
+
const severity = configured && configured !== 'off' ? configured : rule.meta.severity;
|
|
127
|
+
for (const draft of drafts) {
|
|
128
|
+
const finding = toFinding(rule.meta.id, draft, { severity, confidence: rule.meta.confidence }, file.rel);
|
|
129
|
+
// A file that did not parse cleanly has an unreliable structure: keep
|
|
130
|
+
// the finding, but never claim high confidence about it.
|
|
131
|
+
if (hadParseError && finding.confidence === 'high') {
|
|
132
|
+
finding.confidence = 'medium';
|
|
133
|
+
finding.evidence = [
|
|
134
|
+
...finding.evidence,
|
|
135
|
+
'the file has syntax errors, so its structure may be misread',
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
if (isSuppressed(finding, suppressions)) {
|
|
139
|
+
suppressedCount += 1;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const key = `${finding.ruleId}|${finding.file}|${finding.start.line}|${finding.start.column}|${finding.message}`;
|
|
143
|
+
if (seen.has(key))
|
|
144
|
+
continue;
|
|
145
|
+
seen.add(key);
|
|
146
|
+
findings.push(finding);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const disposable = tree;
|
|
150
|
+
try {
|
|
151
|
+
disposable.delete?.();
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
warnings.push(`could not free parse tree for ${file.rel}: ${error.message}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) ||
|
|
158
|
+
a.start.line - b.start.line ||
|
|
159
|
+
a.start.column - b.start.column ||
|
|
160
|
+
a.ruleId.localeCompare(b.ruleId));
|
|
161
|
+
const stats = {
|
|
162
|
+
files: byLanguage.python + byLanguage.typescript + byLanguage.tsx,
|
|
163
|
+
byLanguage,
|
|
164
|
+
skipped,
|
|
165
|
+
parseErrors,
|
|
166
|
+
durationMs: Math.round(performance.now() - started),
|
|
167
|
+
};
|
|
168
|
+
return { findings, suppressedCount, stats, warnings };
|
|
169
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { LanguageId } from './types.ts';
|
|
2
|
+
/** Directories that never contain code we want to lint. */
|
|
3
|
+
export declare const DEFAULT_IGNORE_DIRS: ReadonlySet<string>;
|
|
4
|
+
export declare const DEFAULT_INCLUDE: readonly string[];
|
|
5
|
+
export interface SourceFile {
|
|
6
|
+
/** Absolute path on disk. */
|
|
7
|
+
abs: string;
|
|
8
|
+
/** Repository-relative path with forward slashes. */
|
|
9
|
+
rel: string;
|
|
10
|
+
language: LanguageId;
|
|
11
|
+
size: number;
|
|
12
|
+
}
|
|
13
|
+
export interface DiscoverOptions {
|
|
14
|
+
root: string;
|
|
15
|
+
include?: readonly string[];
|
|
16
|
+
exclude?: readonly string[];
|
|
17
|
+
ignoreDirs?: readonly string[];
|
|
18
|
+
maxBytes?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface DiscoverResult {
|
|
21
|
+
files: SourceFile[];
|
|
22
|
+
skipped: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function toPosix(p: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Translate a small glob dialect into a RegExp anchored at the whole path.
|
|
27
|
+
* Supported: `**` (any depth), `*` (within one segment), `?`, and `{a,b}`
|
|
28
|
+
* alternatives (nested braces allowed).
|
|
29
|
+
*/
|
|
30
|
+
export declare function globToRegExp(glob: string): RegExp;
|
|
31
|
+
export declare function matchesAny(relPath: string, patterns: readonly RegExp[]): boolean;
|
|
32
|
+
export declare function languageFor(fileName: string): LanguageId | undefined;
|
|
33
|
+
export declare function discoverFiles(options: DiscoverOptions): Promise<DiscoverResult>;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
/** Directories that never contain code we want to lint. */
|
|
4
|
+
export const DEFAULT_IGNORE_DIRS = new Set([
|
|
5
|
+
'.git', '.hg', '.svn', '.idea', '.vscode',
|
|
6
|
+
'node_modules', 'bower_components',
|
|
7
|
+
'dist', 'build', 'out', 'target', 'bin', 'obj',
|
|
8
|
+
'vendor', 'third_party', 'external',
|
|
9
|
+
'.venv', 'venv', 'env', 'virtualenv', '.tox', '.eggs',
|
|
10
|
+
'site-packages', 'dist-packages',
|
|
11
|
+
'__pycache__', '.mypy_cache', '.pytest_cache', '.ruff_cache', '.pytype',
|
|
12
|
+
'.next', '.nuxt', '.svelte-kit', '.turbo', '.cache', 'coverage',
|
|
13
|
+
'.slopgate-cache',
|
|
14
|
+
]);
|
|
15
|
+
export const DEFAULT_INCLUDE = [
|
|
16
|
+
'**/*.py', '**/*.pyi',
|
|
17
|
+
'**/*.ts', '**/*.mts', '**/*.cts', '**/*.tsx',
|
|
18
|
+
'**/*.js', '**/*.mjs', '**/*.cjs', '**/*.jsx',
|
|
19
|
+
];
|
|
20
|
+
const EXTENSION_LANGUAGE = {
|
|
21
|
+
'.py': 'python',
|
|
22
|
+
'.pyi': 'python',
|
|
23
|
+
'.ts': 'typescript',
|
|
24
|
+
'.mts': 'typescript',
|
|
25
|
+
'.cts': 'typescript',
|
|
26
|
+
'.tsx': 'tsx',
|
|
27
|
+
'.jsx': 'tsx',
|
|
28
|
+
// JSX is legal in .js, and the tsx grammar is a superset of plain JS.
|
|
29
|
+
'.js': 'tsx',
|
|
30
|
+
'.mjs': 'tsx',
|
|
31
|
+
'.cjs': 'tsx',
|
|
32
|
+
};
|
|
33
|
+
const MAX_FILE_BYTES = 1024 * 1024;
|
|
34
|
+
export function toPosix(p) {
|
|
35
|
+
return p.replace(/\\/g, '/');
|
|
36
|
+
}
|
|
37
|
+
/** Split `a,{b,c},d` on commas that are not inside braces. */
|
|
38
|
+
function splitAlternatives(value) {
|
|
39
|
+
const parts = [];
|
|
40
|
+
let depth = 0;
|
|
41
|
+
let start = 0;
|
|
42
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
43
|
+
const ch = value[i];
|
|
44
|
+
if (ch === '{') {
|
|
45
|
+
depth += 1;
|
|
46
|
+
}
|
|
47
|
+
else if (ch === '}') {
|
|
48
|
+
depth -= 1;
|
|
49
|
+
}
|
|
50
|
+
else if (ch === ',' && depth === 0) {
|
|
51
|
+
parts.push(value.slice(start, i));
|
|
52
|
+
start = i + 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
parts.push(value.slice(start));
|
|
56
|
+
return parts;
|
|
57
|
+
}
|
|
58
|
+
function translateGlob(glob) {
|
|
59
|
+
let re = '';
|
|
60
|
+
for (let i = 0; i < glob.length; i += 1) {
|
|
61
|
+
const ch = glob[i] ?? '';
|
|
62
|
+
if (ch === '{') {
|
|
63
|
+
let depth = 0;
|
|
64
|
+
let end = -1;
|
|
65
|
+
for (let j = i; j < glob.length; j += 1) {
|
|
66
|
+
if (glob[j] === '{')
|
|
67
|
+
depth += 1;
|
|
68
|
+
else if (glob[j] === '}') {
|
|
69
|
+
depth -= 1;
|
|
70
|
+
if (depth === 0) {
|
|
71
|
+
end = j;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (end === -1) {
|
|
77
|
+
re += '\\{';
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const alternatives = splitAlternatives(glob.slice(i + 1, end)).map(translateGlob);
|
|
81
|
+
re += `(?:${alternatives.join('|')})`;
|
|
82
|
+
i = end;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (ch === '*') {
|
|
86
|
+
if (glob[i + 1] === '*') {
|
|
87
|
+
i += 1;
|
|
88
|
+
if (glob[i + 1] === '/') {
|
|
89
|
+
i += 1;
|
|
90
|
+
re += '(?:[^/]+/)*';
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
re += '.*';
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
re += '[^/]*';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (ch === '?') {
|
|
101
|
+
re += '[^/]';
|
|
102
|
+
}
|
|
103
|
+
else if ('\\^$.|+()[]{}'.includes(ch)) {
|
|
104
|
+
re += '\\' + ch;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
re += ch;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return re;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Translate a small glob dialect into a RegExp anchored at the whole path.
|
|
114
|
+
* Supported: `**` (any depth), `*` (within one segment), `?`, and `{a,b}`
|
|
115
|
+
* alternatives (nested braces allowed).
|
|
116
|
+
*/
|
|
117
|
+
export function globToRegExp(glob) {
|
|
118
|
+
const normalized = toPosix(glob).replace(/^\.\//, '');
|
|
119
|
+
return new RegExp('^' + translateGlob(normalized) + '$');
|
|
120
|
+
}
|
|
121
|
+
export function matchesAny(relPath, patterns) {
|
|
122
|
+
return patterns.some((re) => re.test(relPath));
|
|
123
|
+
}
|
|
124
|
+
export function languageFor(fileName) {
|
|
125
|
+
if (fileName.endsWith('.d.ts') || fileName.endsWith('.d.mts') || fileName.endsWith('.d.cts')) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
129
|
+
return EXTENSION_LANGUAGE[ext];
|
|
130
|
+
}
|
|
131
|
+
export async function discoverFiles(options) {
|
|
132
|
+
const root = path.resolve(options.root);
|
|
133
|
+
const includeRe = (options.include ?? DEFAULT_INCLUDE).map(globToRegExp);
|
|
134
|
+
const excludeRe = (options.exclude ?? []).map(globToRegExp);
|
|
135
|
+
const ignoreDirs = new Set([...DEFAULT_IGNORE_DIRS, ...(options.ignoreDirs ?? [])]);
|
|
136
|
+
const maxBytes = options.maxBytes ?? MAX_FILE_BYTES;
|
|
137
|
+
const files = [];
|
|
138
|
+
let skipped = 0;
|
|
139
|
+
// `slopgate src/app.py` scans a single file, not an empty directory walk.
|
|
140
|
+
let rootInfo;
|
|
141
|
+
try {
|
|
142
|
+
rootInfo = await stat(root);
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return { files, skipped: 1 };
|
|
146
|
+
}
|
|
147
|
+
if (rootInfo.isFile()) {
|
|
148
|
+
const name = path.basename(root);
|
|
149
|
+
const language = languageFor(name);
|
|
150
|
+
if (!language || !matchesAny(name, includeRe) || matchesAny(name, excludeRe)) {
|
|
151
|
+
return { files, skipped: 1 };
|
|
152
|
+
}
|
|
153
|
+
if (rootInfo.size > maxBytes || rootInfo.size === 0) {
|
|
154
|
+
return { files, skipped: 1 };
|
|
155
|
+
}
|
|
156
|
+
files.push({ abs: root, rel: name, language, size: rootInfo.size });
|
|
157
|
+
return { files, skipped };
|
|
158
|
+
}
|
|
159
|
+
async function walk(dir) {
|
|
160
|
+
let entries;
|
|
161
|
+
try {
|
|
162
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
168
|
+
for (const entry of entries) {
|
|
169
|
+
const abs = path.join(dir, entry.name);
|
|
170
|
+
const rel = toPosix(path.relative(root, abs));
|
|
171
|
+
if (entry.isSymbolicLink()) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (entry.isDirectory()) {
|
|
175
|
+
if (ignoreDirs.has(entry.name) || matchesAny(rel + '/', excludeRe)) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
await walk(abs);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!entry.isFile()) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (matchesAny(rel, excludeRe)) {
|
|
185
|
+
skipped += 1;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const language = languageFor(entry.name);
|
|
189
|
+
if (!language || !matchesAny(rel, includeRe)) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (entry.name.endsWith('.min.js') || entry.name.endsWith('.min.mjs')) {
|
|
193
|
+
skipped += 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
let info;
|
|
197
|
+
try {
|
|
198
|
+
info = await stat(abs);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
skipped += 1;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (info.size > maxBytes || info.size === 0) {
|
|
205
|
+
skipped += 1;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
files.push({ abs, rel, language, size: info.size });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
await walk(root);
|
|
212
|
+
files.sort((a, b) => a.rel.localeCompare(b.rel));
|
|
213
|
+
return { files, skipped };
|
|
214
|
+
}
|
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Config } from './config.ts';
|
|
2
|
+
import { type DiffSet } from './diff.ts';
|
|
3
|
+
import { type ScanResult } from './engine.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Minimal Model Context Protocol server over stdio.
|
|
6
|
+
*
|
|
7
|
+
* Transport: newline-delimited JSON-RPC 2.0 (MCP stdio transport). Nothing is
|
|
8
|
+
* ever written to stdout except protocol messages.
|
|
9
|
+
*/
|
|
10
|
+
export interface JsonRpcRequest {
|
|
11
|
+
jsonrpc?: string;
|
|
12
|
+
id?: string | number | null;
|
|
13
|
+
method?: string;
|
|
14
|
+
params?: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface JsonRpcError {
|
|
17
|
+
code: number;
|
|
18
|
+
message: string;
|
|
19
|
+
data?: unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface JsonRpcResponse {
|
|
22
|
+
jsonrpc: '2.0';
|
|
23
|
+
id: string | number | null;
|
|
24
|
+
result?: unknown;
|
|
25
|
+
error?: JsonRpcError;
|
|
26
|
+
}
|
|
27
|
+
export interface McpDeps {
|
|
28
|
+
root: string;
|
|
29
|
+
version: string;
|
|
30
|
+
config: Config;
|
|
31
|
+
scan: (root: string) => Promise<ScanResult>;
|
|
32
|
+
diff: (root: string, base: string) => Promise<DiffSet>;
|
|
33
|
+
}
|
|
34
|
+
/** True when `candidate` is `root` itself or lives below it. */
|
|
35
|
+
export declare function isInsideRoot(realRoot: string, candidate: string): boolean;
|
|
36
|
+
export declare function createMcpServer(deps: McpDeps): {
|
|
37
|
+
handle(line: string): Promise<JsonRpcResponse | null>;
|
|
38
|
+
};
|
|
39
|
+
export declare function defaultMcpDeps(root: string, version: string): Omit<McpDeps, 'config'>;
|
|
40
|
+
/** Runs the stdio loop. Resolves when stdin closes. */
|
|
41
|
+
export declare function serveMcp(root: string, version: string): Promise<void>;
|