-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tools: eslint rule for missing internal/error doc #16450
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 }); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could be missing something, but do we need to have this test file if we already have the lint rule, and it's enabled for
lib/internal/errors.js
? In theory the lint rule should run as part ofmake lint
, right? 😬There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test file tests the lint rule --
make lint
runs it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test file is intended to test the lint rule itself. For example, if the rule had a bug where it never reported any errors, we probably wouldn't notice it when running
make lint
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 sorry, my bad, I read
RuleTester
as running the rule.