-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools,test: enforce assert.ifError with lint rule
This adds an ESLint rule to enforce the use of `assert.ifError(err)` instead of `if (err) throw err;` in tests. PR-URL: #10671 Ref: #10543 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Michal Zasso <[email protected]>
- Loading branch information
1 parent
775de9c
commit 5d31448
Showing
10 changed files
with
63 additions
and
21 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
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
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
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
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
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,42 @@ | ||
/** | ||
* @fileoverview Prohibit the `if (err) throw err;` pattern | ||
* @author Teddy Katz | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
function hasSameTokens(nodeA, nodeB) { | ||
const aTokens = sourceCode.getTokens(nodeA); | ||
const bTokens = sourceCode.getTokens(nodeB); | ||
|
||
return aTokens.length === bTokens.length && | ||
aTokens.every((token, index) => { | ||
return token.type === bTokens[index].type && | ||
token.value === bTokens[index].value; | ||
}); | ||
} | ||
|
||
return { | ||
IfStatement(node) { | ||
const firstStatement = node.consequent.type === 'BlockStatement' ? | ||
node.consequent.body[0] : | ||
node.consequent; | ||
if ( | ||
firstStatement && | ||
firstStatement.type === 'ThrowStatement' && | ||
hasSameTokens(node.test, firstStatement.argument) | ||
) { | ||
context.report({ | ||
node: firstStatement, | ||
message: 'Use assert.ifError({{argument}}) instead.', | ||
data: {argument: sourceCode.getText(node.test)} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |