Skip to content

Commit

Permalink
chore: get lint passing new golangci config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Aug 28, 2023
1 parent ded0acf commit 287ae02
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 122 deletions.
14 changes: 9 additions & 5 deletions internal/lint/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,32 @@ func suggest(alert core.Alert, cfg *core.Config) ([]string, error) {
return suggestions, err
}
}
rule := mgr.Rules()[alert.Check].(check.Spelling)

rule, ok := mgr.Rules()[alert.Check].(check.Spelling)
if !ok {
return suggestions, errors.New("unknown check")
}

return rule.Suggest(alert.Match), nil
}

func replace(alert core.Alert, cfg *core.Config) ([]string, error) {
func replace(alert core.Alert, _ *core.Config) ([]string, error) {
return alert.Action.Params, nil
}

func remove(alert core.Alert, cfg *core.Config) ([]string, error) {
func remove(_ core.Alert, _ *core.Config) ([]string, error) {
return []string{""}, nil
}

func convert(alert core.Alert, cfg *core.Config) ([]string, error) {
func convert(alert core.Alert, _ *core.Config) ([]string, error) {
match := alert.Match
if alert.Action.Params[0] == "simple" {
match = nlp.Simple(match)
}
return []string{match}, nil
}

func edit(alert core.Alert, cfg *core.Config) ([]string, error) {
func edit(alert core.Alert, _ *core.Config) ([]string, error) {
match := alert.Match

switch name := alert.Action.Params[0]; name {
Expand Down
10 changes: 5 additions & 5 deletions internal/lint/adoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (l *Linter) lintADoc(f *core.File) error {
if err != nil {
return core.NewE100(f.Path, err)
}
} else if err := l.startAdocServer(exe, attrs); err != nil {
} else if err = l.startAdocServer(exe, attrs); err != nil {
html, err = callAdoc(f, s, exe, attrs)
if err != nil {
return core.NewE100(f.Path, err)
Expand Down Expand Up @@ -167,11 +167,11 @@ func (l *Linter) startAdocServer(exe string, attrs map[string]string) error {
)

tmpfile, _ := os.CreateTemp("", "server.*.rb")
if _, err := tmpfile.WriteString(adocServer); err != nil {
if _, err = tmpfile.WriteString(adocServer); err != nil {
return err
}

if err := tmpfile.Close(); err != nil {
if err = tmpfile.Close(); err != nil {
return err
}

Expand All @@ -180,14 +180,14 @@ func (l *Linter) startAdocServer(exe string, attrs map[string]string) error {
"GEM_HOME="+home,
)

if err := cmd.Start(); err != nil {
if err = cmd.Start(); err != nil {
return err
}

l.pids = append(l.pids, cmd.Process.Pid)
l.temps = append(l.temps, tmpfile)

if err := ping(adocDomain); err != nil {
if err = ping(adocDomain); err != nil {
return err
}

Expand Down
27 changes: 3 additions & 24 deletions internal/lint/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var tagToScope = map[string]string{
"figcaption": "text.figure.caption",
}

func (l *Linter) lintHTMLTokens(f *core.File, raw []byte, offset int) error {
func (l *Linter) lintHTMLTokens(f *core.File, raw []byte, offset int) error { //nolint:unparam
var class, parentClass, attr string
var inBlock, inline, skip, skipClass bool

Expand Down Expand Up @@ -60,7 +60,7 @@ func (l *Linter) lintHTMLTokens(f *core.File, raw []byte, offset int) error {
skipClass = checkClasses(class, skipClasses)

blockSkip := skipClass && !core.StringInSlice(txt, inlineTags)
if tokt == html.ErrorToken {
if tokt == html.ErrorToken { //nolint:gocritic
break
} else if tokt == html.StartTagToken && (core.StringInSlice(txt, skipTags) || blockSkip) {
walker.setCls(txt, blockSkip)
Expand Down Expand Up @@ -141,7 +141,7 @@ func (l *Linter) lintScope(f *core.File, state *walker, txt string) error {
if !match {
scope = "text.heading." + tag
}
f.Metrics[strings.TrimPrefix(scope, "text.")] += 1
f.Metrics[strings.TrimPrefix(scope, "text.")]++

txt = strings.TrimLeft(txt, " ")
b := state.block(txt, scope+f.RealExt)
Expand Down Expand Up @@ -238,24 +238,3 @@ func getAttribute(tok html.Token, key string) string {
}
return ""
}

func getScope(tags []string, new, ext string) string {
if len(tags) == 0 {
tags = []string{"p"}
}

root := tags[0]
if root == "ul" {
root = "li"
} else if root == "p" {
root = ""
}

scope, match := tagToScope[root]
if !match && heading.MatchString(root) {
scope = "text.heading." + root
}

ctx := strings.Join([]string{scope, new}, ".")
return strings.TrimPrefix(ctx, ".") + ext
}
14 changes: 2 additions & 12 deletions internal/lint/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,6 @@ var patterns = map[string]map[string][]*regexp.Regexp{
},
}

func toMarkup(comments []Comment) string {
var markup bytes.Buffer

for _, comment := range comments {
markup.WriteString(strings.TrimLeft(comment.Text, " "))
}

return markup.String()
}

func getSubMatch(r *regexp.Regexp, s string) string {
matches := r.FindStringSubmatch(s)
for i, m := range matches {
Expand Down Expand Up @@ -215,12 +205,12 @@ func getComments(content, ext string) []Comment {
Offset: strings.Index(line, match),
Scope: "text.comment.line",
})
} else if match := doMatch(byLang["blockStart"], line); len(match) > 0 && !ignore {
} else if match = doMatch(byLang["blockStart"], line); len(match) > 0 && !ignore {
// We've found the start of a block comment.
block.WriteString(match)
start = lines
inBlock = true
} else if match := doMatch(byLang["blockEnd"], line); len(match) > 0 {
} else if match = doMatch(byLang["blockEnd"], line); len(match) > 0 {
ignore = !ignore
}
}
Expand Down
24 changes: 18 additions & 6 deletions internal/lint/comments_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package lint

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)

func toMarkup(comments []Comment) string {
var markup bytes.Buffer

for _, comment := range comments {
markup.WriteString(strings.TrimLeft(comment.Text, " "))
}

return markup.String()
}

func TestComments(t *testing.T) {
cases, err := os.ReadDir("../../testdata/comments/in")
if err != nil {
t.Error(err)
}

for i, f := range cases {
b, err := os.ReadFile(fmt.Sprintf("../../testdata/comments/in/%s", f.Name()))
if err != nil {
t.Error(err)
b, err1 := os.ReadFile(fmt.Sprintf("../../testdata/comments/in/%s", f.Name()))
if err1 != nil {
t.Error(err1)
}
comments := getComments(string(b), filepath.Ext(f.Name()))

b2, err := os.ReadFile(fmt.Sprintf("../../testdata/comments/out/%d.txt", i))
if err != nil {
t.Error(err)
b2, err2 := os.ReadFile(fmt.Sprintf("../../testdata/comments/out/%d.txt", i))
if err2 != nil {
t.Error(err2)
}
markup := toMarkup(comments)

Expand Down
2 changes: 1 addition & 1 deletion internal/lint/dita.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (l Linter) lintDITA(file *core.File) error {
}...)
cmd.Stderr = &out

if err := cmd.Run(); err != nil {
if err = cmd.Run(); err != nil {
return core.NewE100(file.Path, err)
}

Expand Down
23 changes: 1 addition & 22 deletions internal/lint/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,6 @@
Package lint implements Vale's syntax-aware linting functionality.
The package is split into core linting logic (this file), source code
(code.go), and markup (markup.go). The general flow is as follows:
Lint (files and directories) LintString (stdin)
\ /
lintFiles /
\ /
+ +
+-------------------+ lintFile ------+|lintMarkdown|lintADoc|lintRST
| / | \ | | /
| / | \ | / /
| / | \ | +--------
| / | \ | /
| + + + + +
| lintCode lintLines lintHTML
| | | |
| | | +
| \ | lintProse
| \ | /
| + + +
| lintText
| <= add Alerts{} |
+-------------------------+
(code.go), and markup (markup.go).
*/
package lint
2 changes: 1 addition & 1 deletion internal/lint/fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func coalesce(comments []Comment) []Comment {

buf := bytes.Buffer{}
for i, comment := range comments {
if comment.Scope == "text.comment.block" {
if comment.Scope == "text.comment.block" { //nolint:gocritic
joined = append(joined, comment)
} else if i == 0 || comments[i-1].Line != comment.Line-1 {
if buf.Len() > 0 {
Expand Down
21 changes: 14 additions & 7 deletions internal/lint/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lint

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -38,10 +39,10 @@ func (l *Linter) applyPatterns(content, block, inline, ext string) (string, erro
return s, err
} else if sec.Match(ext) {
for _, r := range regexes {
pat, err := regexp2.CompileStd(r)
if err != nil {
pat, errc := regexp2.CompileStd(r)
if errc != nil { //nolint:gocritic
return s, core.NewE201FromTarget(
err.Error(),
errc.Error(),
r,
l.Manager.Config.Flags.Path,
)
Expand Down Expand Up @@ -71,10 +72,10 @@ func (l *Linter) applyPatterns(content, block, inline, ext string) (string, erro
return s, err
} else if sec.Match(ext) {
for _, r := range regexes {
pat, err := regexp2.CompileStd(r)
if err != nil {
pat, errc := regexp2.CompileStd(r)
if errc != nil {
return s, core.NewE201FromTarget(
err.Error(),
errc.Error(),
r,
l.Manager.Config.Flags.Path,
)
Expand All @@ -95,10 +96,16 @@ func (l *Linter) applyPatterns(content, block, inline, ext string) (string, erro
}

func (l *Linter) post(f *core.File, text, url string) (string, error) {
req, err := http.NewRequest("POST", url, bytes.NewBufferString(text))
req, err := http.NewRequestWithContext(
context.Background(),
"POST",
url,
bytes.NewBufferString(text))

if err != nil {
return "", core.NewE100(f.Path, err)
}

req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Accept", "text/plain")

Expand Down
Loading

0 comments on commit 287ae02

Please sign in to comment.