1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-08-13 19:52:52 +02:00

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.

This commit is contained in:
Tim Voronov
2025-08-13 11:29:08 -04:00
parent b811f17f6a
commit 2e0a8b6d7c
2 changed files with 40 additions and 7 deletions

View File

@@ -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

View File

@@ -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"),
})
}