Skip to content

Commit

Permalink
fix(valid-describe-callback): False positive when using new options a…
Browse files Browse the repository at this point in the history
…rgument

Fixes #266
  • Loading branch information
mskelton committed Feb 28, 2024
1 parent 99f11c7 commit c25d200
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 10 additions & 3 deletions src/rules/valid-describe-callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ runRuleTester('valid-describe-callback', rule, {
code: 'test.describe("foo")',
errors: [{ column: 15, line: 1, messageId: 'nameAndCallback' }],
},
{
code: 'test.describe("foo", { tag: ["@slow"] });',
errors: [{ column: 15, line: 1, messageId: 'invalidCallback' }],
},
{
code: 'test.describe("foo", "foo2")',
errors: [
{ column: 15, line: 1, messageId: 'secondArgumentMustBeFunction' },
],
errors: [{ column: 15, line: 1, messageId: 'invalidCallback' }],
},
{
code: 'test.describe("foo", foo2)',
errors: [{ column: 15, line: 1, messageId: 'invalidCallback' }],
},
{
code: 'test.describe()',
Expand Down Expand Up @@ -165,6 +171,7 @@ runRuleTester('valid-describe-callback', rule, {
'test.describe("foo", function() {})',
'test.describe("foo", () => {})',
'test.describe(`foo`, () => {})',
'test.describe("another suite", { tag: ["@slow"] }, () => {});',
'test.describe.only("foo", () => {})',
'describe("foo", () => {})',
'describe.only("foo", () => {})',
Expand Down
23 changes: 14 additions & 9 deletions src/rules/valid-describe-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const paramsLocation = (
};
};

function parseArgs(node: ESTree.CallExpression) {
const [name, b, c] = node.arguments;
const options = node.arguments.length === 2 ? b : undefined;
const callback = node.arguments.length === 3 ? c : b;

return [name, options, callback] as const;
}

export default {
create(context) {
return {
Expand All @@ -27,16 +35,16 @@ export default {
return;
}

const [name, _, callback] = parseArgs(node);

if (node.arguments.length < 1) {
return context.report({
loc: node.loc!,
messageId: 'nameAndCallback',
});
}

const [, callback] = node.arguments;

if (!callback) {
if (!name || !callback) {
context.report({
loc: paramsLocation(node.arguments),
messageId: 'nameAndCallback',
Expand All @@ -48,7 +56,7 @@ export default {
if (!isFunction(callback)) {
context.report({
loc: paramsLocation(node.arguments),
messageId: 'secondArgumentMustBeFunction',
messageId: 'invalidCallback',
});

return;
Expand All @@ -61,10 +69,7 @@ export default {
});
}

if (
call.members.every((s) => getStringValue(s) !== 'each') &&
callback.params.length
) {
if (callback.params.length) {
context.report({
loc: paramsLocation(callback.params),
messageId: 'unexpectedDescribeArgument',
Expand Down Expand Up @@ -99,9 +104,9 @@ export default {
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/valid-describe-callback.md',
},
messages: {
invalidCallback: 'Callback argument must be a function',
nameAndCallback: 'Describe requires name and callback arguments',
noAsyncDescribeCallback: 'No async describe callback',
secondArgumentMustBeFunction: 'Second argument must be function',
unexpectedDescribeArgument: 'Unexpected argument(s) in describe callback',
unexpectedReturnInDescribe:
'Unexpected return statement in describe callback',
Expand Down

0 comments on commit c25d200

Please sign in to comment.