}
if rewrite != nil {
- file = rewrite(file)
+ if adjust == nil {
+ file = rewrite(file)
+ } else {
+ fmt.Fprintf(os.Stderr, "warning: rewrite ignored for incomplete programs\n")
+ }
}
if *simplifyAST {
if err != nil {
return err
}
- res := adjust(src, buf.Bytes())
+ res := buf.Bytes()
+ if adjust != nil {
+ res = adjust(src, res)
+ }
if !bytes.Equal(src, res) {
// formatting has changed
// Try as whole source file.
file, err := parser.ParseFile(fset, filename, src, parserMode)
if err == nil {
- adjust := func(orig, src []byte) []byte { return src }
- return file, adjust, nil
+ return file, nil, nil
}
// If the error is that the source file didn't begin with a
// package line and this is standard input, fall through to
for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
j--
}
- return b[:i], b[i:j], b[j:]
+ if i <= j {
+ return b[:i], b[i:j], b[j:]
+ }
+ return nil, nil, b[j:]
}
// matchSpace reformats src to use the same space context as orig.
}
defer func() {
if x := recover(); x != nil {
- if s, ok := x.(string); ok && strings.HasPrefix(s, "type mismatch") {
+ if s, ok := x.(string); ok &&
+ (strings.Contains(s, "type mismatch") || strings.Contains(s, "not assignable")) {
// x cannot be set to y - ignore this rewrite
return
}