@katana-project/laser 0.2.3 → 0.3.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/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # laser
2
2
 
3
- A Java source code analysis engine, built on the Lezer parser.
3
+ A Java source code analysis library, built on the Lezer parser.
@@ -18,14 +18,15 @@ export interface LocalTypeReference extends TypeReference {
18
18
  export interface ExternalTypeReference extends TypeReference {
19
19
  qualifiedName: string;
20
20
  packageName: string | null;
21
+ moduleName: string | null;
21
22
  }
22
23
  /**
23
24
  * Resolved type information.
24
25
  *
25
- * Includes whether the type is declared locally, imported, a built-in type, or unresolved.
26
+ * Includes whether the type is declared locally, imported, or a built-in type.
26
27
  */
27
28
  export interface ResolvedType {
28
- kind: "declared" | "imported" | "builtin" | "unresolved";
29
+ kind: "declared" | "imported" | "builtin";
29
30
  name: string;
30
31
  qualifiedName?: string;
31
32
  declaration?: SyntaxNode;
@@ -42,7 +43,7 @@ export interface ResolvedType {
42
43
  * types in the same package that aren't explicitly imported.
43
44
  * @returns ResolvedType containing complete type information
44
45
  */
45
- export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: CompilationUnit, externalRefs?: ExternalTypeReference[]) => ResolvedType;
46
+ export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: CompilationUnit, externalRefs?: ExternalTypeReference[]) => ResolvedType | null;
46
47
  /**
47
48
  * Type reference resolver for a compilation unit.
48
49
  *
package/dist/resolver.js CHANGED
@@ -97,8 +97,10 @@ const resolveInUnit = (typeName, unit) => {
97
97
  return null;
98
98
  };
99
99
  const resolveImported = (typeRef, unit, externalRefs) => {
100
+ // type imports
100
101
  for (const imp of unit.imports) {
101
- if (!imp.isWildcard && (imp.importedName === typeRef.name || imp.importedName.endsWith(`.${typeRef.name}`))) {
102
+ if (imp.kind === "type" &&
103
+ (imp.importedName === typeRef.name || imp.importedName.endsWith(`.${typeRef.name}`))) {
102
104
  return {
103
105
  kind: "imported",
104
106
  name: typeRef.name,
@@ -107,8 +109,9 @@ const resolveImported = (typeRef, unit, externalRefs) => {
107
109
  };
108
110
  }
109
111
  }
112
+ // wildcard imports
110
113
  for (const imp of unit.imports) {
111
- if (imp.isWildcard) {
114
+ if (imp.kind === "wildcard") {
112
115
  const matchingExternal = externalRefs.find((ext) => `${imp.importedName}.${typeRef.name}` === ext.qualifiedName);
113
116
  if (matchingExternal) {
114
117
  return {
@@ -120,6 +123,21 @@ const resolveImported = (typeRef, unit, externalRefs) => {
120
123
  }
121
124
  }
122
125
  }
126
+ // module imports
127
+ for (const imp of unit.imports) {
128
+ if (imp.kind === "module") {
129
+ const matchingExternal = externalRefs.find((ext) => ext.moduleName === imp.importedName &&
130
+ (ext.name === typeRef.name || ext.qualifiedName.endsWith(`.${typeRef.name}`)));
131
+ if (matchingExternal) {
132
+ return {
133
+ kind: "imported",
134
+ name: typeRef.name,
135
+ qualifiedName: matchingExternal.qualifiedName,
136
+ ref: typeRef,
137
+ };
138
+ }
139
+ }
140
+ }
123
141
  // same-package classes
124
142
  let matchingExternal = externalRefs.find((ext) => ext.packageName === (unit.packageName ?? "") && ext.name === typeRef.name);
125
143
  if (!matchingExternal) {
@@ -134,6 +152,8 @@ const resolveImported = (typeRef, unit, externalRefs) => {
134
152
  ref: typeRef,
135
153
  };
136
154
  }
155
+ // returning unresolved types would contain false positives for references that aren't actually types
156
+ // we don't have enough info to do ambiguity resolution here, so just return nothing
137
157
  return null;
138
158
  };
139
159
  /**
@@ -180,11 +200,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
180
200
  if (importedResolved) {
181
201
  return importedResolved;
182
202
  }
183
- return {
184
- kind: "unresolved",
185
- name: typeName,
186
- ref: typeRef,
187
- };
203
+ return null;
188
204
  };
189
205
  /**
190
206
  * Creates a type reference resolver for a compilation unit.
@@ -227,7 +243,7 @@ export const createTypeReferenceResolver = (unit, refs = []) => {
227
243
  },
228
244
  resolveAll() {
229
245
  const allRefs = collectAllTypeReferences(unit.tree.topNode, unit.source);
230
- return allRefs.map((ref) => resolveTypeReference(ref, unit, refs));
246
+ return allRefs.map((ref) => resolveTypeReference(ref, unit, refs)).filter(Boolean);
231
247
  },
232
248
  };
233
249
  };
package/dist/unit.d.ts CHANGED
@@ -3,16 +3,15 @@ import { SyntaxNode, Tree } from "@lezer/common";
3
3
  * Represents an import statement in a Java source file.
4
4
  */
5
5
  export interface ImportInfo {
6
+ kind: "type" | "wildcard" | "static" | "module";
6
7
  importedName: string;
7
- isStatic: boolean;
8
- isWildcard: boolean;
9
8
  node: SyntaxNode;
10
9
  }
11
10
  /**
12
11
  * Represents a type declaration (class, interface, enum, annotation, module) in a Java source file.
13
12
  */
14
13
  export interface TypeInfo {
15
- kind: "class" | "interface" | "enum" | "annotation" | "module";
14
+ kind: "class" | "interface" | "enum" | "annotation" | "module" | "record";
16
15
  name: string;
17
16
  qualifiedName: string;
18
17
  node: SyntaxNode;
package/dist/unit.js CHANGED
@@ -42,16 +42,19 @@ export const parseUnit = (tree, source) => {
42
42
  }
43
43
  }
44
44
  else if (node.name === "ImportDeclaration") {
45
- const isStatic = !!findChild(node, (c) => c.name === "static");
46
- const isWildcard = !!findChild(node, (c) => c.name === "Asterisk");
47
45
  const nameNode = findChild(node, (c) => c.name === "Identifier" || c.name === "ScopedIdentifier");
48
46
  if (nameNode) {
49
47
  const importedName = source.slice(nameNode.from, nameNode.to);
50
48
  if (importedName) {
51
49
  unit.imports.push({
50
+ kind: findChild(node, (c) => c.name === "module")
51
+ ? "module"
52
+ : findChild(node, (c) => c.name === "static")
53
+ ? "static"
54
+ : findChild(node, (c) => c.name === "Asterisk")
55
+ ? "wildcard"
56
+ : "type",
52
57
  importedName,
53
- isStatic,
54
- isWildcard,
55
58
  node,
56
59
  });
57
60
  }
@@ -61,7 +64,8 @@ export const parseUnit = (tree, source) => {
61
64
  node.name === "InterfaceDeclaration" ||
62
65
  node.name === "EnumDeclaration" ||
63
66
  node.name === "AnnotationTypeDeclaration" ||
64
- node.name === "ModuleDeclaration") {
67
+ node.name === "ModuleDeclaration" ||
68
+ node.name === "RecordDeclaration") {
65
69
  const defNode = findChild(node, (c) => c.name === "Definition" || c.name === "Identifier");
66
70
  if (defNode) {
67
71
  const name = source.slice(defNode.from, defNode.to);
@@ -76,7 +80,9 @@ export const parseUnit = (tree, source) => {
76
80
  ? "enum"
77
81
  : node.name === "AnnotationTypeDeclaration"
78
82
  ? "annotation"
79
- : "module",
83
+ : node.name === "ModuleDeclaration"
84
+ ? "module"
85
+ : "record",
80
86
  name,
81
87
  qualifiedName,
82
88
  node,
@@ -86,7 +92,8 @@ export const parseUnit = (tree, source) => {
86
92
  c.name === "InterfaceBody" ||
87
93
  c.name === "AnnotationTypeBody" ||
88
94
  c.name === "ModuleBody" ||
89
- c.name === "EnumBody");
95
+ c.name === "EnumBody" ||
96
+ c.name === "RecordBody");
90
97
  if (classBody) {
91
98
  let child = classBody.firstChild;
92
99
  while (child) {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@katana-project/laser",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "author": "katana-project",
6
6
  "license": "MIT",
7
- "description": "A Java source code analysis engine, built on the Lezer parser.",
7
+ "description": "A Java source code analysis library, built on the Lezer parser.",
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "@lezer/common": "^1.5.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@lezer/java": "^1.1.3",
29
+ "@run-slicer/lezer-java": "^1.2.1",
30
30
  "@types/chai": "^5.2.3",
31
31
  "@types/mocha": "^10.0.10",
32
32
  "@types/node": "^25.0.5",