default_case

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCaseRule = rule.Rule{
	Name: "default-case",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)

		return rule.RuleListeners{
			ast.KindSwitchStatement: func(node *ast.Node) {
				switchStmt := node.AsSwitchStatement()
				if switchStmt == nil {
					return
				}

				caseBlock := switchStmt.CaseBlock
				if caseBlock == nil {
					return
				}

				clauses := caseBlock.AsCaseBlock()
				if clauses == nil || clauses.Clauses == nil {
					return
				}

				if len(clauses.Clauses.Nodes) == 0 {
					return
				}

				for _, clause := range clauses.Clauses.Nodes {
					if clause.Kind == ast.KindDefaultClause {
						return
					}
				}

				lastClause := clauses.Clauses.Nodes[len(clauses.Clauses.Nodes)-1]
				lastClauseEnd := lastClause.End()
				switchEnd := node.End()

				var lastComment *ast.CommentRange
				commentRange := utils.GetCommentsInRange(ctx.SourceFile, core.NewTextRange(lastClauseEnd, switchEnd))
				for comment := range commentRange {
					c := comment
					lastComment = &c
				}

				if lastComment != nil {
					commentText := strings.TrimSpace(ctx.SourceFile.Text()[lastComment.Pos():lastComment.End()])

					if strings.HasPrefix(commentText, "//") {
						commentText = strings.TrimSpace(commentText[2:])
					} else if strings.HasPrefix(commentText, "/*") && strings.HasSuffix(commentText, "*/") {
						commentText = strings.TrimSpace(commentText[2 : len(commentText)-2])
					}

					if opts.commentPattern.MatchString(commentText) {
						return
					}
				}

				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "missingDefaultCase",
					Description: "Expected a default case.",
				})
			},
		}
	},
}

https://eslint.org/docs/latest/rules/default-case

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