-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The current report may be a bit confusing. if there are more than one fix suggestion, there will be multiple reports, where the first one contain only partial fix, and only the last one contains the final fix. To solve it, for each expression, there will be only one report, containing all the errors in the text, and only one fix suggestion, containing all the fixes.
- Loading branch information
Showing
55 changed files
with
1,918 additions
and
1,766 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,97 @@ | ||
package reports | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/printer" | ||
"go/token" | ||
"golang.org/x/tools/go/analysis" | ||
"strings" | ||
) | ||
|
||
type Builder struct { | ||
pos token.Pos | ||
end token.Pos | ||
oldExpr string | ||
issues []string | ||
fixOffer string | ||
suggestFix bool | ||
} | ||
|
||
func NewBuilder(fset *token.FileSet, oldExpr ast.Expr) *Builder { | ||
b := &Builder{ | ||
pos: oldExpr.Pos(), | ||
end: oldExpr.End(), | ||
oldExpr: goFmt(fset, oldExpr), | ||
suggestFix: false, | ||
} | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) AddIssue(suggestFix bool, issue string, args ...any) { | ||
if len(args) > 0 { | ||
issue = fmt.Sprintf(issue, args...) | ||
} | ||
b.issues = append(b.issues, issue) | ||
|
||
if suggestFix { | ||
b.suggestFix = true | ||
} | ||
} | ||
|
||
func (b *Builder) SetFixOffer(fset *token.FileSet, fixOffer ast.Expr) { | ||
if offer := goFmt(fset, fixOffer); offer != b.oldExpr { | ||
b.fixOffer = offer | ||
} | ||
} | ||
|
||
func (b *Builder) HasReport() bool { | ||
return len(b.issues) > 0 | ||
} | ||
|
||
func (b *Builder) Build() analysis.Diagnostic { | ||
diagnostic := analysis.Diagnostic{ | ||
Pos: b.pos, | ||
Message: b.getMessage(), | ||
} | ||
|
||
if b.suggestFix && len(b.fixOffer) > 0 { | ||
diagnostic.SuggestedFixes = []analysis.SuggestedFix{ | ||
{ | ||
Message: fmt.Sprintf("should replace %s with %s", b.oldExpr, b.fixOffer), | ||
TextEdits: []analysis.TextEdit{ | ||
{ | ||
Pos: b.pos, | ||
End: b.end, | ||
NewText: []byte(b.fixOffer), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
return diagnostic | ||
} | ||
|
||
func goFmt(fset *token.FileSet, x ast.Expr) string { | ||
var b bytes.Buffer | ||
_ = printer.Fprint(&b, fset, x) | ||
return b.String() | ||
} | ||
|
||
func (b *Builder) getMessage() string { | ||
sb := strings.Builder{} | ||
sb.WriteString("ginkgo-linter: ") | ||
if len(b.issues) > 1 { | ||
sb.WriteString("multiple issues: ") | ||
} | ||
sb.WriteString(strings.Join(b.issues, "; ")) | ||
|
||
if b.suggestFix && len(b.fixOffer) != 0 { | ||
sb.WriteString(fmt.Sprintf(". Consider using `%s` instead", b.fixOffer)) | ||
} | ||
|
||
return sb.String() | ||
} |
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
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
Oops, something went wrong.