@katana-project/laser 0.2.2 → 0.2.4

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.
@@ -22,10 +22,10 @@ export interface ExternalTypeReference extends TypeReference {
22
22
  /**
23
23
  * Resolved type information.
24
24
  *
25
- * Includes whether the type is declared locally, imported, a built-in type, or unresolved.
25
+ * Includes whether the type is declared locally, imported, or a built-in type.
26
26
  */
27
27
  export interface ResolvedType {
28
- kind: "declared" | "imported" | "builtin" | "unresolved";
28
+ kind: "declared" | "imported" | "builtin";
29
29
  name: string;
30
30
  qualifiedName?: string;
31
31
  declaration?: SyntaxNode;
@@ -42,7 +42,7 @@ export interface ResolvedType {
42
42
  * types in the same package that aren't explicitly imported.
43
43
  * @returns ResolvedType containing complete type information
44
44
  */
45
- export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: CompilationUnit, externalRefs?: ExternalTypeReference[]) => ResolvedType;
45
+ export declare const resolveTypeReference: (typeRef: LocalTypeReference, unit: CompilationUnit, externalRefs?: ExternalTypeReference[]) => ResolvedType | null;
46
46
  /**
47
47
  * Type reference resolver for a compilation unit.
48
48
  *
package/dist/resolver.js CHANGED
@@ -9,14 +9,16 @@ const ALLOWED_PARENTS = new Set([
9
9
  "Definition",
10
10
  "FieldAccess",
11
11
  ]);
12
- const getTypeName = (node, source) => {
12
+ // the limit here is kinda hacking around cases where we don't want the entire fully qualified name
13
+ // the AST is a bit awkward to work with in this regard
14
+ const getTypeName = (node, source, limit = node.to) => {
13
15
  switch (node.name) {
14
16
  case "TypeName":
15
17
  case "ScopedTypeName": {
16
18
  if (node.parent && ALLOWED_PARENTS.has(node.parent.type.name)) {
17
- return getTypeName(node.parent, source);
19
+ return getTypeName(node.parent, source, limit);
18
20
  }
19
- return source.slice(node.from, node.to);
21
+ return source.slice(node.from, Math.min(limit, node.to));
20
22
  }
21
23
  case "PrimitiveType":
22
24
  case "void": {
@@ -30,10 +32,9 @@ const getTypeName = (node, source) => {
30
32
  }
31
33
  else if (node.parent.name !== "MarkerAnnotation" &&
32
34
  node.parent.name !== "Annotation" &&
33
- node.parent.name !== "AnnotationTypeDeclaration" &&
34
- // the AST seems to be having a bit of trouble with ClassLiteral, mapping it to an Identifier instead
35
- !(node.nextSibling?.name === "." && node.nextSibling?.nextSibling?.name === "class")) {
36
- // only allow getting Identifier-based type names from annotations or class literals
35
+ node.parent.name !== "AnnotationTypeDeclaration") {
36
+ // TODO: class literals are a bit broken in the AST, work around that here
37
+ // only allow getting Identifier-based type names from annotations
37
38
  return null;
38
39
  }
39
40
  }
@@ -133,6 +134,8 @@ const resolveImported = (typeRef, unit, externalRefs) => {
133
134
  ref: typeRef,
134
135
  };
135
136
  }
137
+ // returning unresolved types would contain false positives for references that aren't actually types
138
+ // we don't have enough info to do ambiguity resolution here, so just return nothing
136
139
  return null;
137
140
  };
138
141
  /**
@@ -169,7 +172,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
169
172
  if (localResolved) {
170
173
  return {
171
174
  kind: "declared",
172
- name: localResolved.qualifiedName,
175
+ name: typeName.includes(".") ? typeName : localResolved.qualifiedName,
173
176
  qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
174
177
  declaration: localResolved.node,
175
178
  ref: typeRef,
@@ -179,11 +182,7 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
179
182
  if (importedResolved) {
180
183
  return importedResolved;
181
184
  }
182
- return {
183
- kind: "unresolved",
184
- name: typeName,
185
- ref: typeRef,
186
- };
185
+ return null;
187
186
  };
188
187
  /**
189
188
  * Creates a type reference resolver for a compilation unit.
@@ -226,7 +225,7 @@ export const createTypeReferenceResolver = (unit, refs = []) => {
226
225
  },
227
226
  resolveAll() {
228
227
  const allRefs = collectAllTypeReferences(unit.tree.topNode, unit.source);
229
- return allRefs.map((ref) => resolveTypeReference(ref, unit, refs));
228
+ return allRefs.map((ref) => resolveTypeReference(ref, unit, refs)).filter(Boolean);
230
229
  },
231
230
  };
232
231
  };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@katana-project/laser",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
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",