@katana-project/laser 0.2.1 → 0.2.3

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.
Files changed (2) hide show
  1. package/dist/resolver.js +43 -30
  2. package/package.json +1 -1
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": {
@@ -31,6 +33,7 @@ const getTypeName = (node, source) => {
31
33
  else if (node.parent.name !== "MarkerAnnotation" &&
32
34
  node.parent.name !== "Annotation" &&
33
35
  node.parent.name !== "AnnotationTypeDeclaration") {
36
+ // TODO: class literals are a bit broken in the AST, work around that here
34
37
  // only allow getting Identifier-based type names from annotations
35
38
  return null;
36
39
  }
@@ -38,22 +41,26 @@ const getTypeName = (node, source) => {
38
41
  return source.slice(node.from, node.to);
39
42
  }
40
43
  case "Definition": {
41
- if (node.parent.name !== "EnumDeclaration" &&
42
- node.parent.name !== "ClassDeclaration" &&
43
- node.parent.name !== "ConstructorDeclaration" &&
44
- node.parent.name !== "InterfaceDeclaration" &&
45
- node.parent.name !== "LocalVariableDeclaration") {
44
+ if (!node.parent ||
45
+ (node.parent.name !== "EnumDeclaration" &&
46
+ node.parent.name !== "ClassDeclaration" &&
47
+ node.parent.name !== "ConstructorDeclaration" &&
48
+ node.parent.name !== "InterfaceDeclaration" &&
49
+ node.parent.name !== "LocalVariableDeclaration")) {
46
50
  // only allow getting Definition-based type names from these declarations
47
51
  return null;
48
52
  }
49
53
  return source.slice(node.from, node.to);
50
54
  }
51
55
  case "FieldAccess": {
52
- if (node.parent?.name === "FieldAccess" || node.firstChild.name !== "Identifier") {
53
- // pointing at a field, inner class access or a different expression, we can't resolve that
56
+ if (node.parent?.name === "FieldAccess") {
57
+ // pointing at a field, we can't resolve that
54
58
  return null;
55
59
  }
56
- return source.slice(node.firstChild.from, node.firstChild.to);
60
+ // pointing at an inner class access or a different expression, we can't resolve that
61
+ return node.firstChild?.name === "Identifier"
62
+ ? source.slice(node.firstChild.from, node.firstChild.to)
63
+ : null;
57
64
  }
58
65
  }
59
66
  return null;
@@ -81,33 +88,28 @@ const collectAllTypeReferences = (node, source, refs = []) => {
81
88
  };
82
89
  const resolveInUnit = (typeName, unit) => {
83
90
  for (const cls of unit.types) {
84
- if (cls.name === typeName || cls.qualifiedName === typeName) {
91
+ if (cls.name === typeName ||
92
+ cls.qualifiedName === typeName ||
93
+ (unit.packageName ? `${unit.packageName}.` : "") + cls.qualifiedName === typeName) {
85
94
  return cls;
86
95
  }
87
96
  }
88
97
  return null;
89
98
  };
90
99
  const resolveImported = (typeRef, unit, externalRefs) => {
91
- const parts = typeRef.name.split(".");
92
- const firstPart = parts[0];
93
100
  for (const imp of unit.imports) {
94
- if (!imp.isWildcard) {
95
- const importParts = imp.importedName.split(".");
96
- const importedClass = importParts[importParts.length - 1];
97
- if (importedClass === firstPart) {
98
- return {
99
- kind: "imported",
100
- name: typeRef.name,
101
- qualifiedName: imp.importedName,
102
- ref: typeRef,
103
- };
104
- }
101
+ if (!imp.isWildcard && (imp.importedName === typeRef.name || imp.importedName.endsWith(`.${typeRef.name}`))) {
102
+ return {
103
+ kind: "imported",
104
+ name: typeRef.name,
105
+ qualifiedName: imp.importedName,
106
+ ref: typeRef,
107
+ };
105
108
  }
106
109
  }
107
110
  for (const imp of unit.imports) {
108
111
  if (imp.isWildcard) {
109
- const packageName = imp.importedName;
110
- const matchingExternal = externalRefs.find((ext) => ext.packageName === packageName && ext.name === firstPart);
112
+ const matchingExternal = externalRefs.find((ext) => `${imp.importedName}.${typeRef.name}` === ext.qualifiedName);
111
113
  if (matchingExternal) {
112
114
  return {
113
115
  kind: "imported",
@@ -118,10 +120,11 @@ const resolveImported = (typeRef, unit, externalRefs) => {
118
120
  }
119
121
  }
120
122
  }
121
- let matchingExternal = externalRefs.find((ext) => ext.packageName === (unit.packageName ?? "") && ext.name === firstPart);
123
+ // same-package classes
124
+ let matchingExternal = externalRefs.find((ext) => ext.packageName === (unit.packageName ?? "") && ext.name === typeRef.name);
122
125
  if (!matchingExternal) {
123
126
  // implicit import of java.lang.*
124
- matchingExternal = externalRefs.find((ext) => ext.packageName === "java.lang" && ext.name === firstPart);
127
+ matchingExternal = externalRefs.find((ext) => ext.packageName === "java.lang" && ext.name === typeRef.name);
125
128
  }
126
129
  if (matchingExternal) {
127
130
  return {
@@ -153,11 +156,21 @@ export const resolveTypeReference = (typeRef, unit, externalRefs = []) => {
153
156
  ref: typeRef,
154
157
  };
155
158
  }
159
+ // find exact match in external refs - fully qualified name?
160
+ const externalMatch = externalRefs.find((ext) => ext.qualifiedName === typeName);
161
+ if (externalMatch) {
162
+ return {
163
+ kind: "imported",
164
+ name: typeName,
165
+ qualifiedName: externalMatch.qualifiedName,
166
+ ref: typeRef,
167
+ };
168
+ }
156
169
  const localResolved = resolveInUnit(typeName, unit);
157
170
  if (localResolved) {
158
171
  return {
159
172
  kind: "declared",
160
- name: localResolved.qualifiedName,
173
+ name: typeName.includes(".") ? typeName : localResolved.qualifiedName,
161
174
  qualifiedName: (unit.packageName ? `${unit.packageName}.` : "") + localResolved.qualifiedName,
162
175
  declaration: localResolved.node,
163
176
  ref: typeRef,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katana-project/laser",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "author": "katana-project",
6
6
  "license": "MIT",