forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
124 lines (111 loc) · 3.26 KB
/
javascript.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
/* @flow */
var _ = require('lodash'),
t = require('babel-types'),
parse = require('../parse'),
walkComments = require('../extractors/comments'),
walkExported = require('../extractors/exported'),
util = require('util'),
debuglog = util.debuglog('documentation');
import { parseToAst } from './parse_to_ast';
/**
* Left-pad a string so that it can be sorted lexicographically. We sort
* comments to keep them in order.
* @param {string} str the string
* @param {number} width the width to pad to
* @returns {string} a padded string with the correct width
* @private
*/
function leftPad(str, width) {
str = str.toString();
while (str.length < width) {
str = '0' + str;
}
return str;
}
/**
* Receives a module-dep item,
* reads the file, parses the JavaScript, and parses the JSDoc.
*
* @param {Object} data a chunk of data provided by module-deps
* @param {Object} config config
* @returns {Array<Object>} an array of parsed comments
*/
function parseJavaScript(data: Object, config: DocumentationConfig) {
var visited = new Set();
const commentsByNode = new Map();
var ast = parseToAst(data.source);
var addComment = _addComment.bind(null, visited, commentsByNode);
return _.flatMap(
config.documentExported
? [walkExported]
: [
walkComments.bind(null, 'leadingComments', true),
walkComments.bind(null, 'innerComments', false),
walkComments.bind(null, 'trailingComments', false)
],
fn => fn(ast, data, addComment)
).filter(comment => comment && !comment.lends);
}
function _addComment(
visited,
commentsByNode,
data,
commentValue,
commentLoc,
path,
nodeLoc,
includeContext
) {
// Avoid visiting the same comment twice as a leading
// and trailing node
var key =
data.file + ':' + commentLoc.start.line + ':' + commentLoc.start.column;
if (!visited.has(key)) {
visited.add(key);
var context /* : {
loc: Object,
file: string,
sortKey: string,
ast?: Object,
code?: string
}*/ = {
loc: nodeLoc,
file: data.file,
sortKey: data.sortKey + ' ' + leftPad(nodeLoc.start.line, 8)
};
if (includeContext) {
// This is non-enumerable so that it doesn't get stringified in
// output; e.g. by the documentation binary.
Object.defineProperty(context, 'ast', {
configurable: true,
enumerable: false,
value: path
});
if (path.parentPath && path.parentPath.node) {
var parentNode = path.parentPath.node;
context.code = data.source.substring(parentNode.start, parentNode.end);
}
}
const comment = parse(commentValue, commentLoc, context);
if (includeContext) {
commentsByNode.set(path.node, comment);
if (t.isClassMethod(path) && path.node.kind === 'constructor') {
// #689
if (!comment.hideconstructor) {
debuglog(
'A constructor was documented explicitly: document along with the class instead'
);
}
const parentComment = commentsByNode.get(
path.parentPath.parentPath.node
);
if (parentComment) {
parentComment.constructorComment = comment;
return;
}
}
}
return comment;
}
}
module.exports = parseJavaScript;