防止Screen.findbyx无需等待无限制的Syntax而没有等待?
此中等文章为了防止等待
在属性之前:
"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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此AST选择器似乎有效:
:不(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:
Standalone Functions
For function calls that aren't called by objects (e.g. just
findByRole
instead ofscreen.findByRole
), you can omit theproperty
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.