mirror of https://github.com/lukechilds/node.git
Browse Source
This adds an ESLint rule to enforce the use of `assert.ifError(err)` instead of `if (err) throw err;` in tests. PR-URL: https://github.com/nodejs/node/pull/10671 Ref: https://github.com/nodejs/node/pull/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>v6
Teddy Katz
8 years ago
committed by
James M Snell
10 changed files with 63 additions and 21 deletions
@ -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)} |
|||
}); |
|||
} |
|||
} |
|||
}; |
|||
} |
|||
}; |
Loading…
Reference in new issue