-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
88 lines (77 loc) · 1.88 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
/*!
* get-comments <https://github.com/tunnckoCore/get-comments>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
/**
* Get block code comments from string
*
* @param {String} `<input>` string to extract from
* @param {Boolean} `[resType]` if true, will return array, otherwise object
* @return {Array|Object}
* @api public
*/
module.exports = function getComments (input, resType) {
if (typeof input !== 'string') {
throw new TypeError('get-comments expects a string')
}
var len = input.length
var obj = {start: 0, end: 0}
var res = resType ? [] : {}
var raw = ''
var column = 0
var line = 1
var i = 0
while (i < len) {
var ch = input[i]
var prev = input[i - 1]
var next = input[i + 1]
var post = input[i + 2]
raw += ch
if (prev !== ' ' && ch === '/' && next === '*' && post === '*') {
obj.type = 'Block'
obj.start = i
obj.loc = {}
obj.loc.start = {
line: line,
column: column
}
}
if (obj.start && prev === '*' && ch === '/') {
var value = raw.slice(obj.start + 2, i - 1)
obj.api = value.indexOf('@api') !== -1
obj.end = i + 1
obj.value = value
obj.loc.end = {
line: line,
column: column
}
}
if (obj.start && prev === '/' && (ch === '\n' || ch === '\r')) {
var lines = input.split(/[\r\n]/)
obj.after = lines[line]
if (!lines[line].length) {
obj.after = lines[line + 1]
}
if (resType) {
res.push(obj)
} else {
var l = Object.keys(res).length
var j = l > 0 ? l : 0
res[j] = obj
}
// resets
obj = {start: 0, end: 0}
}
if (ch === '\n' || ch === '\r') {
line++
column = 0
}
column++
i++
}
return res
}