Skip to content

Commit

Permalink
Add lint rule for runtime deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 29, 2020
1 parent ca5f40f commit e9139b6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/parallel/test-eslint-documented-deprecation-codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
common.skipIfEslintMissing();

const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/documented-deprecation-codes');

const mdFile = 'doc/api/deprecations.md';

const invalidCode = 'UNDOCUMENTED INVALID CODE';

new RuleTester().run('documented-deprecation-codes', rule, {
valid: [
`
deprecate(function() {
return this.getHeaders();
}, 'OutgoingMessage.prototype._headers is deprecated', 'DEP0066')
`
],
invalid: [
{
code: `
deprecate(function foo(){}, 'bar', '${invalidCode}');
`,
errors: [
{
message: `"${invalidCode}" does not match the expected pattern`,
line: 2
},
{
message: `"${invalidCode}" is not documented in ${mdFile}`,
line: 2
},
{
message:
`${mdFile} does not have an anchor for "${invalidCode}"`,
line: 2
}
]
}
]
});
45 changes: 45 additions & 0 deletions tools/eslint-rules/documented-deprecation-codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'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 includesAnchor(code) {
return doc.includes(`<a id="${code}"></a>`);
}

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 });
}
if (!includesAnchor(code)) {
const message = `${mdFile} does not have an anchor for "${code}"`;
context.report({ node, message });
}
},
};
},
};
8 changes: 8 additions & 0 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module.exports.isDefiningError = function(node) {
node.expression.arguments.length !== 0;
};

module.exports.isDefiningDeprecation = function(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee &&
node.expression.callee.name.endsWith('deprecate') &&
node.expression.arguments.length !== 0;
};

/**
* Returns true if any of the passed in modules are used in
* require calls.
Expand Down

0 comments on commit e9139b6

Please sign in to comment.