forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
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: nodejs#10671 Ref: nodejs#10543 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Michal Zasso <targos@protonmail.com>
- Loading branch information
1 parent
9da386d
commit 949c24e
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)} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |