Skip to content

Commit

Permalink
fixes #8 fixup nullable types support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexej Yaroshevich committed May 27, 2014
1 parent f709d6c commit 9c170c0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
45 changes: 27 additions & 18 deletions lib/jsdoc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ function jsDocSimplifyNode (node) {
case 'NullableType':
case 'NonNullableType':
result = jsDocSimplifyNode (node.expression);
result.nullable = (node.type === 'NullableType');
if (node.type === 'NullableType') {
result.push({type: 'null'});
}
return result;

case 'NullLiteral':
Expand Down Expand Up @@ -121,42 +123,49 @@ function jsDocSimplifyNode (node) {
* @param {Object} argument - esprima source code node
*/
function jsDocMatchType (variants, argument) {
var i, l, variant, type;
var i, l, variant, type, result = null;

for (i = 0, l = variants.length; i < l; i += 1) {
variant = variants[i][0] || variants[i];
if (variant.unknown || !variant.type) {
continue;
result = true;
break;
}

type = variant.type.toLowerCase();

if (argument.type === 'Literal') {
if (argument.value === null) {
return type === 'null';
}
if (argument.value === undefined) {
return type === 'undefined';
}
if (typeof argument.value !== 'object') {
return type === typeof argument.value;
}
if (!argument.value.type) {
return type === (argument.value instanceof RegExp ? 'regexp' : 'object');
result = result || (type === 'null');

} else if (argument.value === undefined) {
result = result || (type === 'undefined');

} else if (typeof argument.value !== 'object') {
result = result || (type === typeof argument.value);

} else if (!argument.value.type) {
result = result || (type === (argument.value instanceof RegExp ? 'regexp' : 'object'));

} else {
result = result || (type === argument.value.type);
}
return type === argument.value.type;

} else if (argument.type === 'ObjectExpression') {
return (type === 'object');
result = result || (type === 'object');

} else if (argument.type === 'ArrayExpression') {
return (type === 'array');
result = result || (type === 'array');

} else if (argument.type === 'NewExpression') {
return (type === 'object') || (type === argument.callee.name.toLowerCase());
result = result || ((type === 'object') || (type === argument.callee.name.toLowerCase()));
}

if (result) {
break;
}
}

// variables, expressions, another behavior
return true;
return result !== false;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jscs-jsdoc",
"author": "Alexej Yaroshevich <alex@qfox.ru>",
"description": "JSCS jsdoc plugin",
"version": "0.0.5",
"version": "0.0.6",
"main": "lib/index",
"homepage": "https://github.com/zxqfox/jscs-jsdoc",
"license": "MIT",
Expand Down
50 changes: 50 additions & 0 deletions test/test.validate-jsdoc-returns.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,56 @@ describe('rules/validate-jsdoc', function () {
);
});

it('should not report on `@returns {string|null}` vs (null). issue #8', function () {
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
assert(
checker.checkString(
'/**\n' +
' * @return {string|null}\n' +
' */\n' +
'function funcName(flag) {\n' +
' if (!this.something) {\n' +
' return null;\n' +
' }\n' +
'}\n' +
'/**\n' +
' * @return {?string}\n' +
' */\n' +
'function funcName(flag) {\n' +
' if (!this.something) {\n' +
' return null;\n' +
' }\n' +
'}\n'
).isEmpty()
);
});

it('should not report on `@returns {?number}` vs (null|number). issue #8', function () {
checker.configure({ jsDoc: { requireReturnTypes: true, checkReturnTypes: true } });
assert(
checker.checkString(
'/**\n' +
' * @return {number|null}\n' +
' */\n' +
'function funcName(flag) {\n' +
' if (!this.something) {\n' +
' return null;\n' +
' }\n' +
' return 3;\n' +
'}\n' +
'/**\n' +
' * @return {?number}\n' +
' */\n' +
'function funcName(flag) {\n' +
' if (!this.something) {\n' +
' return null;\n' +
' }\n' +
' return 3;\n' +
'}\n'
).isEmpty()
);
});

});

});

0 comments on commit 9c170c0

Please sign in to comment.