no_extend_native

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoExtendNativeRule = rule.Rule{
	Name: "no-extend-native",
	Run: func(ctx rule.RuleContext, opts any) rule.RuleListeners {
		o := parseOptions(opts)

		return rule.RuleListeners{
			ast.KindIdentifier: func(node *ast.Node) {
				name := node.Text()
				if !nativeBuiltins[name] || o.exceptions[name] {
					return
				}

				identExpr := skipParensUp(node)
				parent := identExpr.Parent
				if parent == nil {
					return
				}

				obj := memberObject(parent)
				if obj != identExpr {
					return
				}
				propName, ok := staticMemberName(parent)
				if !ok || propName != "prototype" {
					return
				}

				if utils.IsShadowed(node, name) {
					return
				}

				prototypeAccess := skipParensUp(parent)
				next := prototypeAccess.Parent
				if next == nil {
					return
				}

				if memberObject(next) == prototypeAccess {
					memberAccess := skipParensUp(next)
					assign := memberAccess.Parent
					if assign != nil && assign.Kind == ast.KindBinaryExpression {
						bin := assign.AsBinaryExpression()
						if bin.OperatorToken != nil &&
							isAssignmentOperator(bin.OperatorToken.Kind) &&
							bin.Left == memberAccess {
							ctx.ReportNode(assign, rule.RuleMessage{
								Id:          "unexpected",
								Description: name + " prototype is read only, properties should not be added.",
							})
							return
						}
					}
				}

				if next.Kind == ast.KindCallExpression {
					call := next.AsCallExpression()
					if call.Arguments == nil || len(call.Arguments.Nodes) == 0 ||
						call.Arguments.Nodes[0] != prototypeAccess {
						return
					}
					if utils.IsSpecificMemberAccess(call.Expression, "Object", "defineProperty") ||
						utils.IsSpecificMemberAccess(call.Expression, "Object", "defineProperties") {
						ctx.ReportNode(next, rule.RuleMessage{
							Id:          "unexpected",
							Description: name + " prototype is read only, properties should not be added.",
						})
					}
				}
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-extend-native

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