Skip to content

Commit

Permalink
集中检查类型,针对 doctrine 特有的 ArrayType 报错, fix #100
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswong committed Jul 29, 2015
1 parent ad703bf commit 1588a9a
Showing 1 changed file with 65 additions and 40 deletions.
105 changes: 65 additions & 40 deletions lib/js/rules/valid-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,60 @@ module.exports = function (context) {
}
}

function checkType(type, node, pos, tag, lines) {

if (type.type === 'ArrayType') {
var name = (type.elements[0] || {}).name || 'type';
context.report(
node,
{
line: pos.line,
column: (lines[tag.lineNumber] || '').indexOf('[' + name + ']')
},
'Expected "Array.<{{name}}>" or "{{name}}[]" but "[{{name}}] found".baidu043',
{name: name}
);
}

var elements = type.applications || (type.expression || type).elements;
if (!type.name && elements) {
elements.forEach(function (type) {
checkType(type, node, pos, tag, lines);
});
return;
}

if (type.name && type.type === 'NameExpression') {
var map = {
'string': 'string',
'boolean': 'boolean',
'number': 'number',
'int': 'number',
'array': 'Array',
'function': 'Function',
'date': 'Date',
'object': 'Object',
'regexp': 'RegExp'
};
var preferName = map[type.name.toLowerCase()];

if (preferName && type.name !== preferName) {
context.report(
node,
{
line: pos.line,
column: 1 + (lines[tag.lineNumber] || '').indexOf('{' + type.name + '}')
},
'Expected JSDoc type name "{{name}}" but "{{jsdocName}} found".baidu044',
{
name: preferName,
jsdocName: type.name
}
);
}
}
}

/**
* Validate the JSDoc node and output warnings if anything is wrong.
*
Expand Down Expand Up @@ -240,13 +294,15 @@ module.exports = function (context) {

jsdoc.tags.forEach(function (tag) {

var pos = {line: startLine + tag.lineNumber, column: startColumn};

switch (tag.title) {

case 'param':
if (!tag.type) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Missing JSDoc parameter type for "{{name}}".baidu052',
{name: tag.name}
);
Expand All @@ -255,7 +311,7 @@ module.exports = function (context) {
if (!tag.description && requireParamDescription) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Missing JSDoc parameter description for "{{name}}".baidu053',
{name: tag.name}
);
Expand All @@ -264,14 +320,14 @@ module.exports = function (context) {
if (!tag.name) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Missing JSDoc parameter name.baidu053'
);
}
else if (params[tag.name]) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Duplicate JSDoc parameter "{{name}}".baidu998',
{name: tag.name}
);
Expand Down Expand Up @@ -302,23 +358,23 @@ module.exports = function (context) {

context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Unexpected @' + tag.title + ' tag; function has no return statement.baidu998'
);
}
else {
if (!tag.type) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Missing JSDoc return type.baidu053'
);
}

if (requireReturnDescription && tag.type.name !== 'void' && !tag.description) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Missing JSDoc return description.baidu053'
);
}
Expand Down Expand Up @@ -346,46 +402,15 @@ module.exports = function (context) {
if (prefer.hasOwnProperty(tag.title)) {
context.report(
jsdocNode,
{line: startLine + tag.lineNumber, column: startColumn},
pos,
'Use @{{name}} instead.baidu' + (tag.title === 'constructor' ? '048' : '998'),
{name: prefer[tag.title]}
);
}

var type = tag.type;
if (type) {
var map = {
'string': 'string',
'boolean': 'boolean',
'number': 'number',
'int': 'number',
'array': 'Array',
'function': 'Function',
'date': 'Date',
'object': 'Object',
'regexp': 'RegExp'
};
var preferName;

switch (type.type) {
case 'NameExpression':
preferName = map[type.name.toLowerCase()];
if (preferName && type.name !== preferName) {
context.report(
jsdocNode,
{
line: startLine + tag.lineNumber,
column: 1 + (lines[tag.lineNumber] || '').indexOf('{' + type.name + '}')
},
'Expected JSDoc type name "{{name}}" but found "{{jsdocName}}".baidu044',
{
name: preferName,
jsdocName: type.name
}
);
}
break;
}
checkType(type, jsdocNode, pos, tag, lines);
}

});
Expand Down

0 comments on commit 1588a9a

Please sign in to comment.