Skip to content

Commit a848202

Browse files
committed
Simplify error parsing by attaching errors to comments
1 parent 9919f49 commit a848202

File tree

8 files changed

+23
-24
lines changed

8 files changed

+23
-24
lines changed

bin/documentation.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var documentation = require('../'),
88
path = require('path'),
99
fs = require('fs'),
1010
vfs = require('vinyl-fs'),
11+
formatError = require('../lib/error'),
1112
loadConfig = require('../lib/load_config.js'),
1213
args = require('../lib/args.js'),
1314
argv = args.parse(process.argv.slice(2));
@@ -65,13 +66,15 @@ documentation(inputs, {
6566
polyglot: argv.polyglot,
6667
order: config.order || [],
6768
shallow: argv.shallow
68-
}, function (err, result, lints) {
69+
}, function (err, result) {
6970
if (err) {
7071
throw err;
7172
}
7273

73-
lints.forEach(function (err) {
74-
console.error(err);
74+
result.forEach(function (comment) {
75+
comment.errors.forEach(function (error) {
76+
console.error(formatError(comment, error));
77+
});
7578
});
7679

7780
formatter(result, formatterOptions, function (err, output) {

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ module.exports = function (indexes, options, callback) {
5252
return inputStream.pipe(concat(function (inputs) {
5353
try {
5454

55+
var errors = [];
56+
5557
var docs = inputs
5658
.filter(filterJS)
5759
.reduce(function (memo, file) {
5860
return memo.concat(parse(file));
5961
}, [])
6062
.map(function (comment) {
63+
comment.errors = [];
6164
// compose nesting & membership to avoid intermediate arrays
62-
comment = nestParams(inferMembership(inferKind(inferName(comment))));
65+
comment = nestParams(inferMembership(inferKind(inferName(lint(comment)))));
6366
if (options.github) {
6467
comment = github(comment);
6568
}
@@ -68,9 +71,7 @@ module.exports = function (indexes, options, callback) {
6871
.sort(sort.bind(undefined, options.order))
6972
.filter(filterAccess.bind(undefined, options.private ? [] : undefined));
7073

71-
callback(null, docs, docs.reduce(function (memo, comment) {
72-
return memo.concat(lint(comment));
73-
}, []));
74+
callback(null, docs, errors);
7475
} catch (e) {
7576
callback(e);
7677
}

lib/error.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ var path = require('path');
66
/**
77
* Format an error message regarding a comment, prefixed with file name and line number.
88
*
9-
* @param {Tag|null} tag location in a javascript file, if any
109
* @param {Comment} comment a parsed comment
1110
* @param {string} error error message a string
1211
* @param {...*} varags format arguments
1312
* @returns {undefined} outputs to stderr
1413
*/
15-
module.exports = function (tag, comment, error) {
14+
module.exports = function (comment, error) {
1615
var relativePath = path.relative(process.cwd(), comment.context.file),
17-
lineNumber = (tag ? tag.lineNumber : 0) + comment.loc.start.line;
16+
lineNumber = comment.loc.start.line;
1817

1918
return format.apply(format, ['%s:%d: ' + error, relativePath, lineNumber]
2019
.concat(Array.prototype.slice.call(arguments, 3)));

lib/hierarchy.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ function inferHierarchy(comments) {
6060
var parent = nameIndex[comment.memberof];
6161

6262
if (!parent) {
63-
var err = error(null, comment, 'memberof reference to %s not found', comment.memberof);
64-
comment.errors = (comment.errors || []).concat(err);
65-
console.error(err);
63+
comment.errors.push('memberof reference to ' + comment.memberof + ' not found');
6664
continue;
6765
}
6866

@@ -73,9 +71,7 @@ function inferHierarchy(comments) {
7371

7472
default:
7573
if (!comment.scope) {
76-
var err2 = error(null, comment, 'found memberof but no @scope, @static, or @instance tag');
77-
comment.errors = (comment.errors || []).concat(err2);
78-
console.error(err2);
74+
comment.errors.push('found memberof but no @scope, @static, or @instance tag');
7975
continue;
8076
}
8177
parent.members[comment.scope].push(comment);

lib/lint.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module.exports = function (comment) {
2424
comment.tags.forEach(function (tag) {
2525
function nameInvariant(name) {
2626
if (CANONICAL[name]) {
27-
errors.push(error(tag, comment, 'type %s found, %s is standard', name, CANONICAL[name]));
27+
comment.errors.push(
28+
'type ' + name + ' found, ' + CANONICAL[name] + ' is standard');
2829
}
2930
}
3031

@@ -48,6 +49,5 @@ module.exports = function (comment) {
4849
checkCanonical(tag.type);
4950
}
5051
});
51-
return errors;
52+
return comment;
5253
};
53-

lib/nest_params.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ module.exports = function (comment) {
3030
if (parts.length > 1) {
3131
var parent = index[parts[0]];
3232
if (parent === undefined) {
33-
console.error(
34-
'@param %s\'s parent %s not found',
35-
param.name,
36-
parts[0]);
33+
result.errors.push(
34+
'@param ' + param.name + '\'s parent ' + parts[0] + ' not found');
3735
result.params.push(param);
3836
return;
3937
}

test/lib/hierarchy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ test('hierarchy - missing memberof', function (t) {
9494
});
9595

9696
t.equal(result.length, 1);
97-
t.equal(result[0].errors.length, 1);
97+
t.equal(result[0].errors[0], 'memberof reference to DoesNotExist not found', 'correct error message');
9898
t.end();
9999
});

test/lib/nest_params.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ test('nestParams - missing parent', function (t) {
5353
return 0;
5454
});
5555
t.equal(result[0].params.length, 1);
56+
t.equal(result[0].errors[0], '@param foo.bar\'s parent foo not found',
57+
'correct error message');
5658
t.equal(result[0].params[0].name, 'foo.bar');
5759
t.end();
5860
});

0 commit comments

Comments
 (0)