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

Fix: Allow for third parameter in no-hooks-from-ancestor-modules #231

Merged
merged 1 commit into from
Jun 4, 2022
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
6 changes: 6 additions & 0 deletions docs/rules/no-hooks-from-ancestor-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ The following patterns are not warnings:
QUnit.module("example module", function(hooks) {
hooks.beforeEach(function() {});
});

QUnit.module("outer module", function() {
QUnit.module("inner module", function(hooks) {
hooks.beforeEach(function() {});
});
});
```

## When Not To Use It
Expand Down
10 changes: 8 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,12 @@ module.exports = {
isInModuleCallbackBody(node);
}

function getCallbackArg(args) {
// Callback can be either args[1] or args[2]
// https://api.qunitjs.com/QUnit/module/
return args.slice(1, 3).find(arg => arg.type === "FunctionExpression");
}

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 +84,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
36 changes: 36 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() {}); });
`,
`
Krinkle marked this conversation as resolved.
Show resolved Hide resolved
QUnit.module("module-a", function() {
QUnit.module("module-b", function(hooks) {
hooks.beforeEach(function() {});
Expand Down Expand Up @@ -140,6 +146,36 @@ ruleTester.run("no-hooks-from-ancestor-modules", rule, {
})
]
},
{
code: `
QUnit.module("module-a", {}, function (hooks) {
QUnit.module("module-b", {}, function () {
hooks.beforeEach(function () {});
});
});
`,
errors: [
createError({
invokedMethodName: "beforeEach",
usedHooksIdentifierName: "hooks"
})
]
},
{
code: `
QUnit.module("module-a", makeOptions(), function (hooks) {
QUnit.module("module-b", makeOptions(), function () {
hooks.beforeEach(function () {});
});
});
`,
errors: [
createError({
invokedMethodName: "beforeEach",
usedHooksIdentifierName: "hooks"
})
]
},
{
code: `
QUnit.module("first", function (firstHooks) {
Expand Down