no_labels

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoLabelsRule = rule.Rule{
	Name: "no-labels",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		allowLoop := false
		allowSwitch := false
		optsMap := utils.GetOptionsMap(options)
		if optsMap != nil {
			if v, ok := optsMap["allowLoop"].(bool); ok {
				allowLoop = v
			}
			if v, ok := optsMap["allowSwitch"].(bool); ok {
				allowSwitch = v
			}
		}

		isAllowed := func(kind string) bool {
			switch kind {
			case "loop":
				return allowLoop
			case "switch":
				return allowSwitch
			default:
				return false
			}
		}

		var scopeInfo *labelScope

		getKind := func(label string) string {
			info := scopeInfo
			for info != nil {
				if info.label == label {
					return info.kind
				}
				info = info.upper
			}
			return "other"
		}

		return rule.RuleListeners{
			ast.KindLabeledStatement: func(node *ast.Node) {
				ls := node.AsLabeledStatement()
				scopeInfo = &labelScope{
					label: ls.Label.Text(),
					kind:  getBodyKind(ls.Statement),
					upper: scopeInfo,
				}
			},
			rule.ListenerOnExit(ast.KindLabeledStatement): func(node *ast.Node) {
				if !isAllowed(scopeInfo.kind) {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "unexpectedLabel",
						Description: "Unexpected labeled statement.",
					})
				}
				scopeInfo = scopeInfo.upper
			},
			ast.KindBreakStatement: func(node *ast.Node) {
				bs := node.AsBreakStatement()
				if bs.Label != nil && !isAllowed(getKind(bs.Label.Text())) {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "unexpectedLabelInBreak",
						Description: "Unexpected label in break statement.",
					})
				}
			},
			ast.KindContinueStatement: func(node *ast.Node) {
				cs := node.AsContinueStatement()
				if cs.Label != nil && !isAllowed(getKind(cs.Label.Text())) {
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "unexpectedLabelInContinue",
						Description: "Unexpected label in continue statement.",
					})
				}
			},
		}
	},
}

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

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