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.
17
`Move the URL pieces of package http into a new package, url.
19
http://codereview.appspot.com/4893043
23
var urlRenames = []struct{ in, out string }{
25
{"ParseURL", "Parse"},
26
{"ParseURLReference", "ParseWithReference"},
27
{"ParseQuery", "ParseQuery"},
29
{"URLEscape", "QueryEscape"},
30
{"URLUnescape", "QueryUnescape"},
31
{"URLError", "Error"},
32
{"URLEscapeError", "EscapeError"},
35
func url(f *ast.File) bool {
36
if imports(f, "url") || !imports(f, "http") {
43
urlWalk := func(n interface{}) {
44
// Is it an identifier?
45
if ident, ok := n.(*ast.Ident); ok && ident.Name == "url" {
49
// Parameter and result names.
50
if fn, ok := n.(*ast.FuncType); ok {
51
fixed = urlDoFields(fn.Params) || fixed
52
fixed = urlDoFields(fn.Results) || fixed
56
// Fix up URL code and add import, at most once.
62
walkBeforeAfter(f, urlWalk, nop)
66
walk(f, func(n interface{}) {
67
// Rename functions and methods.
68
if expr, ok := n.(ast.Expr); ok {
69
for _, s := range urlRenames {
70
if isPkgDot(expr, "http", s.in) {
72
expr.(*ast.SelectorExpr).X.(*ast.Ident).Name = "url"
73
expr.(*ast.SelectorExpr).Sel.Name = s.out
80
// Remove the http import if no longer needed.
81
if fixed && !usesImport(f, "http") {
82
deleteImport(f, "http")
88
func urlDoFields(list *ast.FieldList) (fixed bool) {
92
for _, field := range list.List {
93
for _, ident := range field.Names {
94
if ident.Name == "url" {