forked from bazelbuild/buildtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarn_docstring.go
400 lines (345 loc) · 11.2 KB
/
warn_docstring.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package warn
import (
"fmt"
"regexp"
"strings"
"github.com/bazelbuild/buildtools/build"
)
// FunctionLengthDocstringThreshold is a limit for a function size (in statements), above which
// a public function is required to have a docstring.
const FunctionLengthDocstringThreshold = 5
// getDocstring returns a docstring of the statements and true if it exists.
// Otherwise it returns the first non-comment statement and false.
func getDocstring(stmts []build.Expr) (*build.Expr, bool) {
for i, stmt := range stmts {
if stmt == nil {
continue
}
switch stmt.(type) {
case *build.CommentBlock:
continue
case *build.StringExpr:
return &stmts[i], true
default:
return &stmts[i], false
}
}
return nil, false
}
func moduleDocstringWarning(f *build.File) []*LinterFinding {
if f.Type != build.TypeDefault && f.Type != build.TypeBzl {
return nil
}
if stmt, ok := getDocstring(f.Stmt); stmt != nil && !ok {
start, _ := (*stmt).Span()
end := build.Position{
Line: start.Line,
LineRune: start.LineRune + 1,
Byte: start.Byte + 1,
}
finding := makeLinterFinding(*stmt, `The file has no module docstring.
A module docstring is a string literal (not a comment) which should be the first statement of a file (it may follow comment lines).`)
finding.End = end
return []*LinterFinding{finding}
}
return nil
}
func stmtsCount(stmts []build.Expr) int {
result := 0
for _, stmt := range stmts {
result++
switch stmt := stmt.(type) {
case *build.IfStmt:
result += stmtsCount(stmt.True)
result += stmtsCount(stmt.False)
case *build.ForStmt:
result += stmtsCount(stmt.Body)
}
}
return result
}
// docstringInfo contains information about a function docstring
type docstringInfo struct {
hasHeader bool // whether the docstring has a one-line header
args map[string]build.Position // map of documented arguments, the values are line numbers
returns bool // whether the return value is documented
deprecated bool // whether the function is marked as deprecated
argumentsPos build.Position // line of the `Arguments:` block (not `Args:`), if it exists
}
// countLeadingSpaces returns the number of leading spaces of a string.
func countLeadingSpaces(s string) int {
spaces := 0
for _, c := range s {
if c == ' ' {
spaces++
} else {
break
}
}
return spaces
}
var argRegex = regexp.MustCompile(`^ *(\*?\*?\w*)( *\([\w\ ,<>\[\]]+\))?:`)
// parseFunctionDocstring parses a function docstring and returns a docstringInfo object containing
// the parsed information about the function, its arguments and its return value.
func parseFunctionDocstring(doc *build.StringExpr) docstringInfo {
start, _ := doc.Span()
indent := start.LineRune - 1
prefix := strings.Repeat(" ", indent)
lines := strings.Split(doc.Value, "\n")
// Trim "/r" in the end of the lines to parse CRLF-formatted files correctly
for i, line := range lines {
lines[i] = strings.TrimRight(line, "\r")
}
info := docstringInfo{}
info.args = make(map[string]build.Position)
isArgumentsDescription := false // Whether the currently parsed block is an 'Args:' section
argIndentation := 1000000 // Indentation at which previous arg documentation started
for i := range lines {
lines[i] = strings.TrimRight(lines[i], " ")
}
// The first non-empty line should be a single-line header
for i, line := range lines {
if line == "" {
continue
}
if i == len(lines)-1 || lines[i+1] == "" {
info.hasHeader = true
}
break
}
// Search for Args: and Returns: sections
for i, line := range lines {
switch line {
case prefix + "Arguments:":
info.argumentsPos = build.Position{
Line: start.Line + i,
LineRune: indent,
}
isArgumentsDescription = true
continue
case prefix + "Args:":
isArgumentsDescription = true
continue
case prefix + "Returns:":
isArgumentsDescription = false
info.returns = true
continue
case prefix + "Deprecated:":
isArgumentsDescription = false
info.deprecated = true
continue
}
if isArgumentsDescription {
newIndentation := countLeadingSpaces(line)
if line != "" && newIndentation <= indent {
// The indented block is over
isArgumentsDescription = false
continue
} else if newIndentation > argIndentation {
// Continuation of the previous argument description
continue
} else {
// Maybe a new argument is described here
result := argRegex.FindStringSubmatch(line)
if len(result) > 1 {
argIndentation = newIndentation
info.args[result[1]] = build.Position{
Line: start.Line + i,
LineRune: indent + argIndentation,
}
}
}
}
}
return info
}
func hasReturnValues(def *build.DefStmt) bool {
result := false
build.WalkStatements(def, func(expr build.Expr, stack []build.Expr) (err error) {
if _, ok := expr.(*build.DefStmt); ok && len(stack) > 0 {
// Don't go into inner function definitions
return &build.StopTraversalError{}
}
ret, ok := expr.(*build.ReturnStmt)
if ok && ret.Result != nil {
result = true
}
return
})
return result
}
// isDocstringRequired returns whether a function is required to has a docstring.
// A docstring is required for public functions if they are long enough (at least 5 statements)
func isDocstringRequired(def *build.DefStmt) bool {
if start, _ := def.Span(); start.LineRune > 1 {
// Nested functions don't require docstrings
return false
}
return !strings.HasPrefix(def.Name, "_") && stmtsCount(def.Body) >= FunctionLengthDocstringThreshold
}
func functionDocstringWarning(f *build.File) []*LinterFinding {
var findings []*LinterFinding
// Docstrings are required only for top-level functions
for _, stmt := range f.Stmt {
def, ok := stmt.(*build.DefStmt)
if !ok {
continue
}
if !isDocstringRequired(def) {
continue
}
if _, ok = getDocstring(def.Body); ok {
continue
}
message := fmt.Sprintf(`The function %q has no docstring.
A docstring is a string literal (not a comment) which should be the first statement of a function body (it may follow comment lines).`, def.Name)
finding := makeLinterFinding(def, message)
finding.End = def.ColonPos
findings = append(findings, finding)
}
return findings
}
func functionDocstringHeaderWarning(f *build.File) []*LinterFinding {
var findings []*LinterFinding
build.WalkStatements(f, func(expr build.Expr, stack []build.Expr) (err error) {
def, ok := expr.(*build.DefStmt)
if !ok {
return
}
doc, ok := getDocstring(def.Body)
if !ok {
return
}
info := parseFunctionDocstring((*doc).(*build.StringExpr))
if !info.hasHeader {
message := fmt.Sprintf("The docstring for the function %q should start with a one-line summary.", def.Name)
findings = append(findings, makeLinterFinding(*doc, message))
}
return
})
return findings
}
func functionDocstringArgsWarning(f *build.File) []*LinterFinding {
var findings []*LinterFinding
build.WalkStatements(f, func(expr build.Expr, stack []build.Expr) (err error) {
def, ok := expr.(*build.DefStmt)
if !ok {
return
}
doc, ok := getDocstring(def.Body)
if !ok {
return
}
info := parseFunctionDocstring((*doc).(*build.StringExpr))
if info.argumentsPos.LineRune > 0 {
argumentsEnd := info.argumentsPos
argumentsEnd.LineRune += len("Arguments:")
argumentsEnd.Byte += len("Arguments:")
finding := makeLinterFinding(*doc, `Prefer "Args:" to "Arguments:" when documenting function arguments.`)
finding.Start = info.argumentsPos
finding.End = argumentsEnd
findings = append(findings, finding)
}
if !isDocstringRequired(def) && len(info.args) == 0 {
return
}
// If a docstring is required or there are any arguments described, check for their integrity.
// Check whether all arguments are documented.
notDocumentedArguments := []string{}
paramNames := make(map[string]bool)
for _, param := range def.Params {
name, op := build.GetParamName(param)
if name == "" {
continue
}
name = op + name // *args or **kwargs
paramNames[name] = true
if _, ok := info.args[name]; !ok {
notDocumentedArguments = append(notDocumentedArguments, name)
}
}
// Check whether all existing arguments are commented
if len(notDocumentedArguments) > 0 {
message := fmt.Sprintf("Argument %q is not documented.", notDocumentedArguments[0])
plural := ""
if len(notDocumentedArguments) > 1 {
message = fmt.Sprintf(
`Arguments "%s" are not documented.`,
strings.Join(notDocumentedArguments, `", "`),
)
plural = "s"
}
if len(info.args) == 0 {
// No arguments are documented maybe the Args: block doesn't exist at all or
// formatted improperly. Add extra information to the warning message
message += fmt.Sprintf(`
If the documentation for the argument%s exists but is not recognized by Buildifier
make sure it follows the line "Args:" which has the same indentation as the opening """,
and the argument description starts with "<argument_name>:" and indented with at least
one (preferably two) space more than "Args:", for example:
def %s(%s):
"""Function description.
Args:
%s: argument description, can be
multiline with additional indentation.
"""`, plural, def.Name, notDocumentedArguments[0], notDocumentedArguments[0])
}
findings = append(findings, makeLinterFinding(*doc, message))
}
// Check whether all documented arguments actually exist in the function signature.
for name, pos := range info.args {
if paramNames[name] {
continue
}
msg := fmt.Sprintf("Argument %q is documented but doesn't exist in the function signature.", name)
// *args and **kwargs should be documented with asterisks
for _, asterisks := range []string{"*", "**"} {
if paramNames[asterisks+name] {
msg += fmt.Sprintf(` Do you mean "%s%s"?`, asterisks, name)
break
}
}
posEnd := pos
posEnd.LineRune += len(name)
finding := makeLinterFinding(*doc, msg)
finding.Start = pos
finding.End = posEnd
findings = append(findings, finding)
}
return
})
return findings
}
func functionDocstringReturnWarning(f *build.File) []*LinterFinding {
var findings []*LinterFinding
build.WalkStatements(f, func(expr build.Expr, stack []build.Expr) (err error) {
def, ok := expr.(*build.DefStmt)
if !ok {
return
}
doc, ok := getDocstring(def.Body)
if !ok {
return
}
info := parseFunctionDocstring((*doc).(*build.StringExpr))
// Check whether the return value is documented
if isDocstringRequired(def) && hasReturnValues(def) && !info.returns {
message := fmt.Sprintf("Return value of %q is not documented.", def.Name)
findings = append(findings, makeLinterFinding(*doc, message))
}
return
})
return findings
}