summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/documented-deprecation-codes.js
blob: 3317f3c983cc5e67f7474d9e12d3630afdfc2474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'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 });
        }
      },
    };
  },
};