This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add eslint rule for documented errors
PR-URL: nodejs/node#16450 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
- Loading branch information
Showing
3 changed files
with
81 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint documented-errors: "error" */ | ||
/* eslint alphabetize-errors: "error" */ | ||
|
||
'use strict'; | ||
|
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,34 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/documented-errors'); | ||
|
||
const invalidCode = 'UNDOCUMENTED ERROR CODE'; | ||
|
||
new RuleTester().run('documented-errors', rule, { | ||
valid: [ | ||
` | ||
E('ERR_ASSERTION', 'foo'); | ||
` | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
E('${invalidCode}', 'bar'); | ||
`, | ||
errors: [ | ||
{ | ||
message: `"${invalidCode}" is not documented in doc/api/errors.md`, | ||
line: 2 | ||
}, | ||
{ | ||
message: | ||
`doc/api/errors.md 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,46 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const doc = fs.readFileSync(path.resolve(__dirname, '../../doc/api/errors.md'), | ||
'utf8'); | ||
|
||
function isInDoc(code) { | ||
return doc.match(`### ${code}`) != null; | ||
} | ||
|
||
function includesAnchor(code) { | ||
return doc.match(`<a id="${code}"></a>`) != null; | ||
} | ||
|
||
function errorForNode(node) { | ||
return node.expression.arguments[0].value; | ||
} | ||
|
||
function isDefiningError(node) { | ||
return node.expression && | ||
node.expression.type === 'CallExpression' && | ||
node.expression.callee && | ||
node.expression.callee.name === 'E'; | ||
} | ||
|
||
module.exports = { | ||
create: function(context) { | ||
return { | ||
ExpressionStatement: function(node) { | ||
if (!isDefiningError(node)) return; | ||
const code = errorForNode(node); | ||
if (!isInDoc(code)) { | ||
const message = `"${code}" is not documented in doc/api/errors.md`; | ||
context.report({ node, message }); | ||
} | ||
if (!includesAnchor(code)) { | ||
const message = | ||
`doc/api/errors.md does not have an anchor for "${code}"`; | ||
context.report({ node, message }); | ||
} | ||
} | ||
}; | ||
} | ||
}; |