@katana-project/laser 0.3.0 → 0.4.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/dist/resolver.d.ts +42 -9
- package/dist/resolver.js +334 -25
- package/package.json +1 -1
package/dist/resolver.d.ts
CHANGED
|
@@ -20,12 +20,20 @@ export interface ExternalTypeReference extends TypeReference {
|
|
|
20
20
|
packageName: string | null;
|
|
21
21
|
moduleName: string | null;
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Common information returned by resolvers operating on source declarations or references.
|
|
25
|
+
*/
|
|
26
|
+
export interface Resolved {
|
|
27
|
+
name: string;
|
|
28
|
+
category: "type" | "declaration";
|
|
29
|
+
}
|
|
23
30
|
/**
|
|
24
31
|
* Resolved type information.
|
|
25
32
|
*
|
|
26
33
|
* Includes whether the type is declared locally, imported, or a built-in type.
|
|
27
34
|
*/
|
|
28
|
-
export interface ResolvedType {
|
|
35
|
+
export interface ResolvedType extends Resolved {
|
|
36
|
+
category: "type";
|
|
29
37
|
kind: "declared" | "imported" | "builtin";
|
|
30
38
|
name: string;
|
|
31
39
|
qualifiedName?: string;
|
|
@@ -47,8 +55,7 @@ export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: C
|
|
|
47
55
|
/**
|
|
48
56
|
* Type reference resolver for a compilation unit.
|
|
49
57
|
*
|
|
50
|
-
* Provides methods to resolve type references at specific offsets
|
|
51
|
-
* as well as resolving all type references in the unit.
|
|
58
|
+
* Provides methods to resolve type references at specific offsets.
|
|
52
59
|
*/
|
|
53
60
|
export interface TypeReferenceResolver {
|
|
54
61
|
/** The compilation unit being analyzed. */
|
|
@@ -73,12 +80,6 @@ export interface TypeReferenceResolver {
|
|
|
73
80
|
* @returns LocalTypeReference or null
|
|
74
81
|
*/
|
|
75
82
|
resolveReferenceAt(offset: number, side?: -1 | 0 | 1): LocalTypeReference | null;
|
|
76
|
-
/**
|
|
77
|
-
* Resolves all type references in the compilation unit.
|
|
78
|
-
*
|
|
79
|
-
* @returns An array of resolved type information for all references.
|
|
80
|
-
*/
|
|
81
|
-
resolveAll(): ResolvedType[];
|
|
82
83
|
}
|
|
83
84
|
/**
|
|
84
85
|
* Creates a type reference resolver for a compilation unit.
|
|
@@ -100,3 +101,35 @@ export interface TypeReferenceResolver {
|
|
|
100
101
|
* ```
|
|
101
102
|
*/
|
|
102
103
|
export declare const createTypeReferenceResolver: (unit: CompilationUnit, refs?: ExternalTypeReference[]) => TypeReferenceResolver;
|
|
104
|
+
export type DeclarationKind = "field" | "constant" | "method" | "constructor" | "staticInitializer";
|
|
105
|
+
/**
|
|
106
|
+
* JVM-style information for a field or method declaration.
|
|
107
|
+
*/
|
|
108
|
+
export interface ResolvedDeclaration extends Resolved {
|
|
109
|
+
category: "declaration";
|
|
110
|
+
kind: DeclarationKind;
|
|
111
|
+
name: string;
|
|
112
|
+
descriptor: string;
|
|
113
|
+
owner: string;
|
|
114
|
+
node: SyntaxNode;
|
|
115
|
+
declaration: SyntaxNode;
|
|
116
|
+
}
|
|
117
|
+
export interface DeclarationResolver {
|
|
118
|
+
unit: CompilationUnit;
|
|
119
|
+
resolveAt(offset: number, side?: -1 | 0 | 1): ResolvedDeclaration | null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates a resolver for field and method declarations in a compilation unit.
|
|
123
|
+
*/
|
|
124
|
+
export declare const createDeclarationResolver: (unit: CompilationUnit, refs?: ExternalTypeReference[]) => DeclarationResolver;
|
|
125
|
+
/**
|
|
126
|
+
* A resolver that returns either resolved type or declaration information.
|
|
127
|
+
*/
|
|
128
|
+
export interface CompositeResolver {
|
|
129
|
+
unit: CompilationUnit;
|
|
130
|
+
resolveAt(offset: number, side?: -1 | 0 | 1): Resolved | null;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates a composite resolver that checks declarations before type references.
|
|
134
|
+
*/
|
|
135
|
+
export declare const createCompositeResolver: (unit: CompilationUnit, refs?: ExternalTypeReference[]) => CompositeResolver;
|
package/dist/resolver.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findChild, findChildren, findDescendants } from "./tree-utils.js";
|
|
1
2
|
const BUILTIN_TYPES = new Set(["byte", "short", "int", "long", "char", "float", "double", "boolean", "void"]);
|
|
2
3
|
const ALLOWED_PARENTS = new Set([
|
|
3
4
|
"TypeName",
|
|
@@ -46,7 +47,8 @@ const getTypeName = (node, source, limit = node.to) => {
|
|
|
46
47
|
node.parent.name !== "ClassDeclaration" &&
|
|
47
48
|
node.parent.name !== "ConstructorDeclaration" &&
|
|
48
49
|
node.parent.name !== "InterfaceDeclaration" &&
|
|
49
|
-
node.parent.name !== "LocalVariableDeclaration"
|
|
50
|
+
node.parent.name !== "LocalVariableDeclaration" &&
|
|
51
|
+
node.parent.name !== "RecordDeclaration")) {
|
|
50
52
|
// only allow getting Definition-based type names from these declarations
|
|
51
53
|
return null;
|
|
52
54
|
}
|
|
@@ -65,27 +67,6 @@ const getTypeName = (node, source, limit = node.to) => {
|
|
|
65
67
|
}
|
|
66
68
|
return null;
|
|
67
69
|
};
|
|
68
|
-
const collectAllTypeReferences = (node, source, refs = []) => {
|
|
69
|
-
const name = getTypeName(node, source);
|
|
70
|
-
if (name) {
|
|
71
|
-
const existing = refs.find((r) => r.node === node);
|
|
72
|
-
if (!existing) {
|
|
73
|
-
refs.push({
|
|
74
|
-
name,
|
|
75
|
-
node,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
// enter node, as we didn't find a type reference here
|
|
81
|
-
let child = node.firstChild;
|
|
82
|
-
while (child) {
|
|
83
|
-
collectAllTypeReferences(child, source, refs);
|
|
84
|
-
child = child.nextSibling;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return refs;
|
|
88
|
-
};
|
|
89
70
|
const resolveInUnit = (typeName, unit) => {
|
|
90
71
|
for (const cls of unit.types) {
|
|
91
72
|
if (cls.name === typeName ||
|
|
@@ -102,6 +83,7 @@ const resolveImported = (typeRef, unit, externalRefs) => {
|
|
|
102
83
|
if (imp.kind === "type" &&
|
|
103
84
|
(imp.importedName === typeRef.name || imp.importedName.endsWith(`.${typeRef.name}`))) {
|
|
104
85
|
return {
|
|
86
|
+
category: "type",
|
|
105
87
|
kind: "imported",
|
|
106
88
|
name: typeRef.name,
|
|
107
89
|
qualifiedName: imp.importedName,
|
|
@@ -115,6 +97,7 @@ const resolveImported = (typeRef, unit, externalRefs) => {
|
|
|
115
97
|
const matchingExternal = externalRefs.find((ext) => `${imp.importedName}.${typeRef.name}` === ext.qualifiedName);
|
|
116
98
|
if (matchingExternal) {
|
|
117
99
|
return {
|
|
100
|
+
category: "type",
|
|
118
101
|
kind: "imported",
|
|
119
102
|
name: typeRef.name,
|
|
120
103
|
qualifiedName: matchingExternal.qualifiedName,
|
|
@@ -130,6 +113,7 @@ const resolveImported = (typeRef, unit, externalRefs) => {
|
|
|
130
113
|
(ext.name === typeRef.name || ext.qualifiedName.endsWith(`.${typeRef.name}`)));
|
|
131
114
|
if (matchingExternal) {
|
|
132
115
|
return {
|
|
116
|
+
category: "type",
|
|
133
117
|
kind: "imported",
|
|
134
118
|
name: typeRef.name,
|
|
135
119
|
qualifiedName: matchingExternal.qualifiedName,
|
|
@@ -146,6 +130,7 @@ const resolveImported = (typeRef, unit, externalRefs) => {
|
|
|
146
130
|
}
|
|
147
131
|
if (matchingExternal) {
|
|
148
132
|
return {
|
|
133
|
+
category: "type",
|
|
149
134
|
kind: "imported",
|
|
150
135
|
name: typeRef.name,
|
|
151
136
|
qualifiedName: matchingExternal.qualifiedName,
|
|
@@ -171,6 +156,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
|
|
|
171
156
|
const typeName = typeRef.name;
|
|
172
157
|
if (BUILTIN_TYPES.has(typeName)) {
|
|
173
158
|
return {
|
|
159
|
+
category: "type",
|
|
174
160
|
kind: "builtin",
|
|
175
161
|
name: typeName,
|
|
176
162
|
ref: typeRef,
|
|
@@ -180,6 +166,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
|
|
|
180
166
|
const externalMatch = externalRefs.find((ext) => ext.qualifiedName === typeName);
|
|
181
167
|
if (externalMatch) {
|
|
182
168
|
return {
|
|
169
|
+
category: "type",
|
|
183
170
|
kind: "imported",
|
|
184
171
|
name: typeName,
|
|
185
172
|
qualifiedName: externalMatch.qualifiedName,
|
|
@@ -189,6 +176,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
|
|
|
189
176
|
const localResolved = resolveInUnit(typeName, unit);
|
|
190
177
|
if (localResolved) {
|
|
191
178
|
return {
|
|
179
|
+
category: "type",
|
|
192
180
|
kind: "declared",
|
|
193
181
|
name: typeName.includes(".") ? typeName : localResolved.qualifiedName,
|
|
194
182
|
qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
|
|
@@ -241,9 +229,330 @@ export const createTypeReferenceResolver = (unit, refs = []) => {
|
|
|
241
229
|
}
|
|
242
230
|
return null;
|
|
243
231
|
},
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
const DECLARATION_NAMES = new Set([
|
|
235
|
+
"FieldDeclaration",
|
|
236
|
+
"ConstantDeclaration",
|
|
237
|
+
"EnumConstant",
|
|
238
|
+
"MethodDeclaration",
|
|
239
|
+
"ConstructorDeclaration",
|
|
240
|
+
"CompactConstructorDeclaration",
|
|
241
|
+
"StaticInitializer",
|
|
242
|
+
]);
|
|
243
|
+
const findDeclarationAncestor = (node) => {
|
|
244
|
+
let current = node;
|
|
245
|
+
while (current) {
|
|
246
|
+
if (DECLARATION_NAMES.has(current.name))
|
|
247
|
+
return current;
|
|
248
|
+
current = current.parent;
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
};
|
|
252
|
+
const findOwnerNode = (declaration) => {
|
|
253
|
+
let current = declaration.parent;
|
|
254
|
+
while (current) {
|
|
255
|
+
if (current.name === "ClassDeclaration" ||
|
|
256
|
+
current.name === "InterfaceDeclaration" ||
|
|
257
|
+
current.name === "EnumDeclaration" ||
|
|
258
|
+
current.name === "AnnotationTypeDeclaration" ||
|
|
259
|
+
current.name === "RecordDeclaration" ||
|
|
260
|
+
current.name === "ModuleDeclaration") {
|
|
261
|
+
return current;
|
|
262
|
+
}
|
|
263
|
+
current = current.parent;
|
|
264
|
+
}
|
|
265
|
+
return null;
|
|
266
|
+
};
|
|
267
|
+
const primitiveDescriptors = {
|
|
268
|
+
byte: "B",
|
|
269
|
+
char: "C",
|
|
270
|
+
double: "D",
|
|
271
|
+
float: "F",
|
|
272
|
+
int: "I",
|
|
273
|
+
long: "J",
|
|
274
|
+
short: "S",
|
|
275
|
+
boolean: "Z",
|
|
276
|
+
void: "V",
|
|
277
|
+
};
|
|
278
|
+
const TYPE_NODE_NAMES = new Set(["PrimitiveType", "void", "TypeName", "ScopedTypeName", "GenericType", "ArrayType"]);
|
|
279
|
+
const isTypeNode = (node) => TYPE_NODE_NAMES.has(node.name);
|
|
280
|
+
const findDirectTypeNode = (node) => {
|
|
281
|
+
let child = node.firstChild;
|
|
282
|
+
while (child) {
|
|
283
|
+
if (isTypeNode(child))
|
|
284
|
+
return child;
|
|
285
|
+
child = child.nextSibling;
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
};
|
|
289
|
+
const findDefinition = (node) => findChild(node, (child) => child.name === "Definition");
|
|
290
|
+
const findDescendantDefinition = (node) => findDescendants(node, (child) => child.name === "Definition")[0] ?? null;
|
|
291
|
+
const getOwnerName = (unit, ownerNode) => {
|
|
292
|
+
const type = unit.types.find((candidate) => candidate.node.from === ownerNode.from &&
|
|
293
|
+
candidate.node.to === ownerNode.to &&
|
|
294
|
+
candidate.node.name === ownerNode.name);
|
|
295
|
+
if (!type)
|
|
296
|
+
return "";
|
|
297
|
+
const packagePrefix = unit.packageName ? `${unit.packageName.replace(/\./g, "/")}/` : "";
|
|
298
|
+
return packagePrefix + type.qualifiedName.replace(/\./g, "$").replace(/\//g, "/");
|
|
299
|
+
};
|
|
300
|
+
const findTypeParameterBound = (name, context, source) => {
|
|
301
|
+
let current = context;
|
|
302
|
+
while (current) {
|
|
303
|
+
for (const parameter of findDescendants(current, (node) => node.name === "TypeParameter")) {
|
|
304
|
+
const definition = findDefinition(parameter);
|
|
305
|
+
if (!definition || source.slice(definition.from, definition.to) !== name)
|
|
306
|
+
continue;
|
|
307
|
+
const typeBound = findChild(parameter, (node) => node.name === "TypeBound");
|
|
308
|
+
if (typeBound) {
|
|
309
|
+
const bound = findChild(typeBound, (node) => isTypeNode(node));
|
|
310
|
+
if (bound)
|
|
311
|
+
return bound;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
current = current.parent;
|
|
315
|
+
}
|
|
316
|
+
return null;
|
|
317
|
+
};
|
|
318
|
+
const getUnqualifiedDescriptorName = (unit, name, externalRefs, context = null, visited = new Set()) => {
|
|
319
|
+
const exactExternal = externalRefs.find((external) => external.qualifiedName === name);
|
|
320
|
+
if (exactExternal) {
|
|
321
|
+
return exactExternal.qualifiedName.replace(/\./g, "/");
|
|
322
|
+
}
|
|
323
|
+
const javaLangExternal = externalRefs.find((external) => external.packageName === "java.lang" && external.name === name);
|
|
324
|
+
if (javaLangExternal) {
|
|
325
|
+
return javaLangExternal.qualifiedName.replace(/\./g, "/");
|
|
326
|
+
}
|
|
327
|
+
for (const imp of unit.imports) {
|
|
328
|
+
if (imp.kind === "type" && (imp.importedName === name || imp.importedName.endsWith(`.${name}`))) {
|
|
329
|
+
return imp.importedName.replace(/\./g, "/");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const local = unit.types.find((type) => type.name === name);
|
|
333
|
+
if (local) {
|
|
334
|
+
return getOwnerName(unit, local.node);
|
|
335
|
+
}
|
|
336
|
+
if (!visited.has(name)) {
|
|
337
|
+
const bound = findTypeParameterBound(name, context, unit.source);
|
|
338
|
+
if (bound) {
|
|
339
|
+
const nextVisited = new Set(visited);
|
|
340
|
+
nextVisited.add(name);
|
|
341
|
+
return getDescriptorType(bound, unit, unit.source, externalRefs, context, nextVisited).replace(/^L|;$/g, "");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (context) {
|
|
345
|
+
if (name.includes(".") || name.includes("/")) {
|
|
346
|
+
return name.replace(/\./g, "/");
|
|
347
|
+
}
|
|
348
|
+
return "java/lang/Object";
|
|
349
|
+
}
|
|
350
|
+
return name.replace(/\./g, "/");
|
|
351
|
+
};
|
|
352
|
+
const getDescriptorType = (node, unit, source, externalRefs, context = null, visited = new Set()) => {
|
|
353
|
+
if (!node)
|
|
354
|
+
return "V";
|
|
355
|
+
if (node.name === "ArrayType") {
|
|
356
|
+
let dimensions = 0;
|
|
357
|
+
let child = node.firstChild;
|
|
358
|
+
while (child) {
|
|
359
|
+
if (child.name === "Dimension")
|
|
360
|
+
dimensions++;
|
|
361
|
+
child = child.nextSibling;
|
|
362
|
+
}
|
|
363
|
+
const base = node.firstChild;
|
|
364
|
+
return "[".repeat(dimensions) + getDescriptorType(base, unit, source, externalRefs, context, visited);
|
|
365
|
+
}
|
|
366
|
+
if (node.name === "SpreadParameter") {
|
|
367
|
+
const type = node.firstChild;
|
|
368
|
+
return "[" + getDescriptorType(type, unit, source, externalRefs, context, visited);
|
|
369
|
+
}
|
|
370
|
+
if (node.name === "GenericType") {
|
|
371
|
+
const base = node.firstChild?.name === "TypeName" || node.firstChild?.name === "ScopedTypeName" ? node.firstChild : null;
|
|
372
|
+
return getDescriptorType(base, unit, source, externalRefs, context, visited);
|
|
373
|
+
}
|
|
374
|
+
if (node.name === "TypeName" ||
|
|
375
|
+
node.name === "ScopedTypeName" ||
|
|
376
|
+
node.name === "PrimitiveType" ||
|
|
377
|
+
node.name === "void") {
|
|
378
|
+
const name = getTypeName(node, source) ?? source.slice(node.from, node.to);
|
|
379
|
+
if (primitiveDescriptors[name])
|
|
380
|
+
return primitiveDescriptors[name];
|
|
381
|
+
const genericIndex = name.indexOf("<");
|
|
382
|
+
const unqualifiedName = genericIndex >= 0 ? name.slice(0, genericIndex) : name;
|
|
383
|
+
return `L${getUnqualifiedDescriptorName(unit, unqualifiedName, externalRefs, context, visited)};`;
|
|
384
|
+
}
|
|
385
|
+
let child = node.firstChild;
|
|
386
|
+
while (child) {
|
|
387
|
+
const descriptor = getDescriptorType(child, unit, source, externalRefs, context, visited);
|
|
388
|
+
if (descriptor !== "V")
|
|
389
|
+
return descriptor;
|
|
390
|
+
child = child.nextSibling;
|
|
391
|
+
}
|
|
392
|
+
return "V";
|
|
393
|
+
};
|
|
394
|
+
const getFieldNameAndType = (declaration, unit, source, externalRefs) => {
|
|
395
|
+
const typeNode = findDirectTypeNode(declaration);
|
|
396
|
+
const ownerNode = findOwnerNode(declaration);
|
|
397
|
+
const owner = ownerNode ? getOwnerName(unit, ownerNode) : "";
|
|
398
|
+
const descriptor = getDescriptorType(typeNode, unit, source, externalRefs, declaration);
|
|
399
|
+
const result = [];
|
|
400
|
+
for (const variable of findChildren(declaration, (node) => node.name === "VariableDeclarator")) {
|
|
401
|
+
const definition = findDefinition(variable);
|
|
402
|
+
if (definition) {
|
|
403
|
+
result.push({
|
|
404
|
+
category: "declaration",
|
|
405
|
+
kind: "field",
|
|
406
|
+
name: source.slice(definition.from, definition.to),
|
|
407
|
+
descriptor,
|
|
408
|
+
owner,
|
|
409
|
+
node: definition,
|
|
410
|
+
declaration,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return result;
|
|
415
|
+
};
|
|
416
|
+
const getParameterType = (parameter) => {
|
|
417
|
+
return parameter.name === "SpreadParameter" ? parameter : findDirectTypeNode(parameter);
|
|
418
|
+
};
|
|
419
|
+
const resolveDeclarationInfo = (declaration, unit, externalRefs) => {
|
|
420
|
+
const ownerNode = findOwnerNode(declaration);
|
|
421
|
+
const owner = ownerNode ? getOwnerName(unit, ownerNode) : "";
|
|
422
|
+
if (declaration.name === "StaticInitializer") {
|
|
423
|
+
return {
|
|
424
|
+
category: "declaration",
|
|
425
|
+
kind: "staticInitializer",
|
|
426
|
+
name: "<clinit>",
|
|
427
|
+
descriptor: "()V",
|
|
428
|
+
owner,
|
|
429
|
+
node: declaration,
|
|
430
|
+
declaration,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
if (declaration.name === "ConstantDeclaration") {
|
|
434
|
+
const typeNode = findDirectTypeNode(declaration);
|
|
435
|
+
const variable = findChild(declaration, (node) => node.name === "VariableDeclarator");
|
|
436
|
+
const definition = variable ? findDefinition(variable) : null;
|
|
437
|
+
if (!definition)
|
|
438
|
+
return null;
|
|
439
|
+
return {
|
|
440
|
+
category: "declaration",
|
|
441
|
+
kind: "constant",
|
|
442
|
+
name: unit.source.slice(definition.from, definition.to),
|
|
443
|
+
descriptor: getDescriptorType(typeNode, unit, unit.source, externalRefs, declaration),
|
|
444
|
+
owner,
|
|
445
|
+
node: definition,
|
|
446
|
+
declaration,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
if (declaration.name === "EnumConstant") {
|
|
450
|
+
const definition = findDescendantDefinition(declaration);
|
|
451
|
+
if (!definition)
|
|
452
|
+
return null;
|
|
453
|
+
return {
|
|
454
|
+
category: "declaration",
|
|
455
|
+
kind: "constant",
|
|
456
|
+
name: unit.source.slice(definition.from, definition.to),
|
|
457
|
+
descriptor: declaration.name === "EnumConstant" && owner ? `L${owner};` : "V",
|
|
458
|
+
owner,
|
|
459
|
+
node: definition,
|
|
460
|
+
declaration,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
if (declaration.name === "FieldDeclaration") {
|
|
464
|
+
return getFieldNameAndType(declaration, unit, unit.source, externalRefs)[0] ?? null;
|
|
465
|
+
}
|
|
466
|
+
const definition = findDefinition(declaration);
|
|
467
|
+
if (!definition)
|
|
468
|
+
return null;
|
|
469
|
+
const isConstructor = declaration.name === "ConstructorDeclaration" || declaration.name === "CompactConstructorDeclaration";
|
|
470
|
+
const name = isConstructor ? "<init>" : unit.source.slice(definition.from, definition.to);
|
|
471
|
+
const parameters = findChild(declaration, (node) => node.name === "FormalParameters");
|
|
472
|
+
const parameterDescriptors = [];
|
|
473
|
+
if (declaration.name === "CompactConstructorDeclaration" && ownerNode) {
|
|
474
|
+
const recordHeader = findChild(ownerNode, (node) => node.name === "RecordHeader");
|
|
475
|
+
if (recordHeader) {
|
|
476
|
+
for (const component of findChildren(recordHeader, (node) => node.name === "RecordComponent")) {
|
|
477
|
+
const typeNode = findDirectTypeNode(component);
|
|
478
|
+
parameterDescriptors.push(getDescriptorType(typeNode, unit, unit.source, externalRefs, declaration));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else if (parameters) {
|
|
483
|
+
let parameter = parameters.firstChild;
|
|
484
|
+
while (parameter) {
|
|
485
|
+
if (parameter.name === "FormalParameter" || parameter.name === "SpreadParameter") {
|
|
486
|
+
const typeNode = parameter.name === "SpreadParameter" ? parameter : getParameterType(parameter);
|
|
487
|
+
parameterDescriptors.push(getDescriptorType(typeNode, unit, unit.source, externalRefs, declaration));
|
|
488
|
+
}
|
|
489
|
+
parameter = parameter.nextSibling;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
let returnDescriptor = "V";
|
|
493
|
+
if (declaration.name === "MethodDeclaration") {
|
|
494
|
+
const returnType = findDirectTypeNode(declaration);
|
|
495
|
+
returnDescriptor = getDescriptorType(returnType, unit, unit.source, externalRefs, declaration);
|
|
496
|
+
}
|
|
497
|
+
return {
|
|
498
|
+
category: "declaration",
|
|
499
|
+
kind: isConstructor ? "constructor" : "method",
|
|
500
|
+
name,
|
|
501
|
+
descriptor: `(${parameterDescriptors.join("")})${returnDescriptor}`,
|
|
502
|
+
owner,
|
|
503
|
+
node: definition,
|
|
504
|
+
declaration,
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
const declarationAtNode = (unit, offset, side, externalRefs) => {
|
|
508
|
+
let resolvedNode = unit.tree.resolveInner(offset, side);
|
|
509
|
+
let declaration = findDeclarationAncestor(resolvedNode);
|
|
510
|
+
if (!declaration) {
|
|
511
|
+
let nearest = null;
|
|
512
|
+
const findContainingDeclaration = (node) => {
|
|
513
|
+
if (DECLARATION_NAMES.has(node.name) && node.from <= offset && offset <= node.to) {
|
|
514
|
+
if (!nearest || node.to - node.from < nearest.to - nearest.from)
|
|
515
|
+
nearest = node;
|
|
516
|
+
}
|
|
517
|
+
let child = node.firstChild;
|
|
518
|
+
while (child) {
|
|
519
|
+
findContainingDeclaration(child);
|
|
520
|
+
child = child.nextSibling;
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
findContainingDeclaration(unit.tree.topNode);
|
|
524
|
+
declaration = nearest;
|
|
525
|
+
}
|
|
526
|
+
if (!declaration)
|
|
527
|
+
return null;
|
|
528
|
+
if (declaration.name === "FieldDeclaration") {
|
|
529
|
+
const fields = getFieldNameAndType(declaration, unit, unit.source, externalRefs);
|
|
530
|
+
const containing = fields.find((field) => field.node.from <= offset && offset <= field.node.to);
|
|
531
|
+
return containing ?? null;
|
|
532
|
+
}
|
|
533
|
+
return resolveDeclarationInfo(declaration, unit, externalRefs);
|
|
534
|
+
};
|
|
535
|
+
/**
|
|
536
|
+
* Creates a resolver for field and method declarations in a compilation unit.
|
|
537
|
+
*/
|
|
538
|
+
export const createDeclarationResolver = (unit, refs = []) => {
|
|
539
|
+
return {
|
|
540
|
+
unit,
|
|
541
|
+
resolveAt(offset, side = 0) {
|
|
542
|
+
return declarationAtNode(unit, offset, side, refs);
|
|
543
|
+
},
|
|
544
|
+
};
|
|
545
|
+
};
|
|
546
|
+
/**
|
|
547
|
+
* Creates a composite resolver that checks declarations before type references.
|
|
548
|
+
*/
|
|
549
|
+
export const createCompositeResolver = (unit, refs = []) => {
|
|
550
|
+
const declarationResolver = createDeclarationResolver(unit, refs);
|
|
551
|
+
const typeResolver = createTypeReferenceResolver(unit, refs);
|
|
552
|
+
return {
|
|
553
|
+
unit,
|
|
554
|
+
resolveAt(offset, side = 0) {
|
|
555
|
+
return declarationResolver.resolveAt(offset, side) ?? typeResolver.resolveAt(offset, side);
|
|
247
556
|
},
|
|
248
557
|
};
|
|
249
558
|
};
|