-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint rule for runtime deprecations
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters