ignore non-JSDoc comments when creating symbolFound events (#1398)

This commit is contained in:
Jeff Williams 2017-07-12 13:48:51 -07:00
parent b38b8db2d0
commit 11e8c6d5e2
2 changed files with 102 additions and 14 deletions

View File

@ -39,8 +39,20 @@ function isBlockComment(comment) {
return comment.type === 'CommentBlock';
}
/**
* Verify that a block comment exists; that it is a JSDoc comment; and that its leading delimiter
* does not contain three or more asterisks.
*
* @private
* @memberof module:jsdoc/src/parser.Parser
*/
function isValidJsdoc(commentSrc) {
return commentSrc && commentSrc.length > 4 && commentSrc.indexOf('/**') === 0 &&
commentSrc.indexOf('/***') !== 0;
}
// TODO: docs
function getLeadingComment(node) {
function getLeadingJsdocComment(node) {
var comment = null;
var leadingComments = node.leadingComments;
@ -51,6 +63,10 @@ function getLeadingComment(node) {
if (leadingComments.length) {
// treat the comment closest to the node as the leading comment
comment = getRawComment(leadingComments[leadingComments.length - 1]);
if ( !isValidJsdoc(comment) ) {
comment = null;
}
}
}
@ -389,7 +405,7 @@ function SymbolFound(node, filename, extras) {
extras = extras || {};
this.id = extras.id || node.nodeId;
this.comment = extras.comment || getLeadingComment(node) || '@undocumented';
this.comment = extras.comment || getLeadingJsdocComment(node) || '@undocumented';
this.lineno = extras.lineno || node.loc.start.line;
this.columnno = extras.columnno || node.loc.start.column;
this.range = extras.range || node.range;
@ -471,18 +487,6 @@ Visitor.prototype.visit = function(node, filename) {
return true;
};
/**
* Verify that a block comment exists; that it is a JSDoc comment; and that its leading delimiter
* does not contain three or more asterisks.
*
* @private
* @memberof module:jsdoc/src/parser.Parser
*/
function isValidJsdoc(commentSrc) {
return commentSrc && commentSrc.length > 4 && commentSrc.indexOf('/**') === 0 &&
commentSrc.indexOf('/***') !== 0;
}
// TODO: docs
function hasComments(node) {
return (node && node.leadingComments && node.leadingComments.length) ||

View File

@ -136,4 +136,88 @@ describe('jsdoc/src/visitor', function() {
expect(events[0].comment).toBe('/** block comment */');
});
});
// TODO: these tests aren't working; for some strange reason, Node.js 6.10.2 stops running code
// for visitor.visitNode() while it's in the middle of the SymbolFound constructor. maybe a
// version-specific bug?
xdescribe('visitNode', function() {
// TODO: more tests
var events = [];
function listener(event) {
events.push(event);
}
beforeEach(function() {
parser.addListener('symbolFound', listener);
});
afterEach(function() {
parser.removeListener('symbolFound', listener);
events = [];
});
it('should ignore non-JSDoc leading comments', function() {
var node = {
type: 'Property',
key: {
type: 'Identifier',
name: 'foo'
},
value: {
type: 'Literal',
value: true
},
kind: 'init',
leadingComments: [
{
type: 'CommentBlock',
value: ' block comment ',
loc: {
start: {
line: 0,
column: 0
}
}
}
]
};
visitor.visitNode(node, parser, 'fake');
expect(events[0].comment).toBe('');
});
it('should include JSDoc leading comments', function() {
var node = {
type: 'Property',
key: {
type: 'Identifier',
name: 'foo'
},
value: {
type: 'Literal',
value: true
},
kind: 'init',
leadingComments: [
{
type: 'CommentBlock',
value: '* block comment ',
loc: {
start: {
line: 0,
column: 0
}
}
}
]
};
visitor.visitNode(node, parser, 'fake');
expect(events[0].comment).toBe('/** block comment */');
});
});
});