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:
@@ -68,11 +68,19 @@ func matchLiteralErrors(src *file.Source, err *CompilationError, offending *Toke
|
||||
span.End++
|
||||
}
|
||||
|
||||
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
|
||||
|
@@ -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"),
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user