Skip to content

Commit c378c73

Browse files
committed
go/analysis/internal/checker: applyFixes the first character to work
Make sure modifying the first character of the file takes effect Fixes golang/go#54774
1 parent d815cba commit c378c73

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

go/analysis/internal/checker/checker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ func applyFixes(roots []*action) {
408408
if edit.start > cur {
409409
out.Write(contents[cur:edit.start])
410410
out.Write(edit.newText)
411+
} else if cur == 0 && edit.start == 0 { // edit starts at first character?
412+
out.Write(edit.newText)
411413
}
412414
cur = edit.end
413415

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2022 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 checker_test
6+
7+
import (
8+
"go/ast"
9+
"io/ioutil"
10+
"path/filepath"
11+
"strings"
12+
"testing"
13+
14+
"golang.org/x/tools/go/analysis"
15+
"golang.org/x/tools/go/analysis/analysistest"
16+
"golang.org/x/tools/go/analysis/internal/checker"
17+
"golang.org/x/tools/go/analysis/passes/inspect"
18+
"golang.org/x/tools/go/ast/inspector"
19+
"golang.org/x/tools/internal/testenv"
20+
)
21+
22+
// TestStartFixes make sure modifying the first character
23+
// of the file takes effect
24+
func TestStartFixes(t *testing.T) {
25+
testenv.NeedsGoPackages(t)
26+
27+
files := map[string]string{
28+
"comment/doc.go": `/* Package comment */
29+
package comment
30+
`}
31+
32+
want := `// Package comment
33+
package comment
34+
`
35+
36+
testdata, cleanup, err := analysistest.WriteFiles(files)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
path := filepath.Join(testdata, "src/comment/doc.go")
41+
checker.Fix = true
42+
checker.Run([]string{"file=" + path}, []*analysis.Analyzer{commentAnalyzer})
43+
44+
contents, err := ioutil.ReadFile(path)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
got := string(contents)
50+
if got != want {
51+
t.Errorf("contents of rewritten file\ngot: %s\nwant: %s", got, want)
52+
}
53+
54+
defer cleanup()
55+
}
56+
57+
var commentAnalyzer = &analysis.Analyzer{
58+
Name: "comment",
59+
Requires: []*analysis.Analyzer{inspect.Analyzer},
60+
Run: commentRun,
61+
}
62+
63+
func commentRun(pass *analysis.Pass) (interface{}, error) {
64+
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
65+
inspect.Preorder(nil, func(n ast.Node) {
66+
if n, ok := n.(*ast.Comment); ok {
67+
if strings.HasPrefix(n.Text, "/*") && strings.HasSuffix(n.Text, "*/") {
68+
new := "// " + strings.TrimSuffix(strings.TrimPrefix(n.Text, "/*"), "*/")
69+
pass.Report(analysis.Diagnostic{
70+
Pos: n.Pos(),
71+
End: n.End(),
72+
Message: "change /* */ to //",
73+
SuggestedFixes: []analysis.SuggestedFix{{
74+
Message: "change /* */ to //",
75+
TextEdits: []analysis.TextEdit{{
76+
Pos: n.Pos(),
77+
End: n.End(),
78+
NewText: []byte(new),
79+
}},
80+
}},
81+
})
82+
}
83+
}
84+
})
85+
86+
return nil, nil
87+
}

0 commit comments

Comments
 (0)