|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { isDefiningDeprecation } = require('./rules-utils.js'); |
| 6 | + |
| 7 | +const patternToMatch = /^DEP\d+$/; |
| 8 | + |
| 9 | +const mdFile = 'doc/api/deprecations.md'; |
| 10 | +const doc = fs.readFileSync(path.resolve(__dirname, '../..', mdFile), 'utf8'); |
| 11 | + |
| 12 | +function isInDoc(code) { |
| 13 | + return doc.includes(`### ${code}:`); |
| 14 | +} |
| 15 | + |
| 16 | +function includesAnchor(code) { |
| 17 | + return doc.includes(`<a id="${code}"></a>`); |
| 18 | +} |
| 19 | + |
| 20 | +function getDeprecationCode(node) { |
| 21 | + return node.expression.arguments[2].value; |
| 22 | +} |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + create: function(context) { |
| 26 | + return { |
| 27 | + ExpressionStatement: function(node) { |
| 28 | + if (!isDefiningDeprecation(node) || !getDeprecationCode(node)) return; |
| 29 | + const code = getDeprecationCode(node); |
| 30 | + if (!patternToMatch.test(code)) { |
| 31 | + const message = `"${code}" does not match the expected pattern`; |
| 32 | + context.report({ node, message }); |
| 33 | + } |
| 34 | + if (!isInDoc(code)) { |
| 35 | + const message = `"${code}" is not documented in ${mdFile}`; |
| 36 | + context.report({ node, message }); |
| 37 | + } |
| 38 | + if (!includesAnchor(code)) { |
| 39 | + const message = `${mdFile} does not have an anchor for "${code}"`; |
| 40 | + context.report({ node, message }); |
| 41 | + } |
| 42 | + }, |
| 43 | + }; |
| 44 | + }, |
| 45 | +}; |
0 commit comments