-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
279 additions
and
225 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,4 @@ | ||
// Package formatter provides functionality for formatting lint issues | ||
// in a human-readable format. It includes various formatters for different | ||
// types of issues and utility functions for text manipulation. | ||
package formatter |
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,49 @@ | ||
package formatter | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gnoswap-labs/lint/internal" | ||
) | ||
|
||
// rule set | ||
const ( | ||
UnnecessaryElse = "unnecessary-else" | ||
) | ||
|
||
// IssueFormatter is the interface that wraps the Format method. | ||
// Implementations of this interface are responsible for formatting specific types of lint issues. | ||
type IssueFormatter interface { | ||
Format(issue internal.Issue, snippet *internal.SourceCode) string | ||
} | ||
|
||
// GetFormatter is a factory function that returns the appropriate IssueFormatter | ||
// based on the given rule. | ||
// If no specific formatter is found for the given rule, it returns a GeneralIssueFormatter. | ||
func GetFormatter(rule string) IssueFormatter { | ||
switch rule { | ||
case UnnecessaryElse: | ||
return &UnnecessaryElseFormatter{} | ||
default: | ||
return &GeneralIssueFormatter{} | ||
} | ||
} | ||
|
||
// FormatIssuesWithArrows formats a slice of issues into a human-readable string. | ||
// It uses the appropriate formatter for each issue based on its rule. | ||
func FormatIssuesWithArrows(issues []internal.Issue, snippet *internal.SourceCode) string { | ||
var builder strings.Builder | ||
for _, issue := range issues { | ||
builder.WriteString(formatIssueHeader(issue)) | ||
formatter := GetFormatter(issue.Rule) | ||
builder.WriteString(formatter.Format(issue, snippet)) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// formatIssueHeader creates a formatted header string for a given issue. | ||
// The header includes the rule and the filename. (e.g. "error: unused-variable\n --> test.go") | ||
func formatIssueHeader(issue internal.Issue) string { | ||
return errorStyle.Sprint("error: ") + ruleStyle.Sprint(issue.Rule) + "\n" + | ||
lineStyle.Sprint(" --> ") + fileStyle.Sprint(issue.Filename) + "\n" | ||
} |
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,78 @@ | ||
package formatter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/gnoswap-labs/lint/internal" | ||
) | ||
|
||
const tabWidth = 8 | ||
|
||
var ( | ||
errorStyle = color.New(color.FgRed, color.Bold) | ||
ruleStyle = color.New(color.FgYellow, color.Bold) | ||
fileStyle = color.New(color.FgCyan, color.Bold) | ||
lineStyle = color.New(color.FgBlue, color.Bold) | ||
messageStyle = color.New(color.FgRed, color.Bold) | ||
) | ||
|
||
// GeneralIssueFormatter is a formatter for general lint issues. | ||
type GeneralIssueFormatter struct{} | ||
|
||
// Format formats a general lint issue into a human-readable string. | ||
// It takes an Issue and a SourceCode snippet as input and returns a formatted string. | ||
func (f *GeneralIssueFormatter) Format( | ||
issue internal.Issue, | ||
snippet *internal.SourceCode, | ||
) string { | ||
var result strings.Builder | ||
|
||
lineNumberStr := fmt.Sprintf("%d", issue.Start.Line) | ||
padding := strings.Repeat(" ", len(lineNumberStr)-1) | ||
result.WriteString(lineStyle.Sprintf(" %s|\n", padding)) | ||
|
||
line := expandTabs(snippet.Lines[issue.Start.Line-1]) | ||
result.WriteString(lineStyle.Sprintf("%d | ", issue.Start.Line)) | ||
result.WriteString(line + "\n") | ||
|
||
visualColumn := calculateVisualColumn(line, issue.Start.Column) | ||
result.WriteString(lineStyle.Sprintf(" %s| ", padding)) | ||
result.WriteString(strings.Repeat(" ", visualColumn)) | ||
result.WriteString(messageStyle.Sprintf("^ %s\n\n", issue.Message)) | ||
|
||
return result.String() | ||
} | ||
|
||
// expandTabs replaces tab characters('\t') with spaces. | ||
// Assuming a table width of 8. | ||
func expandTabs(line string) string { | ||
var expanded strings.Builder | ||
for i, ch := range line { | ||
if ch == '\t' { | ||
spaceCount := tabWidth - (i % tabWidth) | ||
expanded.WriteString(strings.Repeat(" ", spaceCount)) | ||
} else { | ||
expanded.WriteRune(ch) | ||
} | ||
} | ||
return expanded.String() | ||
} | ||
|
||
// calculateVisualColumn calculates the visual column position | ||
// in a string. taking into account tab characters. | ||
func calculateVisualColumn(line string, column int) int { | ||
visualColumn := 0 | ||
for i, ch := range line { | ||
if i+1 == column { | ||
break | ||
} | ||
if ch == '\t' { | ||
visualColumn += tabWidth - (visualColumn % tabWidth) | ||
} else { | ||
visualColumn++ | ||
} | ||
} | ||
return visualColumn | ||
} |
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,42 @@ | ||
package formatter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gnoswap-labs/lint/internal" | ||
) | ||
|
||
// UnnecessaryElseFormatter is a formatter specifically designed for the "unnecessary-else" rule. | ||
type UnnecessaryElseFormatter struct{} | ||
|
||
func (f *UnnecessaryElseFormatter) Format( | ||
issue internal.Issue, | ||
snippet *internal.SourceCode, | ||
) string { | ||
var result strings.Builder | ||
ifStartLine, elseEndLine := issue.Start.Line-2, issue.End.Line | ||
maxLineNumberStr := fmt.Sprintf("%d", elseEndLine) | ||
padding := strings.Repeat(" ", len(maxLineNumberStr)-1) | ||
|
||
result.WriteString(lineStyle.Sprintf(" %s|\n", padding)) | ||
|
||
maxLen := 0 | ||
for i := ifStartLine; i <= elseEndLine; i++ { | ||
if len(snippet.Lines[i-1]) > maxLen { | ||
maxLen = len(snippet.Lines[i-1]) | ||
} | ||
line := expandTabs(snippet.Lines[i-1]) | ||
lineNumberStr := fmt.Sprintf("%d", i) | ||
linePadding := strings.Repeat(" ", len(maxLineNumberStr)-len(lineNumberStr)) | ||
result.WriteString(lineStyle.Sprintf("%s%s | ", linePadding, lineNumberStr)) | ||
result.WriteString(line + "\n") | ||
} | ||
|
||
result.WriteString(lineStyle.Sprintf(" %s| ", padding)) | ||
result.WriteString(messageStyle.Sprintf("%s\n", strings.Repeat("~", maxLen))) | ||
result.WriteString(lineStyle.Sprintf(" %s| ", padding)) | ||
result.WriteString(messageStyle.Sprintf("%s\n\n", issue.Message)) | ||
|
||
return result.String() | ||
} |
Oops, something went wrong.