@voidh4x0r/slopgate 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -0
- package/LICENSE +21 -0
- package/README.md +304 -2
- package/dist/ast.d.ts +29 -0
- package/dist/ast.js +3 -0
- package/dist/baseline.d.ts +33 -0
- package/dist/baseline.js +104 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +487 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +91 -0
- package/dist/data/python-stdlib.d.ts +21 -0
- package/dist/data/python-stdlib.js +92 -0
- package/dist/deps.d.ts +8 -0
- package/dist/deps.js +491 -0
- package/dist/diff.d.ts +30 -0
- package/dist/diff.js +217 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.js +169 -0
- package/dist/files.d.ts +33 -0
- package/dist/files.js +214 -0
- package/dist/mcp.d.ts +41 -0
- package/dist/mcp.js +226 -0
- package/dist/parser.d.ts +3 -0
- package/dist/parser.js +45 -0
- package/dist/reporters/github.d.ts +2 -0
- package/dist/reporters/github.js +44 -0
- package/dist/reporters/json.d.ts +10 -0
- package/dist/reporters/json.js +36 -0
- package/dist/reporters/pretty.d.ts +12 -0
- package/dist/reporters/pretty.js +103 -0
- package/dist/reporters/sarif.d.ts +2 -0
- package/dist/reporters/sarif.js +75 -0
- package/dist/rules/helpers.d.ts +53 -0
- package/dist/rules/helpers.js +178 -0
- package/dist/rules/index.d.ts +3 -0
- package/dist/rules/index.js +4 -0
- package/dist/rules/python.d.ts +2 -0
- package/dist/rules/python.js +632 -0
- package/dist/rules/typescript.d.ts +2 -0
- package/dist/rules/typescript.js +563 -0
- package/dist/suppress.d.ts +12 -0
- package/dist/suppress.js +52 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.js +1 -0
- package/grammars/README.md +28 -0
- package/grammars/tree-sitter-python.wasm +0 -0
- package/grammars/tree-sitter-tsx.wasm +0 -0
- package/grammars/tree-sitter-typescript.wasm +0 -0
- package/package.json +61 -4
|
@@ -0,0 +1,92 @@
|
|
|
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 path -> distribution name, for packages whose import name differs
|
|
45
|
+
* from the name you put in a manifest. Keys are case-sensitive and may be
|
|
46
|
+
* dotted; the longest matching prefix wins (`google.protobuf` before `google`).
|
|
47
|
+
* Without this map the dependency check would produce false positives on very
|
|
48
|
+
* common packages.
|
|
49
|
+
*/
|
|
50
|
+
export const IMPORT_TO_DISTRIBUTION = {
|
|
51
|
+
attr: 'attrs',
|
|
52
|
+
bs4: 'beautifulsoup4',
|
|
53
|
+
cv2: 'opencv-python',
|
|
54
|
+
Crypto: 'pycryptodome',
|
|
55
|
+
dateutil: 'python-dateutil',
|
|
56
|
+
docx: 'python-docx',
|
|
57
|
+
dotenv: 'python-dotenv',
|
|
58
|
+
fitz: 'pymupdf',
|
|
59
|
+
git: 'gitpython',
|
|
60
|
+
grpc: 'grpcio',
|
|
61
|
+
'google.protobuf': 'protobuf',
|
|
62
|
+
googleapiclient: 'google-api-python-client',
|
|
63
|
+
jose: 'python-jose',
|
|
64
|
+
jwt: 'pyjwt',
|
|
65
|
+
ldap: 'python-ldap',
|
|
66
|
+
magic: 'python-magic',
|
|
67
|
+
MySQLdb: 'mysqlclient',
|
|
68
|
+
nacl: 'pynacl',
|
|
69
|
+
OpenSSL: 'pyopenssl',
|
|
70
|
+
PIL: 'pillow',
|
|
71
|
+
pkg_resources: 'setuptools',
|
|
72
|
+
pptx: 'python-pptx',
|
|
73
|
+
psycopg2: 'psycopg2-binary',
|
|
74
|
+
rest_framework: 'djangorestframework',
|
|
75
|
+
serial: 'pyserial',
|
|
76
|
+
skimage: 'scikit-image',
|
|
77
|
+
sklearn: 'scikit-learn',
|
|
78
|
+
slugify: 'python-slugify',
|
|
79
|
+
socks: 'pysocks',
|
|
80
|
+
win32api: 'pywin32',
|
|
81
|
+
win32com: 'pywin32',
|
|
82
|
+
Xlib: 'python-xlib',
|
|
83
|
+
yaml: 'pyyaml',
|
|
84
|
+
zmq: 'pyzmq',
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Modules that are importable without being declared because the toolchain
|
|
88
|
+
* provides them. `setuptools` ships `pkg_resources` and `_distutils_hack`.
|
|
89
|
+
*/
|
|
90
|
+
export const IMPLICIT_MODULES = new Set([
|
|
91
|
+
'_distutils_hack', 'pkg_resources', 'setuptools', 'pip', 'wheel',
|
|
92
|
+
]);
|
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,491 @@
|
|
|
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
|
+
/** Split an array body on commas outside quotes and inline tables. */
|
|
46
|
+
function splitTopLevel(value) {
|
|
47
|
+
const parts = [];
|
|
48
|
+
let quote;
|
|
49
|
+
let depth = 0;
|
|
50
|
+
let start = 0;
|
|
51
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
52
|
+
const ch = value[i];
|
|
53
|
+
if (quote) {
|
|
54
|
+
if (ch === quote && value[i - 1] !== '\\')
|
|
55
|
+
quote = undefined;
|
|
56
|
+
}
|
|
57
|
+
else if (ch === '"' || ch === "'") {
|
|
58
|
+
quote = ch;
|
|
59
|
+
}
|
|
60
|
+
else if (ch === '{') {
|
|
61
|
+
depth += 1;
|
|
62
|
+
}
|
|
63
|
+
else if (ch === '}') {
|
|
64
|
+
depth -= 1;
|
|
65
|
+
}
|
|
66
|
+
else if (ch === ',' && depth === 0) {
|
|
67
|
+
parts.push(value.slice(start, i));
|
|
68
|
+
start = i + 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
parts.push(value.slice(start));
|
|
72
|
+
return parts;
|
|
73
|
+
}
|
|
74
|
+
/** True when the (possibly multi-line) array body contains its closing `]`. */
|
|
75
|
+
function closesArray(value) {
|
|
76
|
+
let quote;
|
|
77
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
78
|
+
const ch = value[i];
|
|
79
|
+
if (quote) {
|
|
80
|
+
if (ch === quote && value[i - 1] !== '\\')
|
|
81
|
+
quote = undefined;
|
|
82
|
+
}
|
|
83
|
+
else if (ch === '"' || ch === "'") {
|
|
84
|
+
quote = ch;
|
|
85
|
+
}
|
|
86
|
+
else if (ch === ']') {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* PEP 621 / PEP 735 arrays contain requirement strings and
|
|
94
|
+
* `{ include-group = "name" }` tables. Taking the first quoted token of each
|
|
95
|
+
* entry avoids treating environment markers as package names.
|
|
96
|
+
*/
|
|
97
|
+
function arrayEntries(body) {
|
|
98
|
+
const requirements = [];
|
|
99
|
+
const includes = [];
|
|
100
|
+
for (const entry of splitTopLevel(body)) {
|
|
101
|
+
const trimmed = entry.trim();
|
|
102
|
+
if (trimmed.length === 0)
|
|
103
|
+
continue;
|
|
104
|
+
const include = /include-group\s*=\s*["']([^"']+)["']/.exec(trimmed);
|
|
105
|
+
if (include?.[1]) {
|
|
106
|
+
includes.push(include[1]);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const first = /^"([^"\\]*(?:\\.[^"\\]*)*)"|^'([^'\\]*(?:\\.[^'\\]*)*)'/.exec(trimmed);
|
|
110
|
+
const value = first?.[1] ?? first?.[2];
|
|
111
|
+
if (!value)
|
|
112
|
+
continue;
|
|
113
|
+
const name = requirementName(value);
|
|
114
|
+
if (name)
|
|
115
|
+
requirements.push(name);
|
|
116
|
+
}
|
|
117
|
+
return { requirements, includes };
|
|
118
|
+
}
|
|
119
|
+
const POETRY_GROUP = /^tool\.poetry\.group\.([^.]+)\.dependencies$/;
|
|
120
|
+
/** Key of a simple `key = value` / `key = [` line, including quoted keys. */
|
|
121
|
+
function tomlKey(line) {
|
|
122
|
+
const match = /^\s*(?:"([^"]+)"|'([^']+)'|([A-Za-z0-9_.-]+))\s*=/.exec(line);
|
|
123
|
+
return match?.[1] ?? match?.[2] ?? match?.[3];
|
|
124
|
+
}
|
|
125
|
+
function isPyprojectArraySection(section, key) {
|
|
126
|
+
if (section === 'project')
|
|
127
|
+
return key === 'dependencies';
|
|
128
|
+
if (section === 'project.optional-dependencies')
|
|
129
|
+
return true;
|
|
130
|
+
if (section === 'dependency-groups')
|
|
131
|
+
return true;
|
|
132
|
+
if (section === 'tool.uv')
|
|
133
|
+
return key === 'dev-dependencies';
|
|
134
|
+
if (section === 'build-system')
|
|
135
|
+
return key === 'requires';
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
/** Best-effort parse of the Python dependency declarations we can see. */
|
|
139
|
+
export function parsePythonManifestText(fileName, text) {
|
|
140
|
+
const names = [];
|
|
141
|
+
const base = path.basename(fileName).toLowerCase();
|
|
142
|
+
if (base.startsWith('requirements') && base.endsWith('.txt')) {
|
|
143
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
144
|
+
const line = rawLine.replace(/\s+#.*$/, '');
|
|
145
|
+
const name = requirementName(line);
|
|
146
|
+
if (name)
|
|
147
|
+
names.push(name);
|
|
148
|
+
}
|
|
149
|
+
return names;
|
|
150
|
+
}
|
|
151
|
+
if (base === 'pyproject.toml') {
|
|
152
|
+
const names = [];
|
|
153
|
+
const groups = new Map();
|
|
154
|
+
let section = '';
|
|
155
|
+
let pending;
|
|
156
|
+
const collect = (targetSection, key, body) => {
|
|
157
|
+
const entries = arrayEntries(body);
|
|
158
|
+
if (targetSection === 'dependency-groups') {
|
|
159
|
+
const group = groups.get(key) ?? { requirements: [], includes: [] };
|
|
160
|
+
group.requirements.push(...entries.requirements);
|
|
161
|
+
group.includes.push(...entries.includes);
|
|
162
|
+
groups.set(key, group);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
names.push(...entries.requirements);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
169
|
+
const line = stripTomlComments(rawLine);
|
|
170
|
+
if (pending) {
|
|
171
|
+
pending.buffer += '\n' + line;
|
|
172
|
+
if (closesArray(line)) {
|
|
173
|
+
collect(pending.section, pending.key, pending.buffer);
|
|
174
|
+
pending = undefined;
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const sectionMatch = /^\s*\[([^\]]+)\]\s*$/.exec(line);
|
|
179
|
+
if (sectionMatch?.[1]) {
|
|
180
|
+
section = sectionMatch[1].trim();
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const arrayStart = /^\s*(?:"([^"]+)"|'([^']+)'|([A-Za-z0-9_.-]+))\s*=\s*\[(.*)$/.exec(line);
|
|
184
|
+
if (arrayStart) {
|
|
185
|
+
const key = arrayStart[1] ?? arrayStart[2] ?? arrayStart[3] ?? '';
|
|
186
|
+
const rest = arrayStart[4] ?? '';
|
|
187
|
+
if (isPyprojectArraySection(section, key)) {
|
|
188
|
+
if (closesArray(rest))
|
|
189
|
+
collect(section, key, rest);
|
|
190
|
+
else
|
|
191
|
+
pending = { section, key, buffer: rest };
|
|
192
|
+
}
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (section === 'tool.poetry.dependencies' ||
|
|
196
|
+
section === 'tool.poetry.dev-dependencies' ||
|
|
197
|
+
POETRY_GROUP.test(section)) {
|
|
198
|
+
const key = tomlKey(line);
|
|
199
|
+
if (key && key.toLowerCase() !== 'python')
|
|
200
|
+
names.push(normalizePythonName(key));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// Resolve PEP 735 include-group references (with cycle protection).
|
|
204
|
+
const resolved = new Set();
|
|
205
|
+
const resolveGroup = (name, seen) => {
|
|
206
|
+
if (seen.has(name))
|
|
207
|
+
return;
|
|
208
|
+
seen.add(name);
|
|
209
|
+
const group = groups.get(name);
|
|
210
|
+
if (!group)
|
|
211
|
+
return;
|
|
212
|
+
for (const requirement of group.requirements)
|
|
213
|
+
resolved.add(requirement);
|
|
214
|
+
for (const include of group.includes)
|
|
215
|
+
resolveGroup(include, seen);
|
|
216
|
+
};
|
|
217
|
+
for (const name of groups.keys())
|
|
218
|
+
resolveGroup(name, new Set());
|
|
219
|
+
names.push(...resolved);
|
|
220
|
+
return [...new Set(names)];
|
|
221
|
+
}
|
|
222
|
+
if (base === 'setup.py') {
|
|
223
|
+
const blocks = [...text.matchAll(/install_requires\s*=\s*\[([\s\S]*?)\]/g)];
|
|
224
|
+
for (const block of blocks) {
|
|
225
|
+
for (const value of quotedStrings(block[1] ?? '')) {
|
|
226
|
+
const name = requirementName(value);
|
|
227
|
+
if (name)
|
|
228
|
+
names.push(name);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const extras = [...text.matchAll(/extras_require\s*=\s*\{([\s\S]*?)\}\s*[,)]/g)];
|
|
232
|
+
for (const block of extras) {
|
|
233
|
+
for (const value of quotedStrings(block[1] ?? '')) {
|
|
234
|
+
const name = requirementName(value);
|
|
235
|
+
if (name)
|
|
236
|
+
names.push(name);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return names;
|
|
240
|
+
}
|
|
241
|
+
if (base === 'setup.cfg') {
|
|
242
|
+
const sectionMatch = /\[options\]([\s\S]*?)(?=\n\[|$)/.exec(text);
|
|
243
|
+
if (sectionMatch?.[1]) {
|
|
244
|
+
const requires = /install_requires\s*=\s*([\s\S]*?)(?=\n\S|$)/.exec(sectionMatch[1]);
|
|
245
|
+
if (requires?.[1]) {
|
|
246
|
+
for (const rawLine of requires[1].split(/\r?\n/)) {
|
|
247
|
+
const name = requirementName(rawLine);
|
|
248
|
+
if (name)
|
|
249
|
+
names.push(name);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return names;
|
|
254
|
+
}
|
|
255
|
+
if (base === 'pipfile') {
|
|
256
|
+
const sectionMatch = /\[packages\]([\s\S]*?)(?=\n\[|$)/.exec(text);
|
|
257
|
+
if (sectionMatch?.[1]) {
|
|
258
|
+
for (const rawLine of sectionMatch[1].split(/\r?\n/)) {
|
|
259
|
+
const kv = /^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/.exec(rawLine);
|
|
260
|
+
if (kv?.[1])
|
|
261
|
+
names.push(normalizePythonName(kv[1]));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return names;
|
|
265
|
+
}
|
|
266
|
+
return names;
|
|
267
|
+
}
|
|
268
|
+
async function exists(p) {
|
|
269
|
+
try {
|
|
270
|
+
await stat(p);
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
async function readIfExists(p) {
|
|
278
|
+
try {
|
|
279
|
+
const text = await readFile(p, 'utf8');
|
|
280
|
+
return text.replace(/^\uFEFF/, '');
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Parse JSON without turning a broken manifest into a crash. */
|
|
287
|
+
function tryParseJson(text) {
|
|
288
|
+
try {
|
|
289
|
+
return JSON.parse(text);
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async function readDirSafe(dir) {
|
|
296
|
+
try {
|
|
297
|
+
return await readdir(dir, { withFileTypes: true });
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const PYTHON_MANIFESTS = [
|
|
304
|
+
'pyproject.toml', 'setup.py', 'setup.cfg', 'Pipfile', 'requirements.txt',
|
|
305
|
+
'requirements-dev.txt', 'requirements_dev.txt', 'dev-requirements.txt',
|
|
306
|
+
'requirements/base.txt', 'requirements/dev.txt', 'requirements/prod.txt',
|
|
307
|
+
'requirements/main.txt', 'requirements/test.txt', 'requirements/tests.txt',
|
|
308
|
+
];
|
|
309
|
+
async function loadPythonDeps(root) {
|
|
310
|
+
const declared = new Set();
|
|
311
|
+
const manifests = [];
|
|
312
|
+
for (const rel of PYTHON_MANIFESTS) {
|
|
313
|
+
const abs = path.join(root, rel);
|
|
314
|
+
const text = await readIfExists(abs);
|
|
315
|
+
if (text === undefined)
|
|
316
|
+
continue;
|
|
317
|
+
manifests.push(rel.replace(/\\/g, '/'));
|
|
318
|
+
for (const name of parsePythonManifestText(rel, text))
|
|
319
|
+
declared.add(name);
|
|
320
|
+
}
|
|
321
|
+
return { declared, manifests, localModules: new Set() };
|
|
322
|
+
}
|
|
323
|
+
function stripJsonComments(text) {
|
|
324
|
+
let out = '';
|
|
325
|
+
let inString = false;
|
|
326
|
+
let inLine = false;
|
|
327
|
+
let inBlock = false;
|
|
328
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
329
|
+
const ch = text[i];
|
|
330
|
+
const next = text[i + 1];
|
|
331
|
+
if (inLine) {
|
|
332
|
+
if (ch === '\n') {
|
|
333
|
+
inLine = false;
|
|
334
|
+
out += ch;
|
|
335
|
+
}
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (inBlock) {
|
|
339
|
+
if (ch === '*' && next === '/') {
|
|
340
|
+
inBlock = false;
|
|
341
|
+
i += 1;
|
|
342
|
+
}
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
if (inString) {
|
|
346
|
+
out += ch;
|
|
347
|
+
if (ch === '\\') {
|
|
348
|
+
out += next ?? '';
|
|
349
|
+
i += 1;
|
|
350
|
+
}
|
|
351
|
+
else if (ch === '"') {
|
|
352
|
+
inString = false;
|
|
353
|
+
}
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (ch === '"') {
|
|
357
|
+
inString = true;
|
|
358
|
+
out += ch;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (ch === '/' && next === '/') {
|
|
362
|
+
inLine = true;
|
|
363
|
+
i += 1;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (ch === '/' && next === '*') {
|
|
367
|
+
inBlock = true;
|
|
368
|
+
i += 1;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
out += ch;
|
|
372
|
+
}
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
async function loadTsconfigAliases(root) {
|
|
376
|
+
const aliases = [];
|
|
377
|
+
for (const name of ['tsconfig.json', 'tsconfig.base.json']) {
|
|
378
|
+
const text = await readIfExists(path.join(root, name));
|
|
379
|
+
if (!text)
|
|
380
|
+
continue;
|
|
381
|
+
const parsed = tryParseJson(stripJsonComments(text));
|
|
382
|
+
if (!parsed)
|
|
383
|
+
continue;
|
|
384
|
+
const paths = parsed.compilerOptions?.paths;
|
|
385
|
+
if (paths && typeof paths === 'object') {
|
|
386
|
+
for (const key of Object.keys(paths)) {
|
|
387
|
+
const cleaned = key.replace(/\/\*$/, '').replace(/\*$/, '');
|
|
388
|
+
if (cleaned)
|
|
389
|
+
aliases.push(cleaned);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return aliases;
|
|
394
|
+
}
|
|
395
|
+
async function loadInstalledPackages(root) {
|
|
396
|
+
const installed = new Set();
|
|
397
|
+
const modulesDir = path.join(root, 'node_modules');
|
|
398
|
+
const entries = await readDirSafe(modulesDir);
|
|
399
|
+
if (!entries)
|
|
400
|
+
return installed;
|
|
401
|
+
for (const entry of entries) {
|
|
402
|
+
if (entry.name.startsWith('.'))
|
|
403
|
+
continue;
|
|
404
|
+
if (entry.name.startsWith('@')) {
|
|
405
|
+
if (!entry.isDirectory())
|
|
406
|
+
continue;
|
|
407
|
+
const scoped = await readDirSafe(path.join(modulesDir, entry.name));
|
|
408
|
+
if (!scoped)
|
|
409
|
+
continue;
|
|
410
|
+
for (const child of scoped) {
|
|
411
|
+
if (child.isDirectory())
|
|
412
|
+
installed.add(`${entry.name}/${child.name}`);
|
|
413
|
+
}
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (entry.isDirectory())
|
|
417
|
+
installed.add(entry.name);
|
|
418
|
+
}
|
|
419
|
+
return installed;
|
|
420
|
+
}
|
|
421
|
+
async function loadWorkspaceNames(root) {
|
|
422
|
+
const names = new Set();
|
|
423
|
+
const dependencies = new Set();
|
|
424
|
+
const visited = new Set();
|
|
425
|
+
async function scan(dir, depth) {
|
|
426
|
+
if (depth > 3 || visited.has(dir))
|
|
427
|
+
return;
|
|
428
|
+
visited.add(dir);
|
|
429
|
+
const pkgPath = path.join(dir, 'package.json');
|
|
430
|
+
const text = await readIfExists(pkgPath);
|
|
431
|
+
const parsed = text ? tryParseJson(text) : undefined;
|
|
432
|
+
if (parsed) {
|
|
433
|
+
if (typeof parsed.name === 'string' && parsed.name.length > 0)
|
|
434
|
+
names.add(parsed.name);
|
|
435
|
+
for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
|
436
|
+
const deps = parsed[field];
|
|
437
|
+
if (deps && typeof deps === 'object') {
|
|
438
|
+
for (const name of Object.keys(deps))
|
|
439
|
+
dependencies.add(name);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const entries = await readDirSafe(dir);
|
|
444
|
+
if (!entries)
|
|
445
|
+
return;
|
|
446
|
+
for (const entry of entries) {
|
|
447
|
+
if (!entry.isDirectory())
|
|
448
|
+
continue;
|
|
449
|
+
if (entry.name === 'node_modules' || entry.name.startsWith('.'))
|
|
450
|
+
continue;
|
|
451
|
+
await scan(path.join(dir, entry.name), depth + 1);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
await scan(root, 0);
|
|
455
|
+
return { names, dependencies };
|
|
456
|
+
}
|
|
457
|
+
async function loadNodeDeps(root) {
|
|
458
|
+
const declared = new Set();
|
|
459
|
+
const pkgPath = path.join(root, 'package.json');
|
|
460
|
+
const hasPackageJson = await exists(pkgPath);
|
|
461
|
+
const text = await readIfExists(pkgPath);
|
|
462
|
+
const pkg = text ? tryParseJson(text) : undefined;
|
|
463
|
+
if (pkg) {
|
|
464
|
+
for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
|
465
|
+
const deps = pkg[field];
|
|
466
|
+
if (deps && typeof deps === 'object') {
|
|
467
|
+
for (const name of Object.keys(deps))
|
|
468
|
+
declared.add(name);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
const [aliases, installed, workspaceInfo] = await Promise.all([
|
|
473
|
+
loadTsconfigAliases(root),
|
|
474
|
+
loadInstalledPackages(root),
|
|
475
|
+
loadWorkspaceNames(root),
|
|
476
|
+
]);
|
|
477
|
+
for (const dependency of workspaceInfo.dependencies)
|
|
478
|
+
declared.add(dependency);
|
|
479
|
+
return {
|
|
480
|
+
declared,
|
|
481
|
+
workspaces: workspaceInfo.names,
|
|
482
|
+
aliases,
|
|
483
|
+
hasPackageJson,
|
|
484
|
+
installed,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
export async function loadProjectDeps(root) {
|
|
488
|
+
const absRoot = path.resolve(root);
|
|
489
|
+
const [python, node] = await Promise.all([loadPythonDeps(absRoot), loadNodeDeps(absRoot)]);
|
|
490
|
+
return { root: absRoot, python, node, ignoredModules: new Set() };
|
|
491
|
+
}
|
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>;
|