-
Notifications
You must be signed in to change notification settings - Fork 8
/
no-unknown-sections.js
74 lines (67 loc) · 2 KB
/
no-unknown-sections.js
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
var rule = require('unified-lint-rule')
var position = require('unist-util-position')
var stringify = require('unist-util-stringify-position')
var schema = require('../util/schema')
module.exports = rule('standard-readme:no-unknown-sections', noUnknownSections)
var head = [
'table-of-contents',
'security',
'background',
'install',
'usage'
]
var tail = [
'api',
'maintainer',
'maintainers',
'contributing',
'license'
]
function noUnknownSections (ast, file) {
var sections = schema(ast)
var length = sections.length
var index = -1
var state = 'head'
var customPosition
var tailStart
var inHead
var inTail
var section
while (++index < length) {
section = sections[index]
inHead = head.indexOf(section.id) !== -1
inTail = tail.indexOf(section.id) !== -1
if (state === 'head') {
if (!inHead) {
if (inTail) {
state = 'tail'
tailStart = position.start(section.node)
} else {
state = 'custom'
customPosition = { start: position.start(section.node) }
}
}
} else if (state === 'tail') {
if (!inTail) {
if (customPosition) {
file.message('Unexpected unknown heading in tail: move it to the extra sections (' + stringify(customPosition) + ')', section.node)
} else {
file.message('Unexpected unknown section in tail: move it in front of the tail (' + stringify(tailStart) + ')', section.node)
}
}
} else {
customPosition.end = {
line: position.end(section.node).line - 1,
column: 1
}
if (inTail) {
state = 'tail'
tailStart = position.start(section.node)
} else if (inHead) {
/* We had a custom section, and now there’s an entry that should’ve
* been in the head. Suggest switching them around. */
file.message('Unexpected header section after extra sections (' + stringify(customPosition) + '): move it in front of the extra sections', section.node)
}
}
}
}