0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/eslint-rules/documented-errors.js
James M Snell 76b8803630 tools: add eslint rule for documented errors
PR-URL: https://github.com/nodejs/node/pull/16450
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
2017-10-26 07:58:29 -07:00

47 lines
1.2 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const doc = fs.readFileSync(path.resolve(__dirname, '../../doc/api/errors.md'),
'utf8');
function isInDoc(code) {
return doc.match(`### ${code}`) != null;
}
function includesAnchor(code) {
return doc.match(`<a id="${code}"></a>`) != null;
}
function errorForNode(node) {
return node.expression.arguments[0].value;
}
function isDefiningError(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee &&
node.expression.callee.name === 'E';
}
module.exports = {
create: function(context) {
return {
ExpressionStatement: function(node) {
if (!isDefiningError(node)) return;
const code = errorForNode(node);
if (!isInDoc(code)) {
const message = `"${code}" is not documented in doc/api/errors.md`;
context.report({ node, message });
}
if (!includesAnchor(code)) {
const message =
`doc/api/errors.md does not have an anchor for "${code}"`;
context.report({ node, message });
}
}
};
}
};