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()) } } }, } }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.