• Daniel Martí's avatar
    text/template/parse: error on bad range variables · 1399b52d
    Daniel Martí authored
    The package used to accept invalid range pipelines, such as:
    
    	{{range $k, .}}
    	{{range $k, 123 := .}}
    
    This is because the logic that allowed a range pipeline to declare
    multiple variables was broken. When encountering a single comma inside a
    range pipeline, it would happily continue parsing a second variable,
    even if we didn't have a variable token at all.
    
    Then, the loop would immediately break, and we'd parse the pipeline we'd
    be ranging over. That is, we'd parse {{range $k, .}} as if it were
    {{range $k = .}}.
    
    To fix this, only allow the loop to continue if we know we're going to
    parse another variable or a token that would end the pipeline. Also add
    a few test cases for these error edge cases.
    
    While at it, make use of T.Run, which was useful in debugging
    Tree.pipeline via print statements.
    
    Fixes #28437.
    
    Change-Id: Idc9966bf643f0f3bc1b052620357e5b0aa2022ea
    Reviewed-on: https://go-review.googlesource.com/c/145282
    Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
    Reviewed-by: 's avatarRob Pike <r@golang.org>
    1399b52d
parse_test.go 18 KB