`,
},
{
- Name: "import.3",
+ Name: "import.17",
Fn: addImportFn("x/y/z", "x/a/c"),
In: `package main
"d/f"
)
+`,
+ },
+ {
+ Name: "import.18",
+ Fn: addDelImportFn("e", "o"),
+ In: `package main
+
+import (
+ "f"
+ "o"
+ "z"
+)
+`,
+ Out: `package main
+
+import (
+ "e"
+ "f"
+ "z"
+)
`,
},
}
}
}
+func addDelImportFn(p1 string, p2 string) func(*ast.File) bool {
+ return func(f *ast.File) bool {
+ fixed := false
+ if !imports(f, p1) {
+ addImport(f, p1)
+ fixed = true
+ }
+ if imports(f, p2) {
+ deleteImport(f, p2)
+ fixed = true
+ }
+ return fixed
+ }
+}
+
func rewriteImportFn(oldnew ...string) func(*ast.File) bool {
return func(f *ast.File) bool {
fixed := false
// Record positions for specs.
pos := make([]posSpan, len(specs))
for i, s := range specs {
- // Cannot use s.End(), because it looks at len(s.Path.Value),
- // and that string might have gotten longer or shorter.
- // Instead, use s.Pos()+1, which is guaranteed to be > s.Pos()
- // and still before the original end of the string, since any
- // string literal must be at least 2 characters ("" or ``).
- pos[i] = posSpan{s.Pos(), s.Pos() + 1}
+ pos[i] = posSpan{s.Pos(), s.End()}
}
// Identify comments in this range.