node/tools/eslint-rules/documented-errors.js
Antoine du Hamel 3ce4cef4e6
tools: enforce use of trailing commas in tools/
PR-URL: https://github.com/nodejs/node/pull/45889
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2022-12-18 16:39:39 +00:00

41 lines
1.0 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const { isDefiningError } = require('./rules-utils.js');
const doc = fs.readFileSync(path.resolve(__dirname, '../../doc/api/errors.md'),
'utf8');
function isInDoc(code) {
return doc.includes(`### \`${code}\``);
}
function includesAnchor(code) {
return doc.includes(`<a id="${code}"></a>`);
}
function errorForNode(node) {
return node.expression.arguments[0].value;
}
module.exports = {
create: function(context) {
return {
ExpressionStatement: function(node) {
if (!isDefiningError(node) || !errorForNode(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 });
}
},
};
},
};