Skip to content

Commit ea69608

Browse files
anthony-redFoxtmcw
authored andcommitted
fix: Report nesting errors instead of throwing them as errors
Check on parent element: foo.bar sub-parametr should be documeted with parent. Fixed #832 issue
1 parent 15239d1 commit ea69608

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

__tests__/lib/lint.js

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ test('lintComments', function() {
2828
{ message: 'Missing or invalid tag name' }
2929
]);
3030

31+
expect(
32+
evaluate(function() {
33+
/**
34+
* @param {Object} foo.bar
35+
*/
36+
}).errors
37+
).toEqual([{ commentLineNumber: 1, message: 'Parent of foo.bar not found' }]);
38+
3139
expect(
3240
evaluate(function() {
3341
/**

declarations/comment.js

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ type Comment = {
7171
tags: Array<CommentTag>,
7272

7373
augments: Array<CommentTag>,
74-
errors: Array<CommentExample>,
7574
examples: Array<CommentExample>,
7675
params: Array<CommentTag>,
7776
properties: Array<CommentTag>,

src/lint.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var VFile = require('vfile');
44
import { walk } from './walk';
55
import vfileSort from 'vfile-sort';
66
import reporter from 'vfile-reporter';
7+
import nest from './nest';
78

89
var CANONICAL = {
910
String: 'string',
@@ -27,11 +28,8 @@ function lintComments(comment: Comment) {
2728
function nameInvariant(name) {
2829
if (name && typeof CANONICAL[name] === 'string') {
2930
comment.errors.push({
30-
message: 'type ' +
31-
name +
32-
' found, ' +
33-
CANONICAL[name] +
34-
' is standard',
31+
message:
32+
'type ' + name + ' found, ' + CANONICAL[name] + ' is standard',
3533
commentLineNumber: tag.lineNumber
3634
});
3735
}
@@ -60,6 +58,8 @@ function lintComments(comment: Comment) {
6058
checkCanonical(tag.type);
6159
}
6260
});
61+
nest(comment);
62+
6363
return comment;
6464
}
6565

src/nest.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var tagDepth = tag => tag.name.split(PATH_SPLIT).length;
3333
* @returns {Object} nested comment
3434
*/
3535
var nestTag = (
36-
tags: Array<CommentTag>
36+
tags: Array<CommentTag>,
37+
errors: Array<CommentError>
3738
// Use lodash here both for brevity and also because, unlike JavaScript's
3839
// sort, it's stable.
3940
) =>
@@ -59,16 +60,22 @@ var nestTag = (
5960

6061
if (!child) {
6162
if (tag.name.match(/^(\$\d+)/)) {
62-
throw new Error(
63-
`Parent of ${tag.name} not found. To document a destructuring\n` +
63+
errors.push({
64+
message:
65+
`Parent of ${tag.name} not found. To document a destructuring\n` +
6466
`type, add a @param tag in its position to specify the name of the\n` +
65-
`destructured parameter`
66-
);
67+
`destructured parameter`,
68+
commentLineNumber: tag.lineNumber
69+
});
70+
} else {
71+
errors.push({
72+
message: `Parent of ${tag.name} not found`,
73+
commentLineNumber: tag.lineNumber
74+
});
6775
}
68-
throw new Error(`Parent of ${tag.name} not found`);
76+
} else {
77+
insertTag(child, parts.slice(1));
6978
}
70-
71-
insertTag(child, parts.slice(1));
7279
}
7380
}
7481

@@ -91,9 +98,9 @@ var nestTag = (
9198
* @return {Object} nested comment
9299
*/
93100
var nest = (comment: Comment) =>
94-
_.assign(comment, {
95-
params: nestTag(comment.params),
96-
properties: nestTag(comment.properties)
101+
Object.assign(comment, {
102+
params: nestTag(comment.params, comment.errors),
103+
properties: nestTag(comment.properties, comment.errors)
97104
});
98105

99106
module.exports = nest;

0 commit comments

Comments
 (0)