1 | 1 | var escapeHTML = require('escape-html'); |
|
2 | 1 | var jsdoc = require('jsdoc-api'); |
|
3 | |||
4 | 1 | module.exports = function(value, config, cb) { |
|
5 | jsdoc.explain({ files: value }).then(function(data) { |
||
6 | cb(null, processTree(data)); |
||
7 | }); |
||
8 | } |
||
9 | |||
10 | 1 | module.exports.search = function(items, link) { |
|
11 | var results = []; |
||
12 | 8 | var tree = [].concat(items.class || [], items.function || [], items.event || [], items.member || []); |
|
13 | |||
14 | for (var i in tree) { |
||
0 ignored issues
–
show
|
|||
15 | var item = tree[i]; |
||
16 | var name = item.name; |
||
17 | var type = item.kind; |
||
18 | var description = escapeHTML(item.description.replace('\n', '')); |
||
19 | var hash = '#js-' + type.replace('plugin ', '') + 's'; |
||
20 | |||
21 | 2 | if (type === 'class') { |
|
22 | name = name + '()'; |
||
23 | hash = hash.slice(0, -1) |
||
24 | } |
||
25 | |||
26 | 2 | if (type === 'member') { |
|
27 | type = 'plugin option' |
||
28 | } |
||
29 | |||
30 | 2 | if (type === 'function') { |
|
31 | name = item.meta.code.name.replace('prototype.', ''); |
||
32 | hash = '#' + name.split('.')[1]; |
||
33 | name += '()'; |
||
34 | } |
||
35 | |||
36 | results.push({ |
||
37 | type: 'js ' + type, |
||
38 | name: name, |
||
39 | description: description, |
||
40 | link: link + hash |
||
41 | }); |
||
42 | } |
||
43 | |||
44 | return results; |
||
45 | } |
||
46 | |||
47 | function processTree(tree) { |
||
48 | var js = {}; |
||
49 | |||
50 | for (var i in tree) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
51 | var obj = tree[i]; |
||
52 | var group = obj.kind; |
||
53 | |||
54 | 4 | if (obj.undocumented === true || obj.access === 'private') continue; |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
55 | 4 | if (group === 'member' && obj.memberof.indexOf('defaults') < 0) continue; |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
56 | |||
57 | 2 | if (!js[group]) js[group] = []; |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
58 | js[group].push(obj) |
||
59 | } |
||
60 | |||
61 | return js; |
||
62 | } |
||
63 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: