no_fallthrough

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: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoFallthroughRule = rule.Rule{
	Name: "no-fallthrough",
	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 || switchStmt.CaseBlock == nil {
					return
				}

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

				clauses := caseBlock.Clauses.Nodes
				sourceText := ctx.SourceFile.Text()

				for i := range len(clauses) - 1 {
					currentClause := clauses[i].AsCaseOrDefaultClause()
					if currentClause == nil {
						continue
					}

					statements := currentClause.Statements
					if statements == nil || len(statements.Nodes) == 0 {

						continue
					}

					if opts.allowEmptyCase && isCaseBodyEmpty(statements.Nodes) {
						continue
					}

					isFallthrough := currentClause.FallthroughFlowNode != nil
					if isFallthrough && hasASTTerminal(statements.Nodes) {
						isFallthrough = false
					}

					nextClause := clauses[i+1]
					lastStmt := statements.Nodes[len(statements.Nodes)-1]
					commentStart := lastStmt.End()
					nextKeywordPos := scanner.GetRangeOfTokenAtPosition(ctx.SourceFile, nextClause.Pos()).Pos()
					hasComment := hasFallthroughComment(sourceText, commentStart, nextKeywordPos, opts.commentPattern)

					if !isFallthrough {
						if opts.reportUnusedFallthroughComment && hasComment {
							ctx.ReportNode(nextClause, rule.RuleMessage{
								Id:          "unusedFallthroughComment",
								Description: "Found a comment that would permit fallthrough, but case cannot fall through.",
							})
						}
						continue
					}

					if hasComment {
						continue
					}

					var msgID string
					var description string
					if nextClause.Kind == ast.KindDefaultClause {
						msgID = "default"
						description = "Expected a 'break' statement before 'default'."
					} else {
						msgID = "case"
						description = "Expected a 'break' statement before 'case'."
					}

					ctx.ReportNode(nextClause, rule.RuleMessage{
						Id:          msgID,
						Description: description,
					})
				}
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-fallthrough

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