| 1 |
// Copyright 2012 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 "go/ast" |
| 8 |
|
| 9 |
func init() { |
| 10 |
register(printerconfigFix) |
| 11 |
} |
| 12 |
|
| 13 |
var printerconfigFix = fix{ |
| 14 |
name: "printerconfig", |
| 15 |
date: "2012-12-11", |
| 16 |
f: printerconfig, |
| 17 |
desc: `Add element keys to Config composite literals.`, |
| 18 |
} |
| 19 |
|
| 20 |
func printerconfig(f *ast.File) bool { |
| 21 |
if !imports(f, "go/printer") { |
| 22 |
return false |
| 23 |
} |
| 24 |
|
| 25 |
fixed := false |
| 26 |
walk(f, func(n any) { |
| 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 |
} |