valid_typeof

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT, MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

		return rule.RuleListeners{
			ast.KindTypeOfExpression: func(node *ast.Node) {

				parent := node.Parent
				for parent != nil && parent.Kind == ast.KindParenthesizedExpression {
					parent = parent.Parent
				}
				if parent == nil || parent.Kind != ast.KindBinaryExpression {
					return
				}

				bin := parent.AsBinaryExpression()
				if bin == nil || bin.OperatorToken == nil {
					return
				}

				if !isEqualityOperator(bin.OperatorToken.Kind) {
					return
				}

				// Determine which operand is the sibling (not the typeof side).
				// Use SkipParentheses on both sides to match against the typeof node
				// since either side may be wrapped in parentheses.
				var sibling *ast.Node
				if ast.SkipParentheses(bin.Left) == node {
					sibling = ast.SkipParentheses(bin.Right)
				} else {
					sibling = ast.SkipParentheses(bin.Left)
				}

				if sibling == nil {
					return
				}

				switch {
				case ast.IsStringLiteralLike(sibling):

					value := sibling.LiteralLikeData().Text
					if !validTypes[value] {
						ctx.ReportNode(sibling, invalidValueMsg())
					}

				case sibling.Kind == ast.KindNumericLiteral,
					sibling.Kind == ast.KindBigIntLiteral,
					sibling.Kind == ast.KindRegularExpressionLiteral,
					ast.IsBooleanLiteral(sibling),
					utils.IsNullLiteral(sibling):

					ctx.ReportNode(sibling, invalidValueMsg())

				case sibling.Kind == ast.KindIdentifier:
					if sibling.Text() == "undefined" && !utils.IsShadowed(sibling, "undefined") {

						msg := invalidValueMsg()
						if opts.requireStringLiterals {
							msg = notStringMsg()
						}
						ctx.ReportNodeWithSuggestions(sibling, msg, rule.RuleSuggestion{
							Message: suggestStringMsg(),
							FixesArr: []rule.RuleFix{
								rule.RuleFixReplace(ctx.SourceFile, sibling, `"undefined"`),
							},
						})
					} else if opts.requireStringLiterals {

						ctx.ReportNode(sibling, notStringMsg())
					}

				case sibling.Kind == ast.KindTypeOfExpression:

				default:
					if opts.requireStringLiterals {
						ctx.ReportNode(sibling, notStringMsg())
					}
				}
			},
		}
	},
}

https://eslint.org/docs/latest/rules/valid-typeof

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