-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/analysis/internal/checker: make applyFixes work from the first cha…
…racter Make sure modifying the first character of the file takes effect. Fixes golang/go#54774 Change-Id: Ib77231b9bd15f35fe50b2c2d6c7ea260c9c3cba5 GitHub-Last-Rev: b58bbdf GitHub-Pull-Request: #393 Reviewed-on: https://go-review.googlesource.com/c/tools/+/426654 Reviewed-by: Benny Siegert <bsiegert@gmail.com> Reviewed-by: Tim King <taking@google.com>
- Loading branch information
1 parent
c1dd25e
commit bac5507
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2022 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 checker_test | ||
|
||
import ( | ||
"go/ast" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
"golang.org/x/tools/go/analysis/internal/checker" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
"golang.org/x/tools/internal/testenv" | ||
) | ||
|
||
// TestStartFixes make sure modifying the first character | ||
// of the file takes effect. | ||
func TestStartFixes(t *testing.T) { | ||
testenv.NeedsGoPackages(t) | ||
|
||
files := map[string]string{ | ||
"comment/doc.go": `/* Package comment */ | ||
package comment | ||
`} | ||
|
||
want := `// Package comment | ||
package comment | ||
` | ||
|
||
testdata, cleanup, err := analysistest.WriteFiles(files) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
path := filepath.Join(testdata, "src/comment/doc.go") | ||
checker.Fix = true | ||
checker.Run([]string{"file=" + path}, []*analysis.Analyzer{commentAnalyzer}) | ||
|
||
contents, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
got := string(contents) | ||
if got != want { | ||
t.Errorf("contents of rewritten file\ngot: %s\nwant: %s", got, want) | ||
} | ||
|
||
defer cleanup() | ||
} | ||
|
||
var commentAnalyzer = &analysis.Analyzer{ | ||
Name: "comment", | ||
Requires: []*analysis.Analyzer{inspect.Analyzer}, | ||
Run: commentRun, | ||
} | ||
|
||
func commentRun(pass *analysis.Pass) (interface{}, error) { | ||
const ( | ||
from = "/* Package comment */" | ||
to = "// Package comment" | ||
) | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
inspect.Preorder(nil, func(n ast.Node) { | ||
if n, ok := n.(*ast.Comment); ok && n.Text == from { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: n.Pos(), | ||
End: n.End(), | ||
SuggestedFixes: []analysis.SuggestedFix{{ | ||
TextEdits: []analysis.TextEdit{{ | ||
Pos: n.Pos(), | ||
End: n.End(), | ||
NewText: []byte(to), | ||
}}, | ||
}}, | ||
}) | ||
} | ||
}) | ||
|
||
return nil, nil | ||
} |