Skip to content

Commit 844f4b7

Browse files
committed
Use vfile-reporter to format error output
1 parent 85ff395 commit 844f4b7

File tree

11 files changed

+81
-70
lines changed

11 files changed

+81
-70
lines changed

bin/documentation.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var documentation = require('../'),
88
streamArray = require('stream-array'),
99
fs = require('fs'),
1010
vfs = require('vinyl-fs'),
11-
walk = require('../lib/walk'),
12-
formatError = require('../lib/format_error'),
11+
lint = require('../lib/lint'),
1312
args = require('../lib/args.js');
1413

1514
var parsedArgs = args(process.argv.slice(2)),
@@ -22,11 +21,15 @@ documentation(parsedArgs.inputs, parsedArgs.options, function (err, comments) {
2221
throw err;
2322
}
2423

25-
walk(comments, function (comment) {
26-
comment.errors.forEach(function (error) {
27-
console.error(formatError(comment, error));
28-
});
29-
});
24+
if (parsedArgs.options.lint) {
25+
var lintOutput = lint.format(comments);
26+
if (lintOutput) {
27+
console.log(lintOutput);
28+
process.exit(1);
29+
} else {
30+
process.exit(0);
31+
}
32+
}
3033

3134
formatter(comments, formatterOptions, function (err, output) {
3235
if (outputLocation !== 'stdout') {

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = function (indexes, options, callback) {
9292
return memo.concat(parseFn(file));
9393
}, [])
9494
.map(pipeline(
95-
lint,
95+
lint.lint,
9696
inferName(),
9797
inferKind(),
9898
inferParams(),

lib/args.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ function parse(args) {
1515
choices: ['json', 'md', 'html']
1616
})
1717

18-
.describe('lint', 'check output for common style and uniformity mistakes')
18+
.option('lint', {
19+
describe: 'check output for common style and uniformity mistakes',
20+
type: 'boolean'
21+
})
1922

2023
.describe('t', 'specify a theme: this must be a valid theme module')
2124
.alias('t', 'theme')
@@ -112,6 +115,7 @@ module.exports = function (args) {
112115
options: {
113116
private: argv.private,
114117
transform: transform,
118+
lint: argv.lint,
115119
github: argv.github,
116120
polyglot: argv.polyglot,
117121
order: config.order || [],

lib/format_error.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/lint.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
'use strict';
22

3+
var VFile = require('vfile'),
4+
walk = require('../lib/walk'),
5+
parseFilepath = require('parse-filepath'),
6+
vfileSort = require('vfile-sort'),
7+
reporter = require('vfile-reporter');
8+
39
var CANONICAL = {
410
'String': 'string',
511
'Boolean': 'boolean',
@@ -17,7 +23,7 @@ var CANONICAL = {
1723
* @param {Object} comment parsed comment
1824
* @returns {Array<Object>} array of errors
1925
*/
20-
module.exports = function (comment) {
26+
function lint(comment) {
2127
comment.tags.forEach(function (tag) {
2228
function nameInvariant(name) {
2329
if (CANONICAL[name]) {
@@ -46,4 +52,27 @@ module.exports = function (comment) {
4652
}
4753
});
4854
return comment;
49-
};
55+
}
56+
57+
function format(comments) {
58+
var vFiles = {};
59+
walk(comments, function (comment) {
60+
comment.errors.forEach(function (error) {
61+
var p = comment.context.file;
62+
var parts = parseFilepath(p);
63+
vFiles[p] = vFiles[p] || new VFile({
64+
directory: parts.dirname,
65+
filename: parts.basename
66+
});
67+
vFiles[p].warn(error.message, {
68+
line: comment.loc.start.line + error.commentLineNumber || 0
69+
});
70+
});
71+
});
72+
return reporter(Object.keys(vFiles).map(function (p) {
73+
return vfileSort(vFiles[p]);
74+
}));
75+
}
76+
77+
module.exports.lint = lint;
78+
module.exports.format = format;

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"strip-json-comments": "^1.0.2",
4444
"traverse": "^0.6.6",
4545
"unist-builder": "^1.0.0",
46+
"vfile": "^1.1.2",
47+
"vfile-reporter": "^1.4.1",
48+
"vfile-sort": "^1.0.0",
4649
"vinyl": "^0.5.0",
4750
"vinyl-fs": "^1.0.0",
4851
"yargs": "^3.5.4"

test/bin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ test('html with no destination', function (t) {
101101
});
102102
});
103103

104+
test('--lint option', function (t) {
105+
documentation(['--lint fixture/lint/lint.input.js'], function (err, data) {
106+
var output = path.join(__dirname, 'fixture/lint/lint.output.js');
107+
data = data.toString().split('\n').slice(2).join('\n');
108+
if (process.env.UPDATE) {
109+
fs.writeFileSync(output, data);
110+
}
111+
t.equal(err.code, 1);
112+
t.equal(data, fs.readFileSync(output, 'utf8'), 'outputs lint');
113+
t.end();
114+
});
115+
});
116+
117+
test('--lint option on good file', function (t) {
118+
documentation(['--lint fixture/simple.input.js'], {}, function (err, data) {
119+
t.equal(err, null);
120+
t.equal(data, '', 'no output');
121+
t.end();
122+
}, false);
123+
});
124+
104125
test('given no files', function (t) {
105126
documentation([''], function (err) {
106127
t.ok(err.toString()

test/fixture/lint/lint.input.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @param {String} foo bar
3+
* @memberof notfound
4+
*/

test/fixture/lint/lint.output.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1:1 warning could not determine @name for hierarchy
2+
2:1 warning type String found, string is standard
3+
3:1 warning @memberof reference to notfound not found
4+
5+
3 warnings

test/lib/error.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)