]> Cypherpunks repositories - gostls13.git/commitdiff
gofix: fixes for path/filepath changes
authorRobert Hencke <robert.hencke@gmail.com>
Sun, 26 Jun 2011 01:24:28 +0000 (11:24 +1000)
committerRob Pike <r@golang.org>
Sun, 26 Jun 2011 01:24:28 +0000 (11:24 +1000)
Fixes #1970.

R=rsc, r
CC=golang-dev
https://golang.org/cl/4650050

src/cmd/gofix/Makefile
src/cmd/gofix/filepath.go [new file with mode: 0644]
src/cmd/gofix/filepath_test.go [new file with mode: 0644]

index 60035e65e08d114204d6c6fb0014daa701bed230..ab16bd5aa5bf72153741ad13f7f03c1970987fe2 100644 (file)
@@ -6,6 +6,7 @@ include ../../Make.inc
 
 TARG=gofix
 GOFILES=\
+       filepath.go\
        fix.go\
        httpfinalurl.go\
        httpheaders.go\
diff --git a/src/cmd/gofix/filepath.go b/src/cmd/gofix/filepath.go
new file mode 100644 (file)
index 0000000..1d0ad68
--- /dev/null
@@ -0,0 +1,53 @@
+// 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(fix{
+               "filepath",
+               filepathFunc,
+               `Adapt code from filepath.[List]SeparatorString to string(filepath.[List]Separator).
+
+http://codereview.appspot.com/4527090
+`,
+       })
+}
+
+func filepathFunc(f *ast.File) (fixed bool) {
+       if !imports(f, "path/filepath") {
+               return
+       }
+
+       walk(f, func(n interface{}) {
+               e, ok := n.(*ast.Expr)
+               if !ok {
+                       return
+               }
+
+               var ident string
+               switch {
+               case isPkgDot(*e, "filepath", "SeparatorString"):
+                       ident = "filepath.Separator"
+               case isPkgDot(*e, "filepath", "ListSeparatorString"):
+                       ident = "filepath.ListSeparator"
+               default:
+                       return
+               }
+
+               // string(filepath.[List]Separator)
+               *e = &ast.CallExpr{
+                       Fun:  ast.NewIdent("string"),
+                       Args: []ast.Expr{ast.NewIdent(ident)},
+               }
+
+               fixed = true
+       })
+
+       return
+}
diff --git a/src/cmd/gofix/filepath_test.go b/src/cmd/gofix/filepath_test.go
new file mode 100644 (file)
index 0000000..d170c3a
--- /dev/null
@@ -0,0 +1,33 @@
+// 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
+
+func init() {
+       addTestCases(filepathTests)
+}
+
+var filepathTests = []testCase{
+       {
+               Name: "filepath.0",
+               In: `package main
+
+import (
+       "path/filepath"
+)
+
+var _ = filepath.SeparatorString
+var _ = filepath.ListSeparatorString
+`,
+               Out: `package main
+
+import (
+       "path/filepath"
+)
+
+var _ = string(filepath.Separator)
+var _ = string(filepath.ListSeparator)
+`,
+       },
+}