getter_return

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 GetterReturnRule = rule.CreateRule(rule.Rule{
	Name: "getter-return",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)

		return rule.RuleListeners{
			ast.KindGetAccessor: func(node *ast.Node) {
				checkGetterReturn(ctx, node, opts)
			},

			ast.KindCallExpression: func(node *ast.Node) {
				expr := node.Expression()
				if expr == nil {
					return
				}

				var objectName, methodName string

				actualExpr := expr

				for actualExpr != nil && actualExpr.Kind == ast.KindParenthesizedExpression {
					actualExpr = actualExpr.Expression()
				}

				if actualExpr != nil && actualExpr.Kind == ast.KindPropertyAccessExpression {
					obj := actualExpr.Expression()
					if obj != nil && obj.Kind == ast.KindIdentifier {
						objectName = obj.Text()
					}
					name := actualExpr.Name()
					if name != nil && name.Kind == ast.KindIdentifier {
						methodName = name.Text()
					}
				}

				args := node.Arguments()
				if args == nil {
					return
				}

				var descriptorArg *ast.Node

				if (objectName == "Object" && methodName == "defineProperty") ||
					(objectName == "Reflect" && methodName == "defineProperty") {
					if len(args) >= 3 {
						descriptorArg = args[2]
					}
				}

				if objectName == "Object" && methodName == "defineProperties" {
					if len(args) >= 2 {
						propsArg := args[1]
						if propsArg != nil && propsArg.Kind == ast.KindObjectLiteralExpression {
							props := propsArg.Properties()
							for _, prop := range props {
								if prop != nil && (prop.Kind == ast.KindPropertyAssignment || prop.Kind == ast.KindShorthandPropertyAssignment) {
									init := prop.Initializer()
									if init != nil && init.Kind == ast.KindObjectLiteralExpression {
										checkDescriptorForGetter(ctx, init, opts)
									}
								}
							}
						}
					}
					return
				}

				if objectName == "Object" && methodName == "create" {
					if len(args) >= 2 {
						descriptorArg = args[1]
						if descriptorArg != nil && descriptorArg.Kind == ast.KindObjectLiteralExpression {
							props := descriptorArg.Properties()
							for _, prop := range props {
								if prop != nil && (prop.Kind == ast.KindPropertyAssignment || prop.Kind == ast.KindShorthandPropertyAssignment) {
									init := prop.Initializer()
									if init != nil && init.Kind == ast.KindObjectLiteralExpression {
										checkDescriptorForGetter(ctx, init, opts)
									}
								}
							}
						}
					}
					return
				}

				if descriptorArg != nil && descriptorArg.Kind == ast.KindObjectLiteralExpression {
					checkDescriptorForGetter(ctx, descriptorArg, opts)
				}
			},
		}
	},
})

GetterReturnRule enforces return statements in getters

Functions

This section is empty.

Types

type Options

type Options struct {
	AllowImplicit bool `json:"allowImplicit"`
}

Options for getter-return rule

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL