diff options
author | Rob Pike <r@golang.org> | 2022-03-15 10:21:08 +1100 |
---|---|---|
committer | Alex Rakoczy <alex@golang.org> | 2022-05-26 17:27:08 +0000 |
commit | a08baaad1a7c8e88b5a521ae12666b0aa3dc252a (patch) | |
tree | 720cff2776f5dcf3b42c9b97d410cc800552faa6 | |
parent | e1b14f53c13c7ab809ea1a33d2ec7f787efdafa8 (diff) | |
download | go-git-a08baaad1a7c8e88b5a521ae12666b0aa3dc252a.tar.gz |
[release-branch.go1.18] text/template/parse: allow space after continue or break
Trivial fix: We must skip space after either of these keywords
before we expect a closing delimiter.
Also delete the stutter-generating extra 'in' in the error message.
(See what I did there?)
For #51670
Fixes #52878
Change-Id: If5415632c36eaac6699bdc0aa6ce18be956c9b53
Reviewed-on: https://go-review.googlesource.com/c/go/+/392615
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Daniel Martà <mvdan@mvdan.cc>
Trust: Daniel Martà <mvdan@mvdan.cc>
TryBot-Result: Gopher Robot <gobot@golang.org>
(cherry picked from commit 41a82aa9c36bffab2593d50aa55a462fef4e5bd4)
Reviewed-on: https://go-review.googlesource.com/c/go/+/406074
Reviewed-by: Alex Rakoczy <alex@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
-rw-r--r-- | src/text/template/parse/parse.go | 8 | ||||
-rw-r--r-- | src/text/template/parse/parse_test.go | 4 |
2 files changed, 8 insertions, 4 deletions
diff --git a/src/text/template/parse/parse.go b/src/text/template/parse/parse.go index b0cbe9dfc8..ce548b0886 100644 --- a/src/text/template/parse/parse.go +++ b/src/text/template/parse/parse.go @@ -415,8 +415,8 @@ func (t *Tree) action() (n Node) { // {{break}} // Break keyword is past. func (t *Tree) breakControl(pos Pos, line int) Node { - if token := t.next(); token.typ != itemRightDelim { - t.unexpected(token, "in {{break}}") + if token := t.nextNonSpace(); token.typ != itemRightDelim { + t.unexpected(token, "{{break}}") } if t.rangeDepth == 0 { t.errorf("{{break}} outside {{range}}") @@ -428,8 +428,8 @@ func (t *Tree) breakControl(pos Pos, line int) Node { // {{continue}} // Continue keyword is past. func (t *Tree) continueControl(pos Pos, line int) Node { - if token := t.next(); token.typ != itemRightDelim { - t.unexpected(token, "in {{continue}}") + if token := t.nextNonSpace(); token.typ != itemRightDelim { + t.unexpected(token, "{{continue}}") } if t.rangeDepth == 0 { t.errorf("{{continue}} outside {{range}}") diff --git a/src/text/template/parse/parse_test.go b/src/text/template/parse/parse_test.go index 0c4778c7b3..fdb25d78f5 100644 --- a/src/text/template/parse/parse_test.go +++ b/src/text/template/parse/parse_test.go @@ -260,6 +260,10 @@ var parseTests = []parseTest{ {"newline in pipeline", "{{\n\"x\"\n|\nprintf\n}}", noError, `{{"x" | printf}}`}, {"newline in comment", "{{/*\nhello\n*/}}", noError, ""}, {"newline in comment", "{{-\n/*\nhello\n*/\n-}}", noError, ""}, + {"spaces around continue", "{{range .SI}}{{.}}{{ continue }}{{end}}", noError, + `{{range .SI}}{{.}}{{continue}}{{end}}`}, + {"spaces around break", "{{range .SI}}{{.}}{{ break }}{{end}}", noError, + `{{range .SI}}{{.}}{{break}}{{end}}`}, // Errors. {"unclosed action", "hello{{range", hasError, ""}, |