Skip to content

Commit 88e8196

Browse files
authored
Update nestif API to provide just pure message (#5)
1 parent 1a5a711 commit 88e8196

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

cmd/nestif/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ import (
2828

2929
var (
3030
flagSet = flag.NewFlagSet("nestif", flag.ContinueOnError)
31-
usage = func() {
31+
32+
usage = func() {
3233
fmt.Fprintln(os.Stderr, "usage: nestif [<flag> ...] <Go files or directories or packages> ...")
3334
flagSet.PrintDefaults()
3435
}
36+
37+
errformat = func(file string, line, col int, msg string) string {
38+
return fmt.Sprintf("%s:%d:%d: %s", file, line, col, msg)
39+
}
3540
)
3641

3742
type app struct {
@@ -234,7 +239,7 @@ func (a *app) write(issues []nestif.Issue) {
234239
if i >= a.top {
235240
return
236241
}
237-
fmt.Fprintln(a.stdout, issue.Message)
242+
fmt.Fprintln(a.stdout, errformat(issue.Pos.Filename, issue.Pos.Line, issue.Pos.Column, issue.Message))
238243
}
239244
}
240245

cmd/nestif/main_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ func TestRun(t *testing.T) {
3131
args: []string{"../../testdata/a.go"},
3232
minComplexity: 1,
3333
top: 10,
34-
want: "../../testdata/a.go:9:2: `if b1` is nested (complexity: 1)\n",
34+
want: "../../testdata/a.go:9:2: `if b1` is deeply nested (complexity: 1)\n",
3535
code: 0,
3636
},
3737
{
3838
name: "show only top 2",
3939
args: []string{"../../testdata/d.go"},
4040
minComplexity: 1,
4141
top: 2,
42-
want: "../../testdata/d.go:16:2: `if b1` is nested (complexity: 3)\n../../testdata/d.go:6:2: `if b1` is nested (complexity: 1)\n",
42+
want: "../../testdata/d.go:16:2: `if b1` is deeply nested (complexity: 3)\n../../testdata/d.go:6:2: `if b1` is deeply nested (complexity: 1)\n",
4343
code: 0,
4444
},
4545
{
4646
name: "show only those with complexity of 2 or more",
4747
args: []string{"../../testdata/d.go"},
4848
minComplexity: 2,
4949
top: 10,
50-
want: "../../testdata/d.go:16:2: `if b1` is nested (complexity: 3)\n",
50+
want: "../../testdata/d.go:16:2: `if b1` is deeply nested (complexity: 3)\n",
5151
code: 0,
5252
},
5353
{
@@ -63,15 +63,15 @@ func TestRun(t *testing.T) {
6363
args: []string{"../../testdata/a"},
6464
minComplexity: 1,
6565
top: 10,
66-
want: "../../testdata/a/a.go:8:2: `if b1` is nested (complexity: 1)\n",
66+
want: "../../testdata/a/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n",
6767
code: 0,
6868
},
6969
{
7070
name: "Check files recursively",
7171
args: []string{"../../testdata/a/..."},
7272
minComplexity: 1,
7373
top: 10,
74-
want: "../../testdata/a/a.go:8:2: `if b1` is nested (complexity: 1)\n../../testdata/a/b/a.go:8:2: `if b1` is nested (complexity: 1)\n",
74+
want: "../../testdata/a/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n../../testdata/a/b/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n",
7575
code: 0,
7676
},
7777
{
@@ -99,7 +99,7 @@ func TestRun(t *testing.T) {
9999
top: 10,
100100
want: func() string {
101101
path, _ := filepath.Abs("../../testdata/a/a.go")
102-
return path + ":8:2: `if b1` is nested (complexity: 1)\n"
102+
return path + ":8:2: `if b1` is deeply nested (complexity: 1)\n"
103103
}(),
104104
code: 0,
105105
},
@@ -109,7 +109,7 @@ func TestRun(t *testing.T) {
109109
args: []string{"../../testdata/a.go"},
110110
minComplexity: 1,
111111
top: 10,
112-
want: "[{\"Pos\":{\"Filename\":\"../../testdata/a.go\",\"Offset\":78,\"Line\":9,\"Column\":2},\"Complexity\":1,\"Message\":\"../../testdata/a.go:9:2: `if b1` is nested (complexity: 1)\"}]\n",
112+
want: "[{\"Pos\":{\"Filename\":\"../../testdata/a.go\",\"Offset\":78,\"Line\":9,\"Column\":2},\"Complexity\":1,\"Message\":\"`if b1` is deeply nested (complexity: 1)\"}]\n",
113113
code: 0,
114114
},
115115
{

nestif.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (c *Checker) checkIf(stmt *ast.IfStmt, fset *token.FileSet) {
7474
c.issues = append(c.issues, Issue{
7575
Pos: pos,
7676
Complexity: v.complexity,
77-
Message: c.makeMessage(pos.Filename, pos.Line, pos.Column, v.complexity, stmt.Cond, fset),
77+
Message: c.makeMessage(v.complexity, stmt.Cond, fset),
7878
})
7979
}
8080

@@ -127,18 +127,13 @@ func (v *visitor) incComplexity(n *ast.IfStmt) {
127127
}
128128
}
129129

130-
func (c *Checker) makeMessage(file string, line, col, complexity int, cond ast.Expr, fset *token.FileSet) string {
130+
func (c *Checker) makeMessage(complexity int, cond ast.Expr, fset *token.FileSet) string {
131131
p := &printer.Config{}
132132
b := new(bytes.Buffer)
133133
if err := p.Fprint(b, fset, cond); err != nil {
134134
c.debug("failed to convert condition into string: %v", err)
135135
}
136-
msg := fmt.Sprintf("`if %s` is nested (complexity: %d)", b.String(), complexity)
137-
return errformat(file, line, col, msg)
138-
}
139-
140-
func errformat(file string, line, col int, msg string) string {
141-
return fmt.Sprintf("%s:%d:%d: %s", file, line, col, msg)
136+
return fmt.Sprintf("`if %s` is deeply nested (complexity: %d)", b.String(), complexity)
142137
}
143138

144139
// DebugMode makes it possible to emit debug logs.

nestif_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestCheck(t *testing.T) {
3636
Column: 2,
3737
},
3838
Complexity: 1,
39-
Message: "./testdata/a.go:9:2: `if b1` is nested (complexity: 1)",
39+
Message: "`if b1` is deeply nested (complexity: 1)",
4040
},
4141
},
4242
},
@@ -53,7 +53,7 @@ func TestCheck(t *testing.T) {
5353
Column: 2,
5454
},
5555
Complexity: 9,
56-
Message: "./testdata/b.go:5:2: `if b1` is nested (complexity: 9)",
56+
Message: "`if b1` is deeply nested (complexity: 9)",
5757
},
5858
},
5959
},
@@ -70,7 +70,7 @@ func TestCheck(t *testing.T) {
7070
Column: 2,
7171
},
7272
Complexity: 4,
73-
Message: "./testdata/c.go:6:2: `if b1` is nested (complexity: 4)",
73+
Message: "`if b1` is deeply nested (complexity: 4)",
7474
},
7575
{
7676
Pos: token.Position{
@@ -80,7 +80,7 @@ func TestCheck(t *testing.T) {
8080
Column: 2,
8181
},
8282
Complexity: 4,
83-
Message: "./testdata/c.go:14:2: `if b1` is nested (complexity: 4)",
83+
Message: "`if b1` is deeply nested (complexity: 4)",
8484
},
8585
},
8686
},

0 commit comments

Comments
 (0)