Repository: goccy/go-yaml
Pull request #504 (2024-11-03 12:25:01)

Fix invalid map token
fix https://github.com/goccy/go-yaml/issues/407

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/504?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 76.87%. Comparing base [(`ab16112`)](https://app.codecov.io/gh/goccy/go-yaml/commit/ab16112f69bbc7d4f4bb70163f01dd7bb4ac2010?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`28e35e5`)](https://app.codecov.io/gh/goccy/go-yaml/commit/28e35e524b7174fb62632dd6ba83b3fa9b4ba1ce?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     #504      +/-   ##
==========================================
+ Coverage   76.85%   76.87%   +0.01%     
==========================================
  Files          18       18              
  Lines        6136     6141       +5     
==========================================
+ Hits         4716     4721       +5     
  Misses       1112     1112              
  Partials      308      308              
```

</details>

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 7a765c8..5d7bfb3 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -447,6 +447,11 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 		ntk = p.nextNotCommentToken()
 		antk = p.afterNextNotCommentToken()
 	}
+	if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > node.Start.Position.Line && tk.Position.Column > node.Start.Position.Column {
+		// a: b
+		//   c <= this token is invalid.
+		return nil, errors.ErrSyntax("value is not allowed in this context", tk)
+	}
 	if len(node.Values) == 1 {
 		mapKeyCol := mvnode.Key.GetToken().Position.Column
 		commentTk := p.nextToken()


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 6b8b3ae..7adfe77 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -998,6 +998,44 @@ b
 		},
 		{
 			`
+a: 'b'
+  c: d
+`,
+			`
+[3:3] value is not allowed in this context
+   2 | a: 'b'
+>  3 |   c: d
+         ^
+`,
+		},
+		{
+			`
+a: 'b'
+  - c
+`,
+			`
+[3:3] value is not allowed in this context
+   2 | a: 'b'
+>  3 |   - c
+         ^
+`,
+		},
+		{
+			`
+a: 'b'
+  # comment
+  - c
+`,
+			`
+[4:3] value is not allowed in this context
+   2 | a: 'b'
+   3 |   # comment
+>  4 |   - c
+         ^
+`,
+		},
+		{
+			`
 a: 1
 b
 - c