Skip to content

Commit 3925e40

Browse files
cvxbmish
andauthored
fix: error in function contexts for no-hooks-from-ancestor-modules (#615)
* fix: no-hooks-from-ancestor-modules in function contexts Fixes an error: ``` TypeError: Cannot read properties of undefined (reading 'hookIdentifierName') ``` * implement suggestions from the review * Update lib/rules/no-hooks-from-ancestor-modules.js Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com> --------- Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>
1 parent 24ccbf5 commit 3925e40

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

lib/rules/no-hooks-from-ancestor-modules.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ module.exports = {
120120
if (node.arguments.length === 0) {
121121
return;
122122
}
123-
if (
124-
node.arguments[0].type !== "Literal" ||
125-
node.arguments[0].value === null ||
126-
node.arguments[0].value === undefined
127-
) {
128-
return;
129-
}
123+
124+
const arg = node.arguments[0];
125+
const description =
126+
arg.type === "Literal" && typeof arg.value === "string"
127+
? arg.value
128+
: context.getSourceCode().getText(arg);
129+
130130
/** @type {{callExpression: import('eslint').Rule.Node, description: string, hookIdentifierName?: string | null}} */
131131
const moduleStackInfo = {
132132
callExpression: node,
133-
description: node.arguments[0].value.toString(),
133+
description: description,
134134
};
135135
const callback = getCallbackArg(node.arguments);
136136
if (

tests/lib/rules/no-hooks-from-ancestor-modules.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ ruleTester.run("no-hooks-from-ancestor-modules", rule, {
4242
QUnit.module("module");
4343
`,
4444
`
45+
QUnit.module(123);
46+
`,
47+
`
48+
QUnit.module();
49+
`,
50+
`
51+
QUnit.module(undefined);
52+
`,
53+
`
54+
QUnit.module(null);
55+
`,
56+
`
57+
QUnit.module(someVariable);
58+
`,
59+
`
4560
QUnit.module("module", function() { test("it1", function() {}); });
4661
`,
4762
`
@@ -131,6 +146,13 @@ ruleTester.run("no-hooks-from-ancestor-modules", rule, {
131146
});
132147
});
133148
`,
149+
`
150+
function foo(name) {
151+
QUnit.module(name, function (hooks) {
152+
hooks.beforeEach(function () {});
153+
});
154+
}
155+
`,
134156

135157
{
136158
// TypeScript: module callback is adding a type to `this`
@@ -302,5 +324,23 @@ ruleTester.run("no-hooks-from-ancestor-modules", rule, {
302324
}),
303325
],
304326
},
327+
328+
{
329+
code: `
330+
function foo(name) {
331+
QUnit.module(\`\${name} parent\`, function (hooks) {
332+
QUnit.module(name, function () {
333+
hooks.beforeEach(function () {});
334+
});
335+
});
336+
}
337+
`,
338+
errors: [
339+
createError({
340+
invokedMethodName: "beforeEach",
341+
usedHooksIdentifierName: "hooks",
342+
}),
343+
],
344+
},
305345
],
306346
});

0 commit comments

Comments
 (0)