From 3929888f6069dce8fab335ab016a4d6a0e0fff59 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 5 Jan 2026 05:43:30 +0200 Subject: [PATCH] Removed redundant JS type resolution code and improved logic --- .../script/interpreter/ScriptDocument.java | 61 +++---------------- .../script/interpreter/type/TypeInfo.java | 8 ++- .../script/interpreter/type/TypeResolver.java | 33 +++++++--- 3 files changed, 39 insertions(+), 63 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java index 5318490de..70bad1e92 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java @@ -1655,24 +1655,16 @@ public class ScriptDocument { } public TypeInfo resolveType(String typeName) { - // Use language-appropriate resolution - if (isJavaScript()) { - return resolveJSTypeUnified(typeName); - } + /** + * Resolve a JS type name to unified TypeInfo. + * Handles JS primitives, .d.ts defined types, and falls back to Java types. + */ + if (isJavaScript()) + return typeResolver.resolveJSType(typeName); + return resolveTypeAndTrackUsage(typeName); } - /** - * Resolve a JS type name to unified TypeInfo. - * Handles JS primitives, .d.ts defined types, and falls back to Java types. - */ - private TypeInfo resolveJSTypeUnified(String typeName) { - if (typeName == null || typeName.isEmpty()) { - return TypeInfo.fromPrimitive("void"); - } - return typeResolver.resolveJSType(typeName); - } - /** * Resolve a type and track the import usage for unused import detection. * Checks script-defined types first, then falls back to imported types. @@ -2455,45 +2447,6 @@ public class ScriptDocument { marks.add(new ScriptLine.Mark(start, end, TokenType.COMMENT, null)); } } - - /** - * Resolve a type name from JSDoc to a TypeInfo. - */ - private TypeInfo resolveJSDocType(String typeName) { - if (typeName == null || typeName.isEmpty()) { - return null; - } - - // Handle union types - use first type - if (typeName.contains("|")) { - typeName = typeName.split("\\|")[0].trim(); - } - - // Handle nullable - typeName = typeName.replace("?", "").trim(); - - // Map JSDoc types to Java types - switch (typeName.toLowerCase()) { - case "string": - return TypeInfo.fromClass(String.class); - case "number": - case "int": - case "integer": - return isJavaScript() ? TypeInfo.fromClass(double.class) : TypeInfo.fromClass(int.class); - case "boolean": - case "bool": - return TypeInfo.fromClass(boolean.class); - case "void": - return TypeInfo.fromPrimitive("void"); - case "any": - case "object": - case "*": - return TypeInfo.ANY; - } - - // Try through the type resolver - return resolveType(typeName); - } private void markMethodDeclarations(List marks) { Matcher m = METHOD_DECL_PATTERN.matcher(text); diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeInfo.java index 28f307187..cc192e38c 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeInfo.java @@ -37,13 +37,19 @@ public class TypeInfo { */ public static final TypeInfo VOID = fromPrimitive("void"); + public static final TypeInfo BOOLEAN = fromPrimitive("boolean"); + + public static final TypeInfo STRING = TypeInfo.fromClass(String.class); + /** * Singleton constant for the "any" type (used in JavaScript/TypeScript). * The "any" type is universally compatible - it can be assigned to anything * and anything can be assigned to it. */ public static final TypeInfo ANY = new TypeInfo("any", "any", "", Kind.CLASS, null, true, null); - + public static final TypeInfo NUMBER = new TypeInfo("number", "number", "", Kind.CLASS, double.class, true, null); + + private final String simpleName; // e.g., "List", "ColorType" private final String fullName; // e.g., "java.util.List", "kamkeel...IOverlay$ColorType" private final String packageName; // e.g., "java.util", "kamkeel.npcdbc.api.client.overlay" diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeResolver.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeResolver.java index 297256f97..228c26040 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeResolver.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeResolver.java @@ -136,18 +136,35 @@ public class TypeResolver { if (jsTypeName == null || jsTypeName.isEmpty()) { return TypeInfo.fromPrimitive("void"); } + + // Handle union types - use first type + if (jsTypeName.contains("|")) { + jsTypeName = jsTypeName.split("\\|")[0].trim(); + } + + // Handle nullable + jsTypeName = jsTypeName.replace("?", "").trim(); // Strip array brackets for base type resolution boolean isArray = jsTypeName.endsWith("[]"); String baseName = isArray ? jsTypeName.substring(0, jsTypeName.length() - 2) : jsTypeName; - - // Check JS primitives first - if (JS_PRIMITIVE_TO_JAVA.containsKey(baseName)) { - String javaType = JS_PRIMITIVE_TO_JAVA.get(baseName); - TypeInfo baseType = TypeResolver.isPrimitiveType(javaType) - ? TypeInfo.fromPrimitive(javaType) - : resolveFullName("java.lang." + javaType); - return isArray ? TypeInfo.arrayOf(baseType) : baseType; + + switch (baseName.toLowerCase()) { + case "string": + return TypeInfo.STRING; + case "number": + case "int": + case "integer": + return TypeInfo.NUMBER; + case "boolean": + case "bool": + return TypeInfo.BOOLEAN; + case "void": + return TypeInfo.VOID; + case "any": + case "object": + case "*": + return TypeInfo.ANY; } // Check JS type registry