~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/fix/template.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 The Go Authors.  All rights reserved.
2
 
// Use of this source code is governed by a BSD-style
3
 
// license that can be found in the LICENSE file.
4
 
 
5
 
package main
6
 
 
7
 
import (
8
 
        "go/ast"
9
 
)
10
 
 
11
 
func init() {
12
 
        register(templateFix)
13
 
}
14
 
 
15
 
var templateFix = fix{
16
 
        "template",
17
 
        "2011-11-22",
18
 
        template,
19
 
        `Rewrite calls to template.ParseFile to template.ParseFiles
20
 
 
21
 
http://codereview.appspot.com/5433048
22
 
`,
23
 
}
24
 
 
25
 
var templateSetGlobals = []string{
26
 
        "ParseSetFiles",
27
 
        "ParseSetGlob",
28
 
        "ParseTemplateFiles",
29
 
        "ParseTemplateGlob",
30
 
        "Set",
31
 
        "SetMust",
32
 
}
33
 
 
34
 
var templateSetMethods = []string{
35
 
        "ParseSetFiles",
36
 
        "ParseSetGlob",
37
 
        "ParseTemplateFiles",
38
 
        "ParseTemplateGlob",
39
 
}
40
 
 
41
 
var templateTypeConfig = &TypeConfig{
42
 
        Type: map[string]*Type{
43
 
                "template.Template": {
44
 
                        Method: map[string]string{
45
 
                                "Funcs":      "func() *template.Template",
46
 
                                "Delims":     "func() *template.Template",
47
 
                                "Parse":      "func() (*template.Template, error)",
48
 
                                "ParseFile":  "func() (*template.Template, error)",
49
 
                                "ParseInSet": "func() (*template.Template, error)",
50
 
                        },
51
 
                },
52
 
                "template.Set": {
53
 
                        Method: map[string]string{
54
 
                                "ParseSetFiles":      "func() (*template.Set, error)",
55
 
                                "ParseSetGlob":       "func() (*template.Set, error)",
56
 
                                "ParseTemplateFiles": "func() (*template.Set, error)",
57
 
                                "ParseTemplateGlob":  "func() (*template.Set, error)",
58
 
                        },
59
 
                },
60
 
        },
61
 
 
62
 
        Func: map[string]string{
63
 
                "template.New":     "*template.Template",
64
 
                "template.Must":    "(*template.Template, error)",
65
 
                "template.SetMust": "(*template.Set, error)",
66
 
        },
67
 
}
68
 
 
69
 
func template(f *ast.File) bool {
70
 
        if !imports(f, "text/template") && !imports(f, "html/template") {
71
 
                return false
72
 
        }
73
 
 
74
 
        fixed := false
75
 
 
76
 
        typeof, _ := typecheck(templateTypeConfig, f)
77
 
 
78
 
        // Now update the names used by importers.
79
 
        walk(f, func(n interface{}) {
80
 
                if sel, ok := n.(*ast.SelectorExpr); ok {
81
 
                        // Reference to top-level function ParseFile.
82
 
                        if isPkgDot(sel, "template", "ParseFile") {
83
 
                                sel.Sel.Name = "ParseFiles"
84
 
                                fixed = true
85
 
                                return
86
 
                        }
87
 
                        // Reference to ParseFiles method.
88
 
                        if typeof[sel.X] == "*template.Template" && sel.Sel.Name == "ParseFile" {
89
 
                                sel.Sel.Name = "ParseFiles"
90
 
                                fixed = true
91
 
                                return
92
 
                        }
93
 
                        // The Set type and its functions are now gone.
94
 
                        for _, name := range templateSetGlobals {
95
 
                                if isPkgDot(sel, "template", name) {
96
 
                                        warn(sel.Pos(), "reference to template.%s must be fixed manually", name)
97
 
                                        return
98
 
                                }
99
 
                        }
100
 
                        // The methods of Set are now gone.
101
 
                        for _, name := range templateSetMethods {
102
 
                                if typeof[sel.X] == "*template.Set" && sel.Sel.Name == name {
103
 
                                        warn(sel.Pos(), "reference to template.*Set.%s must be fixed manually", name)
104
 
                                        return
105
 
                                }
106
 
                        }
107
 
                }
108
 
        })
109
 
 
110
 
        return fixed
111
 
}