|
| 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 | + "fmt" |
| 9 | + "os" |
| 10 | + "go/ast" |
| 11 | +) |
| 12 | + |
| 13 | +var _ fmt.Stringer |
| 14 | +var _ os.Error |
| 15 | + |
| 16 | +var urlFix = fix{ |
| 17 | + "url", |
| 18 | + url, |
| 19 | + `Move the URL pieces of package http into a new package, url. |
| 20 | +
|
| 21 | +http://codereview.appspot.com/4893043 |
| 22 | +`, |
| 23 | +} |
| 24 | + |
| 25 | +func init() { |
| 26 | + register(urlFix) |
| 27 | +} |
| 28 | + |
| 29 | +var urlRenames = []struct{ in, out string }{ |
| 30 | + {"ParseURL", "Parse"}, |
| 31 | + {"ParseURLReference", "ParseWithReference"}, |
| 32 | + {"ParseQuery", "ParseQuery"}, |
| 33 | + {"Values", "Values"}, |
| 34 | + {"URLEscape", "QueryEscape"}, |
| 35 | + {"URLUnescape", "QueryUnescape"}, |
| 36 | + {"URLError", "Error"}, |
| 37 | + {"URLEscapeError", "EscapeError"}, |
| 38 | +} |
| 39 | + |
| 40 | +func url(f *ast.File) bool { |
| 41 | + if imports(f, "url") || !imports(f, "http") { |
| 42 | + return false |
| 43 | + } |
| 44 | + |
| 45 | + fixed := false |
| 46 | + |
| 47 | + // Update URL code. |
| 48 | + urlWalk := func(n interface{}) { |
| 49 | + // Is it an identifier? |
| 50 | + if ident, ok := n.(*ast.Ident); ok && ident.Name == "url" { |
| 51 | + ident.Name = "url_" |
| 52 | + return |
| 53 | + } |
| 54 | + // Find declared identifiers called url that might be confused. |
| 55 | + // TODO: Why does gofix not walk the Names in a ValueSpec? |
| 56 | + // TODO: Just a bug; fix later as it will have consequences. |
| 57 | + if valSpec, ok := n.(*ast.ValueSpec); ok { |
| 58 | + for _, ident := range valSpec.Names { |
| 59 | + if ident.Name == "url" { |
| 60 | + ident.Name = "url_" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + // Parameter and result names. |
| 65 | + if fn, ok := n.(*ast.FuncType); ok { |
| 66 | + fixed = urlDoFields(fn.Params) || fixed |
| 67 | + fixed = urlDoFields(fn.Results) || fixed |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Fix up URL code and add import, at most once. |
| 72 | + fix := func() { |
| 73 | + if fixed { |
| 74 | + return |
| 75 | + } |
| 76 | + walk(f, urlWalk) |
| 77 | + addImport(f, "url") |
| 78 | + fixed = true |
| 79 | + } |
| 80 | + |
| 81 | + walk(f, func(n interface{}) { |
| 82 | + // Rename functions and methods. |
| 83 | + if expr, ok := n.(ast.Expr); ok { |
| 84 | + for _, s := range urlRenames { |
| 85 | + if isPkgDot(expr, "http", s.in) { |
| 86 | + fix() |
| 87 | + expr.(*ast.SelectorExpr).X.(*ast.Ident).Name = "url" |
| 88 | + expr.(*ast.SelectorExpr).Sel.Name = s.out |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + // Remove the http import if no longer needed. |
| 96 | + if fixed && !usesImport(f, "http") { |
| 97 | + deleteImport(f, "http") |
| 98 | + } |
| 99 | + |
| 100 | + return fixed |
| 101 | +} |
| 102 | + |
| 103 | +func urlDoFields(list *ast.FieldList) (fixed bool) { |
| 104 | + if list == nil { |
| 105 | + return |
| 106 | + } |
| 107 | + for _, field := range list.List { |
| 108 | + for _, ident := range field.Names { |
| 109 | + if ident.Name == "url" { |
| 110 | + fixed = true |
| 111 | + ident.Name = "url_" |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return |
| 116 | +} |
0 commit comments