blob: 6d9399687281134b22fd791ea54bfc4cc2588d40 [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2012 The Go Authors. All rights reserved.
Christopher Cahoonc00371e2012-12-16 19:31:59 -05002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import "go/ast"
8
9func init() {
10 register(printerconfigFix)
11}
12
13var printerconfigFix = fix{
Sam Whited22d3bf12016-09-09 09:49:47 -050014 name: "printerconfig",
15 date: "2012-12-11",
16 f: printerconfig,
17 desc: `Add element keys to Config composite literals.`,
Christopher Cahoonc00371e2012-12-16 19:31:59 -050018}
19
20func printerconfig(f *ast.File) bool {
21 if !imports(f, "go/printer") {
22 return false
23 }
24
25 fixed := false
26 walk(f, func(n interface{}) {
27 cl, ok := n.(*ast.CompositeLit)
28 if !ok {
29 return
30 }
31 se, ok := cl.Type.(*ast.SelectorExpr)
32 if !ok {
33 return
34 }
35 if !isTopName(se.X, "printer") || se.Sel == nil {
36 return
37 }
38
39 if ss := se.Sel.String(); ss == "Config" {
40 for i, e := range cl.Elts {
41 if _, ok := e.(*ast.KeyValueExpr); ok {
42 break
43 }
44 switch i {
45 case 0:
46 cl.Elts[i] = &ast.KeyValueExpr{
47 Key: ast.NewIdent("Mode"),
48 Value: e,
49 }
50 case 1:
51 cl.Elts[i] = &ast.KeyValueExpr{
52 Key: ast.NewIdent("Tabwidth"),
53 Value: e,
54 }
55 }
56 fixed = true
57 }
58 }
59 })
60 return fixed
61}