防止Screen.findbyx无需等待无限制的Syntax而没有等待?

发布于 2025-01-23 03:28:52 字数 1049 浏览 1 评论 0原文

中等文章为了防止等待在属性之前:

"no-restricted-syntax": [
  "error",
  {
    "message": "promise.then is unnecessary when using async/await",
    "selector": "AwaitExpression[argument.callee.property.name=\" then\"]"
  }
]

但是我希望相反,我想限制这些:

expect(screen.findByRole(...))....;
screen.findByRole(...);

但是允许这些:

expect(await screen.findByRole(...))....;
await screen.findByRole(...);

我在我的覆盖下尝试了测试文件:

"no-restricted-syntax": [
  "error",
  {
    "message": "Find By queries must be followed by an await",
    "selector": ":not(AwaitExpression[argument.callee.property.name=/findBy.*/])"
  }
]

但是现在每行都显示该错误。我还尝试将*放在前面,希望它可以允许任何东西(*),除非我被禁止使用的语法表达式,否则没有骰子。

我该如何完成这项工作?

This medium article shows me how to prevent an await preceding a property:

"no-restricted-syntax": [
  "error",
  {
    "message": "promise.then is unnecessary when using async/await",
    "selector": "AwaitExpression[argument.callee.property.name=\" then\"]"
  }
]

But I want the opposite, I want to restrict these:

expect(screen.findByRole(...))....;
screen.findByRole(...);

but allow these:

expect(await screen.findByRole(...))....;
await screen.findByRole(...);

I tried this under my overrides for test files:

"no-restricted-syntax": [
  "error",
  {
    "message": "Find By queries must be followed by an await",
    "selector": ":not(AwaitExpression[argument.callee.property.name=/findBy.*/])"
  }
]

But now every line is showing that error. I also tried putting * in front, hoping it would allow anything (the *) unless is was followed by my banned syntax expression, but no dice.

How can I make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2025-01-30 03:28:52

此AST选择器似乎有效:

:不(awaitexpression)> callexpression [callee.property.name =/^Findby

整个规则可能看起来像:

"no-restricted-syntax": [
  "error",
  {
    "message": "findBy... queries must be preceded by an await",
    "selector": ":not(AwaitExpression) > CallExpression[callee.property.name=/^findBy.*$/]"
  }
]

独立函数的函数调用函数

,这些函数呼叫未由对象调用(例如findbyrole而不是screen.findbyrole.findbyrole),您可以省略属性选择器中的嵌套属性,例如:

:not(awaitexpression)> callexpression [callee.name =/^findby。*$/]

如果要匹配独立函数和方法函数,则可以有两个单独的规则,或将它们与:匹配: 。)选择器。

Try it out

You can try it out here on the ESLint Playground.

This AST selector seems to work:

:not(AwaitExpression) > CallExpression[callee.property.name=/^findBy.*$/]

The selector looks for direct children of non-await expressions that call a method beginning with findBy....

The whole rule may look like:

"no-restricted-syntax": [
  "error",
  {
    "message": "findBy... queries must be preceded by an await",
    "selector": ":not(AwaitExpression) > CallExpression[callee.property.name=/^findBy.*$/]"
  }
]

Standalone Functions

For function calls that aren't called by objects (e.g. just findByRole instead of screen.findByRole), you can omit the property nested attribute in the selector, like so:

:not(AwaitExpression) > CallExpression[callee.name=/^findBy.*$/]

If you want to match both Standalone functions and Method functions, you can have two separate rules, or combine them with the :matches(...) selector.

Try it out

You can try it out here on the ESLint Playground.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文