@katana-project/laser 0.1.1 → 0.2.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/dist/resolver.d.ts +84 -4
- package/dist/resolver.js +66 -22
- package/dist/tree-utils.d.ts +1 -2
- package/dist/tree-utils.js +0 -14
- package/dist/unit.d.ts +18 -0
- package/dist/unit.js +9 -0
- package/package.json +1 -1
package/dist/resolver.d.ts
CHANGED
|
@@ -1,21 +1,101 @@
|
|
|
1
1
|
import { SyntaxNode } from "@lezer/common";
|
|
2
2
|
import { CompilationUnit } from "./unit.js";
|
|
3
|
+
/**
|
|
4
|
+
* Basic type reference information.
|
|
5
|
+
*/
|
|
3
6
|
export interface TypeReference {
|
|
4
7
|
name: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Local type reference within the same compilation unit.
|
|
11
|
+
*/
|
|
12
|
+
export interface LocalTypeReference extends TypeReference {
|
|
5
13
|
node: SyntaxNode;
|
|
6
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* External type reference from another compilation unit.
|
|
17
|
+
*/
|
|
18
|
+
export interface ExternalTypeReference extends TypeReference {
|
|
19
|
+
qualifiedName: string;
|
|
20
|
+
packageName: string | null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolved type information.
|
|
24
|
+
*
|
|
25
|
+
* Includes whether the type is declared locally, imported, a built-in type, or unresolved.
|
|
26
|
+
*/
|
|
7
27
|
export interface ResolvedType {
|
|
8
28
|
kind: "declared" | "imported" | "builtin" | "unresolved";
|
|
9
29
|
name: string;
|
|
10
30
|
qualifiedName?: string;
|
|
11
31
|
declaration?: SyntaxNode;
|
|
12
|
-
ref:
|
|
32
|
+
ref: LocalTypeReference;
|
|
13
33
|
}
|
|
14
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Resolves a local type reference to its complete type information.
|
|
36
|
+
*
|
|
37
|
+
* @param typeRef - The local type reference to resolve
|
|
38
|
+
* @param unit - The compilation unit containing the reference
|
|
39
|
+
* @param externalRefs - Optional array of external type references for resolving wildcard imports and same-package classes.
|
|
40
|
+
* Each external reference should include the type name, qualified name, and package name.
|
|
41
|
+
* This enables resolution of types from wildcard imports (e.g., import java.util.*) and
|
|
42
|
+
* types in the same package that aren't explicitly imported.
|
|
43
|
+
* @returns ResolvedType containing complete type information
|
|
44
|
+
*/
|
|
45
|
+
export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: CompilationUnit, externalRefs?: ExternalTypeReference[]) => ResolvedType;
|
|
46
|
+
/**
|
|
47
|
+
* Type reference resolver for a compilation unit.
|
|
48
|
+
*
|
|
49
|
+
* Provides methods to resolve type references at specific offsets,
|
|
50
|
+
* as well as resolving all type references in the unit.
|
|
51
|
+
*/
|
|
15
52
|
export interface TypeReferenceResolver {
|
|
53
|
+
/** The compilation unit being analyzed. */
|
|
16
54
|
unit: CompilationUnit;
|
|
55
|
+
/**
|
|
56
|
+
* Resolves complete type information of the reference at the given offset.
|
|
57
|
+
*
|
|
58
|
+
* Returns null if no type reference is found at that position.
|
|
59
|
+
*
|
|
60
|
+
* @param offset - The offset in the source code
|
|
61
|
+
* @param side - Optional side to resolve on (-1 = left, 0 = exact, 1 = right)
|
|
62
|
+
* @returns ResolvedType or null
|
|
63
|
+
*/
|
|
17
64
|
resolveAt(offset: number, side?: -1 | 0 | 1): ResolvedType | null;
|
|
18
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Resolves the local type reference at the given offset.
|
|
67
|
+
*
|
|
68
|
+
* Returns null if no type reference is found at that position.
|
|
69
|
+
*
|
|
70
|
+
* @param offset - The offset in the source code
|
|
71
|
+
* @param side - Optional side to resolve on (-1 = left, 0 = exact, 1 = right)
|
|
72
|
+
* @returns LocalTypeReference or null
|
|
73
|
+
*/
|
|
74
|
+
resolveReferenceAt(offset: number, side?: -1 | 0 | 1): LocalTypeReference | null;
|
|
75
|
+
/**
|
|
76
|
+
* Resolves all type references in the compilation unit.
|
|
77
|
+
*
|
|
78
|
+
* @returns An array of resolved type information for all references.
|
|
79
|
+
*/
|
|
19
80
|
resolveAll(): ResolvedType[];
|
|
20
81
|
}
|
|
21
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Creates a type reference resolver for a compilation unit.
|
|
84
|
+
*
|
|
85
|
+
* @param unit - The compilation unit to analyze
|
|
86
|
+
* @param refs - Optional array of external type references for resolving wildcard imports and same-package classes.
|
|
87
|
+
* Each external reference should include the type name, qualified name, and package name.
|
|
88
|
+
* This enables resolution of types from wildcard imports (e.g., import java.util.*) and
|
|
89
|
+
* types in the same package that aren't explicitly imported.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const externalRefs = [
|
|
94
|
+
* { name: "List", qualifiedName: "java.util.List", packageName: "java.util" },
|
|
95
|
+
* { name: "HashMap", qualifiedName: "java.util.HashMap", packageName: "java.util" },
|
|
96
|
+
* { name: "OtherClass", qualifiedName: "com.example.OtherClass", packageName: "com.example" }
|
|
97
|
+
* ];
|
|
98
|
+
* const resolver = createTypeReferenceResolver(unit, externalRefs);
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare const createTypeReferenceResolver: (unit: CompilationUnit, refs?: ExternalTypeReference[]) => TypeReferenceResolver;
|
package/dist/resolver.js
CHANGED
|
@@ -87,7 +87,7 @@ const resolveInUnit = (typeName, unit) => {
|
|
|
87
87
|
}
|
|
88
88
|
return null;
|
|
89
89
|
};
|
|
90
|
-
const resolveImported = (typeRef, unit) => {
|
|
90
|
+
const resolveImported = (typeRef, unit, externalRefs) => {
|
|
91
91
|
const parts = typeRef.name.split(".");
|
|
92
92
|
const firstPart = parts[0];
|
|
93
93
|
for (const imp of unit.imports) {
|
|
@@ -104,9 +104,47 @@ const resolveImported = (typeRef, unit) => {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
+
for (const imp of unit.imports) {
|
|
108
|
+
if (imp.isWildcard) {
|
|
109
|
+
const packageName = imp.importedName;
|
|
110
|
+
const matchingExternal = externalRefs.find((ext) => ext.packageName === packageName && ext.name === firstPart);
|
|
111
|
+
if (matchingExternal) {
|
|
112
|
+
return {
|
|
113
|
+
kind: "imported",
|
|
114
|
+
name: typeRef.name,
|
|
115
|
+
qualifiedName: matchingExternal.qualifiedName,
|
|
116
|
+
ref: typeRef,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
let matchingExternal = externalRefs.find((ext) => ext.packageName === (unit.packageName ?? "") && ext.name === firstPart);
|
|
122
|
+
if (!matchingExternal) {
|
|
123
|
+
// implicit import of java.lang.*
|
|
124
|
+
matchingExternal = externalRefs.find((ext) => ext.packageName === "java.lang" && ext.name === firstPart);
|
|
125
|
+
}
|
|
126
|
+
if (matchingExternal) {
|
|
127
|
+
return {
|
|
128
|
+
kind: "imported",
|
|
129
|
+
name: typeRef.name,
|
|
130
|
+
qualifiedName: matchingExternal.qualifiedName,
|
|
131
|
+
ref: typeRef,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
107
134
|
return null;
|
|
108
135
|
};
|
|
109
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Resolves a local type reference to its complete type information.
|
|
138
|
+
*
|
|
139
|
+
* @param typeRef - The local type reference to resolve
|
|
140
|
+
* @param unit - The compilation unit containing the reference
|
|
141
|
+
* @param externalRefs - Optional array of external type references for resolving wildcard imports and same-package classes.
|
|
142
|
+
* Each external reference should include the type name, qualified name, and package name.
|
|
143
|
+
* This enables resolution of types from wildcard imports (e.g., import java.util.*) and
|
|
144
|
+
* types in the same package that aren't explicitly imported.
|
|
145
|
+
* @returns ResolvedType containing complete type information
|
|
146
|
+
*/
|
|
147
|
+
export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
|
|
110
148
|
const typeName = typeRef.name;
|
|
111
149
|
if (BUILTIN_TYPES.has(typeName)) {
|
|
112
150
|
return {
|
|
@@ -119,43 +157,49 @@ export const resolveTypeReference = (typeRef, unit) => {
|
|
|
119
157
|
if (localResolved) {
|
|
120
158
|
return {
|
|
121
159
|
kind: "declared",
|
|
122
|
-
name: localResolved.
|
|
123
|
-
qualifiedName: localResolved.qualifiedName,
|
|
160
|
+
name: localResolved.qualifiedName,
|
|
161
|
+
qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
|
|
124
162
|
declaration: localResolved.node,
|
|
125
163
|
ref: typeRef,
|
|
126
164
|
};
|
|
127
165
|
}
|
|
128
|
-
const importedResolved = resolveImported(typeRef, unit);
|
|
166
|
+
const importedResolved = resolveImported(typeRef, unit, externalRefs);
|
|
129
167
|
if (importedResolved) {
|
|
130
168
|
return importedResolved;
|
|
131
169
|
}
|
|
132
|
-
if (unit.packageName) {
|
|
133
|
-
const qualifiedName = `${unit.packageName}.${typeName}`;
|
|
134
|
-
const resolved = resolveInUnit(qualifiedName, unit);
|
|
135
|
-
if (resolved) {
|
|
136
|
-
return {
|
|
137
|
-
kind: "imported",
|
|
138
|
-
name: resolved.name,
|
|
139
|
-
qualifiedName: resolved.qualifiedName,
|
|
140
|
-
declaration: resolved.node,
|
|
141
|
-
ref: typeRef,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
170
|
return {
|
|
146
171
|
kind: "unresolved",
|
|
147
172
|
name: typeName,
|
|
148
173
|
ref: typeRef,
|
|
149
174
|
};
|
|
150
175
|
};
|
|
151
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Creates a type reference resolver for a compilation unit.
|
|
178
|
+
*
|
|
179
|
+
* @param unit - The compilation unit to analyze
|
|
180
|
+
* @param refs - Optional array of external type references for resolving wildcard imports and same-package classes.
|
|
181
|
+
* Each external reference should include the type name, qualified name, and package name.
|
|
182
|
+
* This enables resolution of types from wildcard imports (e.g., import java.util.*) and
|
|
183
|
+
* types in the same package that aren't explicitly imported.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const externalRefs = [
|
|
188
|
+
* { name: "List", qualifiedName: "java.util.List", packageName: "java.util" },
|
|
189
|
+
* { name: "HashMap", qualifiedName: "java.util.HashMap", packageName: "java.util" },
|
|
190
|
+
* { name: "OtherClass", qualifiedName: "com.example.OtherClass", packageName: "com.example" }
|
|
191
|
+
* ];
|
|
192
|
+
* const resolver = createTypeReferenceResolver(unit, externalRefs);
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export const createTypeReferenceResolver = (unit, refs = []) => {
|
|
152
196
|
return {
|
|
153
197
|
unit,
|
|
154
198
|
resolveAt(offset, side) {
|
|
155
199
|
const typeRef = this.resolveReferenceAt(offset, side);
|
|
156
200
|
if (!typeRef)
|
|
157
201
|
return null;
|
|
158
|
-
return resolveTypeReference(typeRef, unit);
|
|
202
|
+
return resolveTypeReference(typeRef, unit, refs);
|
|
159
203
|
},
|
|
160
204
|
resolveReferenceAt(offset, side) {
|
|
161
205
|
const node = unit.tree.resolveInner(offset, side);
|
|
@@ -169,8 +213,8 @@ export const createTypeReferenceResolver = (unit) => {
|
|
|
169
213
|
return null;
|
|
170
214
|
},
|
|
171
215
|
resolveAll() {
|
|
172
|
-
const
|
|
173
|
-
return
|
|
216
|
+
const allRefs = collectAllTypeReferences(unit.tree.topNode, unit.source);
|
|
217
|
+
return allRefs.map((ref) => resolveTypeReference(ref, unit, refs));
|
|
174
218
|
},
|
|
175
219
|
};
|
|
176
220
|
};
|
package/dist/tree-utils.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { SyntaxNode
|
|
2
|
-
export declare const getNodeStack: (tree: Tree, offset: number) => SyntaxNode[] | null;
|
|
1
|
+
import { SyntaxNode } from "@lezer/common";
|
|
3
2
|
export declare const getChildren: (node: SyntaxNode) => SyntaxNode[];
|
|
4
3
|
export declare const findChild: (node: SyntaxNode, predicate: (child: SyntaxNode) => boolean) => SyntaxNode | null;
|
|
5
4
|
export declare const findChildren: (node: SyntaxNode, predicate: (child: SyntaxNode) => boolean) => SyntaxNode[];
|
package/dist/tree-utils.js
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
export const getNodeStack = (tree, offset) => {
|
|
2
|
-
if (offset < 0 || offset > tree.length)
|
|
3
|
-
return null;
|
|
4
|
-
let node = tree.resolveInner(offset, 1);
|
|
5
|
-
if (!node)
|
|
6
|
-
return null;
|
|
7
|
-
const nodes = [];
|
|
8
|
-
let current = node;
|
|
9
|
-
while (current) {
|
|
10
|
-
nodes.unshift(current);
|
|
11
|
-
current = current.parent;
|
|
12
|
-
}
|
|
13
|
-
return nodes.length > 0 ? nodes : null;
|
|
14
|
-
};
|
|
15
1
|
export const getChildren = (node) => {
|
|
16
2
|
const children = [];
|
|
17
3
|
let child = node.firstChild;
|
package/dist/unit.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { SyntaxNode, Tree } from "@lezer/common";
|
|
2
|
+
/**
|
|
3
|
+
* Represents an import statement in a Java source file.
|
|
4
|
+
*/
|
|
2
5
|
export interface ImportInfo {
|
|
3
6
|
importedName: string;
|
|
4
7
|
isStatic: boolean;
|
|
5
8
|
isWildcard: boolean;
|
|
6
9
|
node: SyntaxNode;
|
|
7
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Represents a type declaration (class, interface, enum, annotation, module) in a Java source file.
|
|
13
|
+
*/
|
|
8
14
|
export interface TypeInfo {
|
|
9
15
|
kind: "class" | "interface" | "enum" | "annotation" | "module";
|
|
10
16
|
name: string;
|
|
@@ -12,6 +18,9 @@ export interface TypeInfo {
|
|
|
12
18
|
node: SyntaxNode;
|
|
13
19
|
typeParameters: string[];
|
|
14
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Represents a parsed Java compilation unit (source file).
|
|
23
|
+
*/
|
|
15
24
|
export interface CompilationUnit {
|
|
16
25
|
tree: Tree;
|
|
17
26
|
source: string;
|
|
@@ -19,4 +28,13 @@ export interface CompilationUnit {
|
|
|
19
28
|
imports: ImportInfo[];
|
|
20
29
|
types: TypeInfo[];
|
|
21
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Parses a Java compilation unit from a syntax tree and source code.
|
|
33
|
+
*
|
|
34
|
+
* This expects a `@lezer/java` parsed tree.
|
|
35
|
+
*
|
|
36
|
+
* @param tree - The syntax tree of the Java source file.
|
|
37
|
+
* @param source - The source code of the Java file.
|
|
38
|
+
* @returns The parsed compilation unit information.
|
|
39
|
+
*/
|
|
22
40
|
export declare const parseUnit: (tree: Tree, source: string) => CompilationUnit;
|
package/dist/unit.js
CHANGED
|
@@ -17,6 +17,15 @@ const extractTypeParameters = (node, source) => {
|
|
|
17
17
|
}
|
|
18
18
|
return params;
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Parses a Java compilation unit from a syntax tree and source code.
|
|
22
|
+
*
|
|
23
|
+
* This expects a `@lezer/java` parsed tree.
|
|
24
|
+
*
|
|
25
|
+
* @param tree - The syntax tree of the Java source file.
|
|
26
|
+
* @param source - The source code of the Java file.
|
|
27
|
+
* @returns The parsed compilation unit information.
|
|
28
|
+
*/
|
|
20
29
|
export const parseUnit = (tree, source) => {
|
|
21
30
|
const unit = {
|
|
22
31
|
tree,
|