-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add prefer-t-throws
rule
#345
Open
Mesteery
wants to merge
1
commit into
avajs:main
Choose a base branch
from
Mesteery:prefer-t-throws-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−0
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Prefer using `t.throws()` or `t.throwsAsync()` over try/catch | ||
|
||
This rule will enforce the use of `t.throws()` or `t.throwsAsync()` when possible. | ||
|
||
## Fail | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
await throwingFunction(); | ||
t.fail(); | ||
} catch (error) { | ||
t.is(error.message, 'Unicorn overload'); | ||
} | ||
}); | ||
``` | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
await potentiallyThrowingFunction(); | ||
await anotherPromise; | ||
await timeout(100, 'Unicorn timeout'); | ||
t.fail(); | ||
} catch (error) { | ||
t.ok(error.message.startsWith('Unicorn')); | ||
} | ||
}); | ||
``` | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
try { | ||
synchronousThrowingFunction(); | ||
t.fail(); | ||
} catch (error) { | ||
t.is(error.message, 'Missing Unicorn argument'); | ||
} | ||
}); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const test = require('ava'); | ||
|
||
test('some test', async t => { | ||
await t.throwsAsync(asyncThrowingFunction(), {message: 'Unicorn overload'}); | ||
}); | ||
``` |
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,140 @@ | ||
'use strict'; | ||
|
||
const {visitIf} = require('enhance-visitors'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
const util = require('../util'); | ||
|
||
// This function checks if there is an AwaitExpression, which is not inside another function. | ||
// | ||
// TODO: find a simpler way to do this | ||
function hasAwaitExpression(nodes) { | ||
Mesteery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!nodes) { | ||
return false; | ||
} | ||
|
||
for (const node of nodes) { | ||
if (!node) { | ||
continue; | ||
} | ||
|
||
if (node.type === 'ExpressionStatement' && hasAwaitExpression([node.expression])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'AwaitExpression') { | ||
return true; | ||
} | ||
|
||
if (node.expressions && hasAwaitExpression(node.expressions)) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'BlockStatement' && hasAwaitExpression(node.body)) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'MemberExpression' && hasAwaitExpression([node.object, node.property])) { | ||
return true; | ||
} | ||
|
||
if ((node.type === 'CallExpression' || node.type === 'NewExpression') | ||
&& hasAwaitExpression([...node.arguments, node.callee])) { | ||
return true; | ||
} | ||
|
||
if (node.left && node.right && hasAwaitExpression([node.left, node.right])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'SequenceExpression' && hasAwaitExpression(node.expressions)) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'VariableDeclaration' | ||
&& hasAwaitExpression(node.declarations.map(declaration => declaration.init))) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'ThrowStatement' && hasAwaitExpression([node.argument])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'IfStatement' && hasAwaitExpression([node.test, node.consequent, node.alternate])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'SwitchStatement' | ||
// eslint-disable-next-line unicorn/prefer-spread | ||
&& hasAwaitExpression([node.discriminant, ...node.cases.flatMap(caseNode => [caseNode.test].concat(caseNode.consequent))])) { | ||
return true; | ||
} | ||
|
||
if (node.type.endsWith('WhileStatement') && hasAwaitExpression([node.test, node.body])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'ForStatement' && hasAwaitExpression([node.init, node.test, node.update, node.body])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'ForInStatement' && hasAwaitExpression([node.right, node.body])) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'ForOfStatement' && (node.await || hasAwaitExpression([node.right, node.body]))) { | ||
return true; | ||
} | ||
|
||
if (node.type === 'WithStatement' && hasAwaitExpression([node.object, node.body])) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
return ava.merge({ | ||
TryStatement: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode, | ||
])(node => { | ||
const nodes = node.block.body; | ||
if (nodes.length < 2) { | ||
return; | ||
} | ||
|
||
const tFailIndex = [...nodes].reverse().findIndex(node => node.type === 'ExpressionStatement' | ||
&& node.expression.type === 'CallExpression' | ||
&& node.expression.callee.object | ||
&& node.expression.callee.object.name === 't' | ||
&& node.expression.callee.property | ||
&& node.expression.callee.property.name === 'fail'); | ||
|
||
// Return if there is no t.fail() or if it's the first node | ||
if (tFailIndex === -1 || tFailIndex === nodes.length - 1) { | ||
return; | ||
} | ||
|
||
const beforeNodes = nodes.slice(0, nodes.length - 1 - tFailIndex); | ||
|
||
context.report({ | ||
node, | ||
message: `Prefer using the \`t.throws${hasAwaitExpression(beforeNodes) ? 'Async' : ''}()\` assertion.`, | ||
}); | ||
}), | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: util.getDocsUrl(__filename), | ||
}, | ||
schema: [], | ||
}, | ||
}; |
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,68 @@ | ||
'use strict'; | ||
|
||
const test = require('ava'); | ||
const avaRuleTester = require('eslint-ava-rule-tester'); | ||
const rule = require('../rules/prefer-t-throws'); | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
}, | ||
}); | ||
|
||
const header = 'const test = require(\'ava\');\n'; | ||
|
||
ruleTester.run('prefer-t-throws', rule, { | ||
valid: [ | ||
`${header}test(async t => { const error = await t.throwsAsync(promise); t.is(error, 'error'); });`, | ||
`${header}test(t => { const error = t.throws(fn()); t.is(error, 'error'); });`, | ||
`${header}test(async t => { try { t.fail(); unicorn(); } catch (error) { t.is(error, 'error'); } });`, | ||
`${header}test(async t => { try { await promise; } catch (error) { t.is(error, 'error'); } });`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `${header}test(async t => { try { async function unicorn() { throw await Promise.resolve('error') }; unicorn(); t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throws()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { await Promise.reject('error'); t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { if (await promise); t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { (await 1) > 2; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { (await getArray())[0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { getArraySync(await 20)[0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { getArraySync()[await 0]; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { new (await cl())(1); t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { if (false) { await promise; }; t.fail(); } catch (error) { t.is(error, 'error'); } });`, | ||
errors: [{message: 'Prefer using the `t.throwsAsync()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(t => { try { undefined(); t.fail(); } catch (error) { t.ok(error instanceof TypeError); } });`, | ||
errors: [{message: 'Prefer using the `t.throws()` assertion.'}], | ||
}, | ||
{ | ||
code: `${header}test(async t => { try { undefined(); t.fail(); } catch (error) { t.ok(error instanceof TypeError); } });`, | ||
errors: [{message: 'Prefer using the `t.throws()` assertion.'}], | ||
}, | ||
], | ||
}); | ||
Mesteery marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Maybe you could find an
AwaitExpression
, then iterate upwards through its parent scopes until you encounter a function, and then check the function.https://eslint.org/docs/developer-guide/scope-manager-interface
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.
What about the
ReturnStatement
? I doubt there's anything simpler than that in the end.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.
Do you have a realistic example of when someone would actually use
return
in a try/catch when testing an error?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.
No, I don't have one. It's pretty hard to find a realistic example but I have a feeling that there might be some code written that way.
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.
Rule logic should be based on test fixtures, not feelings. My recommendation to simplify the logic here stands.