@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.
Files changed (50) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/LICENSE +21 -0
  3. package/README.md +266 -2
  4. package/dist/ast.d.ts +29 -0
  5. package/dist/ast.js +3 -0
  6. package/dist/baseline.d.ts +33 -0
  7. package/dist/baseline.js +104 -0
  8. package/dist/cli.d.ts +9 -0
  9. package/dist/cli.js +481 -0
  10. package/dist/config.d.ts +25 -0
  11. package/dist/config.js +90 -0
  12. package/dist/data/python-stdlib.d.ts +19 -0
  13. package/dist/data/python-stdlib.js +75 -0
  14. package/dist/deps.d.ts +8 -0
  15. package/dist/deps.js +379 -0
  16. package/dist/diff.d.ts +30 -0
  17. package/dist/diff.js +179 -0
  18. package/dist/engine.d.ts +15 -0
  19. package/dist/engine.js +179 -0
  20. package/dist/files.d.ts +32 -0
  21. package/dist/files.js +168 -0
  22. package/dist/mcp.d.ts +41 -0
  23. package/dist/mcp.js +226 -0
  24. package/dist/parser.d.ts +3 -0
  25. package/dist/parser.js +41 -0
  26. package/dist/reporters/github.d.ts +2 -0
  27. package/dist/reporters/github.js +44 -0
  28. package/dist/reporters/json.d.ts +10 -0
  29. package/dist/reporters/json.js +36 -0
  30. package/dist/reporters/pretty.d.ts +12 -0
  31. package/dist/reporters/pretty.js +103 -0
  32. package/dist/reporters/sarif.d.ts +2 -0
  33. package/dist/reporters/sarif.js +70 -0
  34. package/dist/rules/helpers.d.ts +43 -0
  35. package/dist/rules/helpers.js +163 -0
  36. package/dist/rules/index.d.ts +3 -0
  37. package/dist/rules/index.js +4 -0
  38. package/dist/rules/python.d.ts +2 -0
  39. package/dist/rules/python.js +591 -0
  40. package/dist/rules/typescript.d.ts +2 -0
  41. package/dist/rules/typescript.js +497 -0
  42. package/dist/suppress.d.ts +12 -0
  43. package/dist/suppress.js +52 -0
  44. package/dist/types.d.ts +88 -0
  45. package/dist/types.js +1 -0
  46. package/grammars/README.md +28 -0
  47. package/grammars/tree-sitter-python.wasm +0 -0
  48. package/grammars/tree-sitter-tsx.wasm +0 -0
  49. package/grammars/tree-sitter-typescript.wasm +0 -0
  50. package/package.json +61 -4
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Top-level modules of the Python standard library (3.9 - 3.14).
3
+ *
4
+ * Kept as a static list on purpose: we never execute the target project's
5
+ * Python interpreter, and the list is stable enough to pin. Anything missing
6
+ * can be added to `ignoreModules` in `.slopgate.json`.
7
+ */
8
+ export const PYTHON_STDLIB = new Set([
9
+ '__future__', '__main__', '_thread', 'abc', 'aifc', 'annotationlib', 'argparse', 'array',
10
+ 'ast', 'asynchat', 'asyncio', 'asyncore', 'atexit', 'audioop', 'base64',
11
+ 'bdb', 'binascii', 'bisect', 'builtins', 'bz2', 'cProfile', 'calendar',
12
+ 'cgi', 'cgitb', 'chunk', 'cmath', 'cmd', 'code', 'codecs', 'codeop',
13
+ 'collections', 'colorsys', 'compileall', 'compression', 'concurrent',
14
+ 'configparser', 'contextlib', 'contextvars', 'copy', 'copyreg', 'crypt',
15
+ 'csv', 'ctypes', 'curses', 'dataclasses', 'datetime', 'dbm', 'decimal',
16
+ 'difflib', 'dis', 'distutils', 'doctest', 'email', 'encodings',
17
+ 'ensurepip', 'enum', 'errno', 'faulthandler', 'fcntl', 'filecmp',
18
+ 'fileinput', 'fnmatch', 'fractions', 'ftplib', 'functools', 'gc', 'getopt',
19
+ 'getpass', 'gettext', 'glob', 'graphlib', 'grp', 'gzip', 'hashlib',
20
+ 'heapq', 'hmac', 'html', 'http', 'idlelib', 'imaplib', 'imghdr', 'imp',
21
+ 'importlib', 'inspect', 'io', 'ipaddress', 'itertools', 'json', 'keyword',
22
+ 'lib2to3', 'linecache', 'locale', 'logging', 'lzma', 'mailbox', 'mailcap',
23
+ 'marshal', 'math', 'mimetypes', 'mmap', 'modulefinder', 'msilib', 'msvcrt',
24
+ 'multiprocessing', 'netrc', 'nis', 'nntplib', 'nt', 'ntpath', 'numbers',
25
+ 'operator', 'optparse', 'os', 'ossaudiodev', 'pathlib', 'pdb', 'pickle',
26
+ 'pickletools', 'pipes', 'pkgutil', 'platform', 'plistlib', 'poplib',
27
+ 'posix', 'posixpath', 'pprint', 'profile', 'pstats', 'pty', 'pwd',
28
+ 'py_compile', 'pyclbr', 'pydoc', 'pyexpat', 'queue', 'quopri', 'random', 're',
29
+ 'readline', 'reprlib', 'resource', 'rlcompleter', 'runpy', 'sched',
30
+ 'secrets', 'select', 'selectors', 'shelve', 'shlex', 'shutil', 'signal',
31
+ 'site', 'smtpd', 'smtplib', 'sndhdr', 'socket', 'socketserver', 'spwd',
32
+ 'sqlite3', 'sre_compile', 'sre_constants', 'sre_parse', 'ssl', 'stat',
33
+ 'statistics', 'statvfs', 'string', 'stringprep', 'struct', 'subprocess',
34
+ 'sunau', 'symtable', 'sys', 'sysconfig', 'syslog', 'tabnanny', 'tarfile',
35
+ 'telnetlib', 'tempfile', 'termios', 'textwrap', 'this', 'threading',
36
+ 'time', 'timeit', 'tkinter', 'token', 'tokenize', 'tomllib', 'trace',
37
+ 'traceback', 'tracemalloc', 'tty', 'turtle', 'turtledemo', 'types',
38
+ 'typing', 'unicodedata', 'unittest', 'urllib', 'uu', 'uuid', 'venv',
39
+ 'warnings', 'wave', 'weakref', 'webbrowser', 'winreg', 'winsound',
40
+ 'wsgiref', 'xdrlib', 'xml', 'xmlrpc', 'zipapp', 'zipfile', 'zipimport',
41
+ 'zlib', 'zoneinfo',
42
+ ]);
43
+ /**
44
+ * Import name -> distribution name, for packages whose import name differs
45
+ * from the name you put in a manifest. Without this map the dependency check
46
+ * would produce false positives on very common packages.
47
+ */
48
+ export const IMPORT_TO_DISTRIBUTION = {
49
+ attr: 'attrs',
50
+ bs4: 'beautifulsoup4',
51
+ cv2: 'opencv-python',
52
+ Crypto: 'pycryptodome',
53
+ dateutil: 'python-dateutil',
54
+ dotenv: 'python-dotenv',
55
+ google: 'protobuf',
56
+ jwt: 'pyjwt',
57
+ magic: 'python-magic',
58
+ MySQLdb: 'mysqlclient',
59
+ OpenSSL: 'pyopenssl',
60
+ PIL: 'pillow',
61
+ pkg_resources: 'setuptools',
62
+ psycopg2: 'psycopg2-binary',
63
+ serial: 'pyserial',
64
+ sklearn: 'scikit-learn',
65
+ skimage: 'scikit-image',
66
+ yaml: 'pyyaml',
67
+ zmq: 'pyzmq',
68
+ };
69
+ /**
70
+ * Modules that are importable without being declared because the toolchain
71
+ * provides them. `setuptools` ships `pkg_resources` and `_distutils_hack`.
72
+ */
73
+ export const IMPLICIT_MODULES = new Set([
74
+ '_distutils_hack', 'pkg_resources', 'setuptools', 'pip', 'wheel',
75
+ ]);
package/dist/deps.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { ProjectDeps } from './types.ts';
2
+ /** PEP 503 normalization: lowercase, runs of `[-_.]` collapse to `-`. */
3
+ export declare function normalizePythonName(name: string): string;
4
+ /** `requests[socks]>=2.0 ; python_version > "3.6"` -> `requests` */
5
+ export declare function requirementName(spec: string): string | undefined;
6
+ /** Best-effort parse of the Python dependency declarations we can see. */
7
+ export declare function parsePythonManifestText(fileName: string, text: string): string[];
8
+ export declare function loadProjectDeps(root: string): Promise<ProjectDeps>;
package/dist/deps.js ADDED
@@ -0,0 +1,379 @@
1
+ import { readFile, readdir, stat } from 'node:fs/promises';
2
+ import * as path from 'node:path';
3
+ /** PEP 503 normalization: lowercase, runs of `[-_.]` collapse to `-`. */
4
+ export function normalizePythonName(name) {
5
+ return name.trim().toLowerCase().replace(/[-_.]+/g, '-');
6
+ }
7
+ /** `requests[socks]>=2.0 ; python_version > "3.6"` -> `requests` */
8
+ export function requirementName(spec) {
9
+ const trimmed = spec.trim().replace(/^['"]|['"]$/g, '').trim();
10
+ if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('-'))
11
+ return undefined;
12
+ if (trimmed.includes('://'))
13
+ return undefined;
14
+ const match = /^([A-Za-z0-9][A-Za-z0-9._-]*)/.exec(trimmed);
15
+ return match?.[1] ? normalizePythonName(match[1]) : undefined;
16
+ }
17
+ function stripTomlComments(line) {
18
+ let quote;
19
+ for (let i = 0; i < line.length; i += 1) {
20
+ const ch = line[i];
21
+ if (quote) {
22
+ if (ch === quote && line[i - 1] !== '\\')
23
+ quote = undefined;
24
+ }
25
+ else if (ch === '"' || ch === "'") {
26
+ quote = ch;
27
+ }
28
+ else if (ch === '#') {
29
+ return line.slice(0, i);
30
+ }
31
+ }
32
+ return line;
33
+ }
34
+ function quotedStrings(text) {
35
+ const out = [];
36
+ const re = /"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'/g;
37
+ let match;
38
+ while ((match = re.exec(text)) !== null) {
39
+ const value = match[1] ?? match[2];
40
+ if (value)
41
+ out.push(value);
42
+ }
43
+ return out;
44
+ }
45
+ /** Best-effort parse of the Python dependency declarations we can see. */
46
+ export function parsePythonManifestText(fileName, text) {
47
+ const names = [];
48
+ const base = path.basename(fileName).toLowerCase();
49
+ if (base.startsWith('requirements') && base.endsWith('.txt')) {
50
+ for (const rawLine of text.split(/\r?\n/)) {
51
+ const line = rawLine.replace(/\s+#.*$/, '');
52
+ const name = requirementName(line);
53
+ if (name)
54
+ names.push(name);
55
+ }
56
+ return names;
57
+ }
58
+ if (base === 'pyproject.toml') {
59
+ const lines = text.split(/\r?\n/);
60
+ let section = '';
61
+ let arrayRemaining = 0;
62
+ for (const rawLine of lines) {
63
+ const line = stripTomlComments(rawLine);
64
+ const sectionMatch = /^\s*\[([^\]]+)\]\s*$/.exec(line);
65
+ if (sectionMatch?.[1]) {
66
+ section = sectionMatch[1].trim();
67
+ continue;
68
+ }
69
+ if (arrayRemaining > 0) {
70
+ for (const value of quotedStrings(line)) {
71
+ const name = requirementName(value);
72
+ if (name)
73
+ names.push(name);
74
+ }
75
+ if (line.includes(']'))
76
+ arrayRemaining = 0;
77
+ continue;
78
+ }
79
+ const arrayStart = /^\s*(dependencies|requires)\s*=\s*\[(.*)$/.exec(line);
80
+ if (arrayStart) {
81
+ const inline = arrayStart[2] ?? '';
82
+ for (const value of quotedStrings(inline)) {
83
+ const name = requirementName(value);
84
+ if (name)
85
+ names.push(name);
86
+ }
87
+ if (!inline.includes(']'))
88
+ arrayRemaining = 1;
89
+ continue;
90
+ }
91
+ if (section === 'tool.poetry.dependencies' || section === 'tool.poetry.dev-dependencies' || section === 'tool.poetry.group.dev.dependencies') {
92
+ const kv = /^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/.exec(line);
93
+ if (kv?.[1] && kv[1].toLowerCase() !== 'python') {
94
+ names.push(normalizePythonName(kv[1]));
95
+ }
96
+ }
97
+ if (section === 'build-system') {
98
+ const requires = /^\s*requires\s*=\s*\[(.*)/.exec(line);
99
+ if (requires?.[1] !== undefined) {
100
+ for (const value of quotedStrings(requires[1])) {
101
+ const name = requirementName(value);
102
+ if (name)
103
+ names.push(name);
104
+ }
105
+ }
106
+ }
107
+ }
108
+ return names;
109
+ }
110
+ if (base === 'setup.py') {
111
+ const blocks = [...text.matchAll(/install_requires\s*=\s*\[([\s\S]*?)\]/g)];
112
+ for (const block of blocks) {
113
+ for (const value of quotedStrings(block[1] ?? '')) {
114
+ const name = requirementName(value);
115
+ if (name)
116
+ names.push(name);
117
+ }
118
+ }
119
+ const extras = [...text.matchAll(/extras_require\s*=\s*\{([\s\S]*?)\}\s*[,)]/g)];
120
+ for (const block of extras) {
121
+ for (const value of quotedStrings(block[1] ?? '')) {
122
+ const name = requirementName(value);
123
+ if (name)
124
+ names.push(name);
125
+ }
126
+ }
127
+ return names;
128
+ }
129
+ if (base === 'setup.cfg') {
130
+ const sectionMatch = /\[options\]([\s\S]*?)(?=\n\[|$)/.exec(text);
131
+ if (sectionMatch?.[1]) {
132
+ const requires = /install_requires\s*=\s*([\s\S]*?)(?=\n\S|$)/.exec(sectionMatch[1]);
133
+ if (requires?.[1]) {
134
+ for (const rawLine of requires[1].split(/\r?\n/)) {
135
+ const name = requirementName(rawLine);
136
+ if (name)
137
+ names.push(name);
138
+ }
139
+ }
140
+ }
141
+ return names;
142
+ }
143
+ if (base === 'pipfile') {
144
+ const sectionMatch = /\[packages\]([\s\S]*?)(?=\n\[|$)/.exec(text);
145
+ if (sectionMatch?.[1]) {
146
+ for (const rawLine of sectionMatch[1].split(/\r?\n/)) {
147
+ const kv = /^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/.exec(rawLine);
148
+ if (kv?.[1])
149
+ names.push(normalizePythonName(kv[1]));
150
+ }
151
+ }
152
+ return names;
153
+ }
154
+ return names;
155
+ }
156
+ async function exists(p) {
157
+ try {
158
+ await stat(p);
159
+ return true;
160
+ }
161
+ catch {
162
+ return false;
163
+ }
164
+ }
165
+ async function readIfExists(p) {
166
+ try {
167
+ const text = await readFile(p, 'utf8');
168
+ return text.replace(/^\uFEFF/, '');
169
+ }
170
+ catch {
171
+ return undefined;
172
+ }
173
+ }
174
+ /** Parse JSON without turning a broken manifest into a crash. */
175
+ function tryParseJson(text) {
176
+ try {
177
+ return JSON.parse(text);
178
+ }
179
+ catch {
180
+ return undefined;
181
+ }
182
+ }
183
+ async function readDirSafe(dir) {
184
+ try {
185
+ return await readdir(dir, { withFileTypes: true });
186
+ }
187
+ catch {
188
+ return undefined;
189
+ }
190
+ }
191
+ const PYTHON_MANIFESTS = [
192
+ 'pyproject.toml', 'setup.py', 'setup.cfg', 'Pipfile', 'requirements.txt',
193
+ 'requirements-dev.txt', 'requirements_dev.txt', 'dev-requirements.txt',
194
+ 'requirements/base.txt', 'requirements/dev.txt', 'requirements/prod.txt',
195
+ 'requirements/main.txt', 'requirements/test.txt', 'requirements/tests.txt',
196
+ ];
197
+ async function loadPythonDeps(root) {
198
+ const declared = new Set();
199
+ const manifests = [];
200
+ for (const rel of PYTHON_MANIFESTS) {
201
+ const abs = path.join(root, rel);
202
+ const text = await readIfExists(abs);
203
+ if (text === undefined)
204
+ continue;
205
+ manifests.push(rel.replace(/\\/g, '/'));
206
+ for (const name of parsePythonManifestText(rel, text))
207
+ declared.add(name);
208
+ }
209
+ return { declared, manifests, localModules: new Set() };
210
+ }
211
+ function stripJsonComments(text) {
212
+ let out = '';
213
+ let inString = false;
214
+ let inLine = false;
215
+ let inBlock = false;
216
+ for (let i = 0; i < text.length; i += 1) {
217
+ const ch = text[i];
218
+ const next = text[i + 1];
219
+ if (inLine) {
220
+ if (ch === '\n') {
221
+ inLine = false;
222
+ out += ch;
223
+ }
224
+ continue;
225
+ }
226
+ if (inBlock) {
227
+ if (ch === '*' && next === '/') {
228
+ inBlock = false;
229
+ i += 1;
230
+ }
231
+ continue;
232
+ }
233
+ if (inString) {
234
+ out += ch;
235
+ if (ch === '\\') {
236
+ out += next ?? '';
237
+ i += 1;
238
+ }
239
+ else if (ch === '"') {
240
+ inString = false;
241
+ }
242
+ continue;
243
+ }
244
+ if (ch === '"') {
245
+ inString = true;
246
+ out += ch;
247
+ continue;
248
+ }
249
+ if (ch === '/' && next === '/') {
250
+ inLine = true;
251
+ i += 1;
252
+ continue;
253
+ }
254
+ if (ch === '/' && next === '*') {
255
+ inBlock = true;
256
+ i += 1;
257
+ continue;
258
+ }
259
+ out += ch;
260
+ }
261
+ return out;
262
+ }
263
+ async function loadTsconfigAliases(root) {
264
+ const aliases = [];
265
+ for (const name of ['tsconfig.json', 'tsconfig.base.json']) {
266
+ const text = await readIfExists(path.join(root, name));
267
+ if (!text)
268
+ continue;
269
+ const parsed = tryParseJson(stripJsonComments(text));
270
+ if (!parsed)
271
+ continue;
272
+ const paths = parsed.compilerOptions?.paths;
273
+ if (paths && typeof paths === 'object') {
274
+ for (const key of Object.keys(paths)) {
275
+ const cleaned = key.replace(/\/\*$/, '').replace(/\*$/, '');
276
+ if (cleaned)
277
+ aliases.push(cleaned);
278
+ }
279
+ }
280
+ }
281
+ return aliases;
282
+ }
283
+ async function loadInstalledPackages(root) {
284
+ const installed = new Set();
285
+ const modulesDir = path.join(root, 'node_modules');
286
+ const entries = await readDirSafe(modulesDir);
287
+ if (!entries)
288
+ return installed;
289
+ for (const entry of entries) {
290
+ if (entry.name.startsWith('.'))
291
+ continue;
292
+ if (entry.name.startsWith('@')) {
293
+ if (!entry.isDirectory())
294
+ continue;
295
+ const scoped = await readDirSafe(path.join(modulesDir, entry.name));
296
+ if (!scoped)
297
+ continue;
298
+ for (const child of scoped) {
299
+ if (child.isDirectory())
300
+ installed.add(`${entry.name}/${child.name}`);
301
+ }
302
+ continue;
303
+ }
304
+ if (entry.isDirectory())
305
+ installed.add(entry.name);
306
+ }
307
+ return installed;
308
+ }
309
+ async function loadWorkspaceNames(root) {
310
+ const names = new Set();
311
+ const dependencies = new Set();
312
+ const visited = new Set();
313
+ async function scan(dir, depth) {
314
+ if (depth > 3 || visited.has(dir))
315
+ return;
316
+ visited.add(dir);
317
+ const pkgPath = path.join(dir, 'package.json');
318
+ const text = await readIfExists(pkgPath);
319
+ const parsed = text ? tryParseJson(text) : undefined;
320
+ if (parsed) {
321
+ if (typeof parsed.name === 'string' && parsed.name.length > 0)
322
+ names.add(parsed.name);
323
+ for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
324
+ const deps = parsed[field];
325
+ if (deps && typeof deps === 'object') {
326
+ for (const name of Object.keys(deps))
327
+ dependencies.add(name);
328
+ }
329
+ }
330
+ }
331
+ const entries = await readDirSafe(dir);
332
+ if (!entries)
333
+ return;
334
+ for (const entry of entries) {
335
+ if (!entry.isDirectory())
336
+ continue;
337
+ if (entry.name === 'node_modules' || entry.name.startsWith('.'))
338
+ continue;
339
+ await scan(path.join(dir, entry.name), depth + 1);
340
+ }
341
+ }
342
+ await scan(root, 0);
343
+ return { names, dependencies };
344
+ }
345
+ async function loadNodeDeps(root) {
346
+ const declared = new Set();
347
+ const pkgPath = path.join(root, 'package.json');
348
+ const hasPackageJson = await exists(pkgPath);
349
+ const text = await readIfExists(pkgPath);
350
+ const pkg = text ? tryParseJson(text) : undefined;
351
+ if (pkg) {
352
+ for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
353
+ const deps = pkg[field];
354
+ if (deps && typeof deps === 'object') {
355
+ for (const name of Object.keys(deps))
356
+ declared.add(name);
357
+ }
358
+ }
359
+ }
360
+ const [aliases, installed, workspaceInfo] = await Promise.all([
361
+ loadTsconfigAliases(root),
362
+ loadInstalledPackages(root),
363
+ loadWorkspaceNames(root),
364
+ ]);
365
+ for (const dependency of workspaceInfo.dependencies)
366
+ declared.add(dependency);
367
+ return {
368
+ declared,
369
+ workspaces: workspaceInfo.names,
370
+ aliases,
371
+ hasPackageJson,
372
+ installed,
373
+ };
374
+ }
375
+ export async function loadProjectDeps(root) {
376
+ const absRoot = path.resolve(root);
377
+ const [python, node] = await Promise.all([loadPythonDeps(absRoot), loadNodeDeps(absRoot)]);
378
+ return { root: absRoot, python, node, ignoredModules: new Set() };
379
+ }
package/dist/diff.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type { Finding } from './types.ts';
2
+ export interface DiffSet {
3
+ /** Files with per-line additions. */
4
+ lines: Map<string, Set<number>>;
5
+ /** Files where the whole file is new (untracked or added). */
6
+ full: Set<string>;
7
+ }
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 declare function normalizeGitPath(raw: string): string;
14
+ /**
15
+ * `git diff` parses options anywhere before `--`, so a ref beginning with `-`
16
+ * (`--diff=--output=file`) would be interpreted as a git flag. Reject it.
17
+ */
18
+ export declare function assertSafeRef(ref: string): string;
19
+ export declare function parseUnifiedDiff(stdout: string): DiffSet;
20
+ /** Lines that differ from `ref`, plus every line of untracked files. */
21
+ export declare function diffSince(root: string, ref: string): Promise<DiffSet>;
22
+ export declare function isInDiff(finding: Finding, set: DiffSet): boolean;
23
+ /**
24
+ * Findings carry paths relative to the scan target. When the target is a
25
+ * subdirectory (or a single file) of the repository, prepend the relative
26
+ * prefix before comparing against git's repository-root paths.
27
+ */
28
+ export declare function isInDiffWithPrefix(finding: Finding, set: DiffSet, prefix: string): boolean;
29
+ /** Repository root for a path inside a git working tree. */
30
+ export declare function findRepoRoot(start: string): Promise<string>;
package/dist/diff.js ADDED
@@ -0,0 +1,179 @@
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
+ for (const line of stdout.split(/\r?\n/)) {
94
+ if (line.startsWith('--- ')) {
95
+ previousWasDevNull = line.trim() === '--- /dev/null';
96
+ continue;
97
+ }
98
+ const newFile = NEW_FILE.exec(line);
99
+ if (newFile?.[1] && newFile[1] !== '/dev/null') {
100
+ currentFile = normalizeGitPath(newFile[1]);
101
+ if (previousWasDevNull)
102
+ set.full.add(currentFile);
103
+ previousWasDevNull = false;
104
+ continue;
105
+ }
106
+ previousWasDevNull = false;
107
+ const header = DIFF_HEADER.exec(line);
108
+ if (header?.[1] && currentFile === undefined) {
109
+ // Fallback for binary diffs, which have no `+++` line.
110
+ const match = /^"?a\/(.+?)"? "?b\/(.+?)"?$/.exec(header[1]);
111
+ if (match?.[2])
112
+ currentFile = normalizeGitPath(match[2]);
113
+ continue;
114
+ }
115
+ if (!currentFile)
116
+ continue;
117
+ const hunk = HUNK.exec(line);
118
+ if (!hunk?.[1])
119
+ continue;
120
+ const start = Number.parseInt(hunk[1], 10);
121
+ const count = hunk[2] === undefined ? 1 : Number.parseInt(hunk[2], 10);
122
+ if (!Number.isFinite(start))
123
+ continue;
124
+ let store = set.lines.get(currentFile);
125
+ if (!store) {
126
+ store = new Set();
127
+ set.lines.set(currentFile, store);
128
+ }
129
+ for (let offset = 0; offset < count; offset += 1)
130
+ store.add(start + offset);
131
+ }
132
+ return set;
133
+ }
134
+ /** Lines that differ from `ref`, plus every line of untracked files. */
135
+ export async function diffSince(root, ref) {
136
+ const safeRef = assertSafeRef(ref);
137
+ const stdout = await git(root, ['diff', '--unified=0', '--no-color', '--no-ext-diff', safeRef, '--']);
138
+ const set = parseUnifiedDiff(stdout);
139
+ // `-z` gives NUL-separated raw paths, immune to quoting and newlines in names.
140
+ const untracked = await git(root, ['ls-files', '--others', '--exclude-standard', '-z']).catch(() => '');
141
+ for (const file of untracked.split('\u0000')) {
142
+ if (file.length > 0)
143
+ set.full.add(file);
144
+ }
145
+ return set;
146
+ }
147
+ export function isInDiff(finding, set) {
148
+ if (set.full.has(finding.file))
149
+ return true;
150
+ const lines = set.lines.get(finding.file);
151
+ if (!lines)
152
+ return false;
153
+ for (let line = finding.start.line; line <= Math.max(finding.start.line, finding.end.line); line += 1) {
154
+ if (lines.has(line))
155
+ return true;
156
+ }
157
+ return false;
158
+ }
159
+ /**
160
+ * Findings carry paths relative to the scan target. When the target is a
161
+ * subdirectory (or a single file) of the repository, prepend the relative
162
+ * prefix before comparing against git's repository-root paths.
163
+ */
164
+ export function isInDiffWithPrefix(finding, set, prefix) {
165
+ if (isInDiff(finding, set))
166
+ return true;
167
+ const clean = prefix.replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/+$/, '');
168
+ if (clean.length === 0 || clean.startsWith('..'))
169
+ return false;
170
+ return isInDiff({ ...finding, file: `${clean}/${finding.file}` }, set);
171
+ }
172
+ /** Repository root for a path inside a git working tree. */
173
+ export async function findRepoRoot(start) {
174
+ const stdout = await git(start, ['rev-parse', '--show-toplevel']);
175
+ const root = stdout.trim();
176
+ if (root.length === 0)
177
+ throw new Error('not inside a git repository');
178
+ return path.resolve(root);
179
+ }
@@ -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>;