no_prototype_builtins

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: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoPrototypeBuiltinsRule = rule.Rule{
	Name: "no-prototype-builtins",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				callExpr := node.AsCallExpression()
				if callExpr == nil || callExpr.Expression == nil {
					return
				}

				callee := ast.SkipParentheses(callExpr.Expression)
				if callee == nil {
					return
				}

				var reportNode *ast.Node
				switch callee.Kind {
				case ast.KindPropertyAccessExpression:
					reportNode = callee.AsPropertyAccessExpression().Name()
				case ast.KindElementAccessExpression:
					reportNode = callee.AsElementAccessExpression().ArgumentExpression
				default:
					return
				}
				if reportNode == nil {
					return
				}

				propName, ok := utils.AccessExpressionStaticName(callee)
				if !ok {
					return
				}
				if _, forbidden := disallowedProps[propName]; !forbidden {
					return
				}

				msg := rule.RuleMessage{
					Id:          "prototypeBuildIn",
					Description: fmt.Sprintf("Do not access Object.prototype method '%s' from target object.", propName),
				}

				if isAfterOptional(node) || utils.IsShadowed(node, "Object") {
					ctx.ReportNode(reportNode, msg)
					return
				}

				obj := utils.AccessExpressionObject(callee)
				if obj == nil {
					ctx.ReportNode(reportNode, msg)
					return
				}

				unwrappedObj := ast.SkipParentheses(obj)
				if unwrappedObj == nil {
					ctx.ReportNode(reportNode, msg)
					return
				}

				objText := utils.TrimmedNodeText(ctx.SourceFile, unwrappedObj)
				if isCommaBinaryExpression(unwrappedObj) {
					objText = "(" + objText + ")"
				}

				text := ctx.SourceFile.Text()
				openParenPos := scanner.SkipTrivia(text, callExpr.Expression.End())
				if openParenPos >= len(text) || text[openParenPos] != '(' {
					ctx.ReportNode(reportNode, msg)
					return
				}

				delim := ", "
				if callExpr.Arguments == nil || len(callExpr.Arguments.Nodes) == 0 {
					delim = ""
				}

				suggestion := rule.RuleSuggestion{
					Message: rule.RuleMessage{
						Id:          "callObjectPrototype",
						Description: fmt.Sprintf("Call Object.prototype.%s explicitly.", propName),
					},
					FixesArr: []rule.RuleFix{
						rule.RuleFixReplace(ctx.SourceFile, callee, "Object.prototype."+propName+".call"),
						rule.RuleFixReplaceRange(
							core.NewTextRange(openParenPos+1, openParenPos+1),
							objText+delim,
						),
					},
				}

				ctx.ReportNodeWithSuggestions(reportNode, msg, suggestion)
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-prototype-builtins

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