code-guardian-ai 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +24 -0
- package/LICENSE +21 -0
- package/README.md +774 -2
- package/bin/cli.js +2 -0
- package/dist/build-manifest.json +136 -0
- package/dist/cli/commands/check.js +56 -0
- package/dist/cli/commands/doctor.js +55 -0
- package/dist/cli/commands/find.js +31 -0
- package/dist/cli/commands/init-files.js +13 -0
- package/dist/cli/commands/init.js +50 -0
- package/dist/cli/commands/optimize.js +53 -0
- package/dist/cli/commands/scan.js +47 -0
- package/dist/cli/index.js +62 -0
- package/dist/cli/output.js +4 -0
- package/dist/context.js +11 -0
- package/dist/core/ast/analysis.js +455 -0
- package/dist/core/ast/cached-info.js +46 -0
- package/dist/core/ast/discovery.js +485 -0
- package/dist/core/ast/extract-js.js +531 -0
- package/dist/core/ast/extract-py.js +298 -0
- package/dist/core/ast/graph.js +92 -0
- package/dist/core/ast/health.js +338 -0
- package/dist/core/ast/indexer.js +286 -0
- package/dist/core/ast/packages.js +343 -0
- package/dist/core/ast/parser.js +91 -0
- package/dist/core/ast/report.js +154 -0
- package/dist/core/ast/resolver.js +392 -0
- package/dist/core/ast/rules.js +37 -0
- package/dist/core/ast/scope-js.js +191 -0
- package/dist/core/ast/shared-index.js +131 -0
- package/dist/core/ast/tsconfig.js +301 -0
- package/dist/core/ast/types.js +1 -0
- package/dist/core/ast/util.js +156 -0
- package/dist/core/git/git.js +430 -0
- package/dist/core/llm/policy.js +35 -0
- package/dist/core/llm/provider.js +199 -0
- package/dist/core/prompt/compressor.js +118 -0
- package/dist/core/prompt/criteria.js +36 -0
- package/dist/core/prompt/find.js +47 -0
- package/dist/core/prompt/guidelines.js +22 -0
- package/dist/core/prompt/model-output.js +178 -0
- package/dist/core/prompt/refiner.js +194 -0
- package/dist/core/prompt/relevance.js +265 -0
- package/dist/core/prompt/untrusted.js +57 -0
- package/dist/core/security/masker.js +330 -0
- package/dist/core/security/outbound.js +40 -0
- package/dist/core/security/safe-file.js +396 -0
- package/dist/core/security/sandbox.js +181 -0
- package/dist/core/validation/baseline.js +121 -0
- package/dist/core/validation/delta.js +243 -0
- package/dist/core/validation/policy.js +123 -0
- package/dist/db/cache-key.js +221 -0
- package/dist/db/client.js +563 -0
- package/dist/db/schema-check.js +149 -0
- package/dist/db/schema.js +36 -0
- package/dist/db/windows-acl.js +150 -0
- package/dist/index.js +55 -0
- package/dist/mcp/server.js +20 -0
- package/dist/mcp/tools/find-reusable.js +22 -0
- package/dist/mcp/tools/get-context.js +39 -0
- package/dist/mcp/tools/optimize-prompt.js +31 -0
- package/dist/mcp/tools/result.js +31 -0
- package/dist/mcp/tools/validate-rules.js +28 -0
- package/dist/utils/config.js +117 -0
- package/dist/utils/errors.js +9 -0
- package/dist/utils/logger.js +19 -0
- package/dist/utils/pace.js +19 -0
- package/dist/utils/text.js +22 -0
- package/dist/utils/tokens.js +14 -0
- package/package.json +69 -4
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { builtinModules } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { compareText } from '../../utils/text.js';
|
|
4
|
+
import { packageEntries, packageEntry, publicTargets, resolveMap } from './packages.js';
|
|
5
|
+
import { configCovers } from './tsconfig.js';
|
|
6
|
+
const SOURCE_EXTS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
7
|
+
const TS_FOR_JS = { '.js': ['.ts', '.tsx'], '.jsx': ['.tsx'], '.mjs': ['.mts'], '.cjs': ['.cts'] };
|
|
8
|
+
const ASSET_EXT = /\.(?:css|scss|sass|less|styl|json|svg|png|jpe?g|gif|webp|avif|ico|bmp|woff2?|ttf|otf|eot|md|mdx|html?|txt|wasm|node|graphql|gql|ya?ml|toml|csv|vue|svelte|astro|mp[34]|webm|pdf)$/i;
|
|
9
|
+
const PACKAGE_NAME = /^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*(?:\/.*)?$/i;
|
|
10
|
+
const NONE = { targets: [] };
|
|
11
|
+
// A missing target under a build directory or any hidden tool directory (.next, .source, .nuxt, .svelte-kit, ...) is generated output.
|
|
12
|
+
// Python standard library module names (3.12). A name from this list is external even when a directory of the same name
|
|
13
|
+
// exists in the project, such as tests/typing/.
|
|
14
|
+
const PYTHON_STDLIB = new Set('abc aifc antigravity argparse array ast asyncio atexit audioop base64 bdb binascii bisect builtins bz2 cProfile calendar cgi cgitb chunk cmath cmd code codecs codeop collections colorsys compileall concurrent configparser contextlib contextvars copy copyreg crypt csv ctypes curses dataclasses datetime dbm decimal difflib dis doctest email encodings ensurepip enum errno faulthandler fcntl filecmp fileinput fnmatch fractions ftplib functools gc genericpath getopt getpass gettext glob graphlib grp gzip hashlib heapq hmac html http idlelib imaplib imghdr importlib inspect io ipaddress itertools json keyword lib2to3 linecache locale logging lzma mailbox mailcap marshal math mimetypes mmap modulefinder msilib msvcrt multiprocessing netrc nis nntplib nt ntpath nturl2path numbers opcode operator optparse os ossaudiodev pathlib pdb pickle pickletools pipes pkgutil platform plistlib poplib posix posixpath pprint profile pstats pty pwd py_compile pyclbr pydoc pydoc_data pyexpat queue quopri random re readline reprlib resource rlcompleter runpy sched secrets select selectors shelve shlex shutil signal site smtplib sndhdr socket socketserver spwd sqlite3 sre_compile sre_constants sre_parse ssl stat statistics string stringprep struct subprocess sunau symtable sys sysconfig syslog tabnanny tarfile telnetlib tempfile termios textwrap this threading time timeit tkinter token tokenize tomllib trace traceback tracemalloc tty turtle turtledemo types typing unicodedata unittest urllib uu uuid venv warnings wave weakref webbrowser winreg winsound wsgiref xdrlib xml xmlrpc zipapp zipfile zipimport zlib zoneinfo'.split(' '));
|
|
15
|
+
const BUILD_OUTPUT = /(^|\/)(?:dist|build|out|\.(?!\.?(?:\/|$))[\w-]+)(\/|$)/;
|
|
16
|
+
const ALIAS_LIKE = /^(?:[@~$#]{1,2}(?:\/|$)|\$[a-z])/i;
|
|
17
|
+
const dirOf = (p) => {
|
|
18
|
+
const dir = path.posix.dirname(p);
|
|
19
|
+
return dir === '.' ? '' : dir;
|
|
20
|
+
};
|
|
21
|
+
function joinPath(base, relative) {
|
|
22
|
+
// A trailing slash ('./lib/', '../../') names the directory itself; keep it out of the path so index lookups work.
|
|
23
|
+
const joined = path.posix.normalize(path.posix.join(base || '.', relative)).replace(/\/+$/, '');
|
|
24
|
+
if (joined === '..' || joined.startsWith('../') || path.posix.isAbsolute(joined))
|
|
25
|
+
return null;
|
|
26
|
+
return joined === '.' || joined === '' ? '' : joined;
|
|
27
|
+
}
|
|
28
|
+
function matchPattern(pattern, spec) {
|
|
29
|
+
const star = pattern.indexOf('*');
|
|
30
|
+
if (star === -1)
|
|
31
|
+
return pattern === spec ? { capture: '', score: pattern.length + 1 } : null;
|
|
32
|
+
const prefix = pattern.slice(0, star);
|
|
33
|
+
const suffix = pattern.slice(star + 1);
|
|
34
|
+
if (spec.length < prefix.length + suffix.length || !spec.startsWith(prefix) || !spec.endsWith(suffix))
|
|
35
|
+
return null;
|
|
36
|
+
return { capture: spec.slice(prefix.length, spec.length - suffix.length), score: prefix.length };
|
|
37
|
+
}
|
|
38
|
+
const isRelative = (spec) => spec === '.' || spec === '..' || spec.startsWith('./') || spec.startsWith('../');
|
|
39
|
+
const isHit = (hit) => hit !== null;
|
|
40
|
+
function distinctHits(hits) {
|
|
41
|
+
return [...new Map(hits.map((h) => [h.file, h])).values()].sort((a, b) => compareText(a.file, b.file));
|
|
42
|
+
}
|
|
43
|
+
export function createResolver(options) {
|
|
44
|
+
const indexed = new Set(options.files);
|
|
45
|
+
const known = new Set([...(options.known ?? []), ...indexed]);
|
|
46
|
+
const dirs = new Set(['']);
|
|
47
|
+
for (const file of known) {
|
|
48
|
+
for (let dir = dirOf(file); dir && !dirs.has(dir); dir = dirOf(dir))
|
|
49
|
+
dirs.add(dir);
|
|
50
|
+
}
|
|
51
|
+
const configsByDir = new Map();
|
|
52
|
+
for (const config of options.tsconfigs)
|
|
53
|
+
configsByDir.set(config.dir, [...(configsByDir.get(config.dir) ?? []), config]);
|
|
54
|
+
const pythonRoots = [...new Set(options.pythonRoots.map((r) => joinPath('', r) ?? ''))];
|
|
55
|
+
// The tsconfigs whose include/exclude actually cover this file, deepest directory first. When none claims the file
|
|
56
|
+
// (a config without an include that excludes it, a solution-style root) fall back to the nearest directory's configs.
|
|
57
|
+
const configsFor = (file) => {
|
|
58
|
+
const ancestors = [];
|
|
59
|
+
for (let dir = dirOf(file);; dir = dirOf(dir)) {
|
|
60
|
+
ancestors.push(dir);
|
|
61
|
+
if (dir === '')
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
for (const dir of ancestors) {
|
|
65
|
+
const covering = (configsByDir.get(dir) ?? []).filter((c) => configCovers(c, file));
|
|
66
|
+
if (covering.length > 0)
|
|
67
|
+
return covering;
|
|
68
|
+
}
|
|
69
|
+
for (const dir of ancestors) {
|
|
70
|
+
const found = configsByDir.get(dir);
|
|
71
|
+
if (found)
|
|
72
|
+
return found;
|
|
73
|
+
}
|
|
74
|
+
return [];
|
|
75
|
+
};
|
|
76
|
+
const packages = options.packages ?? [];
|
|
77
|
+
const named = new Map();
|
|
78
|
+
for (const pkg of packages)
|
|
79
|
+
if (pkg.name && !named.has(pkg.name))
|
|
80
|
+
named.set(pkg.name, pkg);
|
|
81
|
+
const packageFor = (file) => {
|
|
82
|
+
let best;
|
|
83
|
+
for (const pkg of packages) {
|
|
84
|
+
if ((pkg.dir === '' || file.startsWith(`${pkg.dir}/`)) && (!best || pkg.dir.length > best.dir.length))
|
|
85
|
+
best = pkg;
|
|
86
|
+
}
|
|
87
|
+
return best;
|
|
88
|
+
};
|
|
89
|
+
const lookup = (p) => (indexed.has(p) ? { file: p, indexed: true } : known.has(p) ? { file: p, indexed: false } : null);
|
|
90
|
+
const firstHit = (candidates) => {
|
|
91
|
+
for (const candidate of candidates) {
|
|
92
|
+
const hit = lookup(candidate);
|
|
93
|
+
if (hit)
|
|
94
|
+
return hit;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
};
|
|
98
|
+
function unresolved(spec, reason, candidates = []) {
|
|
99
|
+
return { targets: [], unresolved: { spec, reason, candidates } };
|
|
100
|
+
}
|
|
101
|
+
function settle(hits, spec, names) {
|
|
102
|
+
const distinct = distinctHits(hits);
|
|
103
|
+
if (distinct.length === 0)
|
|
104
|
+
return unresolved(spec, 'not-found');
|
|
105
|
+
if (distinct.length > 1)
|
|
106
|
+
return unresolved(spec, 'ambiguous', distinct.map((h) => h.file));
|
|
107
|
+
const [hit] = distinct;
|
|
108
|
+
return hit.indexed ? { targets: [{ file: hit.file, names }] } : unresolved(spec, 'not-indexed', [hit.file]);
|
|
109
|
+
}
|
|
110
|
+
function probeJs(base) {
|
|
111
|
+
if (base === null)
|
|
112
|
+
return null;
|
|
113
|
+
const ext = path.posix.extname(base);
|
|
114
|
+
const stem = ext ? base.slice(0, -ext.length) : base;
|
|
115
|
+
const prefix = base ? `${base}/` : '';
|
|
116
|
+
const candidates = [
|
|
117
|
+
...(TS_FOR_JS[ext] ?? []).map((e) => stem + e),
|
|
118
|
+
...(SOURCE_EXTS.includes(ext) ? [base] : []),
|
|
119
|
+
...SOURCE_EXTS.map((e) => base + e),
|
|
120
|
+
...SOURCE_EXTS.map((e) => `${prefix}index${e}`),
|
|
121
|
+
];
|
|
122
|
+
return firstHit(candidates);
|
|
123
|
+
}
|
|
124
|
+
function viaConfig(config, spec) {
|
|
125
|
+
let best = null;
|
|
126
|
+
for (const mapping of config.paths) {
|
|
127
|
+
const match = matchPattern(mapping.pattern, spec);
|
|
128
|
+
if (match && (!best || match.score > best.score))
|
|
129
|
+
best = { targets: mapping.targets, ...match };
|
|
130
|
+
}
|
|
131
|
+
const matched = best !== null;
|
|
132
|
+
// A target inside a build or hidden tool directory that is not in the repository (dist/, .source/) is generated output.
|
|
133
|
+
let generated = false;
|
|
134
|
+
for (const target of best?.targets ?? []) {
|
|
135
|
+
const path = joinPath(config.pathsBase, target.replace('*', best?.capture ?? ''));
|
|
136
|
+
const hit = probeJs(path);
|
|
137
|
+
if (hit)
|
|
138
|
+
return { matched, hit, generated: false };
|
|
139
|
+
if (path !== null && BUILD_OUTPUT.test(path))
|
|
140
|
+
generated = true;
|
|
141
|
+
}
|
|
142
|
+
if (config.baseUrl !== undefined) {
|
|
143
|
+
const hit = probeJs(joinPath(config.baseUrl, spec));
|
|
144
|
+
if (hit)
|
|
145
|
+
return { matched, hit, generated: false };
|
|
146
|
+
}
|
|
147
|
+
return { matched, hit: null, generated };
|
|
148
|
+
}
|
|
149
|
+
function resolveJs(from, imp) {
|
|
150
|
+
const spec = imp.source.replace(/(?!^)[?#].*$/, '');
|
|
151
|
+
if (!spec || ASSET_EXT.test(spec) || spec.startsWith('node:'))
|
|
152
|
+
return NONE;
|
|
153
|
+
if (isRelative(spec))
|
|
154
|
+
return resolveRelative(from, imp, spec);
|
|
155
|
+
if (spec.startsWith('/'))
|
|
156
|
+
return NONE;
|
|
157
|
+
const results = configsFor(from.path).map((config) => viaConfig(config, spec));
|
|
158
|
+
const hits = results.map((r) => r.hit).filter(isHit);
|
|
159
|
+
if (hits.length > 0)
|
|
160
|
+
return settle(hits, imp.source, imp.names);
|
|
161
|
+
if (results.some((r) => r.matched))
|
|
162
|
+
return unresolved(imp.source, results.some((r) => r.generated) ? 'unsupported' : 'not-found');
|
|
163
|
+
if (spec.startsWith('#'))
|
|
164
|
+
return viaImportsField(from, imp, spec);
|
|
165
|
+
const workspace = viaWorkspacePackage(from, imp, spec);
|
|
166
|
+
if (workspace)
|
|
167
|
+
return workspace;
|
|
168
|
+
const framework = viaFrameworkAlias(from, imp, spec);
|
|
169
|
+
if (framework)
|
|
170
|
+
return framework;
|
|
171
|
+
const [top] = spec.split('/');
|
|
172
|
+
if (PACKAGE_NAME.test(spec) || builtinModules.includes(top))
|
|
173
|
+
return NONE;
|
|
174
|
+
// `@/x`, `~/x`, `$lib/x`: an alias that only a bundler config could define.
|
|
175
|
+
return ALIAS_LIKE.test(spec) ? unresolved(imp.source, 'unsupported') : unresolved(imp.source, 'not-found');
|
|
176
|
+
}
|
|
177
|
+
// `require('../..')` or `import './lib'` where the directory is a package: its main/exports wins over a guessed index file.
|
|
178
|
+
function resolveRelative(from, imp, spec) {
|
|
179
|
+
const base = joinPath(dirOf(from.path), spec);
|
|
180
|
+
const direct = probeJs(base);
|
|
181
|
+
if (direct)
|
|
182
|
+
return settle([direct], imp.source, imp.names);
|
|
183
|
+
const pkg = base === null ? undefined : packages.find((p) => p.dir === base);
|
|
184
|
+
const entry = pkg ? packageEntry(pkg, '.') : undefined;
|
|
185
|
+
const viaPackage = pkg && entry ? probePackageTarget(pkg, entry) : null;
|
|
186
|
+
if (viaPackage)
|
|
187
|
+
return settle([viaPackage], imp.source, imp.names);
|
|
188
|
+
// Pointing into a build directory that is not in the repository (dist/, build/) says nothing about the source being broken.
|
|
189
|
+
if (base !== null && BUILD_OUTPUT.test(base))
|
|
190
|
+
return unresolved(imp.source, 'unsupported');
|
|
191
|
+
return settle([], imp.source, imp.names);
|
|
192
|
+
}
|
|
193
|
+
// Maps a package-relative target (often a built dist file) back to the places its source could be.
|
|
194
|
+
function candidateBases(pkg, target) {
|
|
195
|
+
const direct = joinPath(pkg.dir, target.replace(/\.d\.([cm]?)ts$/, '.$1ts'));
|
|
196
|
+
if (direct === null)
|
|
197
|
+
return [];
|
|
198
|
+
const bases = [direct];
|
|
199
|
+
const config = configsFor(`${pkg.dir ? `${pkg.dir}/` : ''}x.ts`).find((c) => c.outDir !== undefined);
|
|
200
|
+
if (config?.outDir !== undefined && (direct === config.outDir || direct.startsWith(`${config.outDir}/`))) {
|
|
201
|
+
const mapped = joinPath(config.rootDir ?? (config.dir ? `${config.dir}/src` : 'src'), direct.slice(config.outDir.length + 1));
|
|
202
|
+
if (mapped !== null)
|
|
203
|
+
bases.push(mapped);
|
|
204
|
+
}
|
|
205
|
+
const relative = direct.slice(pkg.dir ? pkg.dir.length + 1 : 0);
|
|
206
|
+
const swapped = relative.replace(/^(?:dist|build|lib|out|esm|cjs|types)\//, 'src/');
|
|
207
|
+
if (swapped !== relative) {
|
|
208
|
+
const base = joinPath(pkg.dir, swapped);
|
|
209
|
+
if (base !== null)
|
|
210
|
+
bases.push(base);
|
|
211
|
+
}
|
|
212
|
+
return bases;
|
|
213
|
+
}
|
|
214
|
+
function probePackageTarget(pkg, target) {
|
|
215
|
+
for (const base of candidateBases(pkg, target)) {
|
|
216
|
+
const hit = probeJs(base);
|
|
217
|
+
if (hit)
|
|
218
|
+
return hit;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
function viaWorkspacePackage(from, imp, spec) {
|
|
223
|
+
const segments = spec.split('/');
|
|
224
|
+
const nameLength = spec.startsWith('@') ? 2 : 1;
|
|
225
|
+
const name = segments.slice(0, nameLength).join('/');
|
|
226
|
+
const pkg = named.get(name);
|
|
227
|
+
if (!pkg)
|
|
228
|
+
return null;
|
|
229
|
+
const rest = segments.slice(nameLength).join('/');
|
|
230
|
+
const subpath = rest ? `./${rest}` : '.';
|
|
231
|
+
// A package that exists in this workspace but whose entry we cannot map to source is not claimed to be broken.
|
|
232
|
+
for (const target of packageEntries(pkg, subpath)) {
|
|
233
|
+
const hit = probePackageTarget(pkg, target);
|
|
234
|
+
if (hit)
|
|
235
|
+
return settle([hit], imp.source, imp.names);
|
|
236
|
+
}
|
|
237
|
+
return unresolved(imp.source, 'unsupported', [from.path]);
|
|
238
|
+
}
|
|
239
|
+
function viaImportsField(from, imp, spec) {
|
|
240
|
+
const pkg = packageFor(from.path);
|
|
241
|
+
if (!pkg)
|
|
242
|
+
return unresolved(imp.source, 'unsupported');
|
|
243
|
+
if (!pkg.imports)
|
|
244
|
+
return unresolved(imp.source, 'not-found');
|
|
245
|
+
const target = resolveMap(pkg.imports, spec);
|
|
246
|
+
if (!target)
|
|
247
|
+
return unresolved(imp.source, 'not-found');
|
|
248
|
+
if (!target.startsWith('.'))
|
|
249
|
+
return NONE;
|
|
250
|
+
const hit = probePackageTarget(pkg, target);
|
|
251
|
+
return hit ? settle([hit], imp.source, imp.names) : unresolved(imp.source, 'unsupported');
|
|
252
|
+
}
|
|
253
|
+
function viaFrameworkAlias(from, imp, spec) {
|
|
254
|
+
const pkg = packageFor(from.path);
|
|
255
|
+
const frameworks = pkg?.frameworks ?? [];
|
|
256
|
+
const root = pkg?.dir ?? '';
|
|
257
|
+
const rules = [];
|
|
258
|
+
if (frameworks.includes('sveltekit'))
|
|
259
|
+
rules.push([/^\$lib(?:\/|$)/, [joinPath(root, 'src/lib') ?? 'src/lib']]);
|
|
260
|
+
if (frameworks.includes('nuxt')) {
|
|
261
|
+
const app = known.has(`${root ? `${root}/` : ''}app`) || dirs.has(root ? `${root}/app` : 'app') ? 'app' : '';
|
|
262
|
+
rules.push([/^[~@](?:\/|$)/, [joinPath(root, app) ?? root]], [/^(?:~~|@@)(?:\/|$)/, [root]]);
|
|
263
|
+
}
|
|
264
|
+
for (const [pattern, [base]] of rules) {
|
|
265
|
+
if (!pattern.test(spec))
|
|
266
|
+
continue;
|
|
267
|
+
const rest = spec.replace(/^(?:\$lib|~~|@@|[~@])\/?/, '');
|
|
268
|
+
const hit = probeJs(joinPath(base, rest));
|
|
269
|
+
return hit ? settle([hit], imp.source, imp.names) : unresolved(imp.source, 'not-found');
|
|
270
|
+
}
|
|
271
|
+
// SvelteKit and Nuxt also expose virtual modules ($app/*, #imports) that are not files.
|
|
272
|
+
if ((frameworks.includes('sveltekit') && /^\$(?:app|env)\//.test(spec)) || (frameworks.includes('nuxt') && /^#(?:imports|app|build)/.test(spec)))
|
|
273
|
+
return NONE;
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
function modulePath(base, segments) {
|
|
277
|
+
const joined = [base, ...segments].filter(Boolean).join('/');
|
|
278
|
+
if (segments.length === 0)
|
|
279
|
+
return lookup(joined ? `${joined}/__init__.py` : '__init__.py');
|
|
280
|
+
return firstHit([`${joined}.py`, `${joined}/__init__.py`]);
|
|
281
|
+
}
|
|
282
|
+
function isLocalTopLevel(first, bases) {
|
|
283
|
+
return bases.some((base) => {
|
|
284
|
+
const candidate = base ? `${base}/${first}` : first;
|
|
285
|
+
return known.has(`${candidate}.py`) || dirs.has(candidate);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
function resolvePython(from, imp) {
|
|
289
|
+
const dots = /^\.*/.exec(imp.source)?.[0].length ?? 0;
|
|
290
|
+
const segments = imp.source.slice(dots).split('.').filter(Boolean);
|
|
291
|
+
const submodules = imp.names.filter((n) => n !== '*');
|
|
292
|
+
const attempt = (base) => {
|
|
293
|
+
const subs = new Map();
|
|
294
|
+
for (const name of submodules) {
|
|
295
|
+
const hit = modulePath(base, [...segments, name]);
|
|
296
|
+
if (hit)
|
|
297
|
+
subs.set(name, hit);
|
|
298
|
+
}
|
|
299
|
+
return { module: modulePath(base, segments), subs };
|
|
300
|
+
};
|
|
301
|
+
const settleTier = (bases) => {
|
|
302
|
+
const attempts = bases.map(attempt);
|
|
303
|
+
const modules = distinctHits(attempts.map((a) => a.module).filter(isHit));
|
|
304
|
+
const bySub = new Map();
|
|
305
|
+
for (const found of attempts) {
|
|
306
|
+
for (const [name, hit] of found.subs)
|
|
307
|
+
bySub.set(name, [...(bySub.get(name) ?? []), hit]);
|
|
308
|
+
}
|
|
309
|
+
if (modules.length === 0 && bySub.size === 0)
|
|
310
|
+
return null;
|
|
311
|
+
const conflicting = modules.length > 1 ? modules : [...bySub.values()].map(distinctHits).find((hits) => hits.length > 1);
|
|
312
|
+
if (conflicting)
|
|
313
|
+
return unresolved(imp.source, 'ambiguous', conflicting.map((h) => h.file));
|
|
314
|
+
const targets = [];
|
|
315
|
+
const [module] = modules;
|
|
316
|
+
// `from . import types` / `from pkg import sub` where every name is a submodule loads those submodules; the package's
|
|
317
|
+
// own __init__ is only implied (a child always follows its parent), so it is not an edge, and it cannot form a cycle.
|
|
318
|
+
const onlySubmodules = submodules.length > 0 && submodules.length === imp.names.length && submodules.every((n) => bySub.has(n));
|
|
319
|
+
if (module?.indexed && !(onlySubmodules && module.file.endsWith('/__init__.py') || onlySubmodules && module.file === '__init__.py'))
|
|
320
|
+
targets.push({ file: module.file, names: imp.names });
|
|
321
|
+
for (const hits of bySub.values()) {
|
|
322
|
+
const [hit] = distinctHits(hits);
|
|
323
|
+
if (hit?.indexed)
|
|
324
|
+
targets.push({ file: hit.file, names: ['*'] });
|
|
325
|
+
}
|
|
326
|
+
return targets.length > 0 ? { targets } : unresolved(imp.source, 'not-indexed');
|
|
327
|
+
};
|
|
328
|
+
const fromDir = dirOf(from.path);
|
|
329
|
+
if (dots > 0) {
|
|
330
|
+
let base = fromDir;
|
|
331
|
+
const depth = base ? base.split('/').length : 0;
|
|
332
|
+
if (dots - 1 > depth)
|
|
333
|
+
return unresolved(imp.source, 'not-found');
|
|
334
|
+
for (let i = 1; i < dots; i++)
|
|
335
|
+
base = dirOf(base);
|
|
336
|
+
return settleTier([base]) ?? unresolved(imp.source, 'not-found');
|
|
337
|
+
}
|
|
338
|
+
const found = settleTier(pythonRoots) ?? settleTier([fromDir]);
|
|
339
|
+
if (found)
|
|
340
|
+
return found;
|
|
341
|
+
if (segments.length === 0)
|
|
342
|
+
return NONE;
|
|
343
|
+
if (PYTHON_STDLIB.has(segments[0]))
|
|
344
|
+
return NONE;
|
|
345
|
+
if (!isLocalTopLevel(segments[0], [...pythonRoots, fromDir]))
|
|
346
|
+
return NONE;
|
|
347
|
+
// pkg.mod.attr where pkg/mod.py is a plain module: Python only allows that when the module registers the name in
|
|
348
|
+
// sys.modules at run time (requests.packages does), which static analysis cannot see.
|
|
349
|
+
for (let k = segments.length - 1; k >= 1; k--) {
|
|
350
|
+
const prefix = segments.slice(0, k).join('/');
|
|
351
|
+
for (const base of [...pythonRoots, fromDir]) {
|
|
352
|
+
const candidate = base ? `${base}/${prefix}` : prefix;
|
|
353
|
+
if (known.has(`${candidate}.py`) && !dirs.has(candidate))
|
|
354
|
+
return unresolved(imp.source, 'unsupported');
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return unresolved(imp.source, 'not-found');
|
|
358
|
+
}
|
|
359
|
+
// Source files a package exposes to the outside (exports, main, module, types, bin). Their exports are API, not dead code.
|
|
360
|
+
function publicEntries() {
|
|
361
|
+
const found = new Set();
|
|
362
|
+
for (const pkg of packages) {
|
|
363
|
+
for (const target of publicTargets(pkg)) {
|
|
364
|
+
if (target.includes('*')) {
|
|
365
|
+
const prefix = target.slice(0, target.indexOf('*'));
|
|
366
|
+
for (const base of candidateBases(pkg, prefix))
|
|
367
|
+
for (const file of indexed)
|
|
368
|
+
if (file.startsWith(base))
|
|
369
|
+
found.add(file);
|
|
370
|
+
}
|
|
371
|
+
else if (target.endsWith('.py')) {
|
|
372
|
+
// A pyproject.toml console script: the module file itself, no extension probing.
|
|
373
|
+
const file = joinPath(pkg.dir, target);
|
|
374
|
+
if (file !== null && indexed.has(file))
|
|
375
|
+
found.add(file);
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
const hit = probePackageTarget(pkg, target);
|
|
379
|
+
if (hit?.indexed)
|
|
380
|
+
found.add(hit.file);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return [...found].sort(compareText);
|
|
385
|
+
}
|
|
386
|
+
const resolve = (from, imp) => {
|
|
387
|
+
if (!imp.source)
|
|
388
|
+
return NONE;
|
|
389
|
+
return from.lang === 'python' ? resolvePython(from, imp) : resolveJs(from, imp);
|
|
390
|
+
};
|
|
391
|
+
return Object.assign(resolve, { publicEntries });
|
|
392
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const RULES = [
|
|
2
|
+
'duplicate-body',
|
|
3
|
+
'similar-structure',
|
|
4
|
+
'duplicate-symbol',
|
|
5
|
+
'circular-dependency',
|
|
6
|
+
'unused-import',
|
|
7
|
+
'unresolved-import',
|
|
8
|
+
'dead-export',
|
|
9
|
+
'orphan-file',
|
|
10
|
+
'suppression',
|
|
11
|
+
];
|
|
12
|
+
export const PROFILES = ['auto', 'none', 'next', 'nuxt', 'sveltekit', 'vite', 'astro', 'node', 'library'];
|
|
13
|
+
// gitignore-style patterns for files a framework loads by convention, with no import statement pointing at them.
|
|
14
|
+
export const PROFILE_ENTRIES = {
|
|
15
|
+
next: [
|
|
16
|
+
'**/app/**/page.*', '**/app/**/layout.*', '**/app/**/route.*', '**/app/**/loading.*', '**/app/**/error.*',
|
|
17
|
+
'**/app/**/not-found.*', '**/app/**/template.*', '**/app/**/default.*', '**/app/**/global-error.*',
|
|
18
|
+
'**/pages/**', '**/middleware.*', '**/instrumentation.*', 'next.config.*', '**/_app.*', '**/_document.*',
|
|
19
|
+
],
|
|
20
|
+
nuxt: ['**/pages/**', '**/layouts/**', '**/middleware/**', '**/plugins/**', '**/server/**', '**/composables/**', '**/components/**', '**/utils/**', '**/stores/**', 'nuxt.config.*'],
|
|
21
|
+
sveltekit: ['src/routes/**', 'src/hooks.*', 'src/hooks/**', 'svelte.config.*'],
|
|
22
|
+
vite: ['vite.config.*', 'src/main.*', 'vitest.config.*'],
|
|
23
|
+
astro: ['src/pages/**', 'src/middleware.*', 'astro.config.*', 'src/content/**'],
|
|
24
|
+
node: ['src/index.*', 'src/main.*', 'src/server.*', 'src/app.*', 'bin/**', 'scripts/**', 'migrations/**', 'seeds/**'],
|
|
25
|
+
library: ['src/index.*', 'index.*'],
|
|
26
|
+
};
|
|
27
|
+
export const REMEDIATION = {
|
|
28
|
+
'duplicate-body': 'Keep one implementation and import it from the other places.',
|
|
29
|
+
'similar-structure': 'Compare the listed functions; if they do the same job, extract one shared function and call it from each.',
|
|
30
|
+
'duplicate-symbol': 'Check whether these are the same concept. If so reuse one; if not, rename one so they are not confused.',
|
|
31
|
+
'circular-dependency': 'Move the shared code into a third module, or make one direction a type-only import.',
|
|
32
|
+
'unused-import': 'Remove the import. If it is re-exported on purpose, export it explicitly.',
|
|
33
|
+
'unresolved-import': 'Fix the path or alias, or add the missing file or package.',
|
|
34
|
+
'dead-export': 'Remove the export (or the symbol), or import it where it is meant to be used.',
|
|
35
|
+
'orphan-file': 'Delete the file, or import it where it is needed. If it is loaded at runtime, add it to entryPoints in .guardianrc.',
|
|
36
|
+
suppression: 'Fix the underlying type or lint error and remove the suppression, or leave a comment saying why it is needed.',
|
|
37
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { kids, text } from './util.js';
|
|
2
|
+
// Iterative: a destructuring pattern can be nested as deep as the file allows. Names come out in source order.
|
|
3
|
+
export function patternNames(node) {
|
|
4
|
+
const names = [];
|
|
5
|
+
const pending = node ? [node] : [];
|
|
6
|
+
for (let current = pending.pop(); current; current = pending.pop()) {
|
|
7
|
+
switch (current.type) {
|
|
8
|
+
case 'identifier':
|
|
9
|
+
case 'shorthand_property_identifier_pattern':
|
|
10
|
+
names.push(current.text);
|
|
11
|
+
break;
|
|
12
|
+
case 'object_pattern':
|
|
13
|
+
case 'array_pattern':
|
|
14
|
+
case 'rest_pattern': {
|
|
15
|
+
const children = kids(current);
|
|
16
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
17
|
+
pending.push(children[i]);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
case 'pair_pattern': {
|
|
21
|
+
const value = current.childForFieldName('value');
|
|
22
|
+
if (value)
|
|
23
|
+
pending.push(value);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'assignment_pattern':
|
|
27
|
+
case 'object_assignment_pattern': {
|
|
28
|
+
const left = current.childForFieldName('left');
|
|
29
|
+
if (left)
|
|
30
|
+
pending.push(left);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
default:
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return names;
|
|
38
|
+
}
|
|
39
|
+
const FUNCTION_LIKE = new Set([
|
|
40
|
+
'function_declaration',
|
|
41
|
+
'generator_function_declaration',
|
|
42
|
+
'function_expression',
|
|
43
|
+
'function',
|
|
44
|
+
'generator_function',
|
|
45
|
+
'arrow_function',
|
|
46
|
+
'method_definition',
|
|
47
|
+
]);
|
|
48
|
+
const SCOPES = new Set([...FUNCTION_LIKE, 'statement_block', 'for_statement', 'for_in_statement', 'catch_clause']);
|
|
49
|
+
const NAMED_DECLARATIONS = new Set([
|
|
50
|
+
'function_declaration',
|
|
51
|
+
'generator_function_declaration',
|
|
52
|
+
'function_expression',
|
|
53
|
+
'generator_function',
|
|
54
|
+
'class_declaration',
|
|
55
|
+
'abstract_class_declaration',
|
|
56
|
+
'class',
|
|
57
|
+
'enum_declaration',
|
|
58
|
+
'interface_declaration',
|
|
59
|
+
'type_alias_declaration',
|
|
60
|
+
'internal_module',
|
|
61
|
+
'type_parameter',
|
|
62
|
+
]);
|
|
63
|
+
const BLOCK_DECLARATIONS = new Set(['function_declaration', 'generator_function_declaration', 'class_declaration', 'abstract_class_declaration', 'enum_declaration', 'interface_declaration', 'type_alias_declaration']);
|
|
64
|
+
function declaratorNames(declaration) {
|
|
65
|
+
return kids(declaration)
|
|
66
|
+
.filter((k) => k.type === 'variable_declarator')
|
|
67
|
+
.flatMap((d) => patternNames(d.childForFieldName('name')));
|
|
68
|
+
}
|
|
69
|
+
function nearestFunction(node) {
|
|
70
|
+
for (let current = node.parent; current; current = current.parent)
|
|
71
|
+
if (FUNCTION_LIKE.has(current.type))
|
|
72
|
+
return current;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
// Is this identifier the place a name is introduced (a declaration or a parameter), as opposed to a use of it?
|
|
76
|
+
export function isBindingIdentifier(node) {
|
|
77
|
+
const parent = node.parent;
|
|
78
|
+
if (!parent)
|
|
79
|
+
return false;
|
|
80
|
+
if (NAMED_DECLARATIONS.has(parent.type) && parent.childForFieldName('name')?.equals(node))
|
|
81
|
+
return true;
|
|
82
|
+
if (parent.type === 'arrow_function' && parent.childForFieldName('parameter')?.equals(node))
|
|
83
|
+
return true;
|
|
84
|
+
let current = node;
|
|
85
|
+
for (let p = current.parent; p; p = current.parent) {
|
|
86
|
+
switch (p.type) {
|
|
87
|
+
case 'object_pattern':
|
|
88
|
+
case 'array_pattern':
|
|
89
|
+
case 'rest_pattern':
|
|
90
|
+
current = p;
|
|
91
|
+
continue;
|
|
92
|
+
case 'pair_pattern':
|
|
93
|
+
if (!p.childForFieldName('value')?.equals(current))
|
|
94
|
+
return false;
|
|
95
|
+
current = p;
|
|
96
|
+
continue;
|
|
97
|
+
case 'assignment_pattern':
|
|
98
|
+
case 'object_assignment_pattern':
|
|
99
|
+
if (!p.childForFieldName('left')?.equals(current))
|
|
100
|
+
return false;
|
|
101
|
+
current = p;
|
|
102
|
+
continue;
|
|
103
|
+
case 'variable_declarator':
|
|
104
|
+
return p.childForFieldName('name')?.equals(current) ?? false;
|
|
105
|
+
case 'required_parameter':
|
|
106
|
+
case 'optional_parameter':
|
|
107
|
+
return p.childForFieldName('pattern')?.equals(current) ?? false;
|
|
108
|
+
case 'formal_parameters':
|
|
109
|
+
return true;
|
|
110
|
+
case 'catch_clause':
|
|
111
|
+
return p.childForFieldName('parameter')?.equals(current) ?? false;
|
|
112
|
+
case 'for_in_statement':
|
|
113
|
+
return p.childForFieldName('left')?.equals(current) === true && p.children.some((c) => c && ['const', 'let', 'var'].includes(c.type));
|
|
114
|
+
default:
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const cache = new Map();
|
|
121
|
+
function declaredNames(scope) {
|
|
122
|
+
const known = cache.get(scope.id);
|
|
123
|
+
if (known)
|
|
124
|
+
return known;
|
|
125
|
+
const names = new Set();
|
|
126
|
+
if (FUNCTION_LIKE.has(scope.type)) {
|
|
127
|
+
const parameters = scope.childForFieldName('parameters');
|
|
128
|
+
for (const param of kids(parameters)) {
|
|
129
|
+
if (param.type === 'required_parameter' || param.type === 'optional_parameter')
|
|
130
|
+
patternNames(param.childForFieldName('pattern')).forEach((n) => names.add(n));
|
|
131
|
+
else
|
|
132
|
+
patternNames(param).forEach((n) => names.add(n));
|
|
133
|
+
}
|
|
134
|
+
const single = scope.childForFieldName('parameter');
|
|
135
|
+
if (single)
|
|
136
|
+
patternNames(single).forEach((n) => names.add(n));
|
|
137
|
+
if (scope.type === 'function_expression' || scope.type === 'generator_function') {
|
|
138
|
+
const own = text(scope.childForFieldName('name'));
|
|
139
|
+
if (own)
|
|
140
|
+
names.add(own);
|
|
141
|
+
}
|
|
142
|
+
for (const param of kids(scope.childForFieldName('type_parameters'))) {
|
|
143
|
+
const name = text(param.childForFieldName('name'));
|
|
144
|
+
if (name)
|
|
145
|
+
names.add(name);
|
|
146
|
+
}
|
|
147
|
+
for (const declaration of scope.descendantsOfType('variable_declaration')) {
|
|
148
|
+
if (declaration && nearestFunction(declaration)?.equals(scope))
|
|
149
|
+
declaratorNames(declaration).forEach((n) => names.add(n));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else if (scope.type === 'statement_block') {
|
|
153
|
+
for (const child of kids(scope)) {
|
|
154
|
+
if (child.type === 'lexical_declaration')
|
|
155
|
+
declaratorNames(child).forEach((n) => names.add(n));
|
|
156
|
+
else if (BLOCK_DECLARATIONS.has(child.type)) {
|
|
157
|
+
const name = text(child.childForFieldName('name'));
|
|
158
|
+
if (name)
|
|
159
|
+
names.add(name);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (scope.type === 'for_statement') {
|
|
164
|
+
const init = scope.childForFieldName('initializer');
|
|
165
|
+
if (init && (init.type === 'lexical_declaration' || init.type === 'variable_declaration'))
|
|
166
|
+
declaratorNames(init).forEach((n) => names.add(n));
|
|
167
|
+
}
|
|
168
|
+
else if (scope.type === 'for_in_statement') {
|
|
169
|
+
if (scope.children.some((c) => c && ['const', 'let', 'var'].includes(c.type)))
|
|
170
|
+
patternNames(scope.childForFieldName('left')).forEach((n) => names.add(n));
|
|
171
|
+
}
|
|
172
|
+
else if (scope.type === 'catch_clause') {
|
|
173
|
+
patternNames(scope.childForFieldName('parameter')).forEach((n) => names.add(n));
|
|
174
|
+
}
|
|
175
|
+
cache.set(scope.id, names);
|
|
176
|
+
return names;
|
|
177
|
+
}
|
|
178
|
+
export function resetScopeCache() {
|
|
179
|
+
cache.clear();
|
|
180
|
+
}
|
|
181
|
+
// A reference counts as a use of the import only if it is not itself a declaration and no enclosing scope
|
|
182
|
+
// declares the same name (a parameter, a local const, a catch variable, a nested function...).
|
|
183
|
+
export function resolvesToModuleBinding(node) {
|
|
184
|
+
if (isBindingIdentifier(node))
|
|
185
|
+
return false;
|
|
186
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
187
|
+
if (SCOPES.has(current.type) && declaredNames(current).has(node.text))
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|