Skip to content

Commit

Permalink
Calling a .cb test with the first argument being a reference disabl…
Browse files Browse the repository at this point in the history
…es the rule (#234)
  • Loading branch information
Guillaume Martigny authored and sindresorhus committed May 4, 2019
1 parent 0bdb6b4 commit b8549df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
28 changes: 20 additions & 8 deletions rules/test-ended.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,40 @@ const util = require('../util');

const create = context => {
const ava = createAvaRule();
const hasCbModifier = () => ava.hasTestModifier('cb');
let endCalled = false;

return ava.merge({
MemberExpression: visitIf([
ava.isInTestFile,
ava.isInTestNode
ava.isInTestNode,
hasCbModifier
])(node => {
if (ava.hasTestModifier('cb') &&
node.object.name === 't' &&
if (node.object.name === 't' &&
node.property.name === 'end'
) {
endCalled = true;
}
}),
'CallExpression:exit': visitIf([
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode
ava.isTestNode,
hasCbModifier
])(node => {
if (!ava.hasTestModifier('cb')) {
return;
const firstArg = node.arguments[0];
if (node.callee.property.name === 'cb' && firstArg.type === 'Identifier') {
const scope = context.getScope();
const exists = scope.references.some(ref => ref.identifier.name === firstArg.name);
if (exists) {
endCalled = true;
}
}

}),
'CallExpression:exit': visitIf([
ava.isInTestFile,
ava.isTestNode,
hasCbModifier
])(node => {
// Leaving test function
if (endCalled) {
endCalled = false;
Expand Down
7 changes: 7 additions & 0 deletions test/test-ended.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ ruleTester.run('test-ended', rule, {
header + 'test.cb.only(t => { t.end(); });',
header + 'test.cb.skip.only(t => { t.end(); });',
header + 'test.only.cb.skip(t => { t.end(); });',
// Detecting that the called function has `end()` is not required #119
header + 'const macro = t => {};\ntest.cb(macro);',
// Shouldn't be triggered since it's not a callback test
header + 'test(t => { t.pass(); });',
// Shouldn't be triggered since it's not a test file
Expand All @@ -32,6 +34,11 @@ ruleTester.run('test-ended', rule, {
code: header + 'test.cb(function (t) { t.pass(); });',
errors
},
{
// Detecting that the called function has `end()` can turn into a recursive resolution nightmare
code: header + 'const macro = t => t.end();\ntest.cb((t) => macro(t));',
errors
},
{
code: header + 'test.cb(t => { t.pass(); });',
errors
Expand Down

0 comments on commit b8549df

Please sign in to comment.