Skip to content

Commit 684eab9

Browse files
committed
Merge pull request #60 from zxqfox/feature/capitalized-natives
checkTypes: add capitalizedNativeCase mode
2 parents eac5f71 + 4f980de commit 684eab9

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,11 @@ Reports invalid types for bunch of tags
374374

375375
In `strictNativeCase` mode ensures that case of natives is the same as in this list: `boolean`, `number`, `string`, `null`, `Object`, `Array`, `Date`, `RegExp`.
376376

377+
In `capitalizedNativeCase` mode ensures that all types are uppercased except function closure format: `{function(...)}`
378+
377379
Type: `Boolean` or `String`
378380

379-
Values: `true` or `"strictNativeCase"`
381+
Values: `true` or `"strictNativeCase"` or `"capitalizedNativeCase"`
380382

381383
Context: `*`
382384

lib/jsdoc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ function _iterateDocTypes(node, cb) {
330330
subnode.parentNode = node.selfNode;
331331
}
332332
return _iterateDocTypes(subnode, cb);
333-
});
333+
}) || [];
334+
if (node.nullable) {
335+
var subnode = {typeName: 'null', parentNode: node};
336+
res.concat(cb(subnode));
337+
}
334338
break;
335339

336340
case node instanceof TypeBuilder.TypeName:

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var jsdoc = require('../../jsdoc');
33
module.exports = validateTypesInTags;
44
module.exports.scopes = ['file'];
55
module.exports.options = {
6-
checkTypes: {allowedValues: [true, 'strictNativeCase']}
6+
checkTypes: {allowedValues: [true, 'strictNativeCase', 'capitalizedNativeCase']}
77
};
88

99
var allowedTags = {
@@ -53,6 +53,8 @@ var strictNatives = {
5353
*/
5454
function validateTypesInTags(file, errors) {
5555
var strictNativeCase = this._options.checkTypes === 'strictNativeCase';
56+
var capitalizedNativeCase = this._options.checkTypes === 'capitalizedNativeCase';
57+
5658
var comments = file.getComments();
5759
comments.forEach(function(commentNode) {
5860
if (commentNode.type !== 'Block' || commentNode.value[0] !== '*') {
@@ -74,7 +76,7 @@ function validateTypesInTags(file, errors) {
7476
// throw an error if not valid
7577
errors.add('expects valid type instead of ' + tag.type.value, tag.type.loc);
7678
}
77-
if (strictNativeCase) {
79+
if (strictNativeCase || capitalizedNativeCase) {
7880
tag.type.iterate(function(node) {
7981
// it shouldn't be!
8082
if (!node.typeName) {
@@ -85,7 +87,13 @@ function validateTypesInTags(file, errors) {
8587
// is native?
8688
var type = node.typeName;
8789
var lowerType = type.toLowerCase();
88-
if (lowerType in strictNatives && type !== strictNatives[lowerType]) {
90+
//console.log(capitalizedNativeCase, type, lowerType, strictNatives.hasOwnProperty(type));
91+
if (!strictNatives.hasOwnProperty(lowerType)) {
92+
return;
93+
}
94+
var normType = strictNatives[lowerType];
95+
if (strictNativeCase && type !== normType ||
96+
capitalizedNativeCase && type !== (normType[0].toUpperCase() + normType.substr(1))) {
8997
errors.add('invalid case of type `' + type + '`', tag.type.loc);
9098
}
9199
});

test/lib/jsdoc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ describe('jsdoc', function() {
5656
describe('type', function() {
5757
var bool1;
5858
var varnum;
59+
var nullie;
5960

6061
before(function() {
6162
bool1 = new Type('boolean', new Location(3, 4));
6263
varnum = new Type('...number', new Location(10, 20));
64+
nullie = new Type('Null|null', new Location(2, 5));
6365
});
6466

6567
it('should store data', function() {
@@ -76,6 +78,18 @@ describe('jsdoc', function() {
7678
expect(bool1.match({type: 'Literal', value: null})).to.eq(false);
7779
});
7880

81+
it('should parse null', function() {
82+
var passed = '';
83+
nullie.iterate(function(type) {
84+
if (type.typeName === 'Null') {
85+
passed += 1;
86+
}
87+
if (type.typeName === 'null') {
88+
passed += 2;
89+
}
90+
});
91+
expect(passed).to.eq('12');
92+
});
7993
});
8094

8195
describe('tag', function() {

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,79 @@ describe('lib/rules/validate-jsdoc/check-types', function() {
236236

237237
});
238238

239+
describe('with capitalzedNativeCase', function() {
240+
checker.rules({checkTypes: 'capitalizedNativeCase'});
241+
242+
checker.cases([
243+
/* jshint ignore:start *//* jscs: disable */
244+
{
245+
it: 'should not report strict natives',
246+
code: function() {
247+
/**
248+
* @param {Number}
249+
* @param {String}
250+
* @param {Boolean}
251+
* @param {Null}
252+
* @param {Array}
253+
* @param {Object}
254+
* @param {Date}
255+
* @param {Function}
256+
*/
257+
function _f () {}
258+
}
259+
}, {
260+
it: 'should not report joined native types',
261+
code: function() {
262+
/**
263+
* @param {Number|String|Boolean|Null|Array|Object|Date}
264+
*/
265+
function _f () {}
266+
}
267+
}, {
268+
it: 'should not report function arguments',
269+
code: function() {
270+
/**
271+
* @param {function(Number, Array)}
272+
*/
273+
function _f () {}
274+
}
275+
}, {
276+
it: 'should report lowercased natives',
277+
errors: 6,
278+
code: function() {
279+
/**
280+
* @param {number}
281+
* @param {string}
282+
* @param {boolean}
283+
* @param {array}
284+
* @param {object}
285+
* @param {date}
286+
*/
287+
function _f () {}
288+
}
289+
}, {
290+
it: 'should report joined lowercased natives',
291+
errors: 7,
292+
code: function() {
293+
/**
294+
* @param {number|string|boolean|null}
295+
* @param {array|object|date}
296+
*/
297+
function _f () {}
298+
}
299+
}, {
300+
it: 'should report joined strict wrong cased natives',
301+
code: function() {
302+
/**
303+
* @param {number|string|boolean|null|array|object|date|regexp|function}
304+
*/
305+
function _f () {}
306+
},
307+
errors: 9
308+
}
309+
/* jshint ignore:end *//* jscs: enable */
310+
]);
311+
312+
});
313+
239314
});

0 commit comments

Comments
 (0)