|
| 1 | +package no_useless_catch |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/microsoft/typescript-go/shim/ast" |
| 5 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 6 | +) |
| 7 | + |
| 8 | +// https://eslint.org/docs/latest/rules/no-useless-catch |
| 9 | +var NoUselessCatchRule = rule.Rule{ |
| 10 | + Name: "no-useless-catch", |
| 11 | + Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
| 12 | + return rule.RuleListeners{ |
| 13 | + ast.KindCatchClause: func(node *ast.Node) { |
| 14 | + catchClause := node.AsCatchClause() |
| 15 | + if catchClause == nil { |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + // The catch must have a parameter |
| 20 | + if catchClause.VariableDeclaration == nil { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + // The parameter must be a simple identifier (not destructured) |
| 25 | + varDecl := catchClause.VariableDeclaration.AsVariableDeclaration() |
| 26 | + if varDecl == nil || varDecl.Name() == nil { |
| 27 | + return |
| 28 | + } |
| 29 | + paramName := varDecl.Name() |
| 30 | + if paramName.Kind != ast.KindIdentifier { |
| 31 | + return |
| 32 | + } |
| 33 | + catchParamText := paramName.AsIdentifier().Text |
| 34 | + |
| 35 | + // The body must have exactly one statement |
| 36 | + if catchClause.Block == nil { |
| 37 | + return |
| 38 | + } |
| 39 | + block := catchClause.Block.AsBlock() |
| 40 | + if block == nil || block.Statements == nil || len(block.Statements.Nodes) != 1 { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // That statement must be a ThrowStatement |
| 45 | + stmt := block.Statements.Nodes[0] |
| 46 | + if stmt == nil || stmt.Kind != ast.KindThrowStatement { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // The throw argument must be an Identifier |
| 51 | + throwStmt := stmt.AsThrowStatement() |
| 52 | + if throwStmt == nil || throwStmt.Expression == nil { |
| 53 | + return |
| 54 | + } |
| 55 | + if throwStmt.Expression.Kind != ast.KindIdentifier { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // The thrown identifier must match the catch parameter name |
| 60 | + thrownText := throwStmt.Expression.AsIdentifier().Text |
| 61 | + if thrownText != catchParamText { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // Determine whether the parent TryStatement has a finally block |
| 66 | + tryStmt := node.Parent |
| 67 | + if tryStmt == nil || tryStmt.Kind != ast.KindTryStatement { |
| 68 | + return |
| 69 | + } |
| 70 | + tryData := tryStmt.AsTryStatement() |
| 71 | + if tryData == nil { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if tryData.FinallyBlock != nil { |
| 76 | + // Has finally: report on the catch clause |
| 77 | + ctx.ReportNode(node, rule.RuleMessage{ |
| 78 | + Id: "unnecessaryCatchClause", |
| 79 | + Description: "Unnecessary catch clause.", |
| 80 | + }) |
| 81 | + } else { |
| 82 | + // No finally: report on the try statement |
| 83 | + ctx.ReportNode(tryStmt, rule.RuleMessage{ |
| 84 | + Id: "unnecessaryCatch", |
| 85 | + Description: "Unnecessary try/catch wrapper.", |
| 86 | + }) |
| 87 | + } |
| 88 | + }, |
| 89 | + } |
| 90 | + }, |
| 91 | +} |
0 commit comments