no_new_func

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 NoNewFuncRule = rule.Rule{
	Name: "no-new-func",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {

		isGlobalFunction := func(id *ast.Node) bool {
			if utils.IsShadowed(id, "Function") {
				return false
			}
			if ctx.TypeChecker != nil {
				symbol := ctx.TypeChecker.GetSymbolAtLocation(id)
				if symbol == nil {
					return false
				}
				return !utils.IsSymbolDeclaredInFile(symbol, ctx.SourceFile)
			}
			return true
		}

		check := func(node *ast.Node) {
			var callee *ast.Node
			if node.Kind == ast.KindNewExpression {
				callee = node.AsNewExpression().Expression
			} else {
				callee = node.AsCallExpression().Expression
			}

			if callee == nil {
				return
			}

			unwrapped := ast.SkipOuterExpressions(callee, skipTransparent)

			if unwrapped.Kind == ast.KindIdentifier && unwrapped.AsIdentifier().Text == "Function" {
				if !isGlobalFunction(unwrapped) {
					return
				}
				ctx.ReportNode(node, msg)
				return
			}

			if node.Kind != ast.KindCallExpression {
				return
			}

			propName, ok := utils.AccessExpressionStaticName(unwrapped)
			if !ok || !callMethods[propName] {
				return
			}

			obj := ast.SkipOuterExpressions(utils.AccessExpressionObject(unwrapped), skipTransparent)
			if obj == nil || obj.Kind != ast.KindIdentifier || obj.AsIdentifier().Text != "Function" {
				return
			}

			if !isGlobalFunction(obj) {
				return
			}

			ctx.ReportNode(node, msg)
		}

		return rule.RuleListeners{
			ast.KindNewExpression:  check,
			ast.KindCallExpression: check,
		}
	},
}

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