Skip to content

Commit 4a4a3bf

Browse files
committed
fix(ast): quote string literals in diagnostics
Preserve source delimiters when rendering string-literal AST nodes so literal text cannot be confused with missing-expression markers.
1 parent 21c5841 commit 4a4a3bf

3 files changed

Lines changed: 3 additions & 4 deletions

File tree

ast/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ type StringLiteral struct {
278278

279279
func (sl *StringLiteral) expressionNode() {}
280280
func (sl *StringLiteral) Tok() token.Token { return sl.Token }
281-
func (sl *StringLiteral) String() string { return sl.Token.Literal }
281+
func (sl *StringLiteral) String() string { return `"` + sl.Token.Literal + `"` }
282282

283283
// RangeLiteral represents start:stop[:step]
284284
type RangeLiteral struct {

parser/codeparser_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,7 @@ func TestParseStructDefinition(t *testing.T) {
401401
_, constExists := code.ConstNames["p"]
402402
require.True(t, constExists)
403403

404-
require.Equal(t, `p = Person
405-
:name age height
406-
Tejas 35 184.5`, stmt.String())
404+
require.Equal(t, "p = Person\n :name age height\n \"Tejas\" 35 184.5", stmt.String())
407405
}
408406

409407
func TestStructDefErrors(t *testing.T) {

parser/scriptparser_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func TestStringLiteral(t *testing.T) {
197197
lit, ok := printStmt.Expression.Arguments[0].(*ast.StringLiteral)
198198
require.Truef(t, ok, "expected *ast.StringLiteral, got %T", printStmt.Expression.Arguments[0])
199199
require.Equal(t, "hello", lit.Token.Literal)
200+
require.Equal(t, input, lit.String())
200201
}
201202

202203
func TestParsingPrefixExpressions(t *testing.T) {

0 commit comments

Comments
 (0)