claude-db 0.11.1 → 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 +36 -0
- package/README.md +5 -4
- 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/index.js +4 -0
- package/dist/cli/usage.js +3 -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/package.json +16 -5
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { memberAccess } from '../members.js';
|
|
3
|
+
import { shadowing } from '../shadowing.js';
|
|
4
|
+
const selecting = memberAccess({
|
|
5
|
+
member: 'selector_expression',
|
|
6
|
+
object: 'operand',
|
|
7
|
+
name: 'field',
|
|
8
|
+
call: 'call_expression',
|
|
9
|
+
callee: 'function',
|
|
10
|
+
});
|
|
11
|
+
const qualifying = memberAccess({ member: 'qualified_type', object: 'package', name: 'name' });
|
|
12
|
+
const BINDING_PARENTS = new Set([
|
|
13
|
+
'parameter_declaration',
|
|
14
|
+
'variadic_parameter_declaration',
|
|
15
|
+
'var_spec',
|
|
16
|
+
'const_spec',
|
|
17
|
+
]);
|
|
18
|
+
const LISTED_BINDINGS = {
|
|
19
|
+
short_var_declaration: ['left'],
|
|
20
|
+
range_clause: ['left'],
|
|
21
|
+
type_switch_statement: ['alias'],
|
|
22
|
+
};
|
|
23
|
+
const OTHER_PARENTS = new Set(['package_clause', 'import_spec', 'labeled_statement']);
|
|
24
|
+
const OTHER_SLOTS = {
|
|
25
|
+
call_expression: ['function'],
|
|
26
|
+
function_declaration: ['name'],
|
|
27
|
+
type_spec: ['name'],
|
|
28
|
+
type_alias: ['name'],
|
|
29
|
+
};
|
|
30
|
+
const SCOPES = new Set(['function_declaration', 'method_declaration', 'func_literal']);
|
|
31
|
+
const isBinding = (node, parent) => {
|
|
32
|
+
if (BINDING_PARENTS.has(parent.kind()))
|
|
33
|
+
return node.kind() === 'identifier';
|
|
34
|
+
if (parent.kind() !== 'expression_list')
|
|
35
|
+
return false;
|
|
36
|
+
const holder = parent.parent();
|
|
37
|
+
return (LISTED_BINDINGS[holder?.kind() ?? '']?.some((slot) => sameNode(holder?.field(slot) ?? null, parent)) ?? false);
|
|
38
|
+
};
|
|
39
|
+
const isUse = (node, parent) => !isBinding(node, parent) &&
|
|
40
|
+
!OTHER_PARENTS.has(parent.kind()) &&
|
|
41
|
+
!(OTHER_SLOTS[parent.kind()]?.some((slot) => sameNode(parent.field(slot), node)) ?? false);
|
|
42
|
+
const isMemberObject = (node, parent) => selecting.isMemberObject(node, parent) || qualifying.isMemberObject(node, parent);
|
|
43
|
+
export const grammar = {
|
|
44
|
+
kinds: ['identifier', 'type_identifier', 'package_identifier'],
|
|
45
|
+
isUse,
|
|
46
|
+
isLocalUse: isUse,
|
|
47
|
+
isMemberObject,
|
|
48
|
+
memberChain: (parent) => (parent.kind() === 'qualified_type' ? qualifying : selecting).memberChain(parent),
|
|
49
|
+
shadowing: shadowing({
|
|
50
|
+
scopes: SCOPES,
|
|
51
|
+
isBinding,
|
|
52
|
+
runsInside: (scope, child) => sameNode(scope.field('body'), child),
|
|
53
|
+
}),
|
|
54
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { addSpace, newFacts } from '../binding.js';
|
|
2
|
+
const IMPORTS = /\bimport\b/;
|
|
3
|
+
const VERSION_SEGMENT = /^v\d+$/;
|
|
4
|
+
const VERSION_SUFFIX = /\.v\d+$/;
|
|
5
|
+
const IDENTIFIER = /^[A-Za-z_]\w*$/;
|
|
6
|
+
const QUOTES = /^["`]|["`]$/g;
|
|
7
|
+
export function packageName(path) {
|
|
8
|
+
const segments = path.split('/');
|
|
9
|
+
const last = segments.at(-1) ?? '';
|
|
10
|
+
const named = VERSION_SEGMENT.test(last) && segments.length > 1 ? segments.at(-2) : last;
|
|
11
|
+
const name = (named ?? '').replace(VERSION_SUFFIX, '');
|
|
12
|
+
return IDENTIFIER.test(name) ? name : undefined;
|
|
13
|
+
}
|
|
14
|
+
function readSpec(facts, spec) {
|
|
15
|
+
const path = spec.field('path')?.text().replace(QUOTES, '');
|
|
16
|
+
if (!path)
|
|
17
|
+
return;
|
|
18
|
+
const alias = spec.field('name');
|
|
19
|
+
if (alias && alias.kind() !== 'package_identifier')
|
|
20
|
+
return;
|
|
21
|
+
const local = alias?.text() ?? packageName(path);
|
|
22
|
+
if (local)
|
|
23
|
+
addSpace(facts, local, { specifier: path });
|
|
24
|
+
}
|
|
25
|
+
export function readImports(root, source) {
|
|
26
|
+
const facts = newFacts();
|
|
27
|
+
if (!IMPORTS.test(source))
|
|
28
|
+
return facts;
|
|
29
|
+
for (const spec of root.findAll({ rule: { kind: 'import_spec' } }))
|
|
30
|
+
readSpec(facts, spec);
|
|
31
|
+
return facts;
|
|
32
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
const MODULE_LINE = /^\s*module\s+("?)(\S+?)\1\s*(?:\/\/.*)?$/m;
|
|
4
|
+
const ROOT = '.';
|
|
5
|
+
const dirOf = (file) => {
|
|
6
|
+
const slash = file.lastIndexOf('/');
|
|
7
|
+
return slash < 0 ? ROOT : file.slice(0, slash);
|
|
8
|
+
};
|
|
9
|
+
const joinDir = (...parts) => {
|
|
10
|
+
const kept = parts.filter((part) => part && part !== ROOT);
|
|
11
|
+
return kept.length > 0 ? kept.join('/') : ROOT;
|
|
12
|
+
};
|
|
13
|
+
function modulesOf(root, files) {
|
|
14
|
+
const found = [];
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
if (file !== 'go.mod' && !file.endsWith('/go.mod'))
|
|
17
|
+
continue;
|
|
18
|
+
try {
|
|
19
|
+
const path = MODULE_LINE.exec(readFileSync(join(root, file), 'utf8'))?.[2];
|
|
20
|
+
if (path)
|
|
21
|
+
found.push({ path, dir: dirOf(file) });
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return found.sort((a, b) => b.path.length - a.path.length);
|
|
28
|
+
}
|
|
29
|
+
export function goResolver(root, files) {
|
|
30
|
+
const modules = modulesOf(root, files);
|
|
31
|
+
const packages = new Set([...files].filter((file) => file.endsWith('.go')).map(dirOf));
|
|
32
|
+
const inside = (module, specifier) => {
|
|
33
|
+
if (specifier !== module.path && !specifier.startsWith(`${module.path}/`))
|
|
34
|
+
return null;
|
|
35
|
+
const dir = joinDir(module.dir, specifier.slice(module.path.length + 1));
|
|
36
|
+
return packages.has(dir) ? dir : null;
|
|
37
|
+
};
|
|
38
|
+
const vendored = (specifier) => {
|
|
39
|
+
const dir = joinDir('vendor', specifier);
|
|
40
|
+
return packages.has(dir) ? dir : null;
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
resolve(_from, specifier) {
|
|
44
|
+
for (const module of modules) {
|
|
45
|
+
const found = inside(module, specifier);
|
|
46
|
+
if (found)
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
return vendored(specifier);
|
|
50
|
+
},
|
|
51
|
+
foreign: (_from, specifier) => !specifier.startsWith('.'),
|
|
52
|
+
unit: dirOf,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { go as spec } from '../../languages/go.js';
|
|
2
|
+
import { bindModule } from '../binding.js';
|
|
3
|
+
import { grammar } from './grammar.js';
|
|
4
|
+
import { readImports } from './imports.js';
|
|
5
|
+
import { goResolver } from './resolver.js';
|
|
6
|
+
export const go = {
|
|
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: goResolver,
|
|
14
|
+
isExternal: (specifier) => !specifier.startsWith('.'),
|
|
15
|
+
importsNames: false,
|
|
16
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AstNode } from '../../parser.js';
|
|
2
|
+
import type { FindDeclared, Scoped } from '../jvm/receivers.js';
|
|
3
|
+
export interface Declared extends Scoped {
|
|
4
|
+
type: AstNode | null;
|
|
5
|
+
array: boolean;
|
|
6
|
+
holder: AstNode | null;
|
|
7
|
+
}
|
|
8
|
+
export declare const DECLARATION_KINDS: string[];
|
|
9
|
+
export declare function declarationsIn(nodes: ReadonlyMap<string, AstNode[]>): FindDeclared<Declared>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { scopeLookup } from '../jvm/receivers.js';
|
|
2
|
+
export const DECLARATION_KINDS = [
|
|
3
|
+
'field_declaration',
|
|
4
|
+
'constant_declaration',
|
|
5
|
+
'local_variable_declaration',
|
|
6
|
+
'formal_parameter',
|
|
7
|
+
'spread_parameter',
|
|
8
|
+
'catch_formal_parameter',
|
|
9
|
+
'enhanced_for_statement',
|
|
10
|
+
'resource',
|
|
11
|
+
'instanceof_expression',
|
|
12
|
+
];
|
|
13
|
+
const FIELDS = new Set(['field_declaration', 'constant_declaration']);
|
|
14
|
+
const AFTER_DECLARATION = new Set([
|
|
15
|
+
'local_variable_declaration',
|
|
16
|
+
'instanceof_expression',
|
|
17
|
+
'resource',
|
|
18
|
+
]);
|
|
19
|
+
const TYPE_KINDS = new Set([
|
|
20
|
+
'type_identifier',
|
|
21
|
+
'scoped_type_identifier',
|
|
22
|
+
'generic_type',
|
|
23
|
+
'array_type',
|
|
24
|
+
'integral_type',
|
|
25
|
+
'floating_point_type',
|
|
26
|
+
'boolean_type',
|
|
27
|
+
'annotated_type',
|
|
28
|
+
]);
|
|
29
|
+
const BLOCKS = new Set([
|
|
30
|
+
'block',
|
|
31
|
+
'constructor_body',
|
|
32
|
+
'switch_block_statement_group',
|
|
33
|
+
'lambda_expression',
|
|
34
|
+
]);
|
|
35
|
+
function enclosingBlock(node) {
|
|
36
|
+
for (let at = node.parent(); at; at = at.parent())
|
|
37
|
+
if (BLOCKS.has(at.kind()))
|
|
38
|
+
return at;
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function scopeOf(node) {
|
|
42
|
+
const kind = node.kind();
|
|
43
|
+
if (kind === 'formal_parameter' || kind === 'spread_parameter')
|
|
44
|
+
return node.parent()?.parent() ?? null;
|
|
45
|
+
if (kind === 'catch_formal_parameter' || kind === 'resource') {
|
|
46
|
+
const holder = node.parent();
|
|
47
|
+
return kind === 'resource' ? (holder?.parent() ?? null) : holder;
|
|
48
|
+
}
|
|
49
|
+
if (kind === 'enhanced_for_statement')
|
|
50
|
+
return node;
|
|
51
|
+
if (kind === 'instanceof_expression')
|
|
52
|
+
return enclosingBlock(node);
|
|
53
|
+
return node.parent();
|
|
54
|
+
}
|
|
55
|
+
function caughtType(node) {
|
|
56
|
+
const types = node
|
|
57
|
+
.children()
|
|
58
|
+
.find((child) => child.kind() === 'catch_type')
|
|
59
|
+
?.children()
|
|
60
|
+
.filter((child) => TYPE_KINDS.has(child.kind()));
|
|
61
|
+
return types?.length === 1 ? (types[0] ?? null) : null;
|
|
62
|
+
}
|
|
63
|
+
function typeOf(node) {
|
|
64
|
+
const kind = node.kind();
|
|
65
|
+
if (kind === 'instanceof_expression')
|
|
66
|
+
return node.field('right');
|
|
67
|
+
if (kind === 'catch_formal_parameter')
|
|
68
|
+
return caughtType(node);
|
|
69
|
+
if (kind === 'spread_parameter') {
|
|
70
|
+
return node.children().find((child) => TYPE_KINDS.has(child.kind())) ?? null;
|
|
71
|
+
}
|
|
72
|
+
return node.field('type');
|
|
73
|
+
}
|
|
74
|
+
function namesOf(node) {
|
|
75
|
+
const direct = node.field('name');
|
|
76
|
+
if (direct)
|
|
77
|
+
return [{ name: direct, holder: node }];
|
|
78
|
+
return node
|
|
79
|
+
.children()
|
|
80
|
+
.filter((child) => child.kind() === 'variable_declarator')
|
|
81
|
+
.flatMap((holder) => {
|
|
82
|
+
const name = holder.field('name');
|
|
83
|
+
return name ? [{ name, holder }] : [];
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function declaredBy(node) {
|
|
87
|
+
const scope = scopeOf(node);
|
|
88
|
+
if (!scope)
|
|
89
|
+
return [];
|
|
90
|
+
const kind = node.kind();
|
|
91
|
+
const field = FIELDS.has(kind);
|
|
92
|
+
const { start, end } = scope.range();
|
|
93
|
+
const at = AFTER_DECLARATION.has(kind) ? node.range().start.index : start.index;
|
|
94
|
+
const type = typeOf(node);
|
|
95
|
+
return namesOf(node).map(({ name, holder }) => ({
|
|
96
|
+
name: name.text(),
|
|
97
|
+
from: start.index,
|
|
98
|
+
to: end.index,
|
|
99
|
+
at,
|
|
100
|
+
field,
|
|
101
|
+
type,
|
|
102
|
+
array: kind === 'spread_parameter' || holder.field('dimensions') !== null,
|
|
103
|
+
holder: holder === node ? null : holder,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
export function declarationsIn(nodes) {
|
|
107
|
+
return scopeLookup(DECLARATION_KINDS.flatMap((kind) => nodes.get(kind) ?? []).flatMap(declaredBy));
|
|
108
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { memberAccess } from '../members.js';
|
|
3
|
+
import { CLASS_LIKE, here, qualifiedTarget, referenceTarget } from '../jvm/linking.js';
|
|
4
|
+
const fields = memberAccess({ member: 'field_access', object: 'object', name: 'field' });
|
|
5
|
+
const DECLARING_SLOTS = {
|
|
6
|
+
class_declaration: ['name'],
|
|
7
|
+
record_declaration: ['name'],
|
|
8
|
+
interface_declaration: ['name'],
|
|
9
|
+
enum_declaration: ['name'],
|
|
10
|
+
annotation_type_declaration: ['name'],
|
|
11
|
+
method_declaration: ['name'],
|
|
12
|
+
constructor_declaration: ['name'],
|
|
13
|
+
formal_parameter: ['name'],
|
|
14
|
+
spread_parameter: ['name'],
|
|
15
|
+
variable_declarator: ['name'],
|
|
16
|
+
enhanced_for_statement: ['name'],
|
|
17
|
+
catch_formal_parameter: ['name'],
|
|
18
|
+
enum_constant: ['name'],
|
|
19
|
+
method_invocation: ['name'],
|
|
20
|
+
field_access: ['field'],
|
|
21
|
+
instanceof_expression: ['name'],
|
|
22
|
+
};
|
|
23
|
+
const OTHER_PARENTS = new Set([
|
|
24
|
+
'scoped_identifier',
|
|
25
|
+
'package_declaration',
|
|
26
|
+
'import_declaration',
|
|
27
|
+
'type_parameter',
|
|
28
|
+
'inferred_parameters',
|
|
29
|
+
'labeled_statement',
|
|
30
|
+
'break_statement',
|
|
31
|
+
'continue_statement',
|
|
32
|
+
]);
|
|
33
|
+
const OBJECT_PARENTS = new Set(['method_invocation', 'field_access']);
|
|
34
|
+
const ANNOTATIONS = new Set(['marker_annotation', 'annotation']);
|
|
35
|
+
const ANNOTATION_RULES = [...ANNOTATIONS].map((kind) => ({ kind }));
|
|
36
|
+
const occupies = (table, node, parent) => table[parent.kind()]?.some((slot) => sameNode(parent.field(slot), node)) ?? false;
|
|
37
|
+
const HERITAGE_LISTS = new Set(['super_interfaces', 'extends_interfaces']);
|
|
38
|
+
function isHeritage(node, parent) {
|
|
39
|
+
let holder = parent;
|
|
40
|
+
if (holder.kind() === 'generic_type') {
|
|
41
|
+
if (!sameNode(holder.children()[0] ?? null, node))
|
|
42
|
+
return false;
|
|
43
|
+
holder = holder.parent() ?? holder;
|
|
44
|
+
}
|
|
45
|
+
if (holder.kind() === 'superclass')
|
|
46
|
+
return true;
|
|
47
|
+
return holder.kind() === 'type_list' && HERITAGE_LISTS.has(holder.parent()?.kind() ?? '');
|
|
48
|
+
}
|
|
49
|
+
function isCreation(node, parent) {
|
|
50
|
+
const generic = parent.kind() === 'generic_type';
|
|
51
|
+
if (generic && !sameNode(parent.children()[0] ?? null, node))
|
|
52
|
+
return false;
|
|
53
|
+
const holder = generic ? parent.parent() : parent;
|
|
54
|
+
return (holder?.kind() === 'object_creation_expression' &&
|
|
55
|
+
sameNode(holder.field('type'), generic ? parent : node));
|
|
56
|
+
}
|
|
57
|
+
const isUse = (node, parent) => !OTHER_PARENTS.has(parent.kind()) &&
|
|
58
|
+
!occupies(DECLARING_SLOTS, node, parent) &&
|
|
59
|
+
!isHeritage(node, parent) &&
|
|
60
|
+
!isCreation(node, parent);
|
|
61
|
+
function namesAType(node, parent) {
|
|
62
|
+
const kind = parent.kind();
|
|
63
|
+
if (OBJECT_PARENTS.has(kind))
|
|
64
|
+
return sameNode(parent.field('object'), node);
|
|
65
|
+
if (ANNOTATIONS.has(kind))
|
|
66
|
+
return sameNode(parent.field('name'), node);
|
|
67
|
+
if (kind === 'method_reference')
|
|
68
|
+
return sameNode(parent.children()[0] ?? null, node);
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
function qualifiedChain(node) {
|
|
72
|
+
let outer = node.parent();
|
|
73
|
+
while (outer?.parent()?.kind() === 'scoped_type_identifier')
|
|
74
|
+
outer = outer.parent();
|
|
75
|
+
if (!outer || outer.range().start.line !== node.range().start.line)
|
|
76
|
+
return undefined;
|
|
77
|
+
const offset = node.range().start.column - outer.range().start.column;
|
|
78
|
+
const segments = [];
|
|
79
|
+
let at = -1;
|
|
80
|
+
for (const part of outer.text().matchAll(/[\w$]+/g)) {
|
|
81
|
+
if (part.index === offset)
|
|
82
|
+
at = segments.length;
|
|
83
|
+
segments.push(part[0]);
|
|
84
|
+
}
|
|
85
|
+
return at < 0 ? undefined : { segments, at };
|
|
86
|
+
}
|
|
87
|
+
export function grammarFor(own) {
|
|
88
|
+
const qualifiedAnnotation = (node) => {
|
|
89
|
+
const segments = node.text().replace(/\s+/g, '').split('.');
|
|
90
|
+
const name = segments.pop();
|
|
91
|
+
if (!name || segments.length === 0 || !CLASS_LIKE.test(name))
|
|
92
|
+
return undefined;
|
|
93
|
+
return { name, link: { specifier: segments.join('.'), strict: true } };
|
|
94
|
+
};
|
|
95
|
+
const qualified = (node, facts) => {
|
|
96
|
+
const chain = qualifiedChain(node);
|
|
97
|
+
return chain ? qualifiedTarget(own, chain, facts) : undefined;
|
|
98
|
+
};
|
|
99
|
+
const target = (node, parent, facts) => {
|
|
100
|
+
const text = node.text();
|
|
101
|
+
if (node.kind() === 'scoped_identifier') {
|
|
102
|
+
return ANNOTATIONS.has(parent.kind()) && sameNode(parent.field('name'), node)
|
|
103
|
+
? qualifiedAnnotation(node)
|
|
104
|
+
: undefined;
|
|
105
|
+
}
|
|
106
|
+
if (node.kind() === 'identifier') {
|
|
107
|
+
return CLASS_LIKE.test(text) && namesAType(node, parent)
|
|
108
|
+
? { name: text, link: here(own, facts.opens) }
|
|
109
|
+
: undefined;
|
|
110
|
+
}
|
|
111
|
+
if (!isUse(node, parent))
|
|
112
|
+
return undefined;
|
|
113
|
+
if (parent.kind() === 'scoped_type_identifier')
|
|
114
|
+
return qualified(node, facts);
|
|
115
|
+
return { name: text, link: here(own, facts.opens) };
|
|
116
|
+
};
|
|
117
|
+
const reference = (given, facts) => referenceTarget(own, given, facts);
|
|
118
|
+
return {
|
|
119
|
+
kinds: ['identifier', 'type_identifier'],
|
|
120
|
+
isUse,
|
|
121
|
+
isLocalUse: isUse,
|
|
122
|
+
isMemberObject: fields.isMemberObject,
|
|
123
|
+
memberChain: fields.memberChain,
|
|
124
|
+
unbound: {
|
|
125
|
+
rules: [
|
|
126
|
+
{ kind: 'type_identifier' },
|
|
127
|
+
{ kind: 'identifier', regex: '^[A-Z]' },
|
|
128
|
+
{ kind: 'scoped_identifier', inside: { any: ANNOTATION_RULES } },
|
|
129
|
+
],
|
|
130
|
+
claims: (node, parent) => parent.kind() === 'scoped_type_identifier' && (qualifiedChain(node)?.at ?? 0) > 0,
|
|
131
|
+
target,
|
|
132
|
+
reference,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { addImport } from '../jvm/linking.js';
|
|
2
|
+
export const IMPORT_KIND = 'import_declaration';
|
|
3
|
+
const PATHS = new Set(['scoped_identifier', 'identifier']);
|
|
4
|
+
export function readImport(facts, statement) {
|
|
5
|
+
const parts = statement.children();
|
|
6
|
+
const at = parts.find((child) => PATHS.has(child.kind()));
|
|
7
|
+
if (!at)
|
|
8
|
+
return;
|
|
9
|
+
const kinds = parts.map((child) => child.kind());
|
|
10
|
+
const segments = at.text().replace(/\s+/g, '').split('.');
|
|
11
|
+
const wildcard = kinds.includes('asterisk');
|
|
12
|
+
addImport(facts, { segments, at, span: statement, wildcard, member: kinds.includes('static') });
|
|
13
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { originFrom, originOf } from '../../ast.js';
|
|
2
|
+
import { OUTSIDE, PARENT, SELF, TYPE_NAME, UNKNOWN, attachReceivers, memoize, pathReceivers, resultOf, symbolAt, typeParameterNames, } from '../jvm/receivers.js';
|
|
3
|
+
import { DECLARATION_KINDS, declarationsIn } from './declarations.js';
|
|
4
|
+
const INFERRED = 'var';
|
|
5
|
+
const EXACT = true;
|
|
6
|
+
const VALUES = new Set([
|
|
7
|
+
'integral_type',
|
|
8
|
+
'floating_point_type',
|
|
9
|
+
'boolean_type',
|
|
10
|
+
'void_type',
|
|
11
|
+
'array_type',
|
|
12
|
+
]);
|
|
13
|
+
const LITERALS = new Set(['string_literal', 'text_block', 'array_creation_expression']);
|
|
14
|
+
const PATH_KINDS = new Set(['type_identifier', 'scoped_type_identifier', 'generic_type']);
|
|
15
|
+
export const RECEIVER_KINDS = [
|
|
16
|
+
...DECLARATION_KINDS,
|
|
17
|
+
'method_invocation',
|
|
18
|
+
'method_declaration',
|
|
19
|
+
'type_parameter',
|
|
20
|
+
];
|
|
21
|
+
function pathOf(node) {
|
|
22
|
+
const kind = node.kind();
|
|
23
|
+
if (kind === 'type_identifier' || kind === 'identifier')
|
|
24
|
+
return [node.text()];
|
|
25
|
+
if (kind === 'generic_type') {
|
|
26
|
+
const [head] = node.children();
|
|
27
|
+
return head ? pathOf(head) : [];
|
|
28
|
+
}
|
|
29
|
+
if (kind !== 'scoped_type_identifier')
|
|
30
|
+
return [];
|
|
31
|
+
return node
|
|
32
|
+
.children()
|
|
33
|
+
.filter((child) => PATH_KINDS.has(child.kind()))
|
|
34
|
+
.flatMap(pathOf);
|
|
35
|
+
}
|
|
36
|
+
function dottedPath(node) {
|
|
37
|
+
if (node.kind() === 'identifier')
|
|
38
|
+
return [node.text()];
|
|
39
|
+
if (node.kind() !== 'field_access')
|
|
40
|
+
return null;
|
|
41
|
+
const owner = node.field('object');
|
|
42
|
+
const name = node.field('field');
|
|
43
|
+
const head = owner ? dottedPath(owner) : null;
|
|
44
|
+
return head && name ? [...head, name.text()] : null;
|
|
45
|
+
}
|
|
46
|
+
function firstNamed(node) {
|
|
47
|
+
return node.children().find((child) => !['(', ')'].includes(child.kind())) ?? null;
|
|
48
|
+
}
|
|
49
|
+
export function javaTyping(input) {
|
|
50
|
+
const { nodes, facts, own, symbols } = input;
|
|
51
|
+
const generics = typeParameterNames(nodes);
|
|
52
|
+
const find = declarationsIn(nodes);
|
|
53
|
+
const ofPath = pathReceivers({ own, facts, generics, exact: EXACT });
|
|
54
|
+
const ofType = (node) => {
|
|
55
|
+
if (!node)
|
|
56
|
+
return null;
|
|
57
|
+
if (VALUES.has(node.kind()))
|
|
58
|
+
return OUTSIDE;
|
|
59
|
+
if (node.kind() === 'annotated_type') {
|
|
60
|
+
return ofType(node.children().find((child) => !child.kind().endsWith('annotation')) ?? null);
|
|
61
|
+
}
|
|
62
|
+
const path = pathOf(node);
|
|
63
|
+
return path.length === 1 && path[0] === INFERRED ? null : ofPath(path);
|
|
64
|
+
};
|
|
65
|
+
const ofValue = (value) => {
|
|
66
|
+
if (!value)
|
|
67
|
+
return null;
|
|
68
|
+
const kind = value.kind();
|
|
69
|
+
if (kind === 'object_creation_expression')
|
|
70
|
+
return ofType(value.field('type'));
|
|
71
|
+
return kind === 'method_invocation' ? resultOf(originOf(value), EXACT) : null;
|
|
72
|
+
};
|
|
73
|
+
const ofDeclared = memoize((declared) => {
|
|
74
|
+
if (declared.array)
|
|
75
|
+
return OUTSIDE;
|
|
76
|
+
if (declared.type?.text() !== INFERRED)
|
|
77
|
+
return ofType(declared.type);
|
|
78
|
+
return ofValue(declared.holder?.field('value'));
|
|
79
|
+
});
|
|
80
|
+
const ofField = (access, position) => {
|
|
81
|
+
const owner = access.field('object');
|
|
82
|
+
const name = access.field('field');
|
|
83
|
+
if (owner?.kind() === 'this' && name) {
|
|
84
|
+
const declared = find(name.text(), position, true);
|
|
85
|
+
return declared ? ofDeclared(declared) : null;
|
|
86
|
+
}
|
|
87
|
+
const outerThis = access.children().at(-1)?.kind() === 'this';
|
|
88
|
+
if (outerThis)
|
|
89
|
+
return owner?.kind() === 'identifier' ? ofType(owner) : null;
|
|
90
|
+
const path = dottedPath(access);
|
|
91
|
+
return path && TYPE_NAME.test(path.at(-1) ?? '') ? ofPath(path) : null;
|
|
92
|
+
};
|
|
93
|
+
const ofObject = (object, position) => {
|
|
94
|
+
const kind = object.kind();
|
|
95
|
+
if (kind === 'this')
|
|
96
|
+
return SELF;
|
|
97
|
+
if (kind === 'super')
|
|
98
|
+
return PARENT;
|
|
99
|
+
if (LITERALS.has(kind))
|
|
100
|
+
return OUTSIDE;
|
|
101
|
+
if (kind === 'method_invocation')
|
|
102
|
+
return resultOf(originOf(object), EXACT);
|
|
103
|
+
if (kind === 'object_creation_expression' || kind === 'cast_expression') {
|
|
104
|
+
return ofType(object.field('type'));
|
|
105
|
+
}
|
|
106
|
+
if (kind === 'parenthesized_expression') {
|
|
107
|
+
const inner = firstNamed(object);
|
|
108
|
+
return inner ? ofObject(inner, position) : null;
|
|
109
|
+
}
|
|
110
|
+
if (kind === 'field_access')
|
|
111
|
+
return ofField(object, position);
|
|
112
|
+
if (kind !== 'identifier')
|
|
113
|
+
return null;
|
|
114
|
+
const name = object.text();
|
|
115
|
+
const declared = find(name, position);
|
|
116
|
+
if (declared)
|
|
117
|
+
return ofDeclared(declared);
|
|
118
|
+
return TYPE_NAME.test(name) ? ofPath([name]) : null;
|
|
119
|
+
};
|
|
120
|
+
const receiverOf = (object, position) => object ? (ofObject(object, position) ?? UNKNOWN) : SELF;
|
|
121
|
+
const attach = (references) => {
|
|
122
|
+
const calls = new Map();
|
|
123
|
+
for (const call of nodes.get('method_invocation') ?? []) {
|
|
124
|
+
const range = call.range();
|
|
125
|
+
calls.set(originFrom(range), receiverOf(call.field('object'), range.start.index));
|
|
126
|
+
}
|
|
127
|
+
const idOf = symbolAt(symbols);
|
|
128
|
+
const returns = new Map();
|
|
129
|
+
for (const method of nodes.get('method_declaration') ?? []) {
|
|
130
|
+
const name = method.field('name');
|
|
131
|
+
const id = name ? idOf(name) : undefined;
|
|
132
|
+
const receiver = ofType(method.field('type'));
|
|
133
|
+
if (id && receiver)
|
|
134
|
+
returns.set(id, receiver);
|
|
135
|
+
}
|
|
136
|
+
return attachReceivers(references, { calls, returns });
|
|
137
|
+
};
|
|
138
|
+
return { receiverOf, attach };
|
|
139
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const java: import("../system.js").ModuleSystem;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { java as spec } from '../../languages/java.js';
|
|
2
|
+
import { jvmSystem } from '../jvm/system.js';
|
|
3
|
+
import { grammarFor } from './grammar.js';
|
|
4
|
+
import { IMPORT_KIND, readImport } from './imports.js';
|
|
5
|
+
import { RECEIVER_KINDS, javaTyping } from './receivers.js';
|
|
6
|
+
export const java = jvmSystem({
|
|
7
|
+
label: spec.label,
|
|
8
|
+
importKind: IMPORT_KIND,
|
|
9
|
+
readImport,
|
|
10
|
+
grammarFor,
|
|
11
|
+
typing: javaTyping,
|
|
12
|
+
nodeKinds: RECEIVER_KINDS,
|
|
13
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AstNode } from '../../parser.js';
|
|
2
|
+
import type { ModuleLink, Reference } from '../../types.js';
|
|
3
|
+
import type { ImportFacts, Target } from '../binding.js';
|
|
4
|
+
export declare const CLASS_LIKE: RegExp;
|
|
5
|
+
export interface Chain {
|
|
6
|
+
segments: string[];
|
|
7
|
+
at: number;
|
|
8
|
+
}
|
|
9
|
+
export interface ImportPath {
|
|
10
|
+
segments: string[];
|
|
11
|
+
at: AstNode;
|
|
12
|
+
span: AstNode;
|
|
13
|
+
wildcard: boolean;
|
|
14
|
+
member: boolean;
|
|
15
|
+
alias?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare const here: (own: string, opens: string[]) => ModuleLink;
|
|
18
|
+
export declare function addImport(facts: ImportFacts, path: ImportPath): void;
|
|
19
|
+
export declare function qualifiedTarget(own: string, chain: Chain, facts: ImportFacts): Target | undefined;
|
|
20
|
+
export declare function referenceTarget(own: string, given: Reference, facts: ImportFacts): Target | undefined;
|