-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
code.go
executable file
·129 lines (112 loc) · 3.2 KB
/
code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package lint
import (
"bufio"
"bytes"
"fmt"
"regexp"
"strings"
"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/lint/code"
"github.com/errata-ai/vale/v3/internal/nlp"
)
func (l *Linter) lintCode(f *core.File) error {
lang, err := code.GetLanguageFromExt(f.RealExt)
if err != nil {
// No tree-sitter grammar available for this file type.
return l.lintCodeOld(f)
}
ignored := l.Manager.Config.IgnoredScopes
comments, err := code.GetComments([]byte(f.Content), lang)
if err != nil {
return err
}
wholeFile := f.Content
last := 0
for _, comment := range comments {
if core.StringInSlice("comment", ignored) {
continue
} else if core.StringInSlice(comment.Scope, ignored) {
continue
}
f.SetText(comment.Text)
err = l.lintLines(f)
if err != nil {
return err
}
size := len(f.Alerts)
if size != last {
f.Alerts = adjustAlerts(f.Alerts, last, comment, lang)
}
last = size
}
f.SetText(wholeFile)
return nil
}
// lintCodeOld lints source code by analyzing its comments.
//
// Deprecated: we now use tree-sitter to parse code and collect comments.
func (l *Linter) lintCodeOld(f *core.File) error {
var line, match, txt string
var lnLength, padding int
var block bytes.Buffer
lines := 0
comments := core.CommentsByNormedExt[f.NormedExt]
if len(comments) == 0 {
return nil
}
scanner := bufio.NewScanner(strings.NewReader(f.Content))
ignored := l.Manager.Config.IgnoredScopes
skipAll := core.StringInSlice("comment", ignored)
skipInline := core.StringInSlice("comment.line", ignored)
skipBlock := core.StringInSlice("comment.block", ignored)
scope := "%s" + f.RealExt
inline := regexp.MustCompile(comments["inline"])
blockStart := regexp.MustCompile(comments["blockStart"])
blockEnd := regexp.MustCompile(comments["blockEnd"])
ignore := false
inBlock := false
scanner.Split(core.SplitLines)
for scanner.Scan() {
line = core.Sanitize(scanner.Text() + "\n")
lnLength = len(line)
lines++
if inBlock {
// We're in a block comment.
if match = blockEnd.FindString(line); len(match) > 0 {
// We've found the end of the block.
block.WriteString(line)
txt = block.String()
b := nlp.NewBlock(
txt, txt, fmt.Sprintf(scope, "text.comment.block"))
if !(skipAll || skipBlock) {
if err := l.lintBlock(f, b, lines+1, 0, true); err != nil {
return err
}
}
block.Reset()
inBlock = false
} else {
block.WriteString(line)
}
} else if match = inline.FindString(line); len(match) > 0 {
// We've found an inline comment. We need padding here in order to
// calculate the column span because, for example, a line like
// 'print("foo") # ...' will be condensed to '# ...'.
padding = lnLength - len(match)
b := nlp.NewBlock(
match, match, fmt.Sprintf(scope, "text.comment.line"))
if !(skipAll || skipInline) {
if err := l.lintBlock(f, b, lines, padding-1, true); err != nil {
return err
}
}
} else if match = blockStart.FindString(line); len(match) > 0 && !ignore {
// We've found the start of a block comment.
block.WriteString(line)
inBlock = true
} else if match = blockEnd.FindString(line); len(match) > 0 {
ignore = !ignore
}
}
return nil
}