From 2e0a8b6d7c53d0296d0b5a5aa35d9e4d9d0d0c63 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Wed, 13 Aug 2025 11:29:08 -0400 Subject: [PATCH] Enhance diagnostics for computed property expressions: improve error messages and hints for unclosed computed property expressions and invalid computed property syntax, and add comprehensive test cases for better clarity and coverage. --- .../diagnostics/match_error_literals.go | 30 ++++++++++++++++--- .../compiler_errors_syntax_literals_test.go | 17 +++++++++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/pkg/compiler/internal/diagnostics/match_error_literals.go b/pkg/compiler/internal/diagnostics/match_error_literals.go index b6726173..34a48118 100644 --- a/pkg/compiler/internal/diagnostics/match_error_literals.go +++ b/pkg/compiler/internal/diagnostics/match_error_literals.go @@ -68,10 +68,18 @@ func matchLiteralErrors(src *file.Source, err *CompilationError, offending *Toke span.End++ } - err.Message = "Unclosed array literal" - err.Hint = "Add a closing ']' to complete the array." - err.Spans = []ErrorSpan{ - NewMainErrorSpan(span, "missing ']'"), + if !isKeyword(offending.PrevAt(2)) { + err.Message = "Unclosed computed property expression" + err.Hint = "Add a closing ']' to complete the computed property expression." + err.Spans = []ErrorSpan{ + NewMainErrorSpan(span, "missing ']'"), + } + } else { + err.Message = "Unclosed array literal" + err.Hint = "Add a closing ']' to complete the array." + err.Spans = []ErrorSpan{ + NewMainErrorSpan(span, "missing ']'"), + } } return true @@ -182,6 +190,20 @@ func matchLiteralErrors(src *file.Source, err *CompilationError, offending *Toke return true } + + if is(offending, "[") && token == "]" { + span := spanFromTokenSafe(offending.Token(), src) + span.End++ + + val := offending.Prev().String() + err.Message = "Expected expression inside computed property brackets" + err.Hint = fmt.Sprintf("Provide a property key or index inside '[ ]', e.g. %s[0] or %s[\"key\"].", val, val) + err.Spans = []ErrorSpan{ + NewMainErrorSpan(span, "missing expression"), + } + + return true + } } return false diff --git a/test/integration/compiler/compiler_errors_syntax_literals_test.go b/test/integration/compiler/compiler_errors_syntax_literals_test.go index ba0a372f..3841cfef 100644 --- a/test/integration/compiler/compiler_errors_syntax_literals_test.go +++ b/test/integration/compiler/compiler_errors_syntax_literals_test.go @@ -306,8 +306,19 @@ func TestLiteralsSyntaxErrors(t *testing.T) { RETURN v `, E{ Kind: compiler.SyntaxError, - Message: "Expected end value before '..' in range expression", - Hint: "Object properties must have a name before the colon, e.g. { property: 123 }.", - }, "Incomplete array access"), + Message: "Unclosed computed property expression", + Hint: "Add a closing ']' to complete the computed property expression.", + }, "Unclosed computed property expression"), + + ErrorCase( + ` + LET arr = [1, 2, 3] + LET v = arr[] + RETURN v + `, E{ + Kind: compiler.SyntaxError, + Message: "Expected expression inside computed property brackets", + Hint: "Provide a property key or index inside '[ ]', e.g. arr[0] or arr[\"key\"].", + }, "Invalid computed property expression"), }) }