From 6fc0bf4d6b17c1bf6e9d9899eeaf512c02404046 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Tue, 3 Feb 2026 22:12:43 +0200 Subject: [PATCH] Fix expression typing gate to route JS function/lambda expressions into ExpressionParser Problem: - function(act){...} expressions not reaching ExpressionParser - resolveExpressionType only checked containsOperators() or startsWith("(") - Function expressions went through simple resolver and returned null type Solution: - Added looksLikeFunctionOrLambda helper (lines 4766-4773) to detect: * JS functions: function(...) * Arrow functions: => * Java lambdas: -> - Updated gate condition at line 4144 to route these into ExpressionParser - Function expressions now reach ExpressionTypeResolver.resolveJSFunctionType Impact: - SAM typing now works in pass-2 when expected type is known - Fixes schedule("TpGainAction", function(act){...}) argument typing - Enables proper overload resolution for function/lambda parameters --- .../script/interpreter/ScriptDocument.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 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 e7dd78a6f..e25a0ccdd 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 @@ -4138,11 +4138,12 @@ public class ScriptDocument { return null; } - // Check if expression contains operators - if so, use the full expression resolver - // Handle cast expressions: (Type)expr, ((Type)expr).method(), etc. - if (containsOperators(expr) || expr.startsWith("(")) { - return resolveExpressionWithParserAPI(expr, position); - } + // Check if expression contains operators - if so, use the full expression resolver + // Handle cast expressions: (Type)expr, ((Type)expr).method(), etc. + // Also route JS function expressions and arrow lambdas through the parser + if (containsOperators(expr) || expr.startsWith("(") || looksLikeFunctionOrLambda(expr)) { + return resolveExpressionWithParserAPI(expr, position); + } // Invalid expressions starting with brackets if (expr.startsWith("[") || expr.startsWith("]")) { @@ -4755,6 +4756,22 @@ public class ScriptDocument { return false; } + /** + * Check if an expression looks like a JS function or arrow lambda. + * This is a fast heuristic to detect: + * - JS function expressions: function(...) {} + * - Arrow lambdas: (...) => ... + * These need to be routed through the parser for proper SAM typing. + */ + private boolean looksLikeFunctionOrLambda(String expr) { + if (expr == null || expr.isEmpty()) return false; + String trimmed = expr.trim(); + if (trimmed.startsWith("function")) return true; + if (trimmed.contains("=>")) return true; + if (trimmed.contains("->")) return true; + return false; + } + /** * Resolve an expression that contains operators or casts using the full expression parser. * This handles all Java operators with proper precedence and type promotion rules.