diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-12-02 15:23:25 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-12-03 15:12:58 +0300 |
commit | d90e37e0c6e812f9913bf256c9c81aa05b7a08aa (patch) | |
tree | 7b1b14464eefec1188ca2eed53c64e4823453cc9 /tpl | |
parent | 32471b57bde51c55a15dbf1db75d6e5f7232c347 (diff) |
all: Format code with gofumpt
See https://github.com/mvdan/gofumpt
Diffstat (limited to 'tpl')
92 files changed, 393 insertions, 347 deletions
diff --git a/tpl/cast/cast_test.go b/tpl/cast/cast_test.go index d3f8d9733..c6219728b 100644 --- a/tpl/cast/cast_test.go +++ b/tpl/cast/cast_test.go @@ -15,7 +15,6 @@ package cast import ( "html/template" - "testing" qt "github.com/frankban/quicktest" diff --git a/tpl/cast/docshelper.go b/tpl/cast/docshelper.go index a497f6e8a..035db1740 100644 --- a/tpl/cast/docshelper.go +++ b/tpl/cast/docshelper.go @@ -41,7 +41,6 @@ func init() { } return docshelper.DocProvider{"tpl": map[string]interface{}{"funcs": namespaces}} - } docshelper.AddDocProviderFunc(docsProvider) diff --git a/tpl/cast/init.go b/tpl/cast/init.go index 3aee6f036..3f1f3f253 100644 --- a/tpl/cast/init.go +++ b/tpl/cast/init.go @@ -51,7 +51,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/cast/init_test.go b/tpl/cast/init_test.go index 73d9d5adc..dd024b775 100644 --- a/tpl/cast/init_test.go +++ b/tpl/cast/init_test.go @@ -38,5 +38,4 @@ func TestInit(t *testing.T) { c.Assert(found, qt.Equals, true) c.Assert(ns.Context(), hqt.IsSameType, &Namespace{}) - } diff --git a/tpl/collections/append.go b/tpl/collections/append.go index 297328dc8..50fafe3d9 100644 --- a/tpl/collections/append.go +++ b/tpl/collections/append.go @@ -34,5 +34,4 @@ func (ns *Namespace) Append(args ...interface{}) (interface{}, error) { from := args[:len(args)-1] return collections.Append(to, from...) - } diff --git a/tpl/collections/append_test.go b/tpl/collections/append_test.go index a254601b4..9dcc7d2b9 100644 --- a/tpl/collections/append_test.go +++ b/tpl/collections/append_test.go @@ -40,9 +40,11 @@ func TestAppend(t *testing.T) { {"", []interface{}{[]string{"a", "b"}}, false}, {[]string{"a", "b"}, []interface{}{}, false}, // No string concatenation. - {"ab", + { + "ab", []interface{}{"c"}, - false}, + false, + }, } { errMsg := qt.Commentf("[%d]", i) @@ -62,5 +64,4 @@ func TestAppend(t *testing.T) { t.Fatalf("%s got\n%T: %v\nexpected\n%T: %v", errMsg, result, result, test.expected, test.expected) } } - } diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go index 55d29d3a9..86554def1 100644 --- a/tpl/collections/apply.go +++ b/tpl/collections/apply.go @@ -52,7 +52,6 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) ( vv := seqv.Index(i) vvv, err := applyFnToThis(fnv, vv, args...) - if err != nil { return nil, err } diff --git a/tpl/collections/apply_test.go b/tpl/collections/apply_test.go index f9199b6b6..98cb78b51 100644 --- a/tpl/collections/apply_test.go +++ b/tpl/collections/apply_test.go @@ -14,12 +14,11 @@ package collections import ( + "fmt" "io" "reflect" "testing" - "fmt" - qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/output" @@ -58,7 +57,6 @@ func (templateFinder) GetFunc(name string) (reflect.Value, bool) { } return reflect.ValueOf(fmt.Sprint), true - } func TestApply(t *testing.T) { @@ -88,5 +86,4 @@ func TestApply(t *testing.T) { if err == nil { t.Errorf("apply with unknown func should fail") } - } diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go index d90467022..3521f9cd5 100644 --- a/tpl/collections/collections.go +++ b/tpl/collections/collections.go @@ -18,7 +18,6 @@ package collections import ( "fmt" "html/template" - "math/rand" "net/url" "reflect" @@ -478,9 +477,9 @@ func (ns *Namespace) Seq(args ...interface{}) ([]int, error) { return nil, errors.New("invalid arguments to Seq") } - var inc = 1 + inc := 1 var last int - var first = intArgs[0] + first := intArgs[0] if len(intArgs) == 1 { last = first @@ -580,7 +579,6 @@ type intersector struct { } func (i *intersector) appendIfNotSeen(v reflect.Value) { - vi := v.Interface() if !i.seen[vi] { i.r = reflect.Append(i.r, v) @@ -729,7 +727,6 @@ func (ns *Namespace) Uniq(seq interface{}) (interface{}, error) { } return slice.Interface(), nil - } // KeyVals creates a key and values wrapper. diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index 21c8bfb56..a75ec9d16 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -17,7 +17,6 @@ import ( "errors" "fmt" "html/template" - "math/rand" "reflect" "testing" @@ -195,8 +194,10 @@ func TestDictionary(t *testing.T) { }{ {[]interface{}{"a", "b"}, map[string]interface{}{"a": "b"}}, {[]interface{}{[]string{"a", "b"}, "c"}, map[string]interface{}{"a": map[string]interface{}{"b": "c"}}}, - {[]interface{}{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"}, - map[string]interface{}{"a": map[string]interface{}{"b": "c", "b2": "c2"}, "b": "c"}}, + { + []interface{}{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"}, + map[string]interface{}{"a": map[string]interface{}{"b": "c", "b2": "c2"}, "b": "c"}, + }, {[]interface{}{"a", 12, "b", []int{4}}, map[string]interface{}{"a": 12, "b": []int{4}}}, // errors {[]interface{}{5, "b"}, false}, @@ -237,7 +238,6 @@ func TestReverse(t *testing.T) { c.Assert(reversed, qt.IsNil) _, err = ns.Reverse(43) c.Assert(err, qt.Not(qt.IsNil)) - } func TestEchoParam(t *testing.T) { @@ -369,8 +369,10 @@ func (p testPage) String() string { return "p-" + p.Title } -type pagesPtr []*testPage -type pagesVals []testPage +type ( + pagesPtr []*testPage + pagesVals []testPage +) var ( p1 = &testPage{"A"} @@ -716,7 +718,6 @@ func TestSlice(t *testing.T) { c.Assert(result, qt.DeepEquals, test.expected, errMsg) } - } func TestUnion(t *testing.T) { @@ -907,7 +908,6 @@ type TstParams struct { func (x TstParams) Params() maps.Params { return x.params - } type TstXIHolder struct { diff --git a/tpl/collections/complement_test.go b/tpl/collections/complement_test.go index d0e27353c..7e8d695b4 100644 --- a/tpl/collections/complement_test.go +++ b/tpl/collections/complement_test.go @@ -66,7 +66,8 @@ func TestComplement(t *testing.T) { {"error", []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, false}, {[]string{"a", "b", "c"}, []interface{}{[][]string{{"c", "d"}}}, false}, { - []interface{}{[][]string{{"c", "d"}}}, []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, + []interface{}{[][]string{{"c", "d"}}}, + []interface{}{[]string{"c", "d"}, []string{"a", "b"}}, []interface{}{[][]string{{"c", "d"}}}, }, } { @@ -93,5 +94,4 @@ func TestComplement(t *testing.T) { c.Assert(err, qt.Not(qt.IsNil)) _, err = ns.Complement([]string{"a", "b"}) c.Assert(err, qt.Not(qt.IsNil)) - } diff --git a/tpl/collections/index_test.go b/tpl/collections/index_test.go index 0c380d8d5..c690fe648 100644 --- a/tpl/collections/index_test.go +++ b/tpl/collections/index_test.go @@ -52,7 +52,6 @@ func TestIndex(t *testing.T) { {[]int{0, 1}, []interface{}{nil}, nil, true}, {tstNoStringer{}, []interface{}{0}, nil, true}, } { - c.Run(fmt.Sprint(i), func(c *qt.C) { errMsg := qt.Commentf("[%d] %v", i, test) diff --git a/tpl/collections/merge_test.go b/tpl/collections/merge_test.go index 92c973cd4..bde4d90b1 100644 --- a/tpl/collections/merge_test.go +++ b/tpl/collections/merge_test.go @@ -43,7 +43,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 42, "c": 3}, map[string]interface{}{"a": 1, "b": 2}, }, - map[string]interface{}{"a": 1, "b": 2, "c": 3}, false, + map[string]interface{}{"a": 1, "b": 2, "c": 3}, + false, }, { "multi", @@ -52,7 +53,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 1, "b": 2}, map[string]interface{}{"a": 9, "c": 4, "d": 7}, }, - map[string]interface{}{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, false, + map[string]interface{}{"a": 9, "b": 2, "c": 4, "d": 7, "e": 11}, + false, }, { "basic case insensitive", @@ -60,7 +62,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"A": 42, "c": 3}, map[string]interface{}{"a": 1, "b": 2}, }, - map[string]interface{}{"a": 1, "b": 2, "c": 3}, false, + map[string]interface{}{"a": 1, "b": 2, "c": 3}, + false, }, { "nested", @@ -68,7 +71,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2}}, }, - map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3}, false, + map[string]interface{}{"a": 1, "b": map[string]interface{}{"d": 1, "e": 2, "f": 3}, "c": 3}, + false, }, { // https://github.com/gohugoio/hugo/issues/6633 @@ -77,7 +81,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 42, "c": 3}, maps.Params{"a": 1, "b": 2}, }, - maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false, + maps.Params{"a": int(1), "b": int(2), "c": int(3)}, + false, }, { "params dst, upper case src", @@ -85,7 +90,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 42, "C": 3}, maps.Params{"a": 1, "b": 2}, }, - maps.Params{"a": int(1), "b": int(2), "c": int(3)}, false, + maps.Params{"a": int(1), "b": int(2), "c": int(3)}, + false, }, { "params src", @@ -93,7 +99,8 @@ func TestMerge(t *testing.T) { maps.Params{"a": 42, "c": 3}, map[string]interface{}{"a": 1, "c": 2}, }, - map[string]interface{}{"a": int(1), "c": int(2)}, false, + map[string]interface{}{"a": int(1), "c": int(2)}, + false, }, { "params src, upper case dst", @@ -101,7 +108,8 @@ func TestMerge(t *testing.T) { maps.Params{"a": 42, "c": 3}, map[string]interface{}{"a": 1, "C": 2}, }, - map[string]interface{}{"a": int(1), "C": int(2)}, false, + map[string]interface{}{"a": int(1), "C": int(2)}, + false, }, { "nested, params dst", @@ -109,7 +117,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"a": 42, "c": 3, "b": map[string]interface{}{"d": 55, "e": 66, "f": 3}}, maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2}}, }, - maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, false, + maps.Params{"a": 1, "b": maps.Params{"d": 1, "e": 2, "f": 3}, "c": 3}, + false, }, { // https://github.com/gohugoio/hugo/issues/7899 @@ -118,7 +127,8 @@ func TestMerge(t *testing.T) { map[string]interface{}{"k": "v"}, map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, }, - map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, false, + map[string]interface{}{"k": map[string]interface{}{"k2": "v2"}}, + false, }, {"src nil", []interface{}{nil, simpleMap}, simpleMap, false}, // Error cases. diff --git a/tpl/collections/reflect_helpers.go b/tpl/collections/reflect_helpers.go index 3d73b70e1..453d2f356 100644 --- a/tpl/collections/reflect_helpers.go +++ b/tpl/collections/reflect_helpers.go @@ -165,7 +165,6 @@ func convertNumber(v reflect.Value, to reflect.Kind) (reflect.Value, error) { } return n, nil - } func newSliceElement(items interface{}) interface{} { diff --git a/tpl/collections/sort_test.go b/tpl/collections/sort_test.go index 75e23fc7b..4798b5090 100644 --- a/tpl/collections/sort_test.go +++ b/tpl/collections/sort_test.go @@ -238,7 +238,6 @@ func TestSort(t *testing.T) { }, {nil, nil, "asc", false}, } { - t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { var result interface{} var err error @@ -261,6 +260,5 @@ func TestSort(t *testing.T) { } } }) - } } diff --git a/tpl/collections/symdiff.go b/tpl/collections/symdiff.go index 85a2076aa..e61814e26 100644 --- a/tpl/collections/symdiff.go +++ b/tpl/collections/symdiff.go @@ -65,5 +65,4 @@ func (ns *Namespace) SymDiff(s2, s1 interface{}) (interface{}, error) { } return slice.Interface(), nil - } diff --git a/tpl/collections/symdiff_test.go b/tpl/collections/symdiff_test.go index ac40fda55..120dfaac6 100644 --- a/tpl/collections/symdiff_test.go +++ b/tpl/collections/symdiff_test.go @@ -75,5 +75,4 @@ func TestSymDiff(t *testing.T) { c.Assert(err, qt.Not(qt.IsNil)) _, err = ns.Complement([]string{"a", "b"}) c.Assert(err, qt.Not(qt.IsNil)) - } diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index d6a1dd141..75ee109f9 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -62,7 +62,6 @@ func TestWhere(t *testing.T) { } return testVariants - } for i, test := range []testt{ @@ -171,17 +170,17 @@ func TestWhere(t *testing.T) { }, key: "B", match: "b2", expect: []maps.Params{ - maps.Params{"a": "a2", "b": "b2"}, + {"a": "a2", "b": "b2"}, }, }, { seq: []maps.Params{ - maps.Params{ + { "a": map[string]interface{}{ "b": "b1", }, }, - maps.Params{ + { "a": map[string]interface{}{ "b": "b2", }, @@ -189,7 +188,7 @@ func TestWhere(t *testing.T) { }, key: "A.B", match: "b2", expect: []maps.Params{ - maps.Params{ + { "a": map[string]interface{}{ "b": "b2", }, diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go index c21ca11bc..76fe2698a 100644 --- a/tpl/compare/compare_test.go +++ b/tpl/compare/compare_test.go @@ -44,8 +44,10 @@ var testT = &T{ NonEmptyInterfaceTypedNil: (*T)(nil), } -type tstEqerType1 string -type tstEqerType2 string +type ( + tstEqerType1 string + tstEqerType2 string +) func (t tstEqerType2) Eq(other interface{}) bool { return cast.ToString(t) == cast.ToString(other) @@ -397,7 +399,6 @@ func TestCase(t *testing.T) { c.Assert(n.Eq("az", "az"), qt.Equals, true) c.Assert(n.Eq("az", stringType("az")), qt.Equals, true) - } func TestStringType(t *testing.T) { diff --git a/tpl/compare/init.go b/tpl/compare/init.go index 3b9dc6856..f96e382e5 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -79,7 +79,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/crypto/init.go b/tpl/crypto/init.go index 9a958bd38..2c6e94298 100644 --- a/tpl/crypto/init.go +++ b/tpl/crypto/init.go @@ -59,7 +59,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/data/data.go b/tpl/data/data.go index 964844336..15d042e0a 100644 --- a/tpl/data/data.go +++ b/tpl/data/data.go @@ -35,7 +35,6 @@ import ( // New returns a new instance of the data-namespaced template functions. func New(deps *deps.Deps) *Namespace { - return &Namespace{ deps: deps, cacheGetCSV: deps.FileCaches.GetCSVCache(), diff --git a/tpl/data/data_test.go b/tpl/data/data_test.go index e1839dd48..f9e8621f2 100644 --- a/tpl/data/data_test.go +++ b/tpl/data/data_test.go @@ -109,7 +109,7 @@ func TestGetCSV(t *testing.T) { if _, ok := test.expect.(bool); ok { c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 1) - //c.Assert(err, msg, qt.Not(qt.IsNil)) + // c.Assert(err, msg, qt.Not(qt.IsNil)) c.Assert(got, qt.IsNil) continue } @@ -199,7 +199,7 @@ func TestGetJSON(t *testing.T) { if _, ok := test.expect.(bool); ok { c.Assert(int(ns.deps.Log.LogCounters().ErrorCounter.Count()), qt.Equals, 1) - //c.Assert(err, msg, qt.Not(qt.IsNil)) + // c.Assert(err, msg, qt.Not(qt.IsNil)) continue } diff --git a/tpl/data/resources.go b/tpl/data/resources.go index a7b8b3f49..ba98f12b4 100644 --- a/tpl/data/resources.go +++ b/tpl/data/resources.go @@ -81,7 +81,6 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (b } return nil, err - }) if !handled { @@ -100,7 +99,6 @@ func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) { } return afero.ReadFile(fs, filename) - } // getResource loads the content of a local or remote file and returns its content and the diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go index 7350f82f1..5ad0f0970 100644 --- a/tpl/data/resources_test.go +++ b/tpl/data/resources_test.go @@ -69,7 +69,6 @@ func TestScpGetLocal(t *testing.T) { t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c)) } } - } func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) { diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go index 780add0d1..713088b57 100644 --- a/tpl/fmt/fmt.go +++ b/tpl/fmt/fmt.go @@ -50,7 +50,6 @@ func (ns *Namespace) Print(a ...interface{}) string { // Printf returns a formatted string representation of the passed arguments. func (ns *Namespace) Printf(format string, a ...interface{}) string { return _fmt.Sprintf(format, a...) - } // Println returns string representation of the passed arguments ending with a newline. diff --git a/tpl/hugo/init.go b/tpl/hugo/init.go index 1556b759c..c57d4a27a 100644 --- a/tpl/hugo/init.go +++ b/tpl/hugo/init.go @@ -23,7 +23,6 @@ const name = "hugo" func init() { f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { - h := d.Site.Hugo() ns := &internal.TemplateFuncsNamespace{ @@ -34,7 +33,6 @@ func init() { // We just add the Hugo struct as the namespace here. No method mappings. return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/images/init.go b/tpl/images/init.go index 299c76846..e9fd52e73 100644 --- a/tpl/images/init.go +++ b/tpl/images/init.go @@ -35,7 +35,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/inflect/init.go b/tpl/inflect/init.go index 3f258356b..52b234df3 100644 --- a/tpl/inflect/init.go +++ b/tpl/inflect/init.go @@ -54,7 +54,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/internal/go_templates/fmtsort/sort_test.go b/tpl/internal/go_templates/fmtsort/sort_test.go index 364c5bf6d..189c693cc 100644 --- a/tpl/internal/go_templates/fmtsort/sort_test.go +++ b/tpl/internal/go_templates/fmtsort/sort_test.go @@ -6,11 +6,12 @@ package fmtsort_test import ( "fmt" - "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort" "math" "reflect" "strings" "testing" + + "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort" ) var compareTests = [][]reflect.Value{ diff --git a/tpl/internal/go_templates/htmltemplate/content_test.go b/tpl/internal/go_templates/htmltemplate/content_test.go index b5de701d3..f2c5c5ed1 100644 --- a/tpl/internal/go_templates/htmltemplate/content_test.go +++ b/tpl/internal/go_templates/htmltemplate/content_test.go @@ -427,7 +427,7 @@ func TestStringer(t *testing.T) { if err := tmpl.Execute(b, s); err != nil { t.Fatal(err) } - var expect = "string=3" + expect := "string=3" if b.String() != expect { t.Errorf("expected %q got %q", expect, b.String()) } diff --git a/tpl/internal/go_templates/htmltemplate/css.go b/tpl/internal/go_templates/htmltemplate/css.go index eb92fc92b..b006a0718 100644 --- a/tpl/internal/go_templates/htmltemplate/css.go +++ b/tpl/internal/go_templates/htmltemplate/css.go @@ -210,8 +210,10 @@ var cssReplacementTable = []string{ '}': `\7d`, } -var expressionBytes = []byte("expression") -var mozBindingBytes = []byte("mozbinding") +var ( + expressionBytes = []byte("expression") + mozBindingBytes = []byte("mozbinding") +) // cssValueFilter allows innocuous CSS values in the output including CSS // quantities (10px or 25%), ID or class literals (#foo, .bar), keyword values diff --git a/tpl/internal/go_templates/htmltemplate/escape_test.go b/tpl/internal/go_templates/htmltemplate/escape_test.go index 075db4e13..ee5421cc8 100644 --- a/tpl/internal/go_templates/htmltemplate/escape_test.go +++ b/tpl/internal/go_templates/htmltemplate/escape_test.go @@ -891,7 +891,6 @@ func TestEscapeSet(t *testing.T) { t.Errorf("want\n\t%q\ngot\n\t%q", test.want, got) } } - } func TestErrors(t *testing.T) { @@ -1088,7 +1087,6 @@ func TestErrors(t *testing.T) { // Check that we get the same error if we call Execute again. if err := tmpl.Execute(buf, nil); err == nil || err.Error() != got { t.Errorf("input=%q: unexpected error on second call %q", test.input, err) - } } } diff --git a/tpl/internal/go_templates/htmltemplate/example_test.go b/tpl/internal/go_templates/htmltemplate/example_test.go index a93b8d2fb..ee1cbbb4a 100644 --- a/tpl/internal/go_templates/htmltemplate/example_test.go +++ b/tpl/internal/go_templates/htmltemplate/example_test.go @@ -82,7 +82,6 @@ func Example() { // <div><strong>no rows</strong></div> // </body> // </html> - } func Example_autoescaping() { @@ -123,7 +122,6 @@ func Example_escape() { // \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E // \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E - } func ExampleTemplate_Delims() { diff --git a/tpl/internal/go_templates/htmltemplate/js.go b/tpl/internal/go_templates/htmltemplate/js.go index cfd413461..f19e774c7 100644 --- a/tpl/internal/go_templates/htmltemplate/js.go +++ b/tpl/internal/go_templates/htmltemplate/js.go @@ -337,6 +337,7 @@ var jsStrNormReplacementTable = []string{ '<': `\u003c`, '>': `\u003e`, } + var jsRegexpReplacementTable = []string{ 0: `\u0000`, '\t': `\t`, diff --git a/tpl/internal/go_templates/htmltemplate/js_test.go b/tpl/internal/go_templates/htmltemplate/js_test.go index e15087f0f..f7ee3cfaa 100644 --- a/tpl/internal/go_templates/htmltemplate/js_test.go +++ b/tpl/internal/go_templates/htmltemplate/js_test.go @@ -206,7 +206,8 @@ func TestJSStrEscaper(t *testing.T) { {"<!--", `\u003c!--`}, {"-->", `--\u003e`}, // From https://code.google.com/p/doctype/wiki/ArticleUtf7 - {"+ADw-script+AD4-alert(1)+ADw-/script+AD4-", + { + "+ADw-script+AD4-alert(1)+ADw-/script+AD4-", `\u002bADw-script\u002bAD4-alert(1)\u002bADw-\/script\u002bAD4-`, }, // Invalid UTF-8 sequence diff --git a/tpl/internal/go_templates/htmltemplate/transition.go b/tpl/internal/go_templates/htmltemplate/transition.go index 06df67933..5ba7f8017 100644 --- a/tpl/internal/go_templates/htmltemplate/transition.go +++ b/tpl/internal/go_templates/htmltemplate/transition.go @@ -41,8 +41,10 @@ var transitionFunc = [...]func(context, []byte) (context, int){ stateError: tError, } -var commentStart = []byte("<!--") -var commentEnd = []byte("-->") +var ( + commentStart = []byte("<!--") + commentEnd = []byte("-->") +) // tText is the context transition function for the text state. func tText(c context, s []byte) (context, int) { diff --git a/tpl/internal/go_templates/htmltemplate/transition_test.go b/tpl/internal/go_templates/htmltemplate/transition_test.go index 00b0ff6ca..c3257a839 100644 --- a/tpl/internal/go_templates/htmltemplate/transition_test.go +++ b/tpl/internal/go_templates/htmltemplate/transition_test.go @@ -42,7 +42,6 @@ func TestFindEndTag(t *testing.T) { } func BenchmarkTemplateSpecialTags(b *testing.B) { - r := struct { Name, Gift string }{"Aunt Mildred", "bone china tea set"} diff --git a/tpl/internal/go_templates/testenv/testenv.go b/tpl/internal/go_templates/testenv/testenv.go index 90044570d..7c46089f5 100644 --- a/tpl/internal/go_templates/testenv/testenv.go +++ b/tpl/internal/go_templates/testenv/testenv.go @@ -13,7 +13,6 @@ package testenv import ( "errors" "flag" - "github.com/gohugoio/hugo/tpl/internal/go_templates/cfg" "os" "os/exec" "path/filepath" @@ -22,6 +21,8 @@ import ( "strings" "sync" "testing" + + "github.com/gohugoio/hugo/tpl/internal/go_templates/cfg" ) // Builder reports the name of the builder running this test diff --git a/tpl/internal/go_templates/testenv/testenv_windows.go b/tpl/internal/go_templates/testenv/testenv_windows.go index eb8d6ac16..f9e7da5a7 100644 --- a/tpl/internal/go_templates/testenv/testenv_windows.go +++ b/tpl/internal/go_templates/testenv/testenv_windows.go @@ -12,8 +12,10 @@ import ( "syscall" ) -var symlinkOnce sync.Once -var winSymlinkErr error +var ( + symlinkOnce sync.Once + winSymlinkErr error +) func initWinHasSymlink() { tmpdir, err := ioutil.TempDir("", "symtest") diff --git a/tpl/internal/go_templates/texttemplate/example_test.go b/tpl/internal/go_templates/texttemplate/example_test.go index f192cac4f..09555c95a 100644 --- a/tpl/internal/go_templates/texttemplate/example_test.go +++ b/tpl/internal/go_templates/texttemplate/example_test.go @@ -34,7 +34,7 @@ Josie Name, Gift string Attended bool } - var recipients = []Recipient{ + recipients := []Recipient{ {"Aunt Mildred", "bone china tea set", true}, {"Uncle John", "moleskin pants", false}, {"Cousin Rodney", "", false}, diff --git a/tpl/internal/go_templates/texttemplate/exec.go b/tpl/internal/go_templates/texttemplate/exec.go index 879cd0884..db533d735 100644 --- a/tpl/internal/go_templates/texttemplate/exec.go +++ b/tpl/internal/go_templates/texttemplate/exec.go @@ -6,12 +6,13 @@ package template import ( "fmt" - "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort" - "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" "io" "reflect" "runtime" "strings" + + "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort" + "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" ) // maxExecDepth specifies the maximum stack depth of templates within diff --git a/tpl/internal/go_templates/texttemplate/exec_test.go b/tpl/internal/go_templates/texttemplate/exec_test.go index 940a1de6a..cc79d2143 100644 --- a/tpl/internal/go_templates/texttemplate/exec_test.go +++ b/tpl/internal/go_templates/texttemplate/exec_test.go @@ -318,12 +318,16 @@ var execTests = []execTest{ {"$.U.V", "{{$.U.V}}", "v", tVal, true}, {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true}, {"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true}, - {"nested assignment", + { + "nested assignment", "{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}", - "3", tVal, true}, - {"nested assignment changes the last declaration", + "3", tVal, true, + }, + { + "nested assignment changes the last declaration", "{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}", - "1", tVal, true}, + "1", tVal, true, + }, // Type with String method. {"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true}, @@ -370,15 +374,21 @@ var execTests = []execTest{ {".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true}, {".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true}, {"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true}, - {"method on chained var", + { + "method on chained var", "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", - "true", tVal, true}, - {"chained method", + "true", tVal, true, + }, + { + "chained method", "{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", - "true", tVal, true}, - {"chained method on variable", + "true", tVal, true, + }, + { + "chained method on variable", "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}", - "true", tVal, true}, + "true", tVal, true, + }, {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true}, {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true}, {"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true}, @@ -464,10 +474,14 @@ var execTests = []execTest{ {"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true}, // HTML. - {"html", `{{html "<script>alert(\"XSS\");</script>"}}`, - "<script>alert("XSS");</script>", nil, true}, - {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`, - "<script>alert("XSS");</script>", nil, true}, + { + "html", `{{html "<script>alert(\"XSS\");</script>"}}`, + "<script>alert("XSS");</script>", nil, true, + }, + { + "html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`, + "<script>alert("XSS");</script>", nil, true, + }, {"html", `{{html .PS}}`, "a string", tVal, true}, {"html typed nil", `{{html .NIL}}`, "<nil>", tVal, true}, {"html untyped nil", `{{html .Empty0}}`, "<no value>", tVal, true}, @@ -825,7 +839,7 @@ var delimPairs = []string{ func TestDelims(t *testing.T) { const hello = "Hello, world" - var value = struct{ Str string }{hello} + value := struct{ Str string }{hello} for i := 0; i < len(delimPairs); i += 2 { text := ".Str" left := delimPairs[i+0] @@ -848,7 +862,7 @@ func TestDelims(t *testing.T) { if err != nil { t.Fatalf("delim %q text %q parse err %s", left, text, err) } - var b = new(bytes.Buffer) + b := new(bytes.Buffer) err = tmpl.Execute(b, value) if err != nil { t.Fatalf("delim %q exec err %s", left, err) @@ -949,7 +963,7 @@ const treeTemplate = ` ` func TestTree(t *testing.T) { - var tree = &Tree{ + tree := &Tree{ 1, &Tree{ 2, &Tree{ @@ -1195,7 +1209,7 @@ var cmpTests = []cmpTest{ func TestComparison(t *testing.T) { b := new(bytes.Buffer) - var cmpStruct = struct { + cmpStruct := struct { Uthree, Ufour uint NegOne, Three int Ptr, NilPtr *int diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go index 7cd6df0fb..0dc6331c7 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template.go @@ -77,7 +77,6 @@ func (t *executer) Execute(p Preparer, wr io.Writer, data interface{}) error { } return tmpl.executeWithState(state, value) - } // Prepare returns a template ready for execution. diff --git a/tpl/internal/go_templates/texttemplate/hugo_template_test.go b/tpl/internal/go_templates/texttemplate/hugo_template_test.go index 98a2575eb..90bb09315 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template_test.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template_test.go @@ -85,5 +85,4 @@ Method: {{ .Hello1 "v1" }} c.Assert(got, qt.Contains, "hello hugo") c.Assert(got, qt.Contains, "Map: av") c.Assert(got, qt.Contains, "Method: v2 v1") - } diff --git a/tpl/internal/go_templates/texttemplate/multi_test.go b/tpl/internal/go_templates/texttemplate/multi_test.go index 7323be379..569a45dbb 100644 --- a/tpl/internal/go_templates/texttemplate/multi_test.go +++ b/tpl/internal/go_templates/texttemplate/multi_test.go @@ -11,8 +11,9 @@ package template import ( "bytes" "fmt" - "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" "testing" + + "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" ) const ( @@ -29,22 +30,32 @@ type multiParseTest struct { } var multiParseTests = []multiParseTest{ - {"empty", "", noError, + { + "empty", "", noError, nil, - nil}, - {"one", `{{define "foo"}} FOO {{end}}`, noError, + nil, + }, + { + "one", `{{define "foo"}} FOO {{end}}`, noError, []string{"foo"}, - []string{" FOO "}}, - {"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError, + []string{" FOO "}, + }, + { + "two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError, []string{"foo", "bar"}, - []string{" FOO ", " BAR "}}, + []string{" FOO ", " BAR "}, + }, // errors - {"missing end", `{{define "foo"}} FOO `, hasError, + { + "missing end", `{{define "foo"}} FOO `, hasError, + nil, + nil, + }, + { + "malformed name", `{{define "foo}} FOO `, hasError, nil, - nil}, - {"malformed name", `{{define "foo}} FOO `, hasError, nil, - nil}, + }, } func TestMultiParse(t *testing.T) { @@ -400,7 +411,7 @@ func TestIssue19294(t *testing.T) { // by the contents of "stylesheet", but if the internal map associating // names with templates is built in the wrong order, the empty block // looks non-empty and this doesn't happen. - var inlined = map[string]string{ + inlined := map[string]string{ "stylesheet": `{{define "stylesheet"}}stylesheet{{end}}`, "xhtml": `{{block "stylesheet" .}}{{end}}`, } diff --git a/tpl/internal/go_templates/texttemplate/parse/node.go b/tpl/internal/go_templates/texttemplate/parse/node.go index 1c116ea6f..dee67a2ed 100644 --- a/tpl/internal/go_templates/texttemplate/parse/node.go +++ b/tpl/internal/go_templates/texttemplate/parse/node.go @@ -249,7 +249,6 @@ func (a *ActionNode) tree() *Tree { func (a *ActionNode) Copy() Node { return a.tr.newAction(a.Pos, a.Line, a.Pipe.CopyPipe()) - } // CommandNode holds a command (a pipeline inside an evaluating action). diff --git a/tpl/internal/go_templates/texttemplate/parse/parse.go b/tpl/internal/go_templates/texttemplate/parse/parse.go index c9b80f4a2..7a58c5788 100644 --- a/tpl/internal/go_templates/texttemplate/parse/parse.go +++ b/tpl/internal/go_templates/texttemplate/parse/parse.go @@ -453,7 +453,7 @@ func (t *Tree) parseControl(allowElseIf bool, context string) (pos Pos, line int var next Node list, next = t.itemList() switch next.Type() { - case nodeEnd: //done + case nodeEnd: // done case nodeElse: if allowElseIf { // Special case for "else if". If the "else" is followed immediately by an "if", diff --git a/tpl/internal/go_templates/texttemplate/parse/parse_test.go b/tpl/internal/go_templates/texttemplate/parse/parse_test.go index 79e7bb5ae..97511190d 100644 --- a/tpl/internal/go_templates/texttemplate/parse/parse_test.go +++ b/tpl/internal/go_templates/texttemplate/parse/parse_test.go @@ -178,70 +178,134 @@ const ( ) var parseTests = []parseTest{ - {"empty", "", noError, - ``}, - {"comment", "{{/*\n\n\n*/}}", noError, - ``}, - {"spaces", " \t\n", noError, - `" \t\n"`}, - {"text", "some text", noError, - `"some text"`}, - {"emptyAction", "{{}}", hasError, - `{{}}`}, - {"field", "{{.X}}", noError, - `{{.X}}`}, - {"simple command", "{{printf}}", noError, - `{{printf}}`}, - {"$ invocation", "{{$}}", noError, - "{{$}}"}, - {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError, - "{{with $x := 3}}{{$x 23}}{{end}}"}, - {"variable with fields", "{{$.I}}", noError, - "{{$.I}}"}, - {"multi-word command", "{{printf `%d` 23}}", noError, - "{{printf `%d` 23}}"}, - {"pipeline", "{{.X|.Y}}", noError, - `{{.X | .Y}}`}, - {"pipeline with decl", "{{$x := .X|.Y}}", noError, - `{{$x := .X | .Y}}`}, - {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError, - `{{.X (.Y .Z) (.A | .B .C) (.E)}}`}, - {"field applied to parentheses", "{{(.Y .Z).Field}}", noError, - `{{(.Y .Z).Field}}`}, - {"simple if", "{{if .X}}hello{{end}}", noError, - `{{if .X}}"hello"{{end}}`}, - {"if with else", "{{if .X}}true{{else}}false{{end}}", noError, - `{{if .X}}"true"{{else}}"false"{{end}}`}, - {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError, - `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`}, - {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError, - `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`}, - {"simple range", "{{range .X}}hello{{end}}", noError, - `{{range .X}}"hello"{{end}}`}, - {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError, - `{{range .X.Y.Z}}"hello"{{end}}`}, - {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError, - `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`}, - {"range with else", "{{range .X}}true{{else}}false{{end}}", noError, - `{{range .X}}"true"{{else}}"false"{{end}}`}, - {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError, - `{{range .X | .M}}"true"{{else}}"false"{{end}}`}, - {"range []int", "{{range .SI}}{{.}}{{end}}", noError, - `{{range .SI}}{{.}}{{end}}`}, - {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError, - `{{range $x := .SI}}{{.}}{{end}}`}, - {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError, - `{{range $x, $y := .SI}}{{.}}{{end}}`}, - {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError, - `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`}, - {"template", "{{template `x`}}", noError, - `{{template "x"}}`}, - {"template with arg", "{{template `x` .Y}}", noError, - `{{template "x" .Y}}`}, - {"with", "{{with .X}}hello{{end}}", noError, - `{{with .X}}"hello"{{end}}`}, - {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError, - `{{with .X}}"hello"{{else}}"goodbye"{{end}}`}, + { + "empty", "", noError, + ``, + }, + { + "comment", "{{/*\n\n\n*/}}", noError, + ``, + }, + { + "spaces", " \t\n", noError, + `" \t\n"`, + }, + { + "text", "some text", noError, + `"some text"`, + }, + { + "emptyAction", "{{}}", hasError, + `{{}}`, + }, + { + "field", "{{.X}}", noError, + `{{.X}}`, + }, + { + "simple command", "{{printf}}", noError, + `{{printf}}`, + }, + { + "$ invocation", "{{$}}", noError, + "{{$}}", + }, + { + "variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError, + "{{with $x := 3}}{{$x 23}}{{end}}", + }, + { + "variable with fields", "{{$.I}}", noError, + "{{$.I}}", + }, + { + "multi-word command", "{{printf `%d` 23}}", noError, + "{{printf `%d` 23}}", + }, + { + "pipeline", "{{.X|.Y}}", noError, + `{{.X | .Y}}`, + }, + { + "pipeline with decl", "{{$x := .X|.Y}}", noError, + `{{$x := .X | .Y}}`, + }, + { + "nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError, + `{{.X (.Y .Z) (.A | .B .C) (.E)}}`, + }, + { + "field applied to parentheses", "{{(.Y .Z).Field}}", noError, + `{{(.Y .Z).Field}}`, + }, + { + "simple if", "{{if .X}}hello{{end}}", noError, + `{{if .X}}"hello"{{end}}`, + }, + { + "if with else", "{{if .X}}true{{else}}false{{end}}", noError, + `{{if .X}}"true"{{else}}"false"{{end}}`, + }, + { + "if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError, + `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`, + }, + { + "if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError, + `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`, + }, + { + "simple range", "{{range .X}}hello{{end}}", noError, + `{{range .X}}"hello"{{end}}`, + }, + { + "chained field range", "{{range .X.Y.Z}}hello{{end}}", noError, + `{{range .X.Y.Z}}"hello"{{end}}`, + }, + { + "nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError, + `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`, + }, + { + "range with else", "{{range .X}}true{{else}}false{{end}}", noError, + `{{range .X}}"true"{{else}}"false"{{end}}`, + }, + { + "range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError, + `{{range .X | .M}}"true"{{else}}"false"{{end}}`, + }, + { + "range []int", "{{range .SI}}{{.}}{{end}}", noError, + `{{range .SI}}{{.}}{{end}}`, + }, + { + "range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError, + `{{range $x := .SI}}{{.}}{{end}}`, + }, + { + "range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError, + `{{range $x, $y := .SI}}{{.}}{{end}}`, + }, + { + "constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError, + `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`, + }, + { + "template", "{{template `x`}}", noError, + `{{template "x"}}`, + }, + { + "template with arg", "{{template `x` .Y}}", noError, + `{{template "x" .Y}}`, + }, + { + "with", "{{with .X}}hello{{end}}", noError, + `{{with .X}}"hello"{{end}}`, + }, + { + "with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError, + `{{with .X}}"hello"{{else}}"goodbye"{{end}}`, + }, // Trimming spaces. {"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`}, {"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`}, @@ -250,8 +314,10 @@ var parseTests = []parseTest{ {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`}, {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`}, {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`}, - {"block definition", `{{block "foo" .}}hello{{end}}`, noError, - `{{template "foo" .}}`}, + { + "block definition", `{{block "foo" .}}hello{{end}}`, noError, + `{{template "foo" .}}`, + }, // Errors. {"unclosed action", "hello{{range", hasError, ""}, {"unmatched end", "{{end}}", hasError, ""}, @@ -401,89 +467,143 @@ func TestErrorContextWithTreeCopy(t *testing.T) { // All failures, and the result is a string that must appear in the error message. var errorTests = []parseTest{ // Check line numbers are accurate. - {"unclosed1", + { + "unclosed1", "line1\n{{", - hasError, `unclosed1:2: unexpected unclosed action in command`}, - {"unclosed2", + hasError, `unclosed1:2: unexpected unclosed action in command`, + }, + { + "unclosed2", "line1\n{{define `x`}}line2\n{{", - hasError, `unclosed2:3: unexpected unclosed action in command`}, + hasError, `unclosed2:3: unexpected unclosed action in command`, + }, // Specific errors. - {"function", + { + "function", "{{foo}}", - hasError, `function "foo" not defined`}, - {"comment", + hasError, `function "foo" not defined`, + }, + { + "comment", "{{/*}}", - hasError, `unclosed comment`}, - {"lparen", + hasError, `unclosed comment`, + }, + { + "lparen", "{{.X (1 2 3}}", - hasError, `unclosed left paren`}, - {"rparen", + hasError, `unclosed left paren`, + }, + { + "rparen", "{{.X 1 2 3)}}", - hasError, `unexpected ")"`}, - {"space", + hasError, `unexpected ")"`, + }, + { + "space", "{{`x`3}}", - hasError, `in operand`}, - {"idchar", + hasError, `in operand`, + }, + { + "idchar", "{{a#}}", - hasError, `'#'`}, - {"charconst", + hasError, `'#'`, + }, + { + "charconst", "{{'a}}", - hasError, `unterminated character constant`}, - {"stringconst", + hasError, `unterminated character constant`, + }, + { + "stringconst", `{{"a}}`, - hasError, `unterminated quoted string`}, - {"rawstringconst", + hasError, `unterminated quoted string`, + }, + { + "rawstringconst", "{{`a}}", - hasError, `unterminated raw quoted string`}, - {"number", + hasError, `unterminated raw quoted string`, + }, + { + "number", "{{0xi}}", - hasError, `number syntax`}, - {"multidefine", + hasError, `number syntax`, + }, + { + "multidefine", "{{define `a`}}a{{end}}{{define `a`}}b{{end}}", - hasError, `multiple definition of template`}, - {"eof", + hasError, `multiple definition of template`, + }, + { + "eof", "{{range .X}}", - hasError, `unexpected EOF`}, - {"variable", + hasError, `unexpected EOF`, + }, + { + "variable", // Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration. "{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}", - hasError, `unexpected ":="`}, - {"multidecl", + hasError, `unexpected ":="`, + }, + { + "multidecl", "{{$a,$b,$c := 23}}", - hasError, `too many declarations`}, - {"undefvar", + hasError, `too many declarations`, + }, + { + "undefvar", "{{$a}}", - hasError, `undefined variable`}, - {"wrongdot", + hasError, `undefined variable`, + }, + { + "wrongdot", "{{true.any}}", - hasError, `unexpected . after term`}, - {"wrongpipeline", + hasError, `unexpected . after term`, + }, + { + "wrongpipeline", "{{12|false}}", - hasError, `non executable command in pipeline`}, - {"emptypipeline", + hasError, `non executable command in pipeline`, + }, + { + "emptypipeline", `{{ ( ) }}`, - hasError, `missing value for parenthesized pipeline`}, - {"multilinerawstring", + hasError, `missing value for parenthesized pipeline`, + }, + { + "multilinerawstring", "{{ $v := `\n` }} {{", - hasError, `multilinerawstring:2: unexpected unclosed action`}, - {"rangeundefvar", + hasError, `multilinerawstring:2: unexpected unclosed action`, + }, + { + "rangeundefvar", "{{range $k}}{{end}}", - hasError, `undefined variable`}, - {"rangeundefvars", + hasError, `undefined variable`, + }, + { + "rangeundefvars", "{{range $k, $v}}{{end}}", - hasError, `undefined variable`}, - {"rangemissingvalue1", + hasError, `undefined variable`, + }, + { + "rangemissingvalue1", "{{range $k,}}{{end}}", - hasError, `missing value for range`}, - {"rangemissingvalue2", + hasError, `missing value for range`, + }, + { + "rangemissingvalue2", "{{range $k, $v := }}{{end}}", - hasError, `missing value for range`}, - {"rangenotvariable1", + hasError, `missing value for range`, + }, + { + "rangenotvariable1", "{{range $k, .}}{{end}}", - hasError, `range can only initialize variables`}, - {"rangenotvariable2", + hasError, `range can only initialize variables`, + }, + { + "rangenotvariable2", "{{range $k, 123 := .}}{{end}}", - hasError, `range can only initialize variables`}, + hasError, `range can only initialize variables`, + }, } func TestErrors(t *testing.T) { diff --git a/tpl/internal/go_templates/texttemplate/template.go b/tpl/internal/go_templates/texttemplate/template.go index 9c6ba6dfc..755a8426e 100644 --- a/tpl/internal/go_templates/texttemplate/template.go +++ b/tpl/internal/go_templates/texttemplate/template.go @@ -5,9 +5,10 @@ package template import ( - "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" "reflect" "sync" + + "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" ) // common holds the information shared by related templates. diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go index 99877dcca..6d58d8d2b 100644 --- a/tpl/internal/templatefuncsRegistry.go +++ b/tpl/internal/templatefuncsRegistry.go @@ -83,7 +83,6 @@ func (t *TemplateFuncsNamespace) AddMethodMapping(m interface{}, aliases []strin Aliases: aliases, Examples: examples, } - } // TemplateFuncMethodMapping represents a mapping of functions to methods for a @@ -165,7 +164,6 @@ func (namespaces TemplateFuncsNamespaces) MarshalJSON() ([]byte, error) { } func (t *TemplateFuncsNamespace) toJSON() ([]byte, error) { - var buf bytes.Buffer godoc := getGetTplPackagesGoDoc()[t.Name] diff --git a/tpl/js/js.go b/tpl/js/js.go index b7a2fe066..d5d435be7 100644 --- a/tpl/js/js.go +++ b/tpl/js/js.go @@ -62,5 +62,4 @@ func (ns *Namespace) Build(args ...interface{}) (resource.Resource, error) { } return ns.client.Process(r, m) - } diff --git a/tpl/lang/init.go b/tpl/lang/init.go index 6a23cdc4c..520eccb88 100644 --- a/tpl/lang/init.go +++ b/tpl/lang/init.go @@ -45,7 +45,6 @@ func init() { }, ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/math/init.go b/tpl/math/init.go index e7f9114ba..ddbd2fc73 100644 --- a/tpl/math/init.go +++ b/tpl/math/init.go @@ -114,7 +114,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/math/math.go b/tpl/math/math.go index ecaf61ebc..2b2497c3e 100644 --- a/tpl/math/math.go +++ b/tpl/math/math.go @@ -64,7 +64,6 @@ func (ns *Namespace) Floor(x interface{}) (float64, error) { // Log returns the natural logarithm of a number. func (ns *Namespace) Log(a interface{}) (float64, error) { af, err := cast.ToFloat64E(a) - if err != nil { return 0, errors.New("Log operator can't be used with non integer or float value") } @@ -76,7 +75,6 @@ func (ns *Namespace) Log(a interface{}) (float64, error) { // NOTE: will return for NaN for negative values of a func (ns *Namespace) Sqrt(a interface{}) (float64, error) { af, err := cast.ToFloat64E(a) - if err != nil { return 0, errors.New("Sqrt operator can't be used with non integer or float value") } diff --git a/tpl/math/math_test.go b/tpl/math/math_test.go index c48f71837..da59d7938 100644 --- a/tpl/math/math_test.go +++ b/tpl/math/math_test.go @@ -197,7 +197,6 @@ func TestSqrt(t *testing.T) { result, err := ns.Sqrt(-1) c.Assert(err, qt.IsNil) c.Assert(result, qt.Satisfies, math.IsNaN) - } func TestMod(t *testing.T) { diff --git a/tpl/openapi/openapi3/init.go b/tpl/openapi/openapi3/init.go index 1e1a4ae05..4b4396ff4 100644 --- a/tpl/openapi/openapi3/init.go +++ b/tpl/openapi/openapi3/init.go @@ -35,7 +35,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/openapi/openapi3/openapi3.go b/tpl/openapi/openapi3/openapi3.go index 7dfd2f6a7..cc88f4125 100644 --- a/tpl/openapi/openapi3/openapi3.go +++ b/tpl/openapi/openapi3/openapi3.go @@ -49,7 +49,6 @@ type Namespace struct { } func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.Swagger, error) { - key := r.Key() if key == "" { return nil, errors.New("no Key set in Resource") @@ -87,11 +86,9 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.Swagg return s, err }) - if err != nil { return nil, err } return v.(*kopenapi3.Swagger), nil - } diff --git a/tpl/os/init.go b/tpl/os/init.go index 3ef8702d6..9d9b75473 100644 --- a/tpl/os/init.go +++ b/tpl/os/init.go @@ -56,7 +56,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/os/os.go b/tpl/os/os.go index eb31498af..214706593 100644 --- a/tpl/os/os.go +++ b/tpl/os/os.go @@ -28,7 +28,6 @@ import ( // New returns a new instance of the os-namespaced template functions. func New(d *deps.Deps) *Namespace { - var rfs afero.Fs if d.Fs != nil { rfs = d.Fs.WorkingDir @@ -80,7 +79,6 @@ func readFile(fs afero.Fs, filename string) (string, error) { return "", err } b, err := afero.ReadFile(fs, filename) - if err != nil { return "", err } diff --git a/tpl/partials/init.go b/tpl/partials/init.go index c2135bca5..4666fa336 100644 --- a/tpl/partials/init.go +++ b/tpl/partials/init.go @@ -49,7 +49,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/partials/partials.go b/tpl/partials/partials.go index 8b545399e..016e0423c 100644 --- a/tpl/partials/partials.go +++ b/tpl/partials/partials.go @@ -159,7 +159,6 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface } return result, nil - } // IncludeCached executes and caches partial templates. The cache is created with name+variants as the key. diff --git a/tpl/partials/partials_test.go b/tpl/partials/partials_test.go index 60e9dd721..656ab2d59 100644 --- a/tpl/partials/partials_test.go +++ b/tpl/partials/partials_test.go @@ -37,5 +37,4 @@ func TestCreateKey(t *testing.T) { c.Assert(create("a", map[string]string{"a": "av"}), qt.Equals, partialCacheKey{name: "a", variant: "4809626101226749924"}) c.Assert(create("a", []string{"a", "b"}), qt.Equals, partialCacheKey{name: "a", variant: "2712570657419664240"}) } - } diff --git a/tpl/path/init.go b/tpl/path/init.go index 518dcad22..a7f65073c 100644 --- a/tpl/path/init.go +++ b/tpl/path/init.go @@ -55,7 +55,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) } diff --git a/tpl/resources/init.go b/tpl/resources/init.go index df83cb3bb..d0e20d0ae 100644 --- a/tpl/resources/init.go +++ b/tpl/resources/init.go @@ -66,7 +66,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index cdde6bd5d..73f3743b6 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -93,7 +93,6 @@ func (ns *Namespace) Get(filename interface{}) (resource.Resource, error) { filenamestr = filepath.Clean(filenamestr) return ns.createClient.Get(filenamestr) - } // GetMatch finds the first Resource matching the given pattern, or nil if none found. @@ -108,7 +107,6 @@ func (ns *Namespace) GetMatch(pattern interface{}) (resource.Resource, error) { } return ns.createClient.GetMatch(patternStr) - } // Match gets all resources matching the given base path prefix, e.g @@ -281,7 +279,6 @@ func (ns *Namespace) PostCSS(args ...interface{}) (resource.Resource, error) { func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedResource, error) { return ns.deps.ResourceSpec.PostProcess(r) - } // Babel processes the given Resource with Babel. @@ -300,5 +297,4 @@ func (ns *Namespace) Babel(args ...interface{}) (resource.Resource, error) { } return ns.babelClient.Process(r, options) - } diff --git a/tpl/safe/init.go b/tpl/safe/init.go index edb16ed87..becaf38f3 100644 --- a/tpl/safe/init.go +++ b/tpl/safe/init.go @@ -74,7 +74,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/safe/safe_test.go b/tpl/safe/safe_test.go index d5288fef0..e91605762 100644 --- a/tpl/safe/safe_test.go +++ b/tpl/safe/safe_test.go @@ -15,7 +15,6 @@ package safe import ( "html/template" - "testing" qt "github.com/frankban/quicktest" diff --git a/tpl/site/init.go b/tpl/site/init.go index 48713bb3b..4dc934997 100644 --- a/tpl/site/init.go +++ b/tpl/site/init.go @@ -24,7 +24,6 @@ const name = "site" func init() { f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { - s := d.Site ns := &internal.TemplateFuncsNamespace{ Name: name, @@ -38,7 +37,6 @@ func init() { // We just add the Site as the namespace here. No method mappings. return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/strings/truncate.go b/tpl/strings/truncate.go index ff863db7c..6e3a50ed2 100644 --- a/tpl/strings/truncate.go +++ b/tpl/strings/truncate.go @@ -17,7 +17,6 @@ import ( "errors" "html" "html/template" - "regexp" "unicode" "unicode/utf8" diff --git a/tpl/strings/truncate_test.go b/tpl/strings/truncate_test.go index b4aa1ffad..51743e935 100644 --- a/tpl/strings/truncate_test.go +++ b/tpl/strings/truncate_test.go @@ -15,7 +15,6 @@ package strings import ( "html/template" - "reflect" "strings" "testing" @@ -81,5 +80,4 @@ func TestTruncate(t *testing.T) { if err == nil { t.Errorf("Should have errored") } - } diff --git a/tpl/template.go b/tpl/template.go index 82d877545..73db9b3a6 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -14,9 +14,8 @@ package tpl import ( - "reflect" - "io" + "reflect" "regexp" "github.com/gohugoio/hugo/output" diff --git a/tpl/templates/init.go b/tpl/templates/init.go index 8bc53ef49..4ae5c12cc 100644 --- a/tpl/templates/init.go +++ b/tpl/templates/init.go @@ -31,13 +31,13 @@ func init() { ns.AddMethodMapping(ctx.Exists, nil, - [][2]string{{`{{ if (templates.Exists "partials/header.html") }}Yes!{{ end }}`, `Yes!`}, + [][2]string{ + {`{{ if (templates.Exists "partials/header.html") }}Yes!{{ end }}`, `Yes!`}, {`{{ if not (templates.Exists "partials/doesnotexist.html") }}No!{{ end }}`, `No!`}, }, ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/templates/templates.go b/tpl/templates/templates.go index 80eb2d378..e00537330 100644 --- a/tpl/templates/templates.go +++ b/tpl/templates/templates.go @@ -36,5 +36,4 @@ type Namespace struct { func (ns *Namespace) Exists(name string) bool { _, found := ns.deps.Tmpl().Lookup(name) return found - } diff --git a/tpl/time/init.go b/tpl/time/init.go index 7abb36637..23e5263ba 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -91,7 +91,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/tplimpl/embedded/generate/generate.go b/tpl/tplimpl/embedded/generate/generate.go index df4de4799..3f7cc0257 100644 --- a/tpl/tplimpl/embedded/generate/generate.go +++ b/tpl/tplimpl/embedded/generate/generate.go @@ -25,7 +25,6 @@ import ( ) func main() { - templateFolder := filepath.Join("..", "templates") temlatePath := filepath.Join(".", templateFolder) @@ -92,7 +91,6 @@ var EmbeddedTemplates = [][2]string{ fmt.Fprint(file, " ", v, ",\n") } fmt.Fprint(file, "}\n") - } func nameValue(name, value string) string { diff --git a/tpl/tplimpl/shortcodes.go b/tpl/tplimpl/shortcodes.go index cc4d99491..789bc07d3 100644 --- a/tpl/tplimpl/shortcodes.go +++ b/tpl/tplimpl/shortcodes.go @@ -80,7 +80,6 @@ func (s *shortcodeTemplates) fromVariantsSlice(variants []string) (shortcodeVari // calculate a weight for two string slices of same length. // higher value means "better match". func (s *shortcodeTemplates) compareVariants(a, b []string) int { - weight := 0 k := len(a) for i, av := range a { @@ -101,7 +100,6 @@ func templateVariants(name string) []string { } func templateNameAndVariants(name string) (string, []string) { - variants := make([]string, numTemplateVariants) parts := strings.Split(name, ".") diff --git a/tpl/tplimpl/shortcodes_test.go b/tpl/tplimpl/shortcodes_test.go index 4ef8c5cd7..f97c7f278 100644 --- a/tpl/tplimpl/shortcodes_test.go +++ b/tpl/tplimpl/shortcodes_test.go @@ -20,14 +20,12 @@ import ( ) func TestShortcodesTemplate(t *testing.T) { - t.Run("isShortcode", func(t *testing.T) { c := qt.New(t) c.Assert(isShortcode("shortcodes/figures.html"), qt.Equals, true) c.Assert(isShortcode("_internal/shortcodes/figures.html"), qt.Equals, true) c.Assert(isShortcode("shortcodes\\figures.html"), qt.Equals, false) c.Assert(isShortcode("myshortcodes"), qt.Equals, false) - }) t.Run("variantsFromName", func(t *testing.T) { @@ -40,7 +38,6 @@ func TestShortcodesTemplate(t *testing.T) { name, variants := templateNameAndVariants("figure.html") c.Assert(name, qt.Equals, "figure") c.Assert(variants, qt.DeepEquals, []string{"", "html", "html"}) - }) t.Run("compareVariants", func(t *testing.T) { @@ -65,7 +62,6 @@ func TestShortcodesTemplate(t *testing.T) { w := s.compareVariants(templateVariants(test.name1), templateVariants(test.name2)) c.Assert(w, qt.Equals, test.expected) } - }) t.Run("indexOf", func(t *testing.T) { @@ -81,7 +77,6 @@ func TestShortcodesTemplate(t *testing.T) { c.Assert(s.indexOf([]string{"a", "b", "c"}), qt.Equals, 0) c.Assert(s.indexOf([]string{"a", "b", "d"}), qt.Equals, 1) c.Assert(s.indexOf([]string{"a", "b", "x"}), qt.Equals, -1) - }) t.Run("Name", func(t *testing.T) { @@ -92,6 +87,5 @@ func TestShortcodesTemplate(t *testing.T) { c.Assert(templateBaseName(templateShortcode, "shortcodes/test/foo.html"), qt.Equals, "test/foo.html") c.Assert(true, qt.Equals, true) - }) } diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index e3a4ce090..eb91ab837 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -167,7 +167,6 @@ func newTemplateExec(d *deps.Deps) (*templateExec, error) { if d.WithTemplate != nil { if err := d.WithTemplate(e); err != nil { return nil, err - } } @@ -246,7 +245,6 @@ func (t *templateExec) MarkReady() error { }) return err - } type templateHandler struct { @@ -345,7 +343,6 @@ func (t *templateHandler) LookupVariant(name string, variants tpl.TemplateVarian more := len(s.variants) > 1 return sv.ts, true, more - } // LookupVariants returns all variants of name, nil if none found. @@ -362,11 +359,9 @@ func (t *templateHandler) LookupVariants(name string) []tpl.Template { } return variants - } func (t *templateHandler) HasTemplate(name string) bool { - if _, found := t.baseof[name]; found { return true } @@ -501,7 +496,6 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error err, _ := checkFilename(ts.baseInfo, inerr) return err - } func (t *templateHandler) addShortcodeVariant(ts *templateState) { @@ -584,7 +578,6 @@ func (t *templateHandler) addTemplateFile(name, path string) error { t.applyTemplateTransformers(t.main, templ) return nil - } func (t *templateHandler) addTemplateTo(info templateInfo, to *templateNamespace) (*templateState, error) { @@ -716,7 +709,6 @@ func (t *templateHandler) loadTemplates() error { } return nil - } func (t *templateHandler) nameIsText(name string) (string, bool) { @@ -763,7 +755,6 @@ func (t *templateHandler) extractPartials(templ tpl.Template) error { } return nil - } func (t *templateHandler) postTransform() error { @@ -874,7 +865,6 @@ func (t *templateNamespace) newTemplateLookup(in *templateState) func(name strin return newTemplateState(templ, templateInfo{name: templ.Name()}) } return nil - } } @@ -933,7 +923,6 @@ func (t *templateState) isText() bool { func isText(templ tpl.Template) bool { _, isText := templ.(*texttemplate.Template) return isText - } type templateStateMap struct { @@ -1003,7 +992,6 @@ func removeLeadingBOM(s string) string { } return s - } // resolves _internal/shortcodes/param.html => param.html etc. @@ -1015,7 +1003,6 @@ func templateBaseName(typ templateType, name string) string { default: panic("not implemented") } - } func unwrap(templ tpl.Template) tpl.Template { @@ -1041,5 +1028,4 @@ func templates(in tpl.Template) []tpl.Template { } return templs - } diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go index 015cf72af..de9b5424f 100644 --- a/tpl/tplimpl/template_ast_transformers.go +++ b/tpl/tplimpl/template_ast_transformers.go @@ -73,7 +73,6 @@ func (c templateContext) getIfNotVisited(name string) *templateState { func newTemplateContext( t *templateState, lookupFn func(name string) *templateState) *templateContext { - return &templateContext{ t: t, lookupFn: lookupFn, @@ -86,7 +85,6 @@ func newTemplateContext( func applyTemplateTransformers( t *templateState, lookupFn func(name string) *templateState) (*templateContext, error) { - if t == nil { return nil, errors.New("expected template, but none provided") } @@ -117,9 +115,7 @@ const ( partialReturnWrapperTempl = `{{ $_hugo_dot := $ }}{{ $ := .Arg }}{{ with .Arg }}{{ $_hugo_dot.Set ("PLACEHOLDER") }}{{ end }}` ) -var ( - partialReturnWrapper *parse.ListNode -) +var partialReturnWrapper *parse.ListNode func init() { templ, err := texttemplate.New("").Parse(partialReturnWrapperTempl) @@ -127,7 +123,6 @@ func init() { panic(err) } partialReturnWrapper = templ.Tree.Root - } func (c *templateContext) wrapInPartialReturnWrapper(n *parse.ListNode) *parse.ListNode { @@ -142,7 +137,6 @@ func (c *templateContext) wrapInPartialReturnWrapper(n *parse.ListNode) *parse.L withNode.List.Nodes = append(n.Nodes, retn) return wrapper - } // applyTransformations do 2 things: @@ -275,7 +269,6 @@ func (c *templateContext) collectInner(n *parse.CommandNode) { break } } - } var partialRe = regexp.MustCompile(`^partial(Cached)?$|^partials\.Include(Cached)?$`) @@ -330,7 +323,6 @@ func (c *templateContext) collectReturnNode(n *parse.CommandNode) bool { c.returnNode.Args = c.returnNode.Args[1:] return false - } func findTemplateIn(name string, in tpl.Template) (tpl.Template, bool) { @@ -345,5 +337,4 @@ func findTemplateIn(name string, in tpl.Template) (tpl.Template, bool) { return templ, true } return nil, false - } diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go index b38446235..56d970b23 100644 --- a/tpl/tplimpl/template_ast_transformers_test.go +++ b/tpl/tplimpl/template_ast_transformers_test.go @@ -44,7 +44,6 @@ func TestTransformRecursiveTemplate(t *testing.T) { newTestTemplateLookup(ts), ) ctx.applyTransformations(templ.Tree.Root) - } func newTestTemplate(templ tpl.Template) *templateState { @@ -78,7 +77,6 @@ func newTestTemplateLookup(in *templateState) func(name string) *templateState { } func TestCollectInfo(t *testing.T) { - configStr := `{ "version": 42 }` tests := []struct { @@ -114,11 +112,9 @@ func TestCollectInfo(t *testing.T) { c.Assert(ctx.t.parseInfo, qt.DeepEquals, test.expected) }) } - } func TestPartialReturn(t *testing.T) { - tests := []struct { name string tplString string @@ -159,8 +155,6 @@ func TestPartialReturn(t *testing.T) { // Just check that it doesn't fail in this test. We have functional tests // in hugoblib. c.Assert(err, qt.IsNil) - }) } - } diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 25ace365d..4b3abaada 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -60,8 +60,10 @@ import ( _ "github.com/gohugoio/hugo/tpl/urls" ) -var _ texttemplate.ExecHelper = (*templateExecHelper)(nil) -var zero reflect.Value +var ( + _ texttemplate.ExecHelper = (*templateExecHelper)(nil) + zero reflect.Value +) type templateExecHelper struct { running bool // whether we're in server mode. @@ -145,7 +147,6 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec } func createFuncMap(d *deps.Deps) map[string]interface{} { - funcMap := template.FuncMap{} // Merge the namespace funcs @@ -172,5 +173,4 @@ func createFuncMap(d *deps.Deps) map[string]interface{} { } return funcMap - } diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index 852b63930..c142dd672 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -40,9 +40,7 @@ import ( "github.com/spf13/viper" ) -var ( - logger = loggers.NewErrorLogger() -) +var logger = loggers.NewErrorLogger() func newTestConfig() config.Provider { v := viper.New() @@ -188,7 +186,6 @@ func TestPartialCached(t *testing.T) { t.Fatalf("cache mismatch") } } - } func BenchmarkPartial(b *testing.B) { diff --git a/tpl/tplimpl/template_info_test.go b/tpl/tplimpl/template_info_test.go index 1324b458e..db74c29a8 100644 --- a/tpl/tplimpl/template_info_test.go +++ b/tpl/tplimpl/template_info_test.go @@ -38,7 +38,6 @@ func TestTemplateInfoShortcode(t *testing.T) { tti, ok := tt.(tpl.Info) c.Assert(ok, qt.Equals, true) c.Assert(tti.ParseInfo().IsInner, qt.Equals, true) - } // TODO(bep) move and use in other places @@ -55,5 +54,4 @@ func newD(c *qt.C) *deps.Deps { provider.Update(d) return d - } diff --git a/tpl/transform/init.go b/tpl/transform/init.go index 62cb0a9c3..9e57c07f6 100644 --- a/tpl/transform/init.go +++ b/tpl/transform/init.go @@ -46,13 +46,16 @@ func init() { [][2]string{ { `{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, { `{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}`, - `Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`}, + `Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`, + }, { `{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, }, ) @@ -61,16 +64,20 @@ func init() { [][2]string{ { `{{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, { `{{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, { `{{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape }}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, { `{{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlEscape | safeHTML }}`, - `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`, + }, }, ) @@ -104,7 +111,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/transform/remarshal_test.go b/tpl/transform/remarshal_test.go index daf99fdb4..eb5f4253c 100644 --- a/tpl/transform/remarshal_test.go +++ b/tpl/transform/remarshal_test.go @@ -107,7 +107,6 @@ title: Test Metadata } } - } func TestRemarshalComments(t *testing.T) { @@ -168,7 +167,6 @@ func TestTestRemarshalError(t *testing.T) { _, err = ns.Remarshal("json", "asdf") c.Assert(err, qt.Not(qt.IsNil)) - } func TestTestRemarshalMapInput(t *testing.T) { diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go index b168d2a50..82756ed97 100644 --- a/tpl/transform/transform.go +++ b/tpl/transform/transform.go @@ -98,7 +98,6 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { } b, err := ns.deps.ContentSpec.RenderMarkdown([]byte(ss)) - if err != nil { return "", err } diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go index b3f4206ff..b98d82d27 100644 --- a/tpl/transform/transform_test.go +++ b/tpl/transform/transform_test.go @@ -15,7 +15,6 @@ package transform import ( "html/template" - "testing" "github.com/gohugoio/hugo/common/loggers" @@ -206,7 +205,6 @@ And then some. c.Assert(err, qt.IsNil) c.Assert(result, qt.Equals, template.HTML( "<p>#First</p>\n<p>This is some <em>bold</em> text.</p>\n<h2 id=\"second\">Second</h2>\n<p>This is some more text.</p>\n<p>And then some.</p>\n")) - } func TestPlainify(t *testing.T) { diff --git a/tpl/transform/unmarshal.go b/tpl/transform/unmarshal.go index b606c870a..aa84ca1f8 100644 --- a/tpl/transform/unmarshal.go +++ b/tpl/transform/unmarshal.go @@ -39,7 +39,7 @@ func (ns *Namespace) Unmarshal(args ...interface{}) (interface{}, error) { } var data interface{} - var decoder = metadecoders.Default + decoder := metadecoders.Default if len(args) == 1 { data = args[0] diff --git a/tpl/transform/unmarshal_test.go b/tpl/transform/unmarshal_test.go index 183bdefd5..ec81c316a 100644 --- a/tpl/transform/unmarshal_test.go +++ b/tpl/transform/unmarshal_test.go @@ -79,7 +79,6 @@ func (t testContentResource) Key() string { } func TestUnmarshal(t *testing.T) { - v := viper.New() ns := New(newDeps(v)) c := qt.New(t) @@ -120,21 +119,17 @@ func TestUnmarshal(t *testing.T) { }}, {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) { c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) - }}, {"a,b,c", nil, func(r [][]string) { c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) - }}, {"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) { c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) - }}, {testContentResource{key: "r1", content: ` % This is a comment a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) { c.Assert([][]string{{"a", "b", "c"}}, qt.DeepEquals, r) - }}, // errors {"thisisnotavaliddataformat", nil, false}, diff --git a/tpl/urls/init.go b/tpl/urls/init.go index debaaabf9..0abe0d125 100644 --- a/tpl/urls/init.go +++ b/tpl/urls/init.go @@ -67,7 +67,6 @@ func init() { ) return ns - } internal.AddTemplateFuncsNamespace(f) diff --git a/tpl/urls/urls.go b/tpl/urls/urls.go index ee0e55501..652256f44 100644 --- a/tpl/urls/urls.go +++ b/tpl/urls/urls.go @@ -17,9 +17,7 @@ package urls import ( "errors" "fmt" - "html/template" - "net/url" "github.com/gohugoio/hugo/common/urls" |