![](/img/trans.png)
[英]It is possible catch several exceptions with only one try-catch block?
[英]There is ESLint rule to avoid try-catch if there just one line?
假設我有這個代碼:
export class a {
private async func(){
try {
await something();
} catch (error) {
/////
}
}
}
當try塊中只有一行時,我想避免使用try-catch,而是像這樣使用catch:
await something().catch(err => {…});
有任何 ESLint 規則會為此引發錯誤嗎?
謝謝!
編輯 1
我嘗試根據 AST 對我的@typescript-eslint/parser
使用no-restricted-syntax
規則,但它不起作用:
"no-restricted-syntax": [
"error",
{
"selector": "TryStatement > BlockStatement[body.length=0]",
"message": "No try block for one line!"
}
],
您可以使用以下選擇器:
"selector": "TryStatement > BlockStatement[body.length=1] AwaitExpression"
這將捕獲任何包含一個只有一個子節點且包含等待表達式的塊的 try 語句。
如果你想不那么具體,你可以刪除“AwaitExpression”來捕獲任何只有一行的 try 語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.