@katana-project/laser 0.1.0 → 0.2.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.
@@ -1,21 +1,101 @@
1
- import { SyntaxNode, Tree } from "@lezer/common";
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: TypeReference;
32
+ ref: LocalTypeReference;
13
33
  }
14
- export declare const resolveTypeReference: (typeRef: TypeReference, unit: CompilationUnit) => ResolvedType;
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;
17
- resolveAt(offset: number): ResolvedType | null;
18
- resolveReferenceAt(offset: number): TypeReference | null;
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
+ */
64
+ resolveAt(offset: number, side?: -1 | 0 | 1): ResolvedType | null;
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
- export declare const createTypeReferenceResolver: (tree: Tree, source: string) => TypeReferenceResolver;
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
@@ -1,5 +1,3 @@
1
- import { getNodeStack } from "./tree-utils.js";
2
- import { parseUnit } from "./unit.js";
3
1
  const BUILTIN_TYPES = new Set(["byte", "short", "int", "long", "char", "float", "double", "boolean", "void"]);
4
2
  const ALLOWED_PARENTS = new Set([
5
3
  "TypeName",
@@ -60,20 +58,7 @@ const getTypeName = (node, source) => {
60
58
  }
61
59
  return null;
62
60
  };
63
- const findTypeReference = (nodes, source) => {
64
- for (let i = nodes.length - 1; i >= 0; i--) {
65
- const node = nodes[i];
66
- const name = getTypeName(node, source);
67
- if (name) {
68
- return {
69
- name,
70
- node,
71
- };
72
- }
73
- }
74
- return null;
75
- };
76
- const collectAllTypeReferences = (node, source, refs) => {
61
+ const collectAllTypeReferences = (node, source, refs = []) => {
77
62
  const name = getTypeName(node, source);
78
63
  if (name) {
79
64
  const existing = refs.find((r) => r.node === node);
@@ -92,6 +77,7 @@ const collectAllTypeReferences = (node, source, refs) => {
92
77
  child = child.nextSibling;
93
78
  }
94
79
  }
80
+ return refs;
95
81
  };
96
82
  const resolveInUnit = (typeName, unit) => {
97
83
  for (const cls of unit.types) {
@@ -101,7 +87,7 @@ const resolveInUnit = (typeName, unit) => {
101
87
  }
102
88
  return null;
103
89
  };
104
- const resolveImported = (typeRef, unit) => {
90
+ const resolveImported = (typeRef, unit, externalRefs) => {
105
91
  const parts = typeRef.name.split(".");
106
92
  const firstPart = parts[0];
107
93
  for (const imp of unit.imports) {
@@ -118,9 +104,43 @@ const resolveImported = (typeRef, unit) => {
118
104
  }
119
105
  }
120
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
+ const matchingExternal = externalRefs.find((ext) => ext.packageName === (unit.packageName ?? "") && ext.name === firstPart);
122
+ if (matchingExternal) {
123
+ return {
124
+ kind: "imported",
125
+ name: typeRef.name,
126
+ qualifiedName: matchingExternal.qualifiedName,
127
+ ref: typeRef,
128
+ };
129
+ }
121
130
  return null;
122
131
  };
123
- export const resolveTypeReference = (typeRef, unit) => {
132
+ /**
133
+ * Resolves a local type reference to its complete type information.
134
+ *
135
+ * @param typeRef - The local type reference to resolve
136
+ * @param unit - The compilation unit containing the reference
137
+ * @param externalRefs - Optional array of external type references for resolving wildcard imports and same-package classes.
138
+ * Each external reference should include the type name, qualified name, and package name.
139
+ * This enables resolution of types from wildcard imports (e.g., import java.util.*) and
140
+ * types in the same package that aren't explicitly imported.
141
+ * @returns ResolvedType containing complete type information
142
+ */
143
+ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
124
144
  const typeName = typeRef.name;
125
145
  if (BUILTIN_TYPES.has(typeName)) {
126
146
  return {
@@ -133,58 +153,64 @@ export const resolveTypeReference = (typeRef, unit) => {
133
153
  if (localResolved) {
134
154
  return {
135
155
  kind: "declared",
136
- name: localResolved.name,
137
- qualifiedName: localResolved.qualifiedName,
156
+ name: localResolved.qualifiedName,
157
+ qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
138
158
  declaration: localResolved.node,
139
159
  ref: typeRef,
140
160
  };
141
161
  }
142
- const importedResolved = resolveImported(typeRef, unit);
162
+ const importedResolved = resolveImported(typeRef, unit, externalRefs);
143
163
  if (importedResolved) {
144
164
  return importedResolved;
145
165
  }
146
- if (unit.packageName) {
147
- const qualifiedName = `${unit.packageName}.${typeName}`;
148
- const resolved = resolveInUnit(qualifiedName, unit);
149
- if (resolved) {
150
- return {
151
- kind: "imported",
152
- name: resolved.name,
153
- qualifiedName: resolved.qualifiedName,
154
- declaration: resolved.node,
155
- ref: typeRef,
156
- };
157
- }
158
- }
159
166
  return {
160
167
  kind: "unresolved",
161
168
  name: typeName,
162
169
  ref: typeRef,
163
170
  };
164
171
  };
165
- export const createTypeReferenceResolver = (tree, source) => {
166
- const unit = parseUnit(tree.topNode, source);
172
+ /**
173
+ * Creates a type reference resolver for a compilation unit.
174
+ *
175
+ * @param unit - The compilation unit to analyze
176
+ * @param refs - Optional array of external type references for resolving wildcard imports and same-package classes.
177
+ * Each external reference should include the type name, qualified name, and package name.
178
+ * This enables resolution of types from wildcard imports (e.g., import java.util.*) and
179
+ * types in the same package that aren't explicitly imported.
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const externalRefs = [
184
+ * { name: "List", qualifiedName: "java.util.List", packageName: "java.util" },
185
+ * { name: "HashMap", qualifiedName: "java.util.HashMap", packageName: "java.util" },
186
+ * { name: "OtherClass", qualifiedName: "com.example.OtherClass", packageName: "com.example" }
187
+ * ];
188
+ * const resolver = createTypeReferenceResolver(unit, externalRefs);
189
+ * ```
190
+ */
191
+ export const createTypeReferenceResolver = (unit, refs = []) => {
167
192
  return {
168
193
  unit,
169
- resolveAt: (offset) => {
170
- const nodes = getNodeStack(tree, offset);
171
- if (!nodes)
172
- return null;
173
- const typeRef = findTypeReference(nodes, source);
194
+ resolveAt(offset, side) {
195
+ const typeRef = this.resolveReferenceAt(offset, side);
174
196
  if (!typeRef)
175
197
  return null;
176
- return resolveTypeReference(typeRef, unit);
198
+ return resolveTypeReference(typeRef, unit, refs);
177
199
  },
178
- resolveReferenceAt: (offset) => {
179
- const nodes = getNodeStack(tree, offset);
180
- if (!nodes)
181
- return null;
182
- return findTypeReference(nodes, source);
200
+ resolveReferenceAt(offset, side) {
201
+ const node = unit.tree.resolveInner(offset, side);
202
+ const name = getTypeName(node, unit.source);
203
+ if (name) {
204
+ return {
205
+ name,
206
+ node,
207
+ };
208
+ }
209
+ return null;
183
210
  },
184
- resolveAll: () => {
185
- const refs = [];
186
- collectAllTypeReferences(tree.topNode, source, refs);
187
- return refs.map((ref) => resolveTypeReference(ref, unit));
211
+ resolveAll() {
212
+ const allRefs = collectAllTypeReferences(unit.tree.topNode, unit.source);
213
+ return allRefs.map((ref) => resolveTypeReference(ref, unit, refs));
188
214
  },
189
215
  };
190
216
  };
@@ -1,5 +1,4 @@
1
- import { SyntaxNode, Tree } from "@lezer/common";
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[];
@@ -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
- import { SyntaxNode } from "@lezer/common";
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,9 +18,23 @@ 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 {
25
+ tree: Tree;
26
+ source: string;
16
27
  packageName: string | null;
17
28
  imports: ImportInfo[];
18
29
  types: TypeInfo[];
19
30
  }
20
- export declare const parseUnit: (root: SyntaxNode, source: string) => CompilationUnit;
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
+ */
40
+ export declare const parseUnit: (tree: Tree, source: string) => CompilationUnit;
package/dist/unit.js CHANGED
@@ -17,8 +17,19 @@ const extractTypeParameters = (node, source) => {
17
17
  }
18
18
  return params;
19
19
  };
20
- export const parseUnit = (root, source) => {
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
+ */
29
+ export const parseUnit = (tree, source) => {
21
30
  const unit = {
31
+ tree,
32
+ source,
22
33
  packageName: null,
23
34
  imports: [],
24
35
  types: [],
@@ -93,6 +104,6 @@ export const parseUnit = (root, source) => {
93
104
  }
94
105
  }
95
106
  };
96
- processNode(root, "");
107
+ processNode(tree.topNode, "");
97
108
  return unit;
98
109
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katana-project/laser",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "author": "katana-project",
6
6
  "license": "MIT",