Skip to content

Commit f7ce734

Browse files
author
Alexej Yaroshevich
committed
jscsrc: add jsdoc rules, fixup code
1 parent c92384e commit f7ce734

File tree

11 files changed

+143
-26
lines changed

11 files changed

+143
-26
lines changed

.jscsrc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,20 @@
1616
"excludeFiles": [
1717
"test/data/**",
1818
"test/lib/rules/**"
19-
]
19+
],
20+
21+
"plugins": ["jscs-jsdoc"],
22+
23+
"jsDoc": {
24+
"enforceExistence": true,
25+
"checkAnnotations": "closurecompiler",
26+
"checkParamNames": true,
27+
"requireParamTypes": true,
28+
"checkRedundantParams": true,
29+
"checkReturnTypes": true,
30+
"checkRedundantReturns": true,
31+
"requireReturnTypes": true,
32+
"checkTypes": "strictNativeCase",
33+
"checkRedundantAccess": true
34+
}
2035
}

lib/esprima-helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ var scopeNodeTypes = [
1010

1111
/**
1212
* Search for the closest scope node tree for Node
13-
* @param {{type: String}} n
13+
* @param {{type: string}} n
14+
* @returns {EsprimaNode}
1415
*/
1516
function closestScopeNode (n) {
1617
while (n && scopeNodeTypes.indexOf(n.type) === -1) {

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @param {module:jscs/lib/Configuration} configuration
3+
*/
14
module.exports = function(configuration) {
25
configuration.registerRule(require('./rules/validate-jsdoc'));
36
};

lib/jsdoc.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var TypeBuilder = require('jsdoctypeparser').Builder;
88
TypeBuilder.ENABLE_EXCEPTIONS = true;
99

1010
module.exports = {
11+
1112
/**
1213
* @param {string} commentNode
1314
* @returns {DocComment}
@@ -57,18 +58,18 @@ function DocComment(value, loc) {
5758
});
5859

5960
/**
60-
* @param {function} fn
61-
* @chainable
61+
* @param {function(this: DocComment, DocTag): DocComment} fn
62+
* @returns {DocComment}
6263
*/
6364
this.iterate = function forEachTag(fn) {
64-
this.tags.forEach(fn);
65+
this.tags.forEach(fn, this);
6566
return this;
6667
};
6768

6869
/**
69-
* @param {string|array} types
70-
* @param {function} fn
71-
* @chainable
70+
* @param {string|Array.<string>} types
71+
* @param {function(this: DocComment, DocTag): DocComment} fn
72+
* @returns {DocComment}
7273
*/
7374
this.iterateByType = function iterateByTypes(types, fn) {
7475
var k = 0;
@@ -85,7 +86,7 @@ function DocComment(value, loc) {
8586

8687
/**
8788
* Simple jsdoc tag object
88-
* @param {Object} tag
89+
* @param {Object} tag object from comment parser, fields: tag, line, value, name, type, description
8990
* @param {DocLocation} loc
9091
* @constructor
9192
*/
@@ -159,8 +160,8 @@ function DocType(type, loc) {
159160
/**
160161
* DocLocation
161162
* @constructor
162-
* @param {Number} line
163-
* @param {Number} column
163+
* @param {number} line
164+
* @param {number} column
164165
* @param {(Object|DocLocation)} [rel]
165166
*/
166167
function DocLocation(line, column, rel) {
@@ -173,9 +174,9 @@ function DocLocation(line, column, rel) {
173174

174175
/**
175176
* Shift location by line and column
176-
* @param {Number|Object} line
177-
* @param {Number} [column]
178-
* @return {DocLocation}
177+
* @param {number|Object} line
178+
* @param {number} [column]
179+
* @returns {DocLocation}
179180
*/
180181
DocLocation.prototype.shift = function(line, column) {
181182
if (typeof line === 'object') {
@@ -187,7 +188,7 @@ DocLocation.prototype.shift = function(line, column) {
187188

188189
/**
189190
* Comment parsing helper
190-
* @param {String} comment
191+
* @param {string} comment
191192
* @returns {Object}
192193
* @private
193194
*/
@@ -232,7 +233,7 @@ function _parseComment(comment) {
232233
/**
233234
* Additional name parsing logic for our purposes
234235
* @param {string} str - unparsed name thing
235-
* @return {Object}
236+
* @returns {Object}
236237
*/
237238
function _parseNameAgain(str) { // (?:\s+(\S+))?
238239
var out = {};
@@ -296,8 +297,8 @@ function _parseNameAgain(str) { // (?:\s+(\S+))?
296297
}
297298

298299
/**
299-
* @param {String} typeString
300-
* @return {?Array.<SimplifiedType>} - parsed jsdoctype string as array
300+
* @param {string} typeString
301+
* @returns {?Array.<SimplifiedType>} - parsed jsdoctype string as array
301302
*/
302303
function _parseDocType(typeString) {
303304
var parser = new TypeParser();
@@ -313,6 +314,11 @@ function _parseDocType(typeString) {
313314
return node;
314315
}
315316

317+
/**
318+
* @param {EsprimaNode} node
319+
* @param {Function} cb
320+
* @returns {Array}
321+
*/
316322
function _iterateDocTypes(node, cb) {
317323
var res;
318324

@@ -413,7 +419,7 @@ function _iterateDocTypes(node, cb) {
413419
/**
414420
* Converts AST jsDoc node to simple object
415421
* @param {Object} node
416-
* @returns {!(SimplifiedType[])}
422+
* @returns {!Array.<SimplifiedType>}
417423
* @link https://github.com/Kuniwak/jsdoctypeparser
418424
*/
419425
function _simplifyType(node) {
@@ -426,7 +432,7 @@ function _simplifyType(node) {
426432
return res;
427433
}
428434

429-
var jsPrimitives = 'String Number Boolean Object Array Date Null Undefined Function Array RegExp'
435+
var jsPrimitives = 'string number boolean null undefined Object Function Array Date RegExp'
430436
.toLowerCase().split(' ');
431437

432438
/**

lib/rules/validate-jsdoc.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ var jsdoc = require('../jsdoc');
44
var esprimaHelpers = require('../esprima-helpers');
55
var validators = require('./validate-jsdoc/index');
66

7+
/**
8+
* Rule constructor
9+
* @this {module:jscs/Rule}
10+
* @constructor
11+
*/
712
module.exports = function() {};
813

914
module.exports.prototype = {
1015

11-
// load all rules and init them
16+
/**
17+
* Load all rules and init them
18+
* @param {Object} options
19+
* @throws {Error} If options is not an Object
20+
*/
1221
configure: function(options) {
1322
assert(typeof options === 'object', 'jsDoc option requires object value');
1423

@@ -53,10 +62,17 @@ module.exports.prototype = {
5362
}, this);
5463
},
5564

65+
/**
66+
* @returns {string}
67+
*/
5668
getOptionName: function() {
5769
return 'jsDoc';
5870
},
5971

72+
/**
73+
* @param {module:jscs/JsFile} file
74+
* @param {module:jscs/Errors} errors
75+
*/
6076
check: function(file, errors) {
6177
patchNodesInFile(file);
6278
this._iterate = file.iterate;
@@ -120,6 +136,12 @@ module.exports.prototype = {
120136
});
121137
});
122138

139+
/**
140+
* send error to jscs
141+
* @param {string} text
142+
* @param {number|DocLocation} relLine
143+
* @param {number} [relColumn]
144+
*/
123145
function addError(text, relLine, relColumn) {
124146
var line;
125147
var column;
@@ -133,6 +155,12 @@ module.exports.prototype = {
133155
errors.add(text, line, column);
134156
}
135157

158+
/**
159+
* Generates function with location fixing logic to send error to jscs
160+
* @param {function(string, number|Object, ?number)} err
161+
* @param {DocTag} tag
162+
* @returns {function(string, number|Object, ?number)}
163+
*/
136164
function fixErrLocation (err, tag) {
137165
return function(text, line, column) {
138166
line = line || tag.line;
@@ -147,8 +175,7 @@ module.exports.prototype = {
147175
},
148176

149177
/**
150-
* caching scope search
151-
* @todo move to patchNodesInFile
178+
* caching scope search. todo: move to patchNodesInFile
152179
* @param {Object} node
153180
*/
154181
_getReturnStatementsForNode: function(node) {
@@ -189,6 +216,11 @@ function patchNodesInFile(file) {
189216
});
190217
});
191218

219+
/**
220+
* Fetchs jsdoc block for this
221+
* @this {module:esprima/Node}
222+
* @returns {DocComment}
223+
*/
192224
function getJsdoc() {
193225
if (!this.hasOwnProperty('_jsdoc')) {
194226
var res = findDocCommentBeforeLine(this.loc.start.line);
@@ -197,6 +229,11 @@ function patchNodesInFile(file) {
197229
return this._jsdoc;
198230
}
199231

232+
/**
233+
* Finds DocComment in file before passed line number
234+
* @param {number} line
235+
* @returns {?module:esprima/Node}
236+
*/
200237
function findDocCommentBeforeLine(line) {
201238
line--; // todo: buggy behaviour, can't jump back over empty lines
202239
for (var i = 0, l = fileComments.length; i < l; i++) {

lib/rules/validate-jsdoc/check-annotations.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports.options = {
1111

1212
var tags;
1313

14+
/**
15+
* @param {Object} options
16+
*/
1417
validateAnnotations.configure = function(options) {
1518
var o = options.checkAnnotations;
1619

lib/rules/validate-jsdoc/check-param-names.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function validateCheckParamNames(node, err) {
3131
/**
3232
* tag checker
3333
* @param {DocType} tag
34-
* @param {Number} i index
34+
* @param {number} i index
3535
*/
3636
function(tag, i) {
3737
// checking validity

lib/rules/validate-jsdoc/check-redundant-params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function validateCheckParamNames(node, err) {
1818
/**
1919
* tag checker
2020
* @param {DocType} tag
21-
* @param {Number} i index
21+
* @param {number} i index
2222
*/
2323
function(tag, i) {
2424
// skip if there is dot in param name (object's inner param)

lib/rules/validate-jsdoc/check-types.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ function validateTypesInTags(file, errors) {
6767
return;
6868
}
6969

70-
node.iterateByType(Object.keys(allowedTags), function(tag) {
70+
node.iterateByType(Object.keys(allowedTags),
71+
/**
72+
* @param {DocType} tag
73+
*/
74+
function(tag) {
75+
7176
if (!tag.type) {
7277
// skip untyped params
7378
return;

lib/rules/validate-jsdoc/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ var validatorsByName = module.exports = {
2020
};
2121

2222
Object.defineProperty(validatorsByName, 'load', {
23+
/**
24+
* loads and initializes existing and required validators
25+
* @param {Object} passedOptions
26+
* @returns {Array.<Function>}
27+
*/
2328
value: function loadValidators(passedOptions) {
2429
if (!passedOptions) {
2530
return [];
@@ -51,6 +56,12 @@ Object.defineProperty(validatorsByName, 'load', {
5156
});
5257

5358
Object.defineProperty(validatorsByName, 'checkOptions', {
59+
/**
60+
* Validates passed options
61+
* @param {Object} validator
62+
* @param {Object} options
63+
* @throws {Error} If option is not valid
64+
*/
5465
value: function checkOptions(validator, options) {
5566
Object.keys(validator.options).forEach(function(option) {
5667
var data = validator.options[option];

0 commit comments

Comments
 (0)