From f3f77bb387858bcd7e11b4e48a69f2d576cf4bf0 Mon Sep 17 00:00:00 2001 From: Hazhir Derakhshi Date: Sat, 17 May 2025 18:33:14 +0100 Subject: [PATCH] refactor: remove tablewriter dependency (#1351) * Rewrite Friendly.table method to replace tablewriter with fmt.Fprintf * Refactor Stylish formatter to use custom table rendering instead of tablewriter * Refactor Friendly and Stylish formatters to utilize a new custom table rendering function * Remove unused dependencies from go.mod and go.sum * Refactor table formatting by replacing formatTable with a new table function in friendly.go * Utilize text/tabwriter in fromatting a table * Refactor table function to use bytes.Buffer for improved performance --- formatter/friendly.go | 30 +++++++++++++------------- formatter/friendly_test.go | 43 ++++++++++++++++++++++++++++++++++++++ formatter/stylish.go | 12 +---------- go.mod | 3 --- go.sum | 8 ------- 5 files changed, 60 insertions(+), 36 deletions(-) diff --git a/formatter/friendly.go b/formatter/friendly.go index 6ba9ade..3b7bea0 100644 --- a/formatter/friendly.go +++ b/formatter/friendly.go @@ -7,10 +7,10 @@ import ( "io" "slices" "strings" + "text/tabwriter" "github.com/fatih/color" "github.com/mgechev/revive/lint" - "github.com/olekukonko/tablewriter" ) func getErrorEmoji() string { @@ -64,12 +64,12 @@ func (f *Friendly) printFriendlyFailure(sb *strings.Builder, failure lint.Failur sb.WriteString("\n\n") } -func (f *Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) { +func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) { emoji := getWarningEmoji() if severity == lint.SeverityError { emoji = getErrorEmoji() } - sb.WriteString(f.table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}})) + sb.WriteString(table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}})) } func (*Friendly) printFilePosition(sb *strings.Builder, failure lint.Failure) { @@ -109,7 +109,7 @@ func (*Friendly) printSummary(w io.Writer, errors, warnings int) { } } -func (f *Friendly) printStatistics(w io.Writer, header string, stats map[string]int) { +func (*Friendly) printStatistics(w io.Writer, header string, stats map[string]int) { if len(stats) == 0 { return } @@ -125,17 +125,19 @@ func (f *Friendly) printStatistics(w io.Writer, header string, stats map[string] formatted = append(formatted, []string{color.GreenString(fmt.Sprintf("%d", entry.failures)), entry.name}) } fmt.Fprintln(w, header) - fmt.Fprintln(w, f.table(formatted)) + fmt.Fprintln(w, table(formatted)) } -func (*Friendly) table(rows [][]string) string { - buf := new(bytes.Buffer) - table := tablewriter.NewWriter(buf) - table.SetBorder(false) - table.SetColumnSeparator("") - table.SetRowSeparator("") - table.SetAutoWrapText(false) - table.AppendBulk(rows) - table.Render() +func table(rows [][]string) string { + var buf bytes.Buffer + tw := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0) + for _, row := range rows { + tw.Write([]byte{'\t'}) + for _, col := range row { + tw.Write(append([]byte(col), '\t')) + } + tw.Write([]byte{'\n'}) + } + tw.Flush() return buf.String() } diff --git a/formatter/friendly_test.go b/formatter/friendly_test.go index e0deb90..5684b2f 100644 --- a/formatter/friendly_test.go +++ b/formatter/friendly_test.go @@ -31,6 +31,11 @@ func TestFriendly_printStatistics(t *testing.T) { stats: map[string]int{"rule2": 2, "rule1": 1, "rule3": 3}, expected: "Warnings:\n 3 rule3 \n 2 rule2 \n 1 rule1 \n\n", }, + { + name: "multiple stats with different length sorted by failures desc", + stats: map[string]int{"rule2": 2, "rule1": 1, "rule3": 3, "rule100": 40}, + expected: "Warnings:\n 40 rule100 \n 3 rule3 \n 2 rule2 \n 1 rule1 \n\n", + }, } for _, tt := range tests { @@ -44,3 +49,41 @@ func TestFriendly_printStatistics(t *testing.T) { }) } } + +func TestFriendly_table(t *testing.T) { + tests := []struct { + name string + input [][]string + expected string + }{ + { + name: "empty input", + input: [][]string{}, + expected: "", + }, + { + name: "single row", + input: [][]string{{"1", "2", "3"}}, + expected: " 1 2 3 \n", + }, + { + name: "multiple rows", + input: [][]string{{"1", "2", "3"}, {"4", "5", "6"}}, + expected: " 1 2 3 \n 4 5 6 \n", + }, + { + name: "multiple rows with different column lengths", + input: [][]string{{"1", "22", "3"}, {"4", "5", "6"}}, + expected: " 1 22 3 \n 4 5 6 \n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := table(tt.input) + if got != tt.expected { + t.Errorf("got %q, want %q", got, tt.expected) + } + }) + } +} diff --git a/formatter/stylish.go b/formatter/stylish.go index bb3d7cd..fce5baa 100644 --- a/formatter/stylish.go +++ b/formatter/stylish.go @@ -1,12 +1,10 @@ package formatter import ( - "bytes" "fmt" "github.com/fatih/color" "github.com/mgechev/revive/lint" - "github.com/olekukonko/tablewriter" ) // Stylish is an implementation of the Formatter interface @@ -63,17 +61,9 @@ func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string output := "" for filename, val := range fileReport { - buf := new(bytes.Buffer) - table := tablewriter.NewWriter(buf) - table.SetBorder(false) - table.SetColumnSeparator("") - table.SetRowSeparator("") - table.SetAutoWrapText(false) - table.AppendBulk(val) - table.Render() c := color.New(color.Underline) output += c.SprintfFunc()(filename + "\n") - output += buf.String() + "\n" + output += table(val) + "\n" } suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors) diff --git a/go.mod b/go.mod index 07dfe1c..9f968d7 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/fatih/structtag v1.2.0 github.com/hashicorp/go-version v1.7.0 github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 - github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/afero v1.14.0 golang.org/x/mod v0.24.0 golang.org/x/sync v0.14.0 @@ -20,9 +19,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/sys v0.33.0 // indirect golang.org/x/text v0.25.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 216e804..5513451 100644 --- a/go.sum +++ b/go.sum @@ -14,18 +14,10 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0= github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=