claude-db 0.11.1 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -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/commands/pick.js +4 -2
- 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/pick/run.d.ts +6 -0
- package/dist/pick/run.js +8 -1
- package/dist/types.d.ts +1 -1
- package/dist/usages/find.js +1 -0
- package/dist/util/claude-cli.d.ts +1 -0
- package/dist/util/claude-cli.js +15 -3
- package/docs/how-it-works.md +10 -8
- package/package.json +16 -5
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const BODIES = ['class_body', 'enum_class_body'];
|
|
2
|
+
export const kotlin = {
|
|
3
|
+
id: 'kotlin',
|
|
4
|
+
label: 'kotlin',
|
|
5
|
+
extensions: ['.kt', '.kts'],
|
|
6
|
+
definitions: [
|
|
7
|
+
{ kind: 'class_declaration', field: [], nameKind: 'type_identifier', symbol: 'class' },
|
|
8
|
+
{ kind: 'object_declaration', field: [], nameKind: 'type_identifier', symbol: 'class' },
|
|
9
|
+
{ kind: 'type_alias', field: [], nameKind: 'type_identifier', symbol: 'type' },
|
|
10
|
+
{
|
|
11
|
+
kind: 'function_declaration',
|
|
12
|
+
field: [],
|
|
13
|
+
nameKind: 'simple_identifier',
|
|
14
|
+
symbol: 'function',
|
|
15
|
+
memberOf: BODIES,
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
references: [],
|
|
19
|
+
};
|
|
@@ -9,7 +9,15 @@ export const python = {
|
|
|
9
9
|
references: [
|
|
10
10
|
{ kind: 'call', field: ['function'], relation: 'calls' },
|
|
11
11
|
{ kind: 'call', field: ['function', 'attribute'], relation: 'calls' },
|
|
12
|
-
{
|
|
13
|
-
|
|
12
|
+
{
|
|
13
|
+
kind: 'class_definition',
|
|
14
|
+
field: ['superclasses'],
|
|
15
|
+
relation: 'extends',
|
|
16
|
+
listOf: [
|
|
17
|
+
{ kind: 'identifier' },
|
|
18
|
+
{ kind: 'attribute' },
|
|
19
|
+
{ kind: 'subscript', field: 'value' },
|
|
20
|
+
],
|
|
21
|
+
},
|
|
14
22
|
],
|
|
15
23
|
};
|
|
@@ -3,6 +3,13 @@ export interface DefinitionRule {
|
|
|
3
3
|
kind: string;
|
|
4
4
|
field: string[];
|
|
5
5
|
symbol: SymbolKind;
|
|
6
|
+
nameKind?: string;
|
|
7
|
+
memberOf?: string[];
|
|
8
|
+
}
|
|
9
|
+
export interface ListItem {
|
|
10
|
+
kind: string;
|
|
11
|
+
field?: string;
|
|
12
|
+
select?: string[];
|
|
6
13
|
}
|
|
7
14
|
export interface ReferenceRule {
|
|
8
15
|
kind: string;
|
|
@@ -10,6 +17,10 @@ export interface ReferenceRule {
|
|
|
10
17
|
relation: EdgeRelation;
|
|
11
18
|
namePattern?: RegExp;
|
|
12
19
|
excludeParents?: string[];
|
|
20
|
+
objectKinds?: string[];
|
|
21
|
+
listOf?: ListItem[];
|
|
22
|
+
qualifier?: string;
|
|
23
|
+
parents?: string[];
|
|
13
24
|
}
|
|
14
25
|
export interface LanguageSpec {
|
|
15
26
|
id: string;
|
|
@@ -11,6 +11,7 @@ export const rust = {
|
|
|
11
11
|
],
|
|
12
12
|
references: [
|
|
13
13
|
{ kind: 'call_expression', field: ['function'], relation: 'calls' },
|
|
14
|
-
{ kind: '
|
|
14
|
+
{ kind: 'call_expression', field: ['function', 'name'], relation: 'calls' },
|
|
15
|
+
{ kind: 'call_expression', field: ['function', 'field'], relation: 'calls' },
|
|
15
16
|
],
|
|
16
17
|
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CodeSymbol } from '../../types.js';
|
|
2
|
+
import type { AstNode } from '../parser.js';
|
|
3
|
+
import type { ModuleLink, Reference } from '../types.js';
|
|
4
|
+
import type { Shadows } from './shadowing.js';
|
|
5
|
+
export type InContext = (node: AstNode, parent: AstNode) => boolean;
|
|
6
|
+
export interface Lines {
|
|
7
|
+
start: number;
|
|
8
|
+
end: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Imported {
|
|
11
|
+
name: string;
|
|
12
|
+
link: ModuleLink;
|
|
13
|
+
within?: Lines;
|
|
14
|
+
}
|
|
15
|
+
export interface Namespace {
|
|
16
|
+
specifier: string;
|
|
17
|
+
within?: Lines;
|
|
18
|
+
}
|
|
19
|
+
export interface ImportSite {
|
|
20
|
+
at: AstNode;
|
|
21
|
+
span: AstNode;
|
|
22
|
+
imported: Imported;
|
|
23
|
+
}
|
|
24
|
+
export interface ImportFacts {
|
|
25
|
+
named: Map<string, Imported[]>;
|
|
26
|
+
spaces: Map<string, Namespace[]>;
|
|
27
|
+
opens: string[];
|
|
28
|
+
sites: ImportSite[];
|
|
29
|
+
}
|
|
30
|
+
export interface Target {
|
|
31
|
+
name: string;
|
|
32
|
+
link?: ModuleLink;
|
|
33
|
+
}
|
|
34
|
+
export interface Unbound {
|
|
35
|
+
rules: readonly Record<string, unknown>[];
|
|
36
|
+
claims?: InContext;
|
|
37
|
+
target: (node: AstNode, parent: AstNode, facts: ImportFacts) => Target | undefined;
|
|
38
|
+
reference: (reference: Reference, facts: ImportFacts) => Target | undefined;
|
|
39
|
+
}
|
|
40
|
+
export interface Grammar {
|
|
41
|
+
kinds: readonly string[];
|
|
42
|
+
shorthand?: string;
|
|
43
|
+
isUse: InContext;
|
|
44
|
+
isLocalUse: InContext;
|
|
45
|
+
isMemberObject: InContext;
|
|
46
|
+
memberChain: (parent: AstNode) => string[];
|
|
47
|
+
localNames?: (root: AstNode) => Set<string>;
|
|
48
|
+
shadowing?: (nodes: readonly AstNode[]) => Shadows;
|
|
49
|
+
unbound?: Unbound;
|
|
50
|
+
}
|
|
51
|
+
export interface BindInput {
|
|
52
|
+
root: AstNode;
|
|
53
|
+
path: string;
|
|
54
|
+
references: Reference[];
|
|
55
|
+
symbols: CodeSymbol[];
|
|
56
|
+
owner: (line: number) => CodeSymbol | null;
|
|
57
|
+
}
|
|
58
|
+
export declare function newFacts(): ImportFacts;
|
|
59
|
+
export declare const addNamed: (facts: ImportFacts, local: string, imported: Imported) => void;
|
|
60
|
+
export declare const addSpace: (facts: ImportFacts, local: string, space: Namespace) => void;
|
|
61
|
+
export declare function bindModule(input: BindInput, facts: ImportFacts, grammar: Grammar): Reference[];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { originFrom } from '../ast.js';
|
|
2
|
+
import { rebind } from './rebind.js';
|
|
3
|
+
import { NOTHING } from './shadowing.js';
|
|
4
|
+
import { findUses, scopeOf, usesIn } from './uses.js';
|
|
5
|
+
export function newFacts() {
|
|
6
|
+
return { named: new Map(), spaces: new Map(), opens: [], sites: [] };
|
|
7
|
+
}
|
|
8
|
+
const append = (into, key, value) => {
|
|
9
|
+
into.set(key, [...(into.get(key) ?? []), value]);
|
|
10
|
+
};
|
|
11
|
+
export const addNamed = (facts, local, imported) => append(facts.named, local, imported);
|
|
12
|
+
export const addSpace = (facts, local, space) => append(facts.spaces, local, space);
|
|
13
|
+
export function bindModule(input, facts, grammar) {
|
|
14
|
+
const { root, path, references, symbols, owner } = input;
|
|
15
|
+
const scope = scopeOf(root, symbols, facts, grammar);
|
|
16
|
+
const nothingBound = facts.named.size === 0 &&
|
|
17
|
+
facts.spaces.size === 0 &&
|
|
18
|
+
facts.sites.length === 0 &&
|
|
19
|
+
scope.locals.size === 0 &&
|
|
20
|
+
!grammar.unbound;
|
|
21
|
+
if (nothingBound)
|
|
22
|
+
return references;
|
|
23
|
+
const make = (node, target, relation, span = node) => {
|
|
24
|
+
const range = node.range();
|
|
25
|
+
const line = range.start.line + 1;
|
|
26
|
+
return {
|
|
27
|
+
file: path,
|
|
28
|
+
name: target.name,
|
|
29
|
+
relation,
|
|
30
|
+
line,
|
|
31
|
+
from: owner(line),
|
|
32
|
+
origin: originFrom(span === node ? range : span.range()),
|
|
33
|
+
...(target.link ? { link: target.link } : {}),
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const found = findUses(root, scope, grammar);
|
|
37
|
+
const shadows = grammar.shadowing?.(found) ?? NOTHING;
|
|
38
|
+
return [
|
|
39
|
+
...references.map((reference) => rebind(reference, facts, shadows, grammar.unbound)),
|
|
40
|
+
...facts.sites.map((site) => make(site.at, site.imported, 'imports', site.span)),
|
|
41
|
+
...usesIn(found, scope, grammar, make, shadows),
|
|
42
|
+
];
|
|
43
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CodeSymbol } from '../../../types.js';
|
|
2
|
+
import type { AstNode } from '../../parser.js';
|
|
3
|
+
import type { Reference, SourceFile } from '../../types.js';
|
|
4
|
+
export declare function bindImports(root: AstNode, file: SourceFile, references: Reference[], symbols: CodeSymbol[], owner: (line: number) => CodeSymbol | null): Reference[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { bindModule } from '../binding.js';
|
|
2
|
+
import { isLocalContext, isLocalExport, isMemberObject, isUse, memberChain } from './contexts.js';
|
|
3
|
+
import { readImports, readLoadedImports } from './imports.js';
|
|
4
|
+
const SHORTHAND = 'shorthand_property_identifier';
|
|
5
|
+
const grammar = {
|
|
6
|
+
kinds: ['identifier', SHORTHAND],
|
|
7
|
+
shorthand: SHORTHAND,
|
|
8
|
+
isUse: (node, parent) => isLocalExport(node, parent) || isUse(node, parent),
|
|
9
|
+
isLocalUse: isLocalContext,
|
|
10
|
+
isMemberObject,
|
|
11
|
+
memberChain,
|
|
12
|
+
};
|
|
13
|
+
export function bindImports(root, file, references, symbols, owner) {
|
|
14
|
+
const facts = readImports(root);
|
|
15
|
+
readLoadedImports(root, file.source, facts);
|
|
16
|
+
return bindModule({ root, path: file.path, references, symbols, owner }, facts, grammar);
|
|
17
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AstNode } from '../../parser.js';
|
|
2
|
+
import type { InContext } from '../binding.js';
|
|
3
|
+
export declare const isReceiverOfCall: (member: AstNode) => boolean, isMemberObject: InContext, memberChain: (first: AstNode) => string[];
|
|
4
|
+
export declare const isLocalExport: InContext;
|
|
5
|
+
export declare const isUse: InContext;
|
|
6
|
+
export declare const isLocalContext: InContext;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { sameNode } from '../../ast.js';
|
|
2
|
+
import { memberAccess } from '../members.js';
|
|
3
|
+
import { moduleOf } from './specifiers.js';
|
|
4
|
+
const propertyOf = (member) => member.children().at(-1)?.text() ?? '';
|
|
5
|
+
const members = memberAccess({
|
|
6
|
+
member: 'member_expression',
|
|
7
|
+
object: 'object',
|
|
8
|
+
name: propertyOf,
|
|
9
|
+
call: 'call_expression',
|
|
10
|
+
callee: 'function',
|
|
11
|
+
});
|
|
12
|
+
export const { isReceiverOfCall, isMemberObject, memberChain } = members;
|
|
13
|
+
const NON_USE_PARENTS = new Set([
|
|
14
|
+
'import_specifier',
|
|
15
|
+
'import_clause',
|
|
16
|
+
'namespace_import',
|
|
17
|
+
'formal_parameters',
|
|
18
|
+
'pair_pattern',
|
|
19
|
+
'jsx_opening_element',
|
|
20
|
+
'jsx_self_closing_element',
|
|
21
|
+
'jsx_closing_element',
|
|
22
|
+
]);
|
|
23
|
+
const VALUE_PARENTS = new Set([
|
|
24
|
+
'arguments',
|
|
25
|
+
'member_expression',
|
|
26
|
+
'binary_expression',
|
|
27
|
+
'unary_expression',
|
|
28
|
+
'ternary_expression',
|
|
29
|
+
'parenthesized_expression',
|
|
30
|
+
'await_expression',
|
|
31
|
+
'as_expression',
|
|
32
|
+
'satisfies_expression',
|
|
33
|
+
'non_null_expression',
|
|
34
|
+
'spread_element',
|
|
35
|
+
'template_substitution',
|
|
36
|
+
'jsx_expression',
|
|
37
|
+
'return_statement',
|
|
38
|
+
'expression_statement',
|
|
39
|
+
'array',
|
|
40
|
+
'pair',
|
|
41
|
+
'type_query',
|
|
42
|
+
]);
|
|
43
|
+
const DECLARING_SLOTS = ['function', 'constructor', 'name', 'pattern', 'parameter', 'alias'];
|
|
44
|
+
const DEFAULT_PARENTS = new Set([
|
|
45
|
+
'required_parameter',
|
|
46
|
+
'optional_parameter',
|
|
47
|
+
'assignment_pattern',
|
|
48
|
+
'object_assignment_pattern',
|
|
49
|
+
]);
|
|
50
|
+
const DEFAULT_SLOTS = ['value', 'right'];
|
|
51
|
+
export const isLocalExport = (node, parent) => parent.kind() === 'export_specifier' &&
|
|
52
|
+
moduleOf(parent) === undefined &&
|
|
53
|
+
!sameNode(parent.field('alias'), node);
|
|
54
|
+
export const isUse = (node, parent) => {
|
|
55
|
+
const kind = parent.kind();
|
|
56
|
+
if (NON_USE_PARENTS.has(kind))
|
|
57
|
+
return false;
|
|
58
|
+
if (!VALUE_PARENTS.has(kind) && DECLARING_SLOTS.some((s) => sameNode(parent.field(s), node))) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return !isReceiverOfCall(parent);
|
|
62
|
+
};
|
|
63
|
+
const isTypeQuery = (_node, parent) => parent.kind() === 'type_query';
|
|
64
|
+
const isInstanceofTarget = (node, parent) => parent.kind() === 'binary_expression' &&
|
|
65
|
+
parent.field('operator')?.text() === 'instanceof' &&
|
|
66
|
+
sameNode(parent.field('right'), node);
|
|
67
|
+
const isDefaultValue = (node, parent) => DEFAULT_PARENTS.has(parent.kind()) &&
|
|
68
|
+
DEFAULT_SLOTS.some((slot) => sameNode(parent.field(slot), node));
|
|
69
|
+
const isTopLevelCopy = (node, parent) => {
|
|
70
|
+
if (parent.kind() !== 'variable_declarator' || !sameNode(parent.field('value'), node)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const owner = parent.parent()?.parent();
|
|
74
|
+
const top = owner?.kind() === 'export_statement' ? owner.parent() : owner;
|
|
75
|
+
return top?.kind() === 'program';
|
|
76
|
+
};
|
|
77
|
+
const isInstantiated = (_node, parent) => parent.kind() === 'instantiation_expression';
|
|
78
|
+
const LOCAL_CONTEXTS = [
|
|
79
|
+
isDefaultValue,
|
|
80
|
+
isLocalExport,
|
|
81
|
+
isTypeQuery,
|
|
82
|
+
isInstanceofTarget,
|
|
83
|
+
isTopLevelCopy,
|
|
84
|
+
isInstantiated,
|
|
85
|
+
];
|
|
86
|
+
export const isLocalContext = (node, parent) => LOCAL_CONTEXTS.some((inContext) => inContext(node, parent));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CodeSymbol } from '../../../types.js';
|
|
2
|
+
import type { AstNode } from '../../parser.js';
|
|
3
|
+
import type { Declare } from '../../symbols.js';
|
|
4
|
+
import type { Reference, SourceFile } from '../../types.js';
|
|
5
|
+
export interface ExportFacts {
|
|
6
|
+
symbols: CodeSymbol[];
|
|
7
|
+
references: Reference[];
|
|
8
|
+
}
|
|
9
|
+
export declare function readExports(root: AstNode, file: SourceFile, declare: Declare): ExportFacts;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { originOf } from '../../ast.js';
|
|
2
|
+
import { moduleOf } from './specifiers.js';
|
|
3
|
+
const EXPORTS = /\bexport\b/;
|
|
4
|
+
const DEFAULT = 'default';
|
|
5
|
+
const EVERYTHING = '*';
|
|
6
|
+
function alias(site, publicName, target, at, module) {
|
|
7
|
+
const line = at.range().start.line + 1;
|
|
8
|
+
const symbol = site.declare(publicName, 'const', line);
|
|
9
|
+
site.found.symbols.push(symbol);
|
|
10
|
+
if (target === undefined)
|
|
11
|
+
return;
|
|
12
|
+
site.found.references.push({
|
|
13
|
+
file: site.file,
|
|
14
|
+
name: target,
|
|
15
|
+
relation: 'aliases',
|
|
16
|
+
line,
|
|
17
|
+
from: symbol,
|
|
18
|
+
origin: originOf(at),
|
|
19
|
+
...(module === undefined ? {} : { link: { specifier: module } }),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function link(site, at, module, exported, namespace = false) {
|
|
23
|
+
return {
|
|
24
|
+
file: site.file,
|
|
25
|
+
name: EVERYTHING,
|
|
26
|
+
relation: 'imports',
|
|
27
|
+
line: at.range().start.line + 1,
|
|
28
|
+
from: null,
|
|
29
|
+
origin: originOf(at),
|
|
30
|
+
link: { specifier: module, exported, ...(namespace ? { namespace } : {}) },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function readSpecifier(site, specifier, module) {
|
|
34
|
+
const original = specifier.field('name')?.text();
|
|
35
|
+
if (!original)
|
|
36
|
+
return;
|
|
37
|
+
const publicName = specifier.field('alias')?.text() ?? original;
|
|
38
|
+
if (module !== undefined) {
|
|
39
|
+
site.found.references.push({
|
|
40
|
+
file: site.file,
|
|
41
|
+
name: original,
|
|
42
|
+
relation: 'imports',
|
|
43
|
+
line: specifier.range().start.line + 1,
|
|
44
|
+
from: null,
|
|
45
|
+
origin: originOf(specifier),
|
|
46
|
+
link: { specifier: module, exported: publicName },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (publicName !== original)
|
|
50
|
+
alias(site, publicName, original, specifier, module);
|
|
51
|
+
}
|
|
52
|
+
function readDefault(site, statement) {
|
|
53
|
+
const target = statement.field('declaration') ?? statement.field('value');
|
|
54
|
+
const name = target?.kind() === 'identifier' ? target.text() : target?.field('name')?.text();
|
|
55
|
+
alias(site, DEFAULT, name, statement);
|
|
56
|
+
}
|
|
57
|
+
function readStatement(site, statement) {
|
|
58
|
+
const parts = statement.children();
|
|
59
|
+
if (parts.some((part) => part.kind() === DEFAULT))
|
|
60
|
+
return readDefault(site, statement);
|
|
61
|
+
const module = moduleOf(statement);
|
|
62
|
+
for (const part of parts) {
|
|
63
|
+
if (part.kind() === 'export_clause') {
|
|
64
|
+
for (const child of part.children()) {
|
|
65
|
+
if (child.kind() === 'export_specifier')
|
|
66
|
+
readSpecifier(site, child, module);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (part.kind() === 'namespace_export' && module !== undefined) {
|
|
70
|
+
const name = part
|
|
71
|
+
.children()
|
|
72
|
+
.find((child) => child.kind() === 'identifier')
|
|
73
|
+
?.text();
|
|
74
|
+
if (name)
|
|
75
|
+
site.found.references.push(link(site, part, module, name, true));
|
|
76
|
+
}
|
|
77
|
+
else if (part.kind() === EVERYTHING && module !== undefined) {
|
|
78
|
+
site.found.references.push(link(site, statement, module, EVERYTHING));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function readExports(root, file, declare) {
|
|
83
|
+
const found = { symbols: [], references: [] };
|
|
84
|
+
if (!EXPORTS.test(file.source))
|
|
85
|
+
return found;
|
|
86
|
+
const site = { file: file.path, declare, found };
|
|
87
|
+
for (const statement of root.findAll({ rule: { kind: 'export_statement' } })) {
|
|
88
|
+
readStatement(site, statement);
|
|
89
|
+
}
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { addNamed as bind, addSpace, newFacts } from '../binding.js';
|
|
2
|
+
import { moduleOf, stripQuotes } from './specifiers.js';
|
|
3
|
+
const DEFAULT = 'default';
|
|
4
|
+
const LOADERS = '^(import|require)$';
|
|
5
|
+
const LOADER_CALL = /\b(?:import|require)\s*\(/;
|
|
6
|
+
function addNamed(facts, at, span, local, name, module, viaDefault = false) {
|
|
7
|
+
const link = { specifier: module, ...(viaDefault ? { member: DEFAULT } : {}) };
|
|
8
|
+
const imported = { name, link };
|
|
9
|
+
bind(facts, local, imported);
|
|
10
|
+
facts.sites.push({ at, span, imported });
|
|
11
|
+
}
|
|
12
|
+
function addSpecifier(facts, specifier, module) {
|
|
13
|
+
const at = specifier.field('name');
|
|
14
|
+
const name = at?.text();
|
|
15
|
+
const local = specifier.field('alias')?.text() ?? name;
|
|
16
|
+
if (at && name && local)
|
|
17
|
+
addNamed(facts, at, specifier, local, name === DEFAULT ? local : name, module);
|
|
18
|
+
}
|
|
19
|
+
export function readImports(root) {
|
|
20
|
+
const facts = newFacts();
|
|
21
|
+
for (const clause of root.findAll({ rule: { kind: 'import_clause' } })) {
|
|
22
|
+
const module = moduleOf(clause);
|
|
23
|
+
if (module === undefined)
|
|
24
|
+
continue;
|
|
25
|
+
for (const part of clause.children()) {
|
|
26
|
+
if (part.kind() === 'identifier') {
|
|
27
|
+
addNamed(facts, part, part, part.text(), part.text(), module, true);
|
|
28
|
+
}
|
|
29
|
+
else if (part.kind() === 'namespace_import') {
|
|
30
|
+
const local = part.children().find((child) => child.kind() === 'identifier');
|
|
31
|
+
if (local)
|
|
32
|
+
addSpace(facts, local.text(), { specifier: module });
|
|
33
|
+
}
|
|
34
|
+
else if (part.kind() === 'named_imports') {
|
|
35
|
+
for (const specifier of part.children()) {
|
|
36
|
+
if (specifier.kind() === 'import_specifier')
|
|
37
|
+
addSpecifier(facts, specifier, module);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return facts;
|
|
43
|
+
}
|
|
44
|
+
export function readLoadedImports(root, source, facts) {
|
|
45
|
+
if (!LOADER_CALL.test(source))
|
|
46
|
+
return;
|
|
47
|
+
const calls = root.findAll({
|
|
48
|
+
rule: { kind: 'call_expression', has: { field: 'function', regex: LOADERS } },
|
|
49
|
+
});
|
|
50
|
+
for (const call of calls) {
|
|
51
|
+
const literal = call
|
|
52
|
+
.field('arguments')
|
|
53
|
+
?.children()
|
|
54
|
+
.find((child) => child.kind() === 'string');
|
|
55
|
+
const parent = call.parent();
|
|
56
|
+
const declarator = parent?.kind() === 'await_expression' ? parent.parent() : parent;
|
|
57
|
+
const pattern = declarator?.kind() === 'variable_declarator' ? declarator.field('name') : null;
|
|
58
|
+
if (!literal || pattern?.kind() !== 'object_pattern')
|
|
59
|
+
continue;
|
|
60
|
+
const module = stripQuotes(literal.text());
|
|
61
|
+
for (const entry of pattern.children()) {
|
|
62
|
+
if (entry.kind() === 'shorthand_property_identifier_pattern') {
|
|
63
|
+
addNamed(facts, entry, entry, entry.text(), entry.text(), module);
|
|
64
|
+
}
|
|
65
|
+
else if (entry.kind() === 'pair_pattern') {
|
|
66
|
+
const [key, , value] = entry.children();
|
|
67
|
+
if (key && value) {
|
|
68
|
+
addNamed(facts, value, value, value.text(), key.text(), module);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { readFileSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { join, relative, sep } from 'node:path';
|
|
4
|
+
const EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs', '.json'];
|
|
5
|
+
const EXTENSION_ALIAS = {
|
|
6
|
+
'.js': ['.ts', '.tsx', '.js'],
|
|
7
|
+
'.mjs': ['.mts', '.mjs'],
|
|
8
|
+
'.cjs': ['.cts', '.cjs'],
|
|
9
|
+
};
|
|
10
|
+
const CONDITIONS = ['import', 'require', 'node', 'default', 'types'];
|
|
11
|
+
const TSCONFIG = /(^|\/)tsconfig[^/]*\.json$/;
|
|
12
|
+
const CUSTOM_CONDITIONS = /"customConditions"\s*:\s*\[([^\]]*)\]/g;
|
|
13
|
+
const QUOTED = /"([^"]+)"/g;
|
|
14
|
+
function customConditions(root, files) {
|
|
15
|
+
const found = new Set();
|
|
16
|
+
for (const path of files) {
|
|
17
|
+
if (!TSCONFIG.test(path))
|
|
18
|
+
continue;
|
|
19
|
+
try {
|
|
20
|
+
const text = readFileSync(join(root, path), 'utf8');
|
|
21
|
+
for (const list of text.matchAll(CUSTOM_CONDITIONS)) {
|
|
22
|
+
for (const name of (list[1] ?? '').matchAll(QUOTED))
|
|
23
|
+
found.add(name[1] ?? '');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
found.delete('');
|
|
31
|
+
return [...found];
|
|
32
|
+
}
|
|
33
|
+
function loadFactory(conditions) {
|
|
34
|
+
try {
|
|
35
|
+
const { ResolverFactory } = createRequire(import.meta.url)('oxc-resolver');
|
|
36
|
+
return new ResolverFactory({
|
|
37
|
+
extensions: EXTENSIONS,
|
|
38
|
+
extensionAlias: EXTENSION_ALIAS,
|
|
39
|
+
conditionNames: [...conditions, ...CONDITIONS],
|
|
40
|
+
mainFields: ['module', 'main'],
|
|
41
|
+
tsconfig: 'auto',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function realRoot(root) {
|
|
49
|
+
try {
|
|
50
|
+
return realpathSync(root);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return root;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function packageResolver(root, files) {
|
|
57
|
+
const factory = loadFactory(customConditions(root, files));
|
|
58
|
+
if (!factory)
|
|
59
|
+
return () => null;
|
|
60
|
+
const base = realRoot(root);
|
|
61
|
+
return (from, specifier) => {
|
|
62
|
+
try {
|
|
63
|
+
const { path } = factory.resolveFileSync(join(base, from), specifier);
|
|
64
|
+
if (!path)
|
|
65
|
+
return null;
|
|
66
|
+
const found = relative(base, path).split(sep).join('/');
|
|
67
|
+
return files.has(found) ? found : null;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
import { packageResolver } from './package-resolver.js';
|
|
3
|
+
import { workspaceResolver } from './workspace-resolver.js';
|
|
4
|
+
const SCRIPT_SUFFIX = /\.[cm]?jsx?$/;
|
|
5
|
+
const EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
|
|
6
|
+
function probe(base, files) {
|
|
7
|
+
const stem = base.replace(SCRIPT_SUFFIX, '');
|
|
8
|
+
const candidates = [
|
|
9
|
+
base,
|
|
10
|
+
...EXTENSIONS.flatMap((ext) => [`${stem}${ext}`, `${stem}/index${ext}`]),
|
|
11
|
+
];
|
|
12
|
+
return candidates.find((candidate) => files.has(candidate)) ?? null;
|
|
13
|
+
}
|
|
14
|
+
export function ecmascriptResolver(root, files) {
|
|
15
|
+
const packages = packageResolver(root, files);
|
|
16
|
+
const workspaces = workspaceResolver(root, files, (base) => probe(base, files));
|
|
17
|
+
return {
|
|
18
|
+
resolve: (from, specifier) => specifier.startsWith('.')
|
|
19
|
+
? probe(posix.normalize(posix.join(posix.dirname(from), specifier)), files)
|
|
20
|
+
: (packages(from, specifier) ?? workspaces(from, specifier)),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const SOURCE_DEPTH = 4;
|
|
2
|
+
const QUOTES = /^['"`]|['"`]$/g;
|
|
3
|
+
export function stripQuotes(text) {
|
|
4
|
+
return text.replace(QUOTES, '');
|
|
5
|
+
}
|
|
6
|
+
export function moduleOf(node) {
|
|
7
|
+
let current = node;
|
|
8
|
+
for (let depth = 0; current && depth < SOURCE_DEPTH; depth += 1) {
|
|
9
|
+
const source = current.field('source');
|
|
10
|
+
if (source)
|
|
11
|
+
return stripQuotes(source.text());
|
|
12
|
+
current = current.parent();
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { javascript, tsx, typescript } from '../../languages/ecmascript.js';
|
|
2
|
+
import { bindImports } from './bind.js';
|
|
3
|
+
import { readExports } from './exports.js';
|
|
4
|
+
import { ecmascriptResolver } from './resolver.js';
|
|
5
|
+
export const ecmascript = {
|
|
6
|
+
languages: [typescript, tsx, javascript].map((spec) => spec.label),
|
|
7
|
+
read({ root, file, declare, symbols, references, owner }) {
|
|
8
|
+
const exported = readExports(root, file, declare);
|
|
9
|
+
const known = [...symbols, ...exported.symbols];
|
|
10
|
+
return {
|
|
11
|
+
added: exported.symbols,
|
|
12
|
+
references: bindImports(root, file, [...references, ...exported.references], known, owner),
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
resolver: ecmascriptResolver,
|
|
16
|
+
isExternal: (specifier) => !specifier.startsWith('.'),
|
|
17
|
+
importsNames: true,
|
|
18
|
+
};
|