documentation/lib/lint.js
Tom MacWright 631c6925d4 feat(core): Switch to Promises everywhere. Adopt Node v4 ES6 (#648)
* feat(core): Switch to Promises everywhere. Adopt Node v4 ES6

Big changes:

* Uses template strings where appropriate
* Config and argument parsing is unified and there is no such thing
  as formatterOptions anymore. All user-passed options go through
  mergeConfig.
* The node API surface changed (again): `buildSync` is removed,
  building operations return Promises.
* Now using Flow for internal type annotations.

More changes:

* Remove buildSync command
* feat(inference): Partially implement object shorthand support
* Refs #649
* Use Flow annotations to  enforce types
* Keep flow but switch to comment syntax
* Clarify types
* More flow improvements
* Turn server into class
* LinkerStack becomes class too
* Fix comment description type
* Run flow on lint
* Many more flow fixes
* More intense flow refactoring
* Simplify inference steps
* Update inference tests, flow errors down to 1
* Continue refining types
* Fix more flow issues
* Use 'use strict' everywhere
* Make 'ast' property configurable
* Fix many tests
* Fix more tests
* Fix more tests
* Fix augments
* Test Markdown meta support
* Improve test coverage
* Switch back from for of to for for speed
2017-01-27 16:14:19 -05:00

88 lines
2.1 KiB
JavaScript

'use strict';
/* @flow */
var VFile = require('vfile'),
walk = require('../lib/walk'),
vfileSort = require('vfile-sort'),
reporter = require('vfile-reporter');
var CANONICAL = {
'String': 'string',
'Boolean': 'boolean',
'Undefined': 'undefined',
'Number': 'number',
'array': 'Array',
'date': 'Date',
'object': 'Object'
};
/**
* Passively lints and checks documentation data.
*
* @name lint
* @param {Object} comment parsed comment
* @returns {Array<Object>} array of errors
*/
function lintComments(comment/*: Comment*/) {
comment.tags.forEach(function (tag) {
function nameInvariant(name) {
if (name && typeof CANONICAL[name] === 'string') {
comment.errors.push({
message: 'type ' + name + ' found, ' + CANONICAL[name] + ' is standard',
commentLineNumber: tag.lineNumber
});
}
}
function checkCanonical(type) {
if (type.type === 'NameExpression') {
nameInvariant(type.name);
}
if (type.elements) {
checkSubtypes(type.elements);
}
if (type.applications) {
checkSubtypes(type.applications);
}
}
function checkSubtypes(subtypes) {
if (Array.isArray(subtypes)) {
subtypes.forEach(checkCanonical);
}
}
if (tag.type && typeof tag.type === 'object') {
checkCanonical(tag.type);
}
});
return comment;
}
/**
* @private
* Extract lint instructions from comments and generate user-readable output.
* @param {Array<Object>} comments a list of comments
* @return {string} user-readable output
*/
function formatLint(comments/*: Array<Comment>*/)/*: string */ {
var vFiles = {};
walk(comments, function (comment) {
comment.errors.forEach(function (error) {
var p = comment.context.file;
vFiles[p] = vFiles[p] || new VFile({
path: p
});
vFiles[p].warn(error.message, {
line: comment.loc.start.line + (error.commentLineNumber || 0)
});
});
});
return reporter(Object.keys(vFiles).map(p => vfileSort(vFiles[p])));
}
module.exports.lintComments = lintComments;
module.exports.formatLint = formatLint;