claude-db 0.11.0 → 0.12.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/CHANGELOG.md +63 -0
- package/README.md +44 -81
- package/dist/catchup/gather.d.ts +1 -0
- package/dist/catchup/gather.js +9 -2
- package/dist/catchup/git-state.d.ts +2 -0
- package/dist/catchup/git-state.js +3 -0
- package/dist/catchup/render.js +8 -0
- package/dist/cli/commands/graph.js +104 -80
- package/dist/cli/commands/languages.d.ts +1 -0
- package/dist/cli/commands/languages.js +22 -0
- package/dist/cli/files.d.ts +2 -0
- package/dist/cli/files.js +15 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/install.js +4 -3
- package/dist/cli/skills.js +6 -1
- package/dist/cli/usage.js +3 -1
- package/dist/facts/distill.d.ts +0 -2
- package/dist/facts/distill.js +4 -26
- package/dist/facts/handoff.js +11 -1
- package/dist/facts/known.d.ts +4 -0
- package/dist/facts/known.js +38 -0
- package/dist/facts/ops.d.ts +1 -1
- package/dist/facts/ops.js +4 -2
- package/dist/facts/start.js +5 -1
- package/dist/graph/ast.d.ts +4 -0
- package/dist/graph/ast.js +11 -0
- package/dist/graph/grammars.d.ts +14 -0
- package/dist/graph/grammars.js +58 -0
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/index.js +2 -2
- package/dist/graph/languages/basic.js +5 -1
- package/dist/graph/languages/ecmascript.js +7 -0
- package/dist/graph/languages/go.js +0 -1
- package/dist/graph/languages/index.d.ts +1 -1
- package/dist/graph/languages/index.js +34 -9
- package/dist/graph/languages/java.d.ts +2 -0
- package/dist/graph/languages/java.js +38 -0
- package/dist/graph/languages/kotlin.d.ts +2 -0
- package/dist/graph/languages/kotlin.js +19 -0
- package/dist/graph/languages/python.js +10 -2
- package/dist/graph/languages/rules.d.ts +11 -0
- package/dist/graph/languages/rust.js +2 -1
- package/dist/graph/modules/binding.d.ts +61 -0
- package/dist/graph/modules/binding.js +43 -0
- package/dist/graph/modules/ecmascript/bind.d.ts +4 -0
- package/dist/graph/modules/ecmascript/bind.js +17 -0
- package/dist/graph/modules/ecmascript/contexts.d.ts +6 -0
- package/dist/graph/modules/ecmascript/contexts.js +86 -0
- package/dist/graph/modules/ecmascript/exports.d.ts +9 -0
- package/dist/graph/modules/ecmascript/exports.js +91 -0
- package/dist/graph/modules/ecmascript/imports.d.ts +4 -0
- package/dist/graph/modules/ecmascript/imports.js +73 -0
- package/dist/graph/modules/ecmascript/package-resolver.d.ts +4 -0
- package/dist/graph/modules/ecmascript/package-resolver.js +73 -0
- package/dist/graph/modules/ecmascript/resolver.d.ts +2 -0
- package/dist/graph/modules/ecmascript/resolver.js +22 -0
- package/dist/graph/modules/ecmascript/specifiers.d.ts +3 -0
- package/dist/graph/modules/ecmascript/specifiers.js +15 -0
- package/dist/graph/modules/ecmascript/system.d.ts +2 -0
- package/dist/graph/modules/ecmascript/system.js +18 -0
- package/dist/graph/modules/ecmascript/workspace-resolver.d.ts +5 -0
- package/dist/graph/modules/ecmascript/workspace-resolver.js +38 -0
- package/dist/graph/modules/go/grammar.d.ts +2 -0
- package/dist/graph/modules/go/grammar.js +54 -0
- package/dist/graph/modules/go/imports.d.ts +4 -0
- package/dist/graph/modules/go/imports.js +32 -0
- package/dist/graph/modules/go/resolver.d.ts +2 -0
- package/dist/graph/modules/go/resolver.js +54 -0
- package/dist/graph/modules/go/system.d.ts +2 -0
- package/dist/graph/modules/go/system.js +16 -0
- package/dist/graph/modules/java/declarations.d.ts +9 -0
- package/dist/graph/modules/java/declarations.js +108 -0
- package/dist/graph/modules/java/grammar.d.ts +2 -0
- package/dist/graph/modules/java/grammar.js +135 -0
- package/dist/graph/modules/java/imports.d.ts +4 -0
- package/dist/graph/modules/java/imports.js +13 -0
- package/dist/graph/modules/java/receivers.d.ts +3 -0
- package/dist/graph/modules/java/receivers.js +139 -0
- package/dist/graph/modules/java/system.d.ts +1 -0
- package/dist/graph/modules/java/system.js +13 -0
- package/dist/graph/modules/jvm/linking.d.ts +20 -0
- package/dist/graph/modules/jvm/linking.js +64 -0
- package/dist/graph/modules/jvm/packages.d.ts +5 -0
- package/dist/graph/modules/jvm/packages.js +58 -0
- package/dist/graph/modules/jvm/receivers.d.ts +44 -0
- package/dist/graph/modules/jvm/receivers.js +77 -0
- package/dist/graph/modules/jvm/system.d.ts +18 -0
- package/dist/graph/modules/jvm/system.js +29 -0
- package/dist/graph/modules/jvm/types.d.ts +3 -0
- package/dist/graph/modules/jvm/types.js +11 -0
- package/dist/graph/modules/kotlin/declarations.d.ts +8 -0
- package/dist/graph/modules/kotlin/declarations.js +69 -0
- package/dist/graph/modules/kotlin/grammar.d.ts +2 -0
- package/dist/graph/modules/kotlin/grammar.js +78 -0
- package/dist/graph/modules/kotlin/imports.d.ts +4 -0
- package/dist/graph/modules/kotlin/imports.js +25 -0
- package/dist/graph/modules/kotlin/receivers.d.ts +3 -0
- package/dist/graph/modules/kotlin/receivers.js +105 -0
- package/dist/graph/modules/kotlin/references.d.ts +8 -0
- package/dist/graph/modules/kotlin/references.js +69 -0
- package/dist/graph/modules/kotlin/system.d.ts +1 -0
- package/dist/graph/modules/kotlin/system.js +15 -0
- package/dist/graph/modules/members.d.ts +17 -0
- package/dist/graph/modules/members.js +30 -0
- package/dist/graph/modules/module-index.d.ts +9 -0
- package/dist/graph/modules/module-index.js +62 -0
- package/dist/graph/modules/names.d.ts +2 -0
- package/dist/graph/modules/names.js +3 -0
- package/dist/graph/modules/python/grammar.d.ts +2 -0
- package/dist/graph/modules/python/grammar.js +100 -0
- package/dist/graph/modules/python/imports.d.ts +3 -0
- package/dist/graph/modules/python/imports.js +72 -0
- package/dist/graph/modules/python/resolver.d.ts +2 -0
- package/dist/graph/modules/python/resolver.js +78 -0
- package/dist/graph/modules/python/system.d.ts +2 -0
- package/dist/graph/modules/python/system.js +16 -0
- package/dist/graph/modules/rebind.d.ts +8 -0
- package/dist/graph/modules/rebind.js +51 -0
- package/dist/graph/modules/registry.d.ts +5 -0
- package/dist/graph/modules/registry.js +47 -0
- package/dist/graph/modules/rust/grammar.d.ts +2 -0
- package/dist/graph/modules/rust/grammar.js +94 -0
- package/dist/graph/modules/rust/imports.d.ts +3 -0
- package/dist/graph/modules/rust/imports.js +90 -0
- package/dist/graph/modules/rust/resolver.d.ts +3 -0
- package/dist/graph/modules/rust/resolver.js +153 -0
- package/dist/graph/modules/rust/system.d.ts +2 -0
- package/dist/graph/modules/rust/system.js +16 -0
- package/dist/graph/modules/shadowing.d.ts +17 -0
- package/dist/graph/modules/shadowing.js +65 -0
- package/dist/graph/modules/system.d.ts +39 -0
- package/dist/graph/modules/system.js +1 -0
- package/dist/graph/modules/uses.d.ts +14 -0
- package/dist/graph/modules/uses.js +62 -0
- package/dist/graph/parser.d.ts +9 -11
- package/dist/graph/parser.js +5 -4
- package/dist/graph/query/answer.d.ts +15 -0
- package/dist/graph/query/answer.js +22 -0
- package/dist/graph/query/format.d.ts +8 -1
- package/dist/graph/query/format.js +103 -30
- package/dist/graph/query/groups.d.ts +14 -0
- package/dist/graph/query/groups.js +51 -0
- package/dist/graph/query/index.d.ts +2 -1
- package/dist/graph/query/index.js +2 -1
- package/dist/graph/query/lookup.d.ts +2 -0
- package/dist/graph/query/lookup.js +19 -2
- package/dist/graph/query/refresh.js +17 -18
- package/dist/graph/query/stale.d.ts +2 -0
- package/dist/graph/query/stale.js +39 -0
- package/dist/graph/query/text.d.ts +12 -0
- package/dist/graph/query/text.js +26 -0
- package/dist/graph/scan/binder.d.ts +13 -0
- package/dist/graph/scan/binder.js +210 -0
- package/dist/graph/scan/cache.d.ts +11 -0
- package/dist/graph/scan/cache.js +140 -0
- package/dist/graph/scan/extract.d.ts +4 -17
- package/dist/graph/scan/extract.js +105 -83
- package/dist/graph/scan/files.d.ts +3 -9
- package/dist/graph/scan/files.js +3 -2
- package/dist/graph/scan/index.d.ts +8 -3
- package/dist/graph/scan/index.js +96 -36
- package/dist/graph/scan/job.d.ts +1 -0
- package/dist/graph/scan/job.js +1 -0
- package/dist/graph/scan/persist.d.ts +3 -0
- package/dist/graph/scan/persist.js +22 -0
- package/dist/graph/scan/receivers.d.ts +21 -0
- package/dist/graph/scan/receivers.js +87 -0
- package/dist/graph/scan/resolve.d.ts +8 -2
- package/dist/graph/scan/resolve.js +50 -56
- package/dist/graph/scan/spans.d.ts +13 -0
- package/dist/graph/scan/spans.js +42 -0
- package/dist/graph/symbols.d.ts +10 -0
- package/dist/graph/symbols.js +30 -0
- package/dist/graph/types.d.ts +56 -0
- package/dist/graph/types.js +1 -0
- package/dist/hooks/background.js +1 -16
- package/dist/hooks/detached.d.ts +3 -0
- package/dist/hooks/detached.js +16 -0
- package/dist/hooks/graph-refresh.d.ts +1 -0
- package/dist/hooks/graph-refresh.js +9 -0
- package/dist/hooks/prefer-usages.js +53 -12
- package/dist/hooks/session-start.js +8 -8
- package/dist/mcp/tools/usages.js +40 -31
- package/dist/types.d.ts +1 -1
- package/dist/usages/find.js +1 -0
- package/docs/how-it-works.md +10 -8
- package/docs/setup-guide.md +47 -299
- package/package.json +17 -6
- package/skills/catchup/SKILL.md +2 -0
- package/skills/handoff/SKILL.md +23 -7
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sameNode } from '../ast.js';
|
|
2
|
+
const childOf = (node, child) => typeof child === 'string' ? node.field(child) : child(node);
|
|
3
|
+
export function memberAccess(syntax) {
|
|
4
|
+
const isReceiverOfCall = (member) => {
|
|
5
|
+
if (member.kind() !== syntax.member || !syntax.call || !syntax.callee)
|
|
6
|
+
return false;
|
|
7
|
+
const call = member.parent();
|
|
8
|
+
return call?.kind() === syntax.call && sameNode(childOf(call, syntax.callee), member);
|
|
9
|
+
};
|
|
10
|
+
const isMemberObject = (node, parent) => parent.kind() === syntax.member &&
|
|
11
|
+
sameNode(childOf(parent, syntax.object), node) &&
|
|
12
|
+
!isReceiverOfCall(parent);
|
|
13
|
+
const nameOf = (member) => typeof syntax.name === 'string'
|
|
14
|
+
? (member.field(syntax.name)?.text() ?? '')
|
|
15
|
+
: syntax.name(member);
|
|
16
|
+
const memberChain = (first) => {
|
|
17
|
+
const names = [nameOf(first)];
|
|
18
|
+
let top = first;
|
|
19
|
+
for (let up = top.parent(); up?.kind() === syntax.member; up = top.parent()) {
|
|
20
|
+
if (!sameNode(childOf(up, syntax.object), top))
|
|
21
|
+
break;
|
|
22
|
+
top = up;
|
|
23
|
+
names.push(nameOf(top));
|
|
24
|
+
}
|
|
25
|
+
if (isReceiverOfCall(top))
|
|
26
|
+
names.pop();
|
|
27
|
+
return names.filter(Boolean);
|
|
28
|
+
};
|
|
29
|
+
return { isReceiverOfCall, isMemberObject, memberChain };
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CodeSymbol } from '../../types.js';
|
|
2
|
+
import type { Reference } from '../types.js';
|
|
3
|
+
import type { ModuleContext } from './system.js';
|
|
4
|
+
export interface ModuleIndex {
|
|
5
|
+
fileOf(from: string, specifier: string): string | null;
|
|
6
|
+
find(file: string, name: string): CodeSymbol | undefined;
|
|
7
|
+
enter(file: string, segment: string): string | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare function moduleIndex(symbols: CodeSymbol[], references: Reference[], context: ModuleContext): ModuleIndex;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const REEXPORT_DEPTH = 6;
|
|
2
|
+
const EVERYTHING = '*';
|
|
3
|
+
const keyOf = (file, name) => `${file}\0${name}`;
|
|
4
|
+
function pick(symbols) {
|
|
5
|
+
return symbols?.find((symbol) => symbol.kind !== 'method') ?? symbols?.[0];
|
|
6
|
+
}
|
|
7
|
+
export function moduleIndex(symbols, references, context) {
|
|
8
|
+
const declared = new Map();
|
|
9
|
+
for (const symbol of symbols) {
|
|
10
|
+
const key = keyOf(context.unit(symbol.file), symbol.name);
|
|
11
|
+
declared.set(key, [...(declared.get(key) ?? []), symbol]);
|
|
12
|
+
}
|
|
13
|
+
const links = new Map();
|
|
14
|
+
const spaces = new Map();
|
|
15
|
+
for (const reference of references) {
|
|
16
|
+
const link = reference.link;
|
|
17
|
+
if (link?.exported === undefined)
|
|
18
|
+
continue;
|
|
19
|
+
if (link.namespace)
|
|
20
|
+
spaces.set(keyOf(reference.file, link.exported), reference);
|
|
21
|
+
else
|
|
22
|
+
links.set(reference.file, [...(links.get(reference.file) ?? []), reference]);
|
|
23
|
+
}
|
|
24
|
+
const resolved = new Map();
|
|
25
|
+
const fileOf = (from, specifier) => {
|
|
26
|
+
const known = resolved.get(from) ?? new Map();
|
|
27
|
+
resolved.set(from, known);
|
|
28
|
+
const file = known.get(specifier);
|
|
29
|
+
if (file !== undefined)
|
|
30
|
+
return file;
|
|
31
|
+
const found = context.resolve(from, specifier);
|
|
32
|
+
known.set(specifier, found);
|
|
33
|
+
return found;
|
|
34
|
+
};
|
|
35
|
+
const walk = (file, name, own, depth = 0, seen = new Set()) => {
|
|
36
|
+
const here = own(file, name);
|
|
37
|
+
if (here !== undefined || depth >= REEXPORT_DEPTH || seen.has(file))
|
|
38
|
+
return here;
|
|
39
|
+
seen.add(file);
|
|
40
|
+
for (const reference of links.get(file) ?? []) {
|
|
41
|
+
const link = reference.link;
|
|
42
|
+
if (!link || (link.exported !== EVERYTHING && link.exported !== name))
|
|
43
|
+
continue;
|
|
44
|
+
const target = fileOf(file, link.specifier);
|
|
45
|
+
const wanted = link.exported === EVERYTHING ? name : reference.name;
|
|
46
|
+
const found = target ? walk(target, wanted, own, depth + 1, seen) : undefined;
|
|
47
|
+
if (found !== undefined)
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
};
|
|
52
|
+
const declaredIn = (file, name) => pick(declared.get(keyOf(file, name)));
|
|
53
|
+
const spaceIn = (file, name) => {
|
|
54
|
+
const space = spaces.get(keyOf(file, name));
|
|
55
|
+
return space?.link ? (fileOf(file, space.link.specifier) ?? undefined) : undefined;
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
fileOf,
|
|
59
|
+
find: (file, name) => walk(file, name, declaredIn),
|
|
60
|
+
enter: (file, segment) => walk(file, segment, spaceIn) ?? context.child(file, segment) ?? undefined,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { memberAccess } from '../members.js';
|
|
3
|
+
import { shadowing } from '../shadowing.js';
|
|
4
|
+
const members = memberAccess({
|
|
5
|
+
member: 'attribute',
|
|
6
|
+
object: 'object',
|
|
7
|
+
name: 'attribute',
|
|
8
|
+
call: 'call',
|
|
9
|
+
callee: 'function',
|
|
10
|
+
});
|
|
11
|
+
const BINDING_PARENTS = new Set([
|
|
12
|
+
'parameters',
|
|
13
|
+
'lambda_parameters',
|
|
14
|
+
'typed_parameter',
|
|
15
|
+
'list_splat_pattern',
|
|
16
|
+
'dictionary_splat_pattern',
|
|
17
|
+
'pattern_list',
|
|
18
|
+
'tuple_pattern',
|
|
19
|
+
'list_pattern',
|
|
20
|
+
'as_pattern_target',
|
|
21
|
+
]);
|
|
22
|
+
const BINDING_SLOTS = {
|
|
23
|
+
assignment: ['left'],
|
|
24
|
+
augmented_assignment: ['left'],
|
|
25
|
+
for_statement: ['left'],
|
|
26
|
+
for_in_clause: ['left'],
|
|
27
|
+
type_alias_statement: ['left'],
|
|
28
|
+
named_expression: ['name'],
|
|
29
|
+
function_definition: ['name'],
|
|
30
|
+
class_definition: ['name'],
|
|
31
|
+
default_parameter: ['name'],
|
|
32
|
+
typed_default_parameter: ['name'],
|
|
33
|
+
};
|
|
34
|
+
const OTHER_PARENTS = new Set([
|
|
35
|
+
'import_statement',
|
|
36
|
+
'import_from_statement',
|
|
37
|
+
'aliased_import',
|
|
38
|
+
'dotted_name',
|
|
39
|
+
'relative_import',
|
|
40
|
+
'global_statement',
|
|
41
|
+
'nonlocal_statement',
|
|
42
|
+
]);
|
|
43
|
+
const OTHER_SLOTS = {
|
|
44
|
+
attribute: ['attribute'],
|
|
45
|
+
call: ['function'],
|
|
46
|
+
keyword_argument: ['name'],
|
|
47
|
+
};
|
|
48
|
+
const RELEASING = new Set(['global_statement', 'nonlocal_statement']);
|
|
49
|
+
const SCOPES = new Set([
|
|
50
|
+
'function_definition',
|
|
51
|
+
'lambda',
|
|
52
|
+
'list_comprehension',
|
|
53
|
+
'set_comprehension',
|
|
54
|
+
'dictionary_comprehension',
|
|
55
|
+
'generator_expression',
|
|
56
|
+
]);
|
|
57
|
+
const DEFINITIONS = new Set(['function_definition', 'class_definition']);
|
|
58
|
+
const occupies = (table, node, parent) => table[parent.kind()]?.some((slot) => sameNode(parent.field(slot), node)) ?? false;
|
|
59
|
+
const isBinding = (node, parent) => BINDING_PARENTS.has(parent.kind()) || occupies(BINDING_SLOTS, node, parent);
|
|
60
|
+
const isBaseClass = (_node, parent) => parent.kind() === 'argument_list' && parent.parent()?.kind() === 'class_definition';
|
|
61
|
+
const isUse = (node, parent) => !isBinding(node, parent) &&
|
|
62
|
+
!OTHER_PARENTS.has(parent.kind()) &&
|
|
63
|
+
!occupies(OTHER_SLOTS, node, parent) &&
|
|
64
|
+
!isBaseClass(node, parent);
|
|
65
|
+
function scopeOf(node, parent) {
|
|
66
|
+
const defined = DEFINITIONS.has(parent.kind()) && sameNode(parent.field('name'), node);
|
|
67
|
+
for (let up = defined ? parent.parent() : parent; up; up = up.parent()) {
|
|
68
|
+
if (SCOPES.has(up.kind()))
|
|
69
|
+
return up;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const runsInside = (scope, child) => scope.kind().endsWith('comprehension') ||
|
|
74
|
+
scope.kind() === 'generator_expression' ||
|
|
75
|
+
sameNode(scope.field('body'), child);
|
|
76
|
+
function localNames(root) {
|
|
77
|
+
const names = new Set();
|
|
78
|
+
for (const statement of root.children()) {
|
|
79
|
+
const definition = statement.kind() === 'decorated_definition' ? statement.field('definition') : statement;
|
|
80
|
+
const name = definition && DEFINITIONS.has(definition.kind()) && definition.field('name');
|
|
81
|
+
if (name)
|
|
82
|
+
names.add(name.text());
|
|
83
|
+
}
|
|
84
|
+
return names;
|
|
85
|
+
}
|
|
86
|
+
export const grammar = {
|
|
87
|
+
kinds: ['identifier'],
|
|
88
|
+
isUse,
|
|
89
|
+
isLocalUse: isUse,
|
|
90
|
+
isMemberObject: members.isMemberObject,
|
|
91
|
+
memberChain: members.memberChain,
|
|
92
|
+
localNames,
|
|
93
|
+
shadowing: shadowing({
|
|
94
|
+
scopes: SCOPES,
|
|
95
|
+
isBinding,
|
|
96
|
+
isReleased: (_node, parent) => RELEASING.has(parent.kind()),
|
|
97
|
+
scopeOf,
|
|
98
|
+
runsInside,
|
|
99
|
+
}),
|
|
100
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { addNamed, addSpace, newFacts } from '../binding.js';
|
|
3
|
+
const IMPORTS = /\bimport\b/;
|
|
4
|
+
const STATEMENTS = [{ kind: 'import_statement' }, { kind: 'import_from_statement' }];
|
|
5
|
+
const SCOPES = new Set(['function_definition', 'class_definition']);
|
|
6
|
+
const EVERYTHING = '*';
|
|
7
|
+
const compact = (node) => node.text().replace(/\s+/g, '');
|
|
8
|
+
function enclosing(statement) {
|
|
9
|
+
for (let up = statement.parent(); up; up = up.parent()) {
|
|
10
|
+
if (!SCOPES.has(up.kind()))
|
|
11
|
+
continue;
|
|
12
|
+
const { start, end } = up.range();
|
|
13
|
+
return { start: start.line + 1, end: end.line + 1 };
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
function readImport(facts, statement) {
|
|
18
|
+
const within = enclosing(statement);
|
|
19
|
+
const space = (local, specifier) => addSpace(facts, local, { specifier, ...(within ? { within } : {}) });
|
|
20
|
+
for (const part of statement.children()) {
|
|
21
|
+
if (part.kind() === 'dotted_name') {
|
|
22
|
+
const head = compact(part).split('.')[0] ?? '';
|
|
23
|
+
space(head, head);
|
|
24
|
+
}
|
|
25
|
+
else if (part.kind() === 'aliased_import') {
|
|
26
|
+
const module = part.field('name');
|
|
27
|
+
const alias = part.field('alias');
|
|
28
|
+
if (module && alias)
|
|
29
|
+
space(alias.text(), compact(module));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function readFrom(facts, statement) {
|
|
34
|
+
const source = statement.field('module_name');
|
|
35
|
+
if (!source)
|
|
36
|
+
return;
|
|
37
|
+
const specifier = compact(source);
|
|
38
|
+
const within = enclosing(statement);
|
|
39
|
+
const scope = within ? { within } : {};
|
|
40
|
+
const site = (at, span, name, local) => {
|
|
41
|
+
addNamed(facts, local, { name, link: { specifier }, ...scope });
|
|
42
|
+
const link = { specifier, ...(within ? {} : { exported: local }) };
|
|
43
|
+
facts.sites.push({ at, span, imported: { name, link } });
|
|
44
|
+
};
|
|
45
|
+
for (const part of statement.children()) {
|
|
46
|
+
if (part.kind() === 'wildcard_import') {
|
|
47
|
+
const link = { specifier, exported: EVERYTHING };
|
|
48
|
+
facts.sites.push({ at: part, span: part, imported: { name: EVERYTHING, link } });
|
|
49
|
+
}
|
|
50
|
+
else if (part.kind() === 'dotted_name' && !sameNode(source, part)) {
|
|
51
|
+
site(part, part, compact(part), compact(part));
|
|
52
|
+
}
|
|
53
|
+
else if (part.kind() === 'aliased_import') {
|
|
54
|
+
const name = part.field('name');
|
|
55
|
+
const alias = part.field('alias');
|
|
56
|
+
if (name && alias)
|
|
57
|
+
site(name, part, compact(name), alias.text());
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function readImports(root, source) {
|
|
62
|
+
const facts = newFacts();
|
|
63
|
+
if (!IMPORTS.test(source))
|
|
64
|
+
return facts;
|
|
65
|
+
for (const statement of root.findAll({ rule: { any: STATEMENTS } })) {
|
|
66
|
+
if (statement.kind() === 'import_statement')
|
|
67
|
+
readImport(facts, statement);
|
|
68
|
+
else
|
|
69
|
+
readFrom(facts, statement);
|
|
70
|
+
}
|
|
71
|
+
return facts;
|
|
72
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const SOURCES = ['.py', '.pyi'];
|
|
2
|
+
const PACKAGE_FILES = SOURCES.map((extension) => `__init__${extension}`);
|
|
3
|
+
const NAMESPACE_FILE = PACKAGE_FILES[0] ?? '__init__.py';
|
|
4
|
+
const join = (...parts) => parts.filter(Boolean).join('/');
|
|
5
|
+
const parentOf = (path) => path.slice(0, Math.max(path.lastIndexOf('/'), 0));
|
|
6
|
+
const isSource = (path) => SOURCES.some((extension) => path.endsWith(extension));
|
|
7
|
+
function directories(files) {
|
|
8
|
+
const found = new Set(['']);
|
|
9
|
+
for (const file of files) {
|
|
10
|
+
if (!isSource(file))
|
|
11
|
+
continue;
|
|
12
|
+
for (let dir = parentOf(file); dir && !found.has(dir); dir = parentOf(dir))
|
|
13
|
+
found.add(dir);
|
|
14
|
+
}
|
|
15
|
+
return found;
|
|
16
|
+
}
|
|
17
|
+
export function pythonResolver(_root, files) {
|
|
18
|
+
const dirs = directories(files);
|
|
19
|
+
const hasPackage = (dir) => PACKAGE_FILES.some((name) => files.has(join(dir, name)));
|
|
20
|
+
const regular = (path) => [
|
|
21
|
+
...SOURCES.map((extension) => `${path}${extension}`),
|
|
22
|
+
...PACKAGE_FILES.map((name) => join(path, name)),
|
|
23
|
+
].find((candidate) => files.has(candidate)) ?? null;
|
|
24
|
+
const namespace = (path) => dirs.has(path) ? join(path, NAMESPACE_FILE) : null;
|
|
25
|
+
const topLevel = new Map();
|
|
26
|
+
for (const file of files) {
|
|
27
|
+
if (!isSource(file))
|
|
28
|
+
continue;
|
|
29
|
+
const isPackage = PACKAGE_FILES.some((name) => file.endsWith(`/${name}`));
|
|
30
|
+
const unit = isPackage ? parentOf(file) : file.slice(0, file.lastIndexOf('.'));
|
|
31
|
+
const base = parentOf(unit);
|
|
32
|
+
if (hasPackage(base))
|
|
33
|
+
continue;
|
|
34
|
+
const name = unit.slice(base ? base.length + 1 : 0);
|
|
35
|
+
topLevel.set(name, (topLevel.get(name) ?? new Set()).add(base));
|
|
36
|
+
}
|
|
37
|
+
const rootsOf = new Map();
|
|
38
|
+
const rootsFor = (dir) => {
|
|
39
|
+
const cached = rootsOf.get(dir);
|
|
40
|
+
if (cached)
|
|
41
|
+
return cached;
|
|
42
|
+
const roots = [''];
|
|
43
|
+
for (let up = dir; up; up = parentOf(up))
|
|
44
|
+
if (!hasPackage(up))
|
|
45
|
+
roots.push(up);
|
|
46
|
+
rootsOf.set(dir, roots);
|
|
47
|
+
return roots;
|
|
48
|
+
};
|
|
49
|
+
const relative = (from, specifier) => {
|
|
50
|
+
const dots = specifier.length - specifier.replace(/^\.+/, '').length;
|
|
51
|
+
const segments = parentOf(from).split('/').filter(Boolean);
|
|
52
|
+
if (dots - 1 > segments.length)
|
|
53
|
+
return null;
|
|
54
|
+
const base = segments.slice(0, segments.length - (dots - 1));
|
|
55
|
+
const path = join(...base, ...specifier.slice(dots).split('.').filter(Boolean));
|
|
56
|
+
return regular(path) ?? namespace(path);
|
|
57
|
+
};
|
|
58
|
+
const absolute = (from, specifier) => {
|
|
59
|
+
const parts = specifier.split('.');
|
|
60
|
+
const roots = rootsFor(parentOf(from));
|
|
61
|
+
const only = topLevel.get(parts[0] ?? '');
|
|
62
|
+
const known = only?.size === 1 ? [...only] : [];
|
|
63
|
+
const places = [...roots, ...known.filter((root) => !roots.includes(root))];
|
|
64
|
+
const at = (find) => places.map((root) => find(join(root, ...parts))).find(Boolean) ?? null;
|
|
65
|
+
return at(regular) ?? at(namespace);
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
resolve: (from, specifier) => specifier.startsWith('.') ? relative(from, specifier) : absolute(from, specifier),
|
|
69
|
+
foreign: (_from, specifier) => !specifier.startsWith('.'),
|
|
70
|
+
child(module, segment) {
|
|
71
|
+
const isPackage = PACKAGE_FILES.some((name) => module.endsWith(name));
|
|
72
|
+
if (!isPackage)
|
|
73
|
+
return null;
|
|
74
|
+
const path = join(parentOf(module), segment);
|
|
75
|
+
return regular(path) ?? namespace(path);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { python as spec } from '../../languages/python.js';
|
|
2
|
+
import { bindModule } from '../binding.js';
|
|
3
|
+
import { grammar } from './grammar.js';
|
|
4
|
+
import { readImports } from './imports.js';
|
|
5
|
+
import { pythonResolver } from './resolver.js';
|
|
6
|
+
export const python = {
|
|
7
|
+
languages: [spec.label],
|
|
8
|
+
read({ root, file, symbols, references, owner }) {
|
|
9
|
+
const facts = readImports(root, file.source);
|
|
10
|
+
const input = { root, path: file.path, references, symbols, owner };
|
|
11
|
+
return { added: [], references: bindModule(input, facts, grammar) };
|
|
12
|
+
},
|
|
13
|
+
resolver: pythonResolver,
|
|
14
|
+
isExternal: (specifier) => !specifier.startsWith('.'),
|
|
15
|
+
importsNames: true,
|
|
16
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Reference } from '../types.js';
|
|
2
|
+
import type { ImportFacts, Imported, Lines, Target, Unbound } from './binding.js';
|
|
3
|
+
import type { Shadows } from './shadowing.js';
|
|
4
|
+
export declare function visible<T extends {
|
|
5
|
+
within?: Lines;
|
|
6
|
+
}>(entries: readonly T[] | undefined, lineOf: () => number): T | undefined;
|
|
7
|
+
export declare function targetOf(imported: Imported, written: string): Target;
|
|
8
|
+
export declare function rebind(reference: Reference, facts: ImportFacts, shadows: Shadows, unbound: Unbound | undefined): Reference;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { segmentsOf } from './names.js';
|
|
2
|
+
const REBOUND = new Set(['calls', 'references', 'extends', 'implements', 'aliases']);
|
|
3
|
+
const width = (lines) => lines.end - lines.start;
|
|
4
|
+
export function visible(entries, lineOf) {
|
|
5
|
+
let best;
|
|
6
|
+
for (const entry of entries ?? []) {
|
|
7
|
+
const { within } = entry;
|
|
8
|
+
if (within) {
|
|
9
|
+
const line = lineOf();
|
|
10
|
+
if (line < within.start || line > within.end)
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const tighter = !best?.within || (within !== undefined && width(within) <= width(best.within));
|
|
14
|
+
if (!best || tighter)
|
|
15
|
+
best = entry;
|
|
16
|
+
}
|
|
17
|
+
return best;
|
|
18
|
+
}
|
|
19
|
+
export function targetOf(imported, written) {
|
|
20
|
+
const renamed = imported.name !== written;
|
|
21
|
+
return {
|
|
22
|
+
name: imported.name,
|
|
23
|
+
link: renamed ? { ...imported.link, strict: true } : imported.link,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function rebind(reference, facts, shadows, unbound) {
|
|
27
|
+
if (reference.link !== undefined || !REBOUND.has(reference.relation))
|
|
28
|
+
return reference;
|
|
29
|
+
const line = () => reference.line;
|
|
30
|
+
const bound = (entries, name) => shadows.at(name, reference.line) ? undefined : visible(entries, line);
|
|
31
|
+
const imported = bound(facts.named.get(reference.name), reference.name);
|
|
32
|
+
if (imported)
|
|
33
|
+
return { ...reference, ...targetOf(imported, reference.name) };
|
|
34
|
+
const free = () => {
|
|
35
|
+
const target = unbound?.reference(reference, facts);
|
|
36
|
+
return target ? { ...reference, ...target } : reference;
|
|
37
|
+
};
|
|
38
|
+
const path = segmentsOf(reference.name);
|
|
39
|
+
if (path.length < 2)
|
|
40
|
+
return free();
|
|
41
|
+
const head = path[0] ?? '';
|
|
42
|
+
const via = path.slice(1, -1);
|
|
43
|
+
const space = bound(facts.spaces.get(head), head);
|
|
44
|
+
if (space)
|
|
45
|
+
return { ...reference, link: { specifier: space.specifier, via } };
|
|
46
|
+
const first = bound(facts.named.get(head), head);
|
|
47
|
+
if (first) {
|
|
48
|
+
return { ...reference, link: { specifier: first.link.specifier, via: [first.name, ...via] } };
|
|
49
|
+
}
|
|
50
|
+
return free();
|
|
51
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ModuleContext, ModuleSystem } from './system.js';
|
|
2
|
+
export declare const moduleSystemFor: (label: string) => ModuleSystem | undefined;
|
|
3
|
+
export declare function isTracked(label: string): boolean;
|
|
4
|
+
export declare const importsNames: (label: string) => boolean;
|
|
5
|
+
export declare function createModuleContext(root: string, files: ReadonlySet<string>): ModuleContext;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { hasGrammar } from '../grammars.js';
|
|
2
|
+
import { LANGUAGES, languageFor, needsGrammar } from '../languages/index.js';
|
|
3
|
+
import { ecmascript } from './ecmascript/system.js';
|
|
4
|
+
import { go } from './go/system.js';
|
|
5
|
+
import { java } from './java/system.js';
|
|
6
|
+
import { kotlin } from './kotlin/system.js';
|
|
7
|
+
import { python } from './python/system.js';
|
|
8
|
+
import { rust } from './rust/system.js';
|
|
9
|
+
const SYSTEMS = [ecmascript, python, go, rust, java, kotlin];
|
|
10
|
+
export const moduleSystemFor = (label) => SYSTEMS.find((system) => system.languages.includes(label));
|
|
11
|
+
export function isTracked(label) {
|
|
12
|
+
const spec = LANGUAGES.find((candidate) => candidate.label === label);
|
|
13
|
+
const available = !spec || !needsGrammar(spec) || hasGrammar(spec.id);
|
|
14
|
+
return moduleSystemFor(label) !== undefined && available;
|
|
15
|
+
}
|
|
16
|
+
export const importsNames = (label) => moduleSystemFor(label)?.importsNames ?? true;
|
|
17
|
+
const systemOfPath = (path) => {
|
|
18
|
+
const label = languageFor(path)?.label;
|
|
19
|
+
return label ? moduleSystemFor(label) : undefined;
|
|
20
|
+
};
|
|
21
|
+
export function createModuleContext(root, files) {
|
|
22
|
+
const resolvers = new Map();
|
|
23
|
+
const units = new Map();
|
|
24
|
+
const resolverOf = (path) => {
|
|
25
|
+
const system = systemOfPath(path);
|
|
26
|
+
if (!system)
|
|
27
|
+
return undefined;
|
|
28
|
+
const resolver = resolvers.get(system) ?? system.resolver(root, files);
|
|
29
|
+
resolvers.set(system, resolver);
|
|
30
|
+
return resolver;
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
files,
|
|
34
|
+
resolve: (from, specifier) => resolverOf(from)?.resolve(from, specifier) ?? null,
|
|
35
|
+
child: (module, segment) => resolverOf(module)?.child?.(module, segment) ?? null,
|
|
36
|
+
unit(file) {
|
|
37
|
+
const known = units.get(file);
|
|
38
|
+
if (known !== undefined)
|
|
39
|
+
return known;
|
|
40
|
+
const unit = resolverOf(file)?.unit?.(file) ?? file;
|
|
41
|
+
units.set(file, unit);
|
|
42
|
+
return unit;
|
|
43
|
+
},
|
|
44
|
+
isExternal: (from, specifier) => systemOfPath(from)?.isExternal(specifier) ?? false,
|
|
45
|
+
isForeign: (from, specifier) => resolverOf(from)?.foreign?.(from, specifier) ?? false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { memberAccess } from '../members.js';
|
|
3
|
+
import { shadowing } from '../shadowing.js';
|
|
4
|
+
const valuePath = memberAccess({
|
|
5
|
+
member: 'scoped_identifier',
|
|
6
|
+
object: 'path',
|
|
7
|
+
name: 'name',
|
|
8
|
+
call: 'call_expression',
|
|
9
|
+
callee: 'function',
|
|
10
|
+
});
|
|
11
|
+
const typePath = memberAccess({ member: 'scoped_type_identifier', object: 'path', name: 'name' });
|
|
12
|
+
const BINDING_PARENTS = new Set([
|
|
13
|
+
'parameter',
|
|
14
|
+
'let_declaration',
|
|
15
|
+
'closure_parameters',
|
|
16
|
+
'for_expression',
|
|
17
|
+
'tuple_pattern',
|
|
18
|
+
'reference_pattern',
|
|
19
|
+
'mut_pattern',
|
|
20
|
+
'slice_pattern',
|
|
21
|
+
'or_pattern',
|
|
22
|
+
'captured_pattern',
|
|
23
|
+
'field_pattern',
|
|
24
|
+
'tuple_struct_pattern',
|
|
25
|
+
'match_pattern',
|
|
26
|
+
'let_condition',
|
|
27
|
+
]);
|
|
28
|
+
const EXPRESSION_SLOTS = ['value', 'type', 'body', 'alternative', 'condition'];
|
|
29
|
+
const USE_PARTS = new Set([
|
|
30
|
+
'scoped_identifier',
|
|
31
|
+
'scoped_use_list',
|
|
32
|
+
'use_list',
|
|
33
|
+
'use_as_clause',
|
|
34
|
+
'use_wildcard',
|
|
35
|
+
]);
|
|
36
|
+
const OTHER_SLOTS = {
|
|
37
|
+
call_expression: ['function'],
|
|
38
|
+
macro_invocation: ['macro'],
|
|
39
|
+
function_item: ['name'],
|
|
40
|
+
struct_item: ['name'],
|
|
41
|
+
enum_item: ['name'],
|
|
42
|
+
trait_item: ['name'],
|
|
43
|
+
type_item: ['name'],
|
|
44
|
+
mod_item: ['name'],
|
|
45
|
+
const_item: ['name'],
|
|
46
|
+
static_item: ['name'],
|
|
47
|
+
union_item: ['name'],
|
|
48
|
+
enum_variant: ['name'],
|
|
49
|
+
type_parameter: ['name'],
|
|
50
|
+
};
|
|
51
|
+
const SCOPES = new Set(['function_item', 'closure_expression']);
|
|
52
|
+
const ITEMS = new Set(['function_item', 'struct_item', 'enum_item', 'trait_item', 'type_item']);
|
|
53
|
+
const isBinding = (node, parent) => BINDING_PARENTS.has(parent.kind()) &&
|
|
54
|
+
!EXPRESSION_SLOTS.some((slot) => sameNode(parent.field(slot), node));
|
|
55
|
+
function inUse(parent) {
|
|
56
|
+
let up = parent;
|
|
57
|
+
while (up && USE_PARTS.has(up.kind()))
|
|
58
|
+
up = up.parent();
|
|
59
|
+
return up?.kind() === 'use_declaration';
|
|
60
|
+
}
|
|
61
|
+
const isUse = (node, parent) => !isBinding(node, parent) &&
|
|
62
|
+
!inUse(parent) &&
|
|
63
|
+
!(OTHER_SLOTS[parent.kind()]?.some((slot) => sameNode(parent.field(slot), node)) ?? false);
|
|
64
|
+
const isMemberObject = (node, parent) => valuePath.isMemberObject(node, parent) || typePath.isMemberObject(node, parent);
|
|
65
|
+
function localNames(root) {
|
|
66
|
+
const names = new Set();
|
|
67
|
+
const visit = (scope) => {
|
|
68
|
+
for (const item of scope.children()) {
|
|
69
|
+
const name = ITEMS.has(item.kind()) && item.field('name');
|
|
70
|
+
if (name)
|
|
71
|
+
names.add(name.text());
|
|
72
|
+
if (item.kind() === 'mod_item') {
|
|
73
|
+
const body = item.field('body');
|
|
74
|
+
if (body)
|
|
75
|
+
visit(body);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
visit(root);
|
|
80
|
+
return names;
|
|
81
|
+
}
|
|
82
|
+
export const grammar = {
|
|
83
|
+
kinds: ['identifier', 'type_identifier'],
|
|
84
|
+
isUse,
|
|
85
|
+
isLocalUse: isUse,
|
|
86
|
+
isMemberObject,
|
|
87
|
+
memberChain: (parent) => (parent.kind() === 'scoped_type_identifier' ? typePath : valuePath).memberChain(parent),
|
|
88
|
+
localNames,
|
|
89
|
+
shadowing: shadowing({
|
|
90
|
+
scopes: SCOPES,
|
|
91
|
+
isBinding,
|
|
92
|
+
runsInside: (scope, child) => sameNode(scope.field('body'), child),
|
|
93
|
+
}),
|
|
94
|
+
};
|