no_useless_catch

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUselessCatchRule = rule.Rule{
	Name: "no-useless-catch",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindCatchClause: func(node *ast.Node) {
				catchClause := node.AsCatchClause()
				if catchClause == nil {
					return
				}

				if catchClause.VariableDeclaration == nil {
					return
				}

				varDecl := catchClause.VariableDeclaration.AsVariableDeclaration()
				if varDecl == nil || varDecl.Name() == nil {
					return
				}
				paramName := varDecl.Name()
				if paramName.Kind != ast.KindIdentifier {
					return
				}
				catchParamText := paramName.AsIdentifier().Text

				if catchClause.Block == nil {
					return
				}
				block := catchClause.Block.AsBlock()
				if block == nil || block.Statements == nil || len(block.Statements.Nodes) == 0 {
					return
				}

				stmt := block.Statements.Nodes[0]
				if stmt == nil || stmt.Kind != ast.KindThrowStatement {
					return
				}

				throwStmt := stmt.AsThrowStatement()
				if throwStmt == nil || throwStmt.Expression == nil {
					return
				}
				thrown := ast.SkipParentheses(throwStmt.Expression)
				if thrown == nil || thrown.Kind != ast.KindIdentifier {
					return
				}

				if thrown.AsIdentifier().Text != catchParamText {
					return
				}

				tryStmt := node.Parent
				if tryStmt == nil || tryStmt.Kind != ast.KindTryStatement {
					return
				}
				tryData := tryStmt.AsTryStatement()
				if tryData == nil {
					return
				}

				if tryData.FinallyBlock != nil {

					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "unnecessaryCatchClause",
						Description: "Unnecessary catch clause.",
					})
				} else {

					ctx.ReportNode(tryStmt, rule.RuleMessage{
						Id:          "unnecessaryCatch",
						Description: "Unnecessary try/catch wrapper.",
					})
				}
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-useless-catch

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL