-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
219 lines (182 loc) · 5.4 KB
/
index.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
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
var CDocParser = require('cdocparser')
/**
* Extract the code following `offset` in `code` buffer,
* delimited by braces.
*
* `offset` should be set to the position of the opening brace. If not,
* the function will jump to the next opening brace.
*
* @param {String} code Code buffer.
* @param {Number} offset Index of the opening brace.
* @return {String} Extracted code between braces.
*/
var extractCode = function (code, offset) {
offset = offset || 0
if (code[offset] !== '{') {
// The position is not valid, jump to next opening brace
offset = code.indexOf('{', offset)
}
var start = offset + 1 // Ignore the opening brace
var cursor = start
var depth = 1 // The opening brace is consumed
var length = code.length
var inString = false
var openChar = ''
// In block comment (line comments are instantly consumed)
var inComment = false
while (cursor < length && depth > 0) {
var cb = code[cursor - 1] // Char before
var c = code[cursor] // Char
var cn = code[cursor + 1] // Char next
if (!inString) {
if (c === '/' && cn === '/' && !inComment) {
// Swallow line comment
cursor = Math.min(
Math.max(code.indexOf('\r', cursor), code.indexOf('\n', cursor)),
length
)
continue
} else if (c === '/' && cn === '*') {
// Block comment: begin
cursor += 2 // Swallow opening
inComment = true
continue
} else if (c === '*' && cn === '/') {
// Block comment: end
cursor += 2 // Swallow closing
inComment = false
continue
}
}
if (!inComment) {
if ((c === '"' || c === '\'') && cb !== '\\') {
if (!inString) {
// String: begin
openChar = c
inString = true
cursor++
continue
} else if (openChar === c) {
// String: end
inString = false
cursor++
continue
}
}
}
if (!(inString || inComment)) {
if (c === '{') {
depth++
} else if (c === '}') {
depth--
}
}
cursor++
}
if (depth > 0) {
return ''
}
// Ignore the closing brace
cursor--
return code.substring(start, cursor)
}
var findCodeStart = function (ctxCode, lastMatch) {
var codeStart = ctxCode.indexOf('{', lastMatch)
if (codeStart < 0 || ctxCode[codeStart - 1] !== '#') {
return codeStart
}
return findCodeStart(ctxCode, codeStart + 1)
}
var addCodeToContext = function (context, ctxCode, match) {
var codeStart = findCodeStart(ctxCode, match.index)
if (codeStart >= 0) {
context.code = extractCode(ctxCode, codeStart)
return codeStart + context.code.length + 1 // Add closing brace!
}
}
/**
* SCSS Context Parser
*/
var scssContextParser = (function () {
var ctxRegEx = /^(@|%|\$)([\w-_]+)*(?:\s+([\w-_]+)|[\s\S]*?:([\s\S]*?)(?:\s!(\w+))?;[ \t]*?(?=\/\/|\n|$))?/
var parser = function (ctxCode, lineNumberFor) {
var match = ctxRegEx.exec(ctxCode.trim())
var startIndex, endIndex
var context = {
type: 'unknown'
}
if (match) {
var wsOffset = Math.min(ctxCode.match(/\s*/).length - 1, 0)
startIndex = wsOffset + match.index
endIndex = startIndex + match[0].length
if (match[1] === '@' && (match[2] === 'function' || match[2] === 'mixin')) {
context.type = match[2]
context.name = match[3]
endIndex = addCodeToContext(context, ctxCode, match)
} else if (match[1] === '%') {
context.type = 'placeholder'
context.name = match[2]
endIndex = addCodeToContext(context, ctxCode, match)
} else if (match[1] === '$') {
context.type = 'variable'
context.name = match[2]
context.value = match[4].trim()
context.scope = match[5] || 'private'
}
} else {
startIndex = findCodeStart(ctxCode, 0)
endIndex = ctxCode.length - 1
if (startIndex > 0) {
context.type = 'css'
context.name = ctxCode.slice(0, startIndex).trim()
context.value = extractCode(ctxCode, startIndex).trim()
}
}
if (lineNumberFor !== undefined && startIndex !== undefined) {
context.line = {
start: lineNumberFor(startIndex) + 1,
end: lineNumberFor(endIndex) + 1
}
}
return context
}
return parser
})()
var filterAndGroup = function (lines) {
var nLines = []
var group = false
lines.forEach(function (line) {
var isAnnotation = line.indexOf('@') === 0
if (line.trim().indexOf('---') !== 0) { // Ignore lines that start with "---"
if (group) {
if (isAnnotation) {
nLines.push(line)
} else {
nLines[nLines.length - 1] += '\n' + line
}
} else if (isAnnotation) {
group = true
nLines.push(line)
} else {
nLines.push(line)
}
}
})
return nLines
}
var extractor = new CDocParser.CommentExtractor(scssContextParser, {
blockComment: false
})
var Parser = function (annotations, config) {
this.commentParser = new CDocParser.CommentParser(annotations, config)
}
Parser.prototype.parse = function (code, id) {
var comments = extractor.extract(code)
comments.forEach(function (comment) {
comment.lines = filterAndGroup(comment.lines)
})
return this.commentParser.parse(comments, id)
}
Parser.prototype.contextParser = scssContextParser
Parser.prototype.extractCode = extractCode
module.exports = Parser