-
Notifications
You must be signed in to change notification settings - Fork 35
/
validate-jsdoc.js
288 lines (250 loc) · 8.55 KB
/
validate-jsdoc.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var assert = require('assert');
var jsdoc = require('../jsdoc');
var getLineCountBetween = require('../utils').getLineCountBetween;
var getNodeColumn = require('../utils').getNodeColumn;
var esprimaHelpers = require('../esprima-helpers');
var validators = require('./validate-jsdoc/index');
/** @type {string[]} - list of function node types */
var functionNodeTypes = [
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
];
/** @type {string[]} - list of node types with jsdocs */
var jsdocableTypes = []
.concat(functionNodeTypes);
/**
* Rule constructor
*
* @this {module:jscs/Rule}
* @constructor
*/
module.exports = function() {};
module.exports.prototype = {
/**
* Load all rules and init them
*
* @param {Object} options
* @throws {Error} If options is not an Object
*/
configure: function(options) {
assert(typeof options === 'object', 'jsDoc option requires object value');
// rules structured by scopes-tags for jsdoc-tags
var rulesForTags = this._rulesForTags = {};
// rules structured by scopes for nodes
var rulesForNodes = this._rulesForNodes = {};
this._options = options;
this._optionsList = Object.keys(options);
// load validators
this._validators = validators.load(this._optionsList);
assert(this._validators.length, 'jsDoc plugin was not configured properly');
// registering validators
this._validators.forEach(function(v) {
// check options
if (v.options) {
validators.checkOptions(v, options);
}
// configure
if (v.configure) {
v.configure.call(this, options);
}
// index rules by tags and scopes
(v.scopes || ['']).forEach(function(scope) {
if (!v.tags) {
assert(v.length === 2, 'jsDoc rules: Wrong arity in ' + v._name + ' validator');
rulesForNodes[scope] = rulesForNodes[scope] || [];
rulesForNodes[scope].push(v);
return;
}
assert(v.length === 3, 'jsDoc rules: Wrong arity in ' + v._name + ' validator');
rulesForTags[scope] = rulesForTags[scope] || {};
v.tags.forEach(function(tag) {
var dtag = '@' + tag;
rulesForTags[scope][dtag] = rulesForTags[scope][dtag] || [];
rulesForTags[scope][dtag].push(v);
});
});
}, this);
},
/**
* @returns {string}
*/
getOptionName: function() {
return 'jsDoc';
},
/**
* @param {module:jscs/JsFile} file
* @param {module:jscs/Errors} errors
*/
check: function(file, errors) {
patchNodesInFile(file);
this._iterate = file.iterate;
var _this = this;
var scopes = {
'function': functionNodeTypes,
};
// classic checker
if (_this._rulesForNodes.file) {
// call file checkers
var validators = _this._rulesForNodes.file;
if (validators) {
validators.forEach(function(v) {
v.call(_this, file, errors);
});
}
}
// iterate over scopes
Object.keys(scopes).forEach(function(scope) {
// skip unused
if (!_this._rulesForNodes[scope] && !_this._rulesForTags[scope]) {
return;
}
// traverse ast tree and search scope node types
file.iterateNodesByType(scopes[scope], function(node) {
// init
var validators;
// call node checkers
validators = _this._rulesForNodes[scope];
if (validators) {
validators.forEach(function(v) {
v.call(_this, node, addError);
});
}
validators = _this._rulesForTags[scope];
if (!validators || !node.jsdoc || !node.jsdoc.valid) {
return;
}
// call rule checkers
node.jsdoc.iterate(function(tag) {
if (!validators['@' + tag.id]) {
return;
}
// call tag validator
validators['@' + tag.id].forEach(function(v) {
v.call(_this, node, tag, fixErrLocation(addError, tag));
});
});
/**
* Send error to jscs
*
* @param {string} text
* @param {number|DocLocation} offset
*/
function addError(text, offset) {
if (typeof offset === 'object') {
if (offset.type) {
errors.add(text, offset, 0);
return;
} else {
offset = offset.offset;
}
}
errors.add(text, node.jsdoc.node, offset);
}
/**
* Generates function with location fixing logic to send error to jscs
*
* @param {function(string, number|Object)} err
* @param {DocTag} tag
* @returns {function(string, number|Object)}
*/
function fixErrLocation(err, tag) {
return function(text, offset) {
err(text, offset || tag.loc.offset);
};
}
});
});
},
/**
* Caching scope search. todo: move to patchNodesInFile
*
* @param {Object} node
*/
_getReturnStatementsForNode: function(node) {
if (node.jsdoc.returnStatements) {
return node.jsdoc.returnStatements;
}
var statements = [];
this._iterate(function(n) {
if (n && n.type === 'ReturnStatement' && n.argument) {
if (node === esprimaHelpers.closestScopeNode(n)) {
statements.push(n.argument);
}
}
}, node);
node.jsdoc.returnStatements = statements;
return statements;
}
};
/**
* Extends each node with helper properties
*
* @param {Object} file
*/
function patchNodesInFile(file) {
if (file._jsdocs) {
return;
}
// jsdoc property for nodes
file.iterateNodesByType(jsdocableTypes, function(node) {
Object.defineProperty(node, 'jsdoc', {
get: getJsdoc
});
});
/**
* Fetchs jsdoc block for this
*
* @this {module:esprima/Node}
* @returns {DocComment}
*/
function getJsdoc() {
if (this.hasOwnProperty('_jsdoc')) {
return this._jsdoc;
}
var node = functionNodeTypes.indexOf(this.type) !== -1 ?
findFirstNodeInLine(this) : this;
var res = findDocCommentBeforeNode(node, this);
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
return this._jsdoc;
}
/**
* Finds the first node on the same line as passed node
*
* @param {?module:esprima/Node} node
* @returns {?module:esprima/Node}
*/
function findFirstNodeInLine(node) {
var parent = node.parentElement;
if (!parent || getLineCountBetween(parent.getFirstToken(), node.getFirstToken()) > 0) {
return node;
}
return findFirstNodeInLine(parent);
}
/**
* Finds DocComment in file before passed line number
*
* @param {?module:esprima/Node} node
* @returns {?module:esprima/Node}
*/
function findDocCommentBeforeNode(node, self) {
var res = node.getFirstToken().getPreviousNonWhitespaceToken();
if (!res || res.type !== 'CommentBlock' || res.value.charAt(0) !== '*') {
return null;
}
// Indent should be the same
if (getNodeColumn(res) !== getNodeColumn(node)) {
return null;
}
// IIFE should be on the next line to be sticked
if ((self.type === 'FunctionExpression' || self.type === 'ArrowFunctionExpression') &&
self.parentElement.type === 'CallExpression' &&
getLineCountBetween(res, self.getFirstToken()) > 1
) {
return null;
}
return res;
}
// mark object as patched
file._jsdocs = true;
}