0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-25 08:19:38 +01:00
nodejs/tools/eslint-rules/documented-deprecation-codes.js
Antoine du Hamel 5d4da62514
tools: lint deprecation codes
Add a rule to make sure deprecation codes are in order.

PR-URL: https://github.com/nodejs/node/pull/41992
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-02-19 18:14:09 +01:00

38 lines
1.0 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const { isDefiningDeprecation } = require('./rules-utils.js');
const patternToMatch = /^DEP\d+$/;
const mdFile = 'doc/api/deprecations.md';
const doc = fs.readFileSync(path.resolve(__dirname, '../..', mdFile), 'utf8');
function isInDoc(code) {
return doc.includes(`### ${code}:`);
}
function getDeprecationCode(node) {
return node.expression.arguments[2].value;
}
module.exports = {
create: function(context) {
return {
ExpressionStatement: function(node) {
if (!isDefiningDeprecation(node) || !getDeprecationCode(node)) return;
const code = getDeprecationCode(node);
if (!patternToMatch.test(code)) {
const message = `"${code}" does not match the expected pattern`;
context.report({ node, message });
}
if (!isInDoc(code)) {
const message = `"${code}" is not documented in ${mdFile}`;
context.report({ node, message });
}
},
};
},
};