Repository: goccy/go-yaml
Pull request #493 (2024-10-31 07:00:22)

Fix invalid sequence at map context
fix https://github.com/goccy/go-yaml/issues/472

This example should be treated as error case

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/493?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
Attention: Patch coverage is `88.88889%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 77.73%. Comparing base [(`bbcd692`)](https://app.codecov.io/gh/goccy/go-yaml/commit/bbcd692a279a590c4f4804412603f0b47fc101ca?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`e0beda4`)](https://app.codecov.io/gh/goccy/go-yaml/commit/e0beda43fd84d4d78ddf6489d9dcd358a1b2aad2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #493      +/-   ##
==========================================
+ Coverage   77.66%   77.73%   +0.06%     
==========================================
  Files          18       18              
  Lines        5897     5901       +4     
==========================================
+ Hits         4580     4587       +7     
+ Misses       1041     1039       -2     
+ Partials      276      275       -1     
```

</details>

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 54f2946..b59f2e1 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -231,9 +231,13 @@ func (p *parser) createMapValueNode(ctx *context, key ast.MapKeyNode, colonToken
 }
 
 func (p *parser) validateMapValue(ctx *context, key, value ast.Node) error {
-	keyColumn := key.GetToken().Position.Column
-	valueColumn := value.GetToken().Position.Column
-	if keyColumn != valueColumn {
+	keyTk := key.GetToken()
+	valueTk := value.GetToken()
+
+	if keyTk.Position.Line == valueTk.Position.Line && valueTk.Type == token.SequenceEntryType {
+		return errors.ErrSyntax("block sequence entries are not allowed in this context", valueTk)
+	}
+	if keyTk.Position.Column != valueTk.Position.Column {
 		return nil
 	}
 	if value.Type() != ast.StringType {
@@ -241,7 +245,7 @@ func (p *parser) validateMapValue(ctx *context, key, value ast.Node) error {
 	}
 	ntk := ctx.nextToken()
 	if ntk == nil || (ntk.Type != token.MappingValueType && ntk.Type != token.SequenceEntryType) {
-		return errors.ErrSyntax("could not find expected ':' token", value.GetToken())
+		return errors.ErrSyntax("could not find expected ':' token", valueTk)
 	}
 	return nil
 }
@@ -352,7 +356,7 @@ func (p *parser) parseSequenceEntry(ctx *context) (*ast.SequenceNode, error) {
 		ctx.progress(1) // skip sequence token
 		tk = ctx.currentToken()
 		if tk == nil {
-			return nil, errors.ErrSyntax("empty sequence entry", ctx.previousToken())
+			return nil, errors.ErrSyntax("empty sequence value", ctx.previousToken())
 		}
 		var comment *ast.CommentGroupNode
 		if tk.Type == token.CommentType {


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index c6e3318..342e352 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -706,7 +706,7 @@ a:
 - b: c
 - `,
 			`
-[4:1] empty sequence entry
+[4:1] empty sequence value
    2 | a:
    3 | - b: c
 >  4 | -
@@ -774,6 +774,29 @@ b
                 ^
 `,
 		},
+		{
+			`
+a: -
+b: -
+`,
+			`
+[3:4] empty sequence value
+   2 | a: -
+>  3 | b: -
+          ^
+`,
+		},
+		{
+			`
+a: - 1
+b: - 2
+`,
+			`
+[2:4] block sequence entries are not allowed in this context
+>  2 | a: - 1
+          ^
+   3 | b: - 2`,
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.source, func(t *testing.T) {