@katana-project/laser 0.2.1 → 0.2.2

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