no_const_assign

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoConstAssignRule = rule.CreateRule(rule.Rule{
	Name: "no-const-assign",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {

		constSymbols := make(map[*ast.Symbol]bool)

		return rule.RuleListeners{

			ast.KindVariableDeclarationList: func(node *ast.Node) {
				if !isConstBinding(node) {
					return
				}

				varDeclList := node.AsVariableDeclarationList()
				if varDeclList == nil || varDeclList.Declarations == nil {
					return
				}

				for _, decl := range varDeclList.Declarations.Nodes {
					if decl.Kind != ast.KindVariableDeclaration {
						continue
					}

					varDecl := decl.AsVariableDeclaration()
					if varDecl == nil || varDecl.Name() == nil {
						continue
					}

					collectSymbols(varDecl.Name(), &ctx, constSymbols)
				}
			},

			ast.KindIdentifier: func(node *ast.Node) {
				checkIdentifierWrite(node, &ctx, constSymbols)
			},

			ast.KindShorthandPropertyAssignment: func(node *ast.Node) {
				shorthand := node.AsShorthandPropertyAssignment()
				if shorthand == nil || shorthand.Name() == nil {
					return
				}

				if !isInDestructuringAssignment(node) {
					return
				}

				if ctx.TypeChecker == nil {
					return
				}

				symbol := ctx.TypeChecker.GetSymbolAtLocation(shorthand.Name())
				if symbol == nil {
					return
				}

				if !constSymbols[symbol] {
					return
				}

				identName := getIdentifierName(shorthand.Name())
				ctx.ReportNode(shorthand.Name(), buildConstMessage(identName))
			},
		}
	},
})

NoConstAssignRule disallows reassigning const variables

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