Repository: goccy/go-yaml
Pull request #506 (2024-11-05 06:04:44)

Support suffix keyword for document header option
fix https://github.com/goccy/go-yaml/issues/399

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/506?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.82%. Comparing base [(`97070fb`)](https://app.codecov.io/gh/goccy/go-yaml/commit/97070fb2f1d0e7fdf16d4f787849bd64e9cc5f29?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`9bc7616`)](https://app.codecov.io/gh/goccy/go-yaml/commit/9bc76166e70d766cfc26e023194ab6961685666d?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     #506      +/-   ##
==========================================
+ Coverage   76.81%   76.82%   +0.01%     
==========================================
  Files          18       18              
  Lines        6193     6197       +4     
==========================================
+ Hits         4757     4761       +4     
  Misses       1123     1123              
  Partials      313      313              
```

</details>

Patch:
diff --git a/scanner/context.go b/scanner/context.go
index 24b96c4..415cf07 100644
--- a/scanner/context.go
+++ b/scanner/context.go
@@ -104,9 +104,12 @@ func (c *Context) updateDocumentIndentColumn() {
 }
 
 func (c *Context) docFirstLineIndentColumnByDocOpt() int {
-	trimmed := strings.TrimPrefix(c.docOpt, "-")
-	trimmed = strings.TrimPrefix(trimmed, "+")
-	i, _ := strconv.ParseInt(trimmed, 10, 64)
+	opt := c.docOpt
+	opt = strings.TrimPrefix(opt, "-")
+	opt = strings.TrimPrefix(opt, "+")
+	opt = strings.TrimSuffix(opt, "-")
+	opt = strings.TrimSuffix(opt, "+")
+	i, _ := strconv.ParseInt(opt, 10, 64)
 	return int(i)
 }
 
@@ -270,7 +273,7 @@ func (c *Context) existsBuffer() bool {
 
 func (c *Context) bufferedSrc() []rune {
 	src := c.buf[:c.notSpaceCharPos]
-	if c.isDocument() && strings.HasPrefix(c.docOpt, "-") {
+	if c.isDocument() && (strings.HasPrefix(c.docOpt, "-") || strings.HasSuffix(c.docOpt, "-")) {
 		// remove end '\n' character and trailing empty lines
 		// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
 		for {
diff --git a/scanner/scanner.go b/scanner/scanner.go
index 371bd8a..e89290a 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -833,9 +833,10 @@ func (s *Scanner) validateDocumentHeaderOption(opt string) error {
 	if len(opt) == 0 {
 		return nil
 	}
-	if opt[0] == '+' || opt[0] == '-' {
-		opt = opt[1:]
-	}
+	opt = strings.TrimPrefix(opt, "-")
+	opt = strings.TrimPrefix(opt, "+")
+	opt = strings.TrimSuffix(opt, "-")
+	opt = strings.TrimSuffix(opt, "+")
 	if len(opt) == 0 {
 		return nil
 	}


Test patch:
diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go
index d4eec75..81b8be9 100644
--- a/lexer/lexer_test.go
+++ b/lexer/lexer_test.go
@@ -2149,6 +2149,29 @@ s: >-3
 				},
 			},
 		},
+		{
+			YAML: `
+|2-
+
+                  text
+`,
+			Tokens: token.Tokens{
+				{
+					Type:          token.LiteralType,
+					CharacterType: token.CharacterTypeIndicator,
+					Indicator:     token.BlockScalarIndicator,
+					Value:         "|2-",
+					Origin:        "\n|2-\n",
+				},
+				{
+					Type:          token.StringType,
+					CharacterType: token.CharacterTypeMiscellaneous,
+					Indicator:     token.NotIndicator,
+					Value:         "\n                text",
+					Origin:        "\n                  text\n",
+				},
+			},
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.YAML, func(t *testing.T) {