Skip to content

Commit

Permalink
Fix: Allow for third parameter in no-hooks-from-ancestor-modules
Browse files Browse the repository at this point in the history
Fixes #230.
  • Loading branch information
Krinkle committed May 13, 2022
1 parent f3878c1 commit 9382c93
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/rules/no-hooks-from-ancestor-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ module.exports = {
isInModuleCallbackBody(node);
}

function getCallbackArg(args) {
// Callback can be either args[1] or args[2]
// https://api.qunitjs.com/QUnit/module/
if (args[1] && args[1].type === "FunctionExpression") {
return args[1];
}
if (args[2] && args[2].type === "FunctionExpression") {
return args[2];
}
return null;
}

function getHooksIdentifierFromParams(params) {
// In TypeScript, `this` can be passed as the first function parameter to add a type to it,
// and we want to ignore that parameter since we're looking for the `hooks` variable.
Expand All @@ -78,8 +90,8 @@ module.exports = {
description: node.arguments[0].value
};

const callback = node.arguments[1];
const hooksParam = callback && callback.type === "FunctionExpression" ? getHooksIdentifierFromParams(callback.params) : null;
const callback = getCallbackArg(node.arguments);
const hooksParam = callback ? getHooksIdentifierFromParams(callback.params) : null;
moduleStackInfo.hookIdentifierName = hooksParam ? hooksParam.name : null;
moduleStack.push(moduleStackInfo);
} else if (isHookInvocation(node)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-hooks-from-ancestor-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ ruleTester.run("no-hooks-from-ancestor-modules", rule, {
QUnit.module("module", function(hooks) { hooks.afterEach(function() {}); });
`,
`
QUnit.module("module", {}, function(hooks) { hooks.beforeEach(function() {}); });
`,
`
QUnit.module("module", makeOptions(), function(hooks) { hooks.beforeEach(function() {}); });
`,
`
QUnit.module("module-a", function() {
QUnit.module("module-b", function(hooks) {
hooks.beforeEach(function() {});
Expand Down

0 comments on commit 9382c93

Please sign in to comment.