From 7e833a2d40134729e82c98cd5862ded2bb91e1ad Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 11 Nov 2024 21:37:43 +0900 Subject: [PATCH] fix parsing of invalid map key (#520) --- parser/parser.go | 2 ++ parser/parser_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index 36840565..846469b7 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -899,6 +899,8 @@ func (p *parser) createNodeFromToken(ctx *context, tk *token.Token) (ast.Node, e return p.parseTag(ctx) case token.LiteralType, token.FoldedType: return p.parseLiteral(ctx) + case token.MappingValueType: + return nil, errors.ErrSyntax("found an invalid key for this map", tk) } return nil, nil } diff --git a/parser/parser_test.go b/parser/parser_test.go index 34369c1b..78baba9a 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -1224,6 +1224,22 @@ a: "key: [`val]", "\n[1:7] found invalid token\n> 1 | key: [`val]\n ^\n", }, + { + `{a: b}: v`, + ` +[1:7] found an invalid key for this map +> 1 | {a: b}: v + ^ +`, + }, + { + `[a]: v`, + ` +[1:4] found an invalid key for this map +> 1 | [a]: v + ^ +`, + }, } for _, test := range tests { t.Run(test.source, func(t *testing.T) {