Added JS [] array resolution i.e. var list = ["hi", player]

+ added missing Array.d.ts overloads
This commit is contained in:
bigguy345 committed 2026-03-09 00:35:05 +02:00
1 parent 14a4c440b6
commit dc3e25bcba
4 files changed
+47 -4

No files matched your search

@@ -1114,10 +1114,6 @@ public final class ObjectLiteralParser {
return TypeInfo.ANY;
}
if (first == '[') {
return TypeInfo.ANY;
}
TypeInfo inferred = typeResolver.resolve(valueExpr, valueStartAbs);
return (inferred != null) ? inferred : TypeInfo.ANY;
}
@@ -5347,6 +5347,12 @@ public class ScriptDocument {
return resolveExpressionWithParserAPI(expr, position);
}
// JS array literals: [], [a, b], ["hi", "bye"]
if (isJavaScript() && expr.startsWith("[") && expr.endsWith("]")) {
String inner = expr.substring(1, expr.length() - 1).trim();
return TypeInfo.arrayOf(unifyJsArrayElementType(inner, position + 1));
}
// Invalid expressions starting with brackets
if (expr.startsWith("[") || expr.startsWith("]")) {
return null; // Invalid syntax
@@ -5430,6 +5436,13 @@ public class ScriptDocument {
if (varInfo != null && varInfo.getTypeInfo() instanceof ClassTypeInfo) {
return ((ClassTypeInfo) varInfo.getTypeInfo()).getInstanceType();
}
if (isJavaScript() && typeName.equals("Array")) {
String rest = expr.substring(newMatcher.end()).trim();
String argsText = (rest.startsWith("(") && rest.endsWith(")"))
? rest.substring(1, rest.length() - 1).trim()
: "";
return TypeInfo.arrayOf(unifyJsArrayElementType(argsText, position));
}
TypeInfo baseType = resolveType(typeName);
if (baseType == null) return null;
String rest = expr.substring(newMatcher.end());
@@ -5753,6 +5766,28 @@ public class ScriptDocument {
return argTypes;
}
/**
* For JavaScript array literals, unify the element types to determine the array's element type.
*/
private TypeInfo unifyJsArrayElementType(String argsText, int position) {
if (argsText.isEmpty()) return TypeInfo.ANY;
TypeInfo[] elementTypes = parseArgumentTypes(argsText, position);
if (elementTypes.length == 1 && TypeInfo.NUMBER.equals(elementTypes[0])) return TypeInfo.ANY;
TypeInfo common = null;
boolean allSame = true;
for (TypeInfo t : elementTypes) {
boolean isUnresolvable = t == null || TypeInfo.NULL.equals(t);
if (isUnresolvable) continue;
if (common == null) {
common = t;
} else if (!common.equals(t)) {
allSame = false;
break;
}
}
return allSame && common != null ? common : TypeInfo.ANY;
}
/**
* Helper class for chain segments (can be field access or method call).
*/
@@ -33,6 +33,14 @@ public final class TypeChecker {
if (expected == null) return true; // void can accept anything (shouldn't happen)
if (actual == null) return true; // Can't verify, assume compatible
// Handle array types: compatible if element types are compatible
if (expected.isArray() && actual.isArray()) {
TypeInfo expectedElement = expected.getElementType();
TypeInfo actualElement = actual.getElementType();
if (expectedElement != null && actualElement != null)
return isTypeCompatible(expectedElement, actualElement);
}
// Script method reference placeholder: only compatible with functional interface params.
// Used to help overload selection choose SAM overloads in JavaScript.
if ("__script_method_ref__".equals(actual.getFullName())) {