no_import_assign

package
v0.4.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoImportAssignRule = rule.Rule{
	Name: "no-import-assign",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindImportDeclaration: func(node *ast.Node) {
				importDecl := node.AsImportDeclaration()
				if importDecl == nil || importDecl.ImportClause == nil {
					return
				}

				importClause := importDecl.ImportClause.AsImportClause()
				if importClause == nil {
					return
				}

				var bindings []importedBinding

				if importClause.Name() != nil {
					bindings = append(bindings, makeImportedBinding(importClause.Name(), false, &ctx))
				}

				if importClause.NamedBindings != nil {
					nb := importClause.NamedBindings

					switch nb.Kind {
					case ast.KindNamespaceImport:
						nsImport := nb.AsNamespaceImport()
						if nsImport != nil && nsImport.Name() != nil {
							bindings = append(bindings, makeImportedBinding(nsImport.Name(), true, &ctx))
						}
					case ast.KindNamedImports:
						namedImports := nb.AsNamedImports()
						if namedImports != nil && namedImports.Elements != nil {
							for _, elem := range namedImports.Elements.Nodes {
								importSpec := elem.AsImportSpecifier()
								if importSpec != nil && importSpec.Name() != nil {
									bindings = append(bindings, makeImportedBinding(importSpec.Name(), false, &ctx))
								}
							}
						}
					}
				}

				if len(bindings) == 0 {
					return
				}

				sourceFile := ctx.SourceFile
				var walk func(*ast.Node)
				walk = func(n *ast.Node) {
					if n == nil {
						return
					}

					if n.Kind == ast.KindIdentifier {
						for _, binding := range bindings {
							if n.Text() != binding.name {
								continue
							}

							if isImportBindingName(n) {
								continue
							}

							if binding.symbol != nil && ctx.TypeChecker != nil {
								refSym := ctx.TypeChecker.GetSymbolAtLocation(n)
								if refSym != binding.symbol {
									continue
								}
							}

							if utils.IsWriteReference(n) {
								ctx.ReportNode(n, rule.RuleMessage{
									Id:          "readonly",
									Description: "'" + binding.name + "' is read-only.",
								})
							} else if binding.isNamespace && isMemberWrite(n, &ctx) {
								ctx.ReportNode(n, rule.RuleMessage{
									Id:          "readonlyMember",
									Description: "The members of '" + binding.name + "' are read-only.",
								})
							}
						}
					}

					n.ForEachChild(func(child *ast.Node) bool {
						walk(child)
						return false
					})
				}
				walk(sourceFile.AsNode())
			},
		}
	},
}

NoImportAssignRule disallows assigning to imported bindings.

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