@katana-project/laser 0.1.1 → 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
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;
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
- resolveReferenceAt(offset: number, side?: -1 | 0 | 1): TypeReference | 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: (unit: CompilationUnit) => 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
@@ -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,43 @@ 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
+ 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
+ }
107
130
  return null;
108
131
  };
109
- 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 = []) => {
110
144
  const typeName = typeRef.name;
111
145
  if (BUILTIN_TYPES.has(typeName)) {
112
146
  return {
@@ -119,43 +153,49 @@ export const resolveTypeReference = (typeRef, unit) => {
119
153
  if (localResolved) {
120
154
  return {
121
155
  kind: "declared",
122
- name: localResolved.name,
123
- qualifiedName: localResolved.qualifiedName,
156
+ name: localResolved.qualifiedName,
157
+ qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
124
158
  declaration: localResolved.node,
125
159
  ref: typeRef,
126
160
  };
127
161
  }
128
- const importedResolved = resolveImported(typeRef, unit);
162
+ const importedResolved = resolveImported(typeRef, unit, externalRefs);
129
163
  if (importedResolved) {
130
164
  return importedResolved;
131
165
  }
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
166
  return {
146
167
  kind: "unresolved",
147
168
  name: typeName,
148
169
  ref: typeRef,
149
170
  };
150
171
  };
151
- export const createTypeReferenceResolver = (unit) => {
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 = []) => {
152
192
  return {
153
193
  unit,
154
194
  resolveAt(offset, side) {
155
195
  const typeRef = this.resolveReferenceAt(offset, side);
156
196
  if (!typeRef)
157
197
  return null;
158
- return resolveTypeReference(typeRef, unit);
198
+ return resolveTypeReference(typeRef, unit, refs);
159
199
  },
160
200
  resolveReferenceAt(offset, side) {
161
201
  const node = unit.tree.resolveInner(offset, side);
@@ -169,8 +209,8 @@ export const createTypeReferenceResolver = (unit) => {
169
209
  return null;
170
210
  },
171
211
  resolveAll() {
172
- const refs = collectAllTypeReferences(unit.tree.topNode, unit.source);
173
- return refs.map((ref) => resolveTypeReference(ref, unit));
212
+ const allRefs = collectAllTypeReferences(unit.tree.topNode, unit.source);
213
+ return allRefs.map((ref) => resolveTypeReference(ref, unit, refs));
174
214
  },
175
215
  };
176
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
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katana-project/laser",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "author": "katana-project",
6
6
  "license": "MIT",