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.
Files changed (70) hide show
  1. package/.env.example +24 -0
  2. package/LICENSE +21 -0
  3. package/README.md +774 -2
  4. package/bin/cli.js +2 -0
  5. package/dist/build-manifest.json +136 -0
  6. package/dist/cli/commands/check.js +56 -0
  7. package/dist/cli/commands/doctor.js +55 -0
  8. package/dist/cli/commands/find.js +31 -0
  9. package/dist/cli/commands/init-files.js +13 -0
  10. package/dist/cli/commands/init.js +50 -0
  11. package/dist/cli/commands/optimize.js +53 -0
  12. package/dist/cli/commands/scan.js +47 -0
  13. package/dist/cli/index.js +62 -0
  14. package/dist/cli/output.js +4 -0
  15. package/dist/context.js +11 -0
  16. package/dist/core/ast/analysis.js +455 -0
  17. package/dist/core/ast/cached-info.js +46 -0
  18. package/dist/core/ast/discovery.js +485 -0
  19. package/dist/core/ast/extract-js.js +531 -0
  20. package/dist/core/ast/extract-py.js +298 -0
  21. package/dist/core/ast/graph.js +92 -0
  22. package/dist/core/ast/health.js +338 -0
  23. package/dist/core/ast/indexer.js +286 -0
  24. package/dist/core/ast/packages.js +343 -0
  25. package/dist/core/ast/parser.js +91 -0
  26. package/dist/core/ast/report.js +154 -0
  27. package/dist/core/ast/resolver.js +392 -0
  28. package/dist/core/ast/rules.js +37 -0
  29. package/dist/core/ast/scope-js.js +191 -0
  30. package/dist/core/ast/shared-index.js +131 -0
  31. package/dist/core/ast/tsconfig.js +301 -0
  32. package/dist/core/ast/types.js +1 -0
  33. package/dist/core/ast/util.js +156 -0
  34. package/dist/core/git/git.js +430 -0
  35. package/dist/core/llm/policy.js +35 -0
  36. package/dist/core/llm/provider.js +199 -0
  37. package/dist/core/prompt/compressor.js +118 -0
  38. package/dist/core/prompt/criteria.js +36 -0
  39. package/dist/core/prompt/find.js +47 -0
  40. package/dist/core/prompt/guidelines.js +22 -0
  41. package/dist/core/prompt/model-output.js +178 -0
  42. package/dist/core/prompt/refiner.js +194 -0
  43. package/dist/core/prompt/relevance.js +265 -0
  44. package/dist/core/prompt/untrusted.js +57 -0
  45. package/dist/core/security/masker.js +330 -0
  46. package/dist/core/security/outbound.js +40 -0
  47. package/dist/core/security/safe-file.js +396 -0
  48. package/dist/core/security/sandbox.js +181 -0
  49. package/dist/core/validation/baseline.js +121 -0
  50. package/dist/core/validation/delta.js +243 -0
  51. package/dist/core/validation/policy.js +123 -0
  52. package/dist/db/cache-key.js +221 -0
  53. package/dist/db/client.js +563 -0
  54. package/dist/db/schema-check.js +149 -0
  55. package/dist/db/schema.js +36 -0
  56. package/dist/db/windows-acl.js +150 -0
  57. package/dist/index.js +55 -0
  58. package/dist/mcp/server.js +20 -0
  59. package/dist/mcp/tools/find-reusable.js +22 -0
  60. package/dist/mcp/tools/get-context.js +39 -0
  61. package/dist/mcp/tools/optimize-prompt.js +31 -0
  62. package/dist/mcp/tools/result.js +31 -0
  63. package/dist/mcp/tools/validate-rules.js +28 -0
  64. package/dist/utils/config.js +117 -0
  65. package/dist/utils/errors.js +9 -0
  66. package/dist/utils/logger.js +19 -0
  67. package/dist/utils/pace.js +19 -0
  68. package/dist/utils/text.js +22 -0
  69. package/dist/utils/tokens.js +14 -0
  70. package/package.json +69 -4
@@ -0,0 +1,531 @@
1
+ import { isBindingIdentifier, patternNames, resetScopeCache, resolvesToModuleBinding } from './scope-js.js';
2
+ import { hasParseError } from './health.js';
3
+ import { structuralHash, bodyHash, codeText, kids, leadingComment, lineOf, oneLine, rangeOf, summarizeNames, text, within } from './util.js';
4
+ const COMMENT_RULES = [
5
+ ['@ts-ignore', /@ts-ignore\b/],
6
+ ['@ts-nocheck', /@ts-nocheck\b/],
7
+ ['@ts-expect-error', /@ts-expect-error\b/],
8
+ ['eslint-disable', /eslint-disable/],
9
+ ];
10
+ const TS_SUPPRESSIONS = `
11
+ (comment) @comment
12
+ ((predefined_type) @any (#eq? @any "any"))
13
+ `;
14
+ const JS_SUPPRESSIONS = '(comment) @comment';
15
+ const CALL_IMPORTS = `
16
+ (call_expression function: (import) arguments: (arguments . (string) @source)) @call
17
+ ((call_expression function: (identifier) @fn arguments: (arguments . (string) @source)) @call (#eq? @fn "require"))
18
+ `;
19
+ const DYNAMIC_CALLS = `
20
+ (call_expression function: (import) arguments: (arguments . (_) @arg)) @call
21
+ ((call_expression function: (identifier) @fn arguments: (arguments . (_) @arg)) @call (#eq? @fn "require"))
22
+ (call_expression function: (member_expression) @member arguments: (arguments . (_) @arg)) @call
23
+ `;
24
+ const GLOB_CALLS = new Set(['import.meta.glob', 'import.meta.globEager', 'require.context']);
25
+ // The literal text known before the first computed piece: `./plugins/${name}` and './plugins/' + name both give './plugins/'.
26
+ function literalPrefix(arg) {
27
+ // `a + b + c + ...` parses as a left-leaning chain as long as the expression, so this follows the left side in a loop.
28
+ let current = arg;
29
+ for (;;) {
30
+ if (current.type === 'string')
31
+ return unquote(current);
32
+ if (current.type === 'template_string') {
33
+ const first = current.children.find((c) => c?.type === 'string_fragment');
34
+ const start = current.children.find((c) => c && c.type !== '`');
35
+ return first && start?.equals(first) ? first.text : '';
36
+ }
37
+ if (current.type === 'binary_expression' && current.childForFieldName('operator')?.text === '+') {
38
+ const left = current.childForFieldName('left');
39
+ if (!left)
40
+ return undefined;
41
+ current = left;
42
+ continue;
43
+ }
44
+ return undefined;
45
+ }
46
+ }
47
+ const REFERENCE_NODES = ['identifier', 'type_identifier', 'shorthand_property_identifier'];
48
+ const FUNCTION_NODES = new Set([
49
+ 'function_declaration',
50
+ 'generator_function_declaration',
51
+ 'function_expression',
52
+ 'function',
53
+ 'generator_function',
54
+ 'arrow_function',
55
+ 'method_definition',
56
+ ]);
57
+ const FUNCTION_VALUES = new Set(['arrow_function', 'function_expression', 'function', 'generator_function']);
58
+ const unquote = (node) => text(node).slice(1, -1);
59
+ const hasToken = (node, type) => node.children.some((c) => c?.type === type);
60
+ const isRequireCall = (node) => node.type === 'call_expression' && text(node.childForFieldName('function')) === 'require';
61
+ const structHashOf = (fn) => fn ? structuralHash([fn.childForFieldName('parameters') ?? fn.childForFieldName('parameter'), fn.childForFieldName('body')], isBindingIdentifier) : undefined;
62
+ function head(node, source) {
63
+ const body = node.childForFieldName('body');
64
+ return oneLine(codeText(node, source, body ? body.startIndex : node.endIndex));
65
+ }
66
+ function isPublicMethod(member) {
67
+ if (member.type !== 'method_definition')
68
+ return false;
69
+ const name = text(member.childForFieldName('name'));
70
+ if (!name || name === 'constructor' || name.startsWith('#'))
71
+ return false;
72
+ return !kids(member).some((c) => c.type === 'accessibility_modifier' && c.text !== 'public');
73
+ }
74
+ function insideFunction(node) {
75
+ for (let current = node.parent; current; current = current.parent) {
76
+ if (FUNCTION_NODES.has(current.type))
77
+ return true;
78
+ }
79
+ return false;
80
+ }
81
+ function declare(node, source, mode) {
82
+ const line = lineOf(node);
83
+ const doc = leadingComment(node);
84
+ const make = (sym) => ({
85
+ ...sym,
86
+ ...(doc ? { doc } : {}),
87
+ ...(hasParseError(node) ? { partial: true } : {}),
88
+ line,
89
+ exportName: mode === 'default' ? 'default' : mode ? sym.name : undefined,
90
+ });
91
+ switch (node.type) {
92
+ case 'function_declaration':
93
+ case 'generator_function_declaration':
94
+ return [
95
+ make({
96
+ name: text(node.childForFieldName('name')) || 'default',
97
+ kind: 'function',
98
+ signature: head(node, source),
99
+ bodyHash: bodyHash(node.childForFieldName('body')),
100
+ structHash: structHashOf(node),
101
+ }),
102
+ ];
103
+ case 'class_declaration':
104
+ case 'abstract_class_declaration': {
105
+ const methods = kids(node.childForFieldName('body')).filter(isPublicMethod).map((m) => text(m.childForFieldName('name')));
106
+ const signature = head(node, source);
107
+ return [
108
+ make({
109
+ name: text(node.childForFieldName('name')) || 'default',
110
+ kind: 'class',
111
+ signature: methods.length ? `${signature} { ${summarizeNames(methods)} }` : signature,
112
+ }),
113
+ ];
114
+ }
115
+ case 'lexical_declaration':
116
+ case 'variable_declaration': {
117
+ const keyword = node.text.match(/^(const|let|var)/)?.[1] ?? 'const';
118
+ const out = [];
119
+ for (const declarator of kids(node).filter((k) => k.type === 'variable_declarator')) {
120
+ const nameNode = declarator.childForFieldName('name');
121
+ if (!nameNode)
122
+ continue;
123
+ if (nameNode.type !== 'identifier') {
124
+ for (const name of patternNames(nameNode))
125
+ out.push(make({ name, kind: 'const', signature: `${keyword} ${name}` }));
126
+ continue;
127
+ }
128
+ const value = declarator.childForFieldName('value');
129
+ out.push(value && FUNCTION_VALUES.has(value.type)
130
+ ? make({
131
+ name: nameNode.text,
132
+ kind: 'function',
133
+ signature: oneLine(`${keyword} ${nameNode.text} = ${head(value, source)}`),
134
+ bodyHash: bodyHash(value.childForFieldName('body')),
135
+ structHash: structHashOf(value),
136
+ })
137
+ : make({
138
+ name: nameNode.text,
139
+ kind: 'const',
140
+ signature: oneLine(`${keyword} ${nameNode.text}${codeText(declarator.childForFieldName('type'), source)}`),
141
+ }));
142
+ }
143
+ return out;
144
+ }
145
+ case 'type_alias_declaration': {
146
+ const name = text(node.childForFieldName('name'));
147
+ const generics = codeText(node.childForFieldName('type_parameters'), source);
148
+ return [make({ name, kind: 'type', signature: oneLine(`type ${name}${generics} = ${codeText(node.childForFieldName('value'), source)}`, 140) })];
149
+ }
150
+ case 'interface_declaration': {
151
+ const name = text(node.childForFieldName('name'));
152
+ const members = kids(node.childForFieldName('body')).map((m) => text(m.childForFieldName('name'))).filter(Boolean);
153
+ return [make({ name, kind: 'interface', signature: members.length ? `interface ${name} { ${summarizeNames(members)} }` : `interface ${name}` })];
154
+ }
155
+ case 'enum_declaration': {
156
+ const name = text(node.childForFieldName('name'));
157
+ const members = kids(node.childForFieldName('body')).map((m) => (m.type === 'enum_assignment' ? text(m.childForFieldName('name')) : m.text));
158
+ return [make({ name, kind: 'enum', signature: members.length ? `enum ${name} { ${summarizeNames(members)} }` : `enum ${name}` })];
159
+ }
160
+ default:
161
+ return [];
162
+ }
163
+ }
164
+ function requireBindings(call) {
165
+ const none = { bindings: [], skip: null };
166
+ let target = call;
167
+ let member;
168
+ const parent = call.parent;
169
+ if (parent?.type === 'member_expression' && parent.childForFieldName('object')?.equals(call)) {
170
+ member = text(parent.childForFieldName('property'));
171
+ target = parent;
172
+ }
173
+ const declarator = target.parent;
174
+ if (declarator?.type !== 'variable_declarator' || !declarator.childForFieldName('value')?.equals(target))
175
+ return none;
176
+ const name = declarator.childForFieldName('name');
177
+ if (!name)
178
+ return none;
179
+ const skip = rangeOf(name);
180
+ if (name.type === 'identifier')
181
+ return { bindings: [{ local: name.text, imported: member ?? '*' }], skip };
182
+ if (name.type !== 'object_pattern' || member)
183
+ return { bindings: patternNames(name).map((local) => ({ local, imported: '*' })), skip };
184
+ const bindings = [];
185
+ for (const part of kids(name)) {
186
+ if (part.type === 'shorthand_property_identifier_pattern') {
187
+ bindings.push({ local: part.text, imported: part.text });
188
+ }
189
+ else if (part.type === 'object_assignment_pattern') {
190
+ const local = text(part.childForFieldName('left'));
191
+ if (local)
192
+ bindings.push({ local, imported: local });
193
+ }
194
+ else if (part.type === 'pair_pattern') {
195
+ const key = text(part.childForFieldName('key'));
196
+ for (const local of patternNames(part.childForFieldName('value')))
197
+ bindings.push({ local, imported: key });
198
+ }
199
+ else if (part.type === 'rest_pattern') {
200
+ for (const local of patternNames(part))
201
+ bindings.push({ local, imported: '*' });
202
+ }
203
+ }
204
+ return { bindings, skip };
205
+ }
206
+ // `module.exports` -> { name: null }, `exports.x` / `module.exports.x` -> { name: 'x' }
207
+ function commonJsTarget(left) {
208
+ const object = left.childForFieldName('object');
209
+ const property = text(left.childForFieldName('property'));
210
+ if (object?.type === 'identifier' && object.text === 'module' && property === 'exports')
211
+ return { name: null };
212
+ if (object?.type === 'identifier' && object.text === 'exports')
213
+ return { name: property };
214
+ if (object?.type === 'member_expression' && text(object.childForFieldName('object')) === 'module' && text(object.childForFieldName('property')) === 'exports') {
215
+ return { name: property };
216
+ }
217
+ return null;
218
+ }
219
+ function isModuleExportsAssignment(call) {
220
+ const parent = call.parent;
221
+ if (parent?.type !== 'assignment_expression' || !parent.childForFieldName('right')?.equals(call))
222
+ return false;
223
+ const left = parent.childForFieldName('left');
224
+ return left?.type === 'member_expression' && commonJsTarget(left)?.name === null;
225
+ }
226
+ // `require('./x')` as a bare statement is an import, not a program that runs.
227
+ function isModuleExportsCall(statement) {
228
+ const call = kids(statement).find((c) => c.type === 'call_expression');
229
+ return call !== undefined && isRequireCall(call);
230
+ }
231
+ function memberNames(references) {
232
+ const names = new Set();
233
+ for (const { node } of references) {
234
+ const parent = node.parent;
235
+ if (parent?.type === 'member_expression' && parent.childForFieldName('object')?.equals(node)) {
236
+ const property = parent.childForFieldName('property');
237
+ if (!property)
238
+ return '*';
239
+ names.add(property.text);
240
+ }
241
+ else if (parent?.type === 'nested_type_identifier' && parent.childForFieldName('module')?.equals(node)) {
242
+ names.add(text(parent.childForFieldName('name')));
243
+ }
244
+ else {
245
+ return '*';
246
+ }
247
+ }
248
+ return [...names];
249
+ }
250
+ // A reference counts only when it resolves to the module-level import: declarations and shadowed names do not.
251
+ function resolveReferences(root, bindings, skipRanges, exportRanges) {
252
+ if (bindings.length === 0)
253
+ return [];
254
+ const wanted = new Set(bindings.map((b) => b.local));
255
+ const references = new Map();
256
+ for (const node of root.descendantsOfType(REFERENCE_NODES)) {
257
+ if (!node || !wanted.has(node.text) || within(node.startIndex, skipRanges) || !resolvesToModuleBinding(node))
258
+ continue;
259
+ const list = references.get(node.text) ?? [];
260
+ list.push({ node, exported: within(node.startIndex, exportRanges) });
261
+ references.set(node.text, list);
262
+ }
263
+ const groups = new Map();
264
+ for (const binding of bindings)
265
+ groups.set(binding.ref, [...(groups.get(binding.ref) ?? []), binding]);
266
+ const unused = [];
267
+ for (const [ref, group] of groups) {
268
+ const names = new Set();
269
+ for (const binding of group) {
270
+ const all = references.get(binding.local) ?? [];
271
+ const runtime = all.filter((r) => !r.exported);
272
+ if (all.length === 0 && binding.local !== 'React')
273
+ unused.push({ name: binding.local, source: binding.source, line: binding.line });
274
+ if (binding.imported === '*') {
275
+ const members = memberNames(runtime);
276
+ if (members === '*')
277
+ names.add('*');
278
+ else
279
+ members.forEach((m) => names.add(m));
280
+ }
281
+ else if (runtime.length > 0 || all.length === 0) {
282
+ names.add(binding.imported);
283
+ }
284
+ }
285
+ ref.names = [...names];
286
+ }
287
+ return unused;
288
+ }
289
+ export function extractJs({ root, source, lang, query }) {
290
+ resetScopeCache();
291
+ const symbols = [];
292
+ const imports = [];
293
+ const reexports = [];
294
+ const bindings = [];
295
+ const locals = new Map();
296
+ const aliases = [];
297
+ const reexportFromRanges = [];
298
+ const exportListRanges = [];
299
+ const keep = (found, isLocal) => {
300
+ symbols.push(...found);
301
+ if (isLocal)
302
+ for (const sym of found)
303
+ locals.set(sym.name, sym);
304
+ };
305
+ const addRef = (specifier, names, node, kind, typeOnly) => {
306
+ const ref = { source: specifier, names, line: lineOf(node), kind, typeOnly };
307
+ imports.push(ref);
308
+ return ref;
309
+ };
310
+ function readImport(node) {
311
+ const requireClause = kids(node).find((c) => c.type === 'import_require_clause');
312
+ if (requireClause) {
313
+ const specifier = unquote(requireClause.childForFieldName('source'));
314
+ const local = text(kids(requireClause).find((c) => c.type === 'identifier'));
315
+ const ref = addRef(specifier, ['*'], node, 'require', false);
316
+ if (local)
317
+ bindings.push({ local, imported: '*', ref, source: specifier, line: ref.line, skip: rangeOf(node) });
318
+ return;
319
+ }
320
+ const specifier = unquote(node.childForFieldName('source'));
321
+ const clause = kids(node).find((c) => c.type === 'import_clause');
322
+ const found = [];
323
+ const typedSpecifiers = [];
324
+ for (const part of kids(clause)) {
325
+ if (part.type === 'identifier') {
326
+ found.push({ local: part.text, imported: 'default' });
327
+ }
328
+ else if (part.type === 'namespace_import') {
329
+ found.push({ local: text(kids(part)[0]), imported: '*' });
330
+ }
331
+ else if (part.type === 'named_imports') {
332
+ for (const spec of kids(part).filter((s) => s.type === 'import_specifier')) {
333
+ const imported = text(spec.childForFieldName('name'));
334
+ found.push({ local: text(spec.childForFieldName('alias')) || imported, imported });
335
+ typedSpecifiers.push(hasToken(spec, 'type'));
336
+ }
337
+ }
338
+ }
339
+ const onlyTypedSpecifiers = found.length > 0 && typedSpecifiers.length === found.length && typedSpecifiers.every(Boolean);
340
+ const ref = addRef(specifier, [...new Set(found.map((b) => b.imported))], node, 'import', hasToken(node, 'type') || onlyTypedSpecifiers);
341
+ for (const binding of found)
342
+ bindings.push({ ...binding, ref, source: specifier, line: ref.line, skip: rangeOf(node) });
343
+ }
344
+ function readExport(node) {
345
+ const line = lineOf(node);
346
+ const from = node.childForFieldName('source');
347
+ const declaration = node.childForFieldName('declaration');
348
+ const value = node.childForFieldName('value');
349
+ const clause = kids(node).find((c) => c.type === 'export_clause');
350
+ const isDefault = hasToken(node, 'default');
351
+ const typeOnly = hasToken(node, 'type');
352
+ const specifiers = kids(clause).filter((s) => s.type === 'export_specifier');
353
+ if (from) {
354
+ const specifier = unquote(from);
355
+ reexportFromRanges.push(rangeOf(node));
356
+ if (clause) {
357
+ addRef(specifier, specifiers.map((s) => text(s.childForFieldName('name'))), node, 'reexport', typeOnly);
358
+ for (const spec of specifiers) {
359
+ const original = text(spec.childForFieldName('name'));
360
+ const exported = text(spec.childForFieldName('alias')) || original;
361
+ reexports.push({ source: specifier, imported: original, exported });
362
+ symbols.push({ name: exported, kind: 'reexport', line, signature: `re-export ${original}`, exportName: exported });
363
+ }
364
+ return;
365
+ }
366
+ addRef(specifier, ['*'], node, 'reexport', typeOnly);
367
+ const nsName = text(kids(kids(node).find((c) => c.type === 'namespace_export'))[0]);
368
+ reexports.push({ source: specifier, imported: '*', exported: nsName || '*' });
369
+ if (nsName)
370
+ symbols.push({ name: nsName, kind: 'reexport', line, signature: `re-export * as ${nsName}`, exportName: nsName });
371
+ return;
372
+ }
373
+ if (declaration) {
374
+ keep(declare(declaration, source, isDefault ? 'default' : 'named'), false);
375
+ }
376
+ else if (clause) {
377
+ exportListRanges.push(rangeOf(node));
378
+ for (const spec of specifiers) {
379
+ const local = text(spec.childForFieldName('name'));
380
+ aliases.push({ local, exported: text(spec.childForFieldName('alias')) || local, line });
381
+ }
382
+ }
383
+ else if (isDefault && value) {
384
+ if (value.type === 'identifier') {
385
+ exportListRanges.push(rangeOf(node));
386
+ aliases.push({ local: value.text, exported: 'default', line });
387
+ }
388
+ else {
389
+ symbols.push({ name: 'default', kind: 'default', line, signature: `default ${value.type.replace(/_/g, ' ')}`, exportName: 'default' });
390
+ }
391
+ }
392
+ }
393
+ function readCommonJsExport(node) {
394
+ const assignment = kids(node).find((c) => c.type === 'assignment_expression');
395
+ const left = assignment?.childForFieldName('left');
396
+ const right = assignment?.childForFieldName('right');
397
+ if (left?.type !== 'member_expression' || !right)
398
+ return;
399
+ const target = commonJsTarget(left);
400
+ if (!target)
401
+ return;
402
+ const line = lineOf(node);
403
+ const exportValue = (name, value) => {
404
+ if (isRequireCall(value))
405
+ return;
406
+ if (value.type === 'identifier') {
407
+ aliases.push({ local: value.text, exported: name, line });
408
+ }
409
+ else if (FUNCTION_VALUES.has(value.type)) {
410
+ symbols.push({
411
+ name,
412
+ kind: 'function',
413
+ line,
414
+ signature: oneLine(`${name} = ${head(value, source)}`),
415
+ exportName: name,
416
+ bodyHash: bodyHash(value.childForFieldName('body')),
417
+ structHash: structHashOf(value),
418
+ });
419
+ }
420
+ else if (value.type === 'class') {
421
+ symbols.push({ name, kind: 'class', line, signature: head(value, source), exportName: name });
422
+ }
423
+ else {
424
+ symbols.push({ name, kind: 'const', line, signature: `exports.${name}`, exportName: name });
425
+ }
426
+ };
427
+ if (target.name !== null)
428
+ return exportValue(target.name, right);
429
+ if (right.type !== 'object')
430
+ return exportValue('default', right);
431
+ for (const member of kids(right)) {
432
+ if (member.type === 'shorthand_property_identifier') {
433
+ aliases.push({ local: member.text, exported: member.text, line });
434
+ }
435
+ else if (member.type === 'pair') {
436
+ const value = member.childForFieldName('value');
437
+ const key = text(member.childForFieldName('key'));
438
+ if (key && value)
439
+ exportValue(key, value);
440
+ }
441
+ else if (member.type === 'method_definition') {
442
+ const name = text(member.childForFieldName('name'));
443
+ if (!name)
444
+ continue;
445
+ symbols.push({
446
+ name,
447
+ kind: 'function',
448
+ line: lineOf(member),
449
+ signature: oneLine(`${name}${text(member.childForFieldName('parameters'))}`),
450
+ exportName: name,
451
+ bodyHash: bodyHash(member.childForFieldName('body')),
452
+ });
453
+ }
454
+ }
455
+ }
456
+ for (const node of kids(root)) {
457
+ if (node.type === 'import_statement')
458
+ readImport(node);
459
+ else if (node.type === 'export_statement')
460
+ readExport(node);
461
+ else if (node.type === 'expression_statement')
462
+ readCommonJsExport(node);
463
+ else
464
+ keep(declare(node, source, false), true);
465
+ }
466
+ for (const match of query(CALL_IMPORTS).matches(root)) {
467
+ const call = match.captures.find((c) => c.name === 'call')?.node;
468
+ const target = match.captures.find((c) => c.name === 'source')?.node;
469
+ if (!call || !target)
470
+ continue;
471
+ const isRequire = call.childForFieldName('function')?.type !== 'import';
472
+ const specifier = unquote(target);
473
+ const found = isRequire ? requireBindings(call) : { bindings: [], skip: null };
474
+ const kind = isRequire && !insideFunction(call) ? 'require' : 'dynamic';
475
+ const ref = addRef(specifier, found.bindings.length > 0 ? [] : ['*'], call, kind, false);
476
+ if (isRequire && isModuleExportsAssignment(call))
477
+ reexports.push({ source: specifier, imported: '*', exported: 'default' });
478
+ if (found.skip)
479
+ for (const binding of found.bindings)
480
+ bindings.push({ ...binding, ref, source: specifier, line: ref.line, skip: found.skip });
481
+ }
482
+ const dynamicLoads = [];
483
+ for (const match of query(DYNAMIC_CALLS).matches(root)) {
484
+ const call = match.captures.find((c) => c.name === 'call')?.node;
485
+ const arg = match.captures.find((c) => c.name === 'arg')?.node;
486
+ const member = match.captures.find((c) => c.name === 'member')?.node;
487
+ if (!call || !arg)
488
+ continue;
489
+ if (member) {
490
+ if (!GLOB_CALLS.has(member.text))
491
+ continue;
492
+ const pattern = arg.type === 'array' ? kids(arg).find((k) => k.type === 'string') : arg;
493
+ const prefix = pattern?.type === 'string' ? unquote(pattern).split('*')[0] : undefined;
494
+ dynamicLoads.push({ line: lineOf(call), kind: 'glob', prefix: prefix || undefined });
495
+ continue;
496
+ }
497
+ if (arg.type === 'string')
498
+ continue;
499
+ const prefix = literalPrefix(arg);
500
+ dynamicLoads.push({ line: lineOf(call), kind: call.childForFieldName('function')?.type === 'import' ? 'import' : 'require', prefix: prefix || undefined });
501
+ }
502
+ const decorated = root.descendantsOfType(['class_declaration', 'abstract_class_declaration']).some((c) => c !== null && (kids(c).some((k) => k.type === 'decorator') || kids(c.parent).some((k) => k.type === 'decorator')));
503
+ const script = kids(root).some((n) => n.type === 'expression_statement' && kids(n).some((c) => ['call_expression', 'await_expression', 'new_expression'].includes(c.type)) && !isModuleExportsCall(n));
504
+ const byLocal = new Map(bindings.map((b) => [b.local, b]));
505
+ for (const alias of aliases) {
506
+ const local = locals.get(alias.local);
507
+ if (local && !local.exportName) {
508
+ local.exportName = alias.exported;
509
+ continue;
510
+ }
511
+ const imported = byLocal.get(alias.local);
512
+ if (imported)
513
+ reexports.push({ source: imported.source, imported: imported.imported, exported: alias.exported });
514
+ symbols.push({ name: alias.local, kind: 'reexport', line: alias.line, signature: `re-export ${alias.local}`, exportName: alias.exported });
515
+ }
516
+ const skipRanges = [...bindings.map((b) => b.skip), ...reexportFromRanges];
517
+ const unusedImports = resolveReferences(root, bindings, skipRanges, exportListRanges);
518
+ const suppressions = [];
519
+ const hasTypes = lang !== 'javascript';
520
+ for (const capture of query(hasTypes ? TS_SUPPRESSIONS : JS_SUPPRESSIONS).captures(root)) {
521
+ const line = lineOf(capture.node);
522
+ if (capture.name === 'any') {
523
+ suppressions.push({ kind: 'any', line });
524
+ continue;
525
+ }
526
+ const rule = COMMENT_RULES.find(([, pattern]) => pattern.test(capture.node.text));
527
+ if (rule)
528
+ suppressions.push({ kind: rule[0], line });
529
+ }
530
+ return { symbols, imports, reexports, unusedImports, suppressions, dynamicLoads, decorated, script };
531
+ }