Skip to content

Commit

Permalink
fix: Report nesting errors instead of throwing them as errors
Browse files Browse the repository at this point in the history
Check on parent element: foo.bar sub-parametr should be documeted with parent.
Fixed #832 issue
  • Loading branch information
anthony-redFox authored and tmcw committed Jul 7, 2017
1 parent 15239d1 commit ea69608
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
8 changes: 8 additions & 0 deletions __tests__/lib/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test('lintComments', function() {
{ message: 'Missing or invalid tag name' }
]);

expect(
evaluate(function() {
/**
* @param {Object} foo.bar
*/
}).errors
).toEqual([{ commentLineNumber: 1, message: 'Parent of foo.bar not found' }]);

expect(
evaluate(function() {
/**
Expand Down
1 change: 0 additions & 1 deletion declarations/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ type Comment = {
tags: Array<CommentTag>,

augments: Array<CommentTag>,
errors: Array<CommentExample>,
examples: Array<CommentExample>,
params: Array<CommentTag>,
properties: Array<CommentTag>,
Expand Down
10 changes: 5 additions & 5 deletions src/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var VFile = require('vfile');
import { walk } from './walk';
import vfileSort from 'vfile-sort';
import reporter from 'vfile-reporter';
import nest from './nest';

var CANONICAL = {
String: 'string',
Expand All @@ -27,11 +28,8 @@ function lintComments(comment: Comment) {
function nameInvariant(name) {
if (name && typeof CANONICAL[name] === 'string') {
comment.errors.push({
message: 'type ' +
name +
' found, ' +
CANONICAL[name] +
' is standard',
message:
'type ' + name + ' found, ' + CANONICAL[name] + ' is standard',
commentLineNumber: tag.lineNumber
});
}
Expand Down Expand Up @@ -60,6 +58,8 @@ function lintComments(comment: Comment) {
checkCanonical(tag.type);
}
});
nest(comment);

return comment;
}

Expand Down
29 changes: 18 additions & 11 deletions src/nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var tagDepth = tag => tag.name.split(PATH_SPLIT).length;
* @returns {Object} nested comment
*/
var nestTag = (
tags: Array<CommentTag>
tags: Array<CommentTag>,
errors: Array<CommentError>
// Use lodash here both for brevity and also because, unlike JavaScript's
// sort, it's stable.
) =>
Expand All @@ -59,16 +60,22 @@ var nestTag = (

if (!child) {
if (tag.name.match(/^(\$\d+)/)) {
throw new Error(
`Parent of ${tag.name} not found. To document a destructuring\n` +
errors.push({
message:
`Parent of ${tag.name} not found. To document a destructuring\n` +
`type, add a @param tag in its position to specify the name of the\n` +
`destructured parameter`
);
`destructured parameter`,
commentLineNumber: tag.lineNumber
});
} else {
errors.push({
message: `Parent of ${tag.name} not found`,
commentLineNumber: tag.lineNumber
});
}
throw new Error(`Parent of ${tag.name} not found`);
} else {
insertTag(child, parts.slice(1));
}

insertTag(child, parts.slice(1));
}
}

Expand All @@ -91,9 +98,9 @@ var nestTag = (
* @return {Object} nested comment
*/
var nest = (comment: Comment) =>
_.assign(comment, {
params: nestTag(comment.params),
properties: nestTag(comment.properties)
Object.assign(comment, {
params: nestTag(comment.params, comment.errors),
properties: nestTag(comment.properties, comment.errors)
});

module.exports = nest;
Expand Down

0 comments on commit ea69608

Please sign in to comment.