summaryrefslogtreecommitdiff
path: root/src/cmd/fix/imagenew.go
blob: b4e36d4f0c9c104191e4804f3582a30ada40aeb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"go/ast"
)

func init() {
	register(imagenewFix)
}

var imagenewFix = fix{
	"imagenew",
	"2011-09-14",
	imagenew,
	`Adapt image.NewXxx calls to pass an image.Rectangle instead of (w, h int).

http://codereview.appspot.com/4964073
`,
}

var imagenewFuncs = map[string]bool{
	"NewRGBA":    true,
	"NewRGBA64":  true,
	"NewNRGBA":   true,
	"NewNRGBA64": true,
	"NewAlpha":   true,
	"NewAlpha16": true,
	"NewGray":    true,
	"NewGray16":  true,
}

func imagenew(f *ast.File) bool {
	if !imports(f, "image") {
		return false
	}

	fixed := false
	walk(f, func(n interface{}) {
		call, ok := n.(*ast.CallExpr)
		if !ok {
			return
		}
		isNewFunc := false
		for newFunc := range imagenewFuncs {
			if len(call.Args) == 2 && isPkgDot(call.Fun, "image", newFunc) {
				isNewFunc = true
				break
			}
		}
		if len(call.Args) == 3 && isPkgDot(call.Fun, "image", "NewPaletted") {
			isNewFunc = true
		}
		if !isNewFunc {
			return
		}
		// Replace image.NewXxx(w, h) with image.NewXxx(image.Rect(0, 0, w, h)).
		rectArgs := []ast.Expr{
			&ast.BasicLit{Value: "0"},
			&ast.BasicLit{Value: "0"},
		}
		rectArgs = append(rectArgs, call.Args[:2]...)
		rect := []ast.Expr{
			&ast.CallExpr{
				Fun: &ast.SelectorExpr{
					X: &ast.Ident{
						Name: "image",
					},
					Sel: &ast.Ident{
						Name: "Rect",
					},
				},
				Args: rectArgs,
			},
		}
		call.Args = append(rect, call.Args[2:]...)
		fixed = true
	})
	return fixed
}