Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var NoUndefInitRule = rule.Rule{ Name: "no-undef-init", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { return rule.RuleListeners{ ast.KindVariableDeclaration: func(node *ast.Node) { varDecl := node.AsVariableDeclaration() if varDecl == nil { return } init := varDecl.Initializer if init == nil { return } unwrapped := ast.SkipParentheses(init) if unwrapped.Kind != ast.KindIdentifier || unwrapped.Text() != "undefined" { return } parent := node.Parent if parent == nil || parent.Kind != ast.KindVariableDeclarationList { return } if ast.IsVarConst(parent) || ast.IsVarUsing(parent) || ast.IsVarAwaitUsing(parent) { return } if utils.IsShadowed(unwrapped, "undefined") { return } nameNode := varDecl.Name() if nameNode == nil { return } nameText := scanner.GetSourceTextOfNodeFromSourceFile(ctx.SourceFile, nameNode, false) msg := rule.RuleMessage{ Id: "unnecessaryUndefinedInit", Description: "It's not necessary to initialize '" + nameText + "' to undefined.", } if !ast.IsVarLet(parent) { ctx.ReportNode(node, msg) return } if ast.IsBindingPattern(nameNode) { ctx.ReportNode(node, msg) return } // Compute fix range: remove " = undefined" while preserving type annotations // and the definite assignment token (!). // NOTE: Unlike ESLint, which removes from name-end to declarator-end // (losing type annotations in TS), we start the removal from the end of the // last node before the initializer to preserve TypeScript syntax. // VariableDeclaration children in order: Name, ExclamationToken?, Type?, Initializer var fixStart int if varDecl.Type != nil { fixStart = varDecl.Type.End() } else if varDecl.ExclamationToken != nil { fixStart = varDecl.ExclamationToken.End() } else { fixStart = nameNode.End() } nodeEnd := node.End() between := ctx.SourceFile.Text()[fixStart:nodeEnd] if strings.Contains(between, "/*") || strings.Contains(between, "//") { ctx.ReportNode(node, msg) return } fix := rule.RuleFixRemoveRange(core.NewTextRange(fixStart, nodeEnd)) ctx.ReportNodeWithFixes(node, msg, fix) }, } }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.