Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var UseIsNaNRule = rule.Rule{ Name: "use-isnan", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { opts := parseOptions(options) comparisonOperators := map[ast.Kind]bool{ ast.KindEqualsEqualsToken: true, ast.KindEqualsEqualsEqualsToken: true, ast.KindExclamationEqualsToken: true, ast.KindExclamationEqualsEqualsToken: true, ast.KindGreaterThanToken: true, ast.KindGreaterThanEqualsToken: true, ast.KindLessThanToken: true, ast.KindLessThanEqualsToken: true, } listeners := rule.RuleListeners{ ast.KindBinaryExpression: func(node *ast.Node) { binary := node.AsBinaryExpression() if binary == nil || binary.OperatorToken == nil { return } if !comparisonOperators[binary.OperatorToken.Kind] { return } if isNaNIdentifier(binary.Left) || isNaNIdentifier(binary.Right) { ctx.ReportNode(node, rule.RuleMessage{ Id: "comparisonWithNaN", Description: "Use the isNaN function to compare with NaN.", }) } }, } if opts.enforceForSwitchCase { listeners[ast.KindSwitchStatement] = func(node *ast.Node) { switchStmt := node.AsSwitchStatement() if switchStmt == nil { return } if isNaNIdentifier(switchStmt.Expression) { ctx.ReportNode(node, rule.RuleMessage{ Id: "switchNaN", Description: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.", }) } if switchStmt.CaseBlock == nil { return } caseBlock := switchStmt.CaseBlock.AsCaseBlock() if caseBlock == nil || caseBlock.Clauses == nil { return } for _, clause := range caseBlock.Clauses.Nodes { if clause.Kind != ast.KindCaseClause { continue } caseClause := clause.AsCaseOrDefaultClause() if caseClause == nil || caseClause.Expression == nil { continue } if isNaNIdentifier(caseClause.Expression) { ctx.ReportNode(clause, rule.RuleMessage{ Id: "caseNaN", Description: "'case NaN' can never match. Use Number.isNaN before the switch.", }) } } } } if opts.enforceForIndexOf { listeners[ast.KindCallExpression] = func(node *ast.Node) { callee := ast.SkipParentheses(node.Expression()) if callee == nil { return } methodName, ok := utils.AccessExpressionStaticName(callee) if !ok { return } if methodName != "indexOf" && methodName != "lastIndexOf" { return } args := node.Arguments() if len(args) == 0 || len(args) > 2 { return } if isNaNIdentifier(args[0]) { description := strings.Replace( "Array prototype method '{{methodName}}' cannot find NaN.", "{{methodName}}", methodName, 1, ) ctx.ReportNode(node, rule.RuleMessage{ Id: "indexOfNaN", Description: description, }) } } } return listeners }, }
UseIsNaNRule requires calls to isNaN() when checking for NaN
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.