Skip to content
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

Improve detection if done callback is handled #23

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@ module.exports = function (context) {
});
}

function isReferenceHandled(reference) {
var parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;

return parent.type === 'CallExpression';
}

function hasHandledReferences(references) {
return references.some(isReferenceHandled);
}

function checkAsyncMochaFunction(functionExpression) {
var scope = context.getScope(),
callback = functionExpression.params[0],
callbackName = callback.name,
callbackVariable = findParamInScope(callbackName, scope);

if (callbackVariable && callbackVariable.references.length === 0) {
if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ ruleTester.run('handle-done-callback', rules['handle-done-callback'], {
code: 'it("", (done) => { });',
ecmaFeatures: { arrowFunctions: true },
errors: [ { message: 'Expected "done" callback to be handled.', column: 9, line: 1 } ]
},
{
code: 'it("", function (done) { return done; });',
errors: [ { message: 'Expected "done" callback to be handled.', column: 18, line: 1 } ]
},
{
code: 'it("", function (done) { done; });',
errors: [ { message: 'Expected "done" callback to be handled.', column: 18, line: 1 } ]
},
{
code: 'it("", function (done) { var foo = done; });',
errors: [ { message: 'Expected "done" callback to be handled.', column: 18, line: 1 } ]
}
]
});