no_eval

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoEvalRule = rule.Rule{
	Name: "no-eval",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		allowIndirect := false
		optsMap := utils.GetOptionsMap(options)
		if optsMap != nil {
			if v, ok := optsMap["allowIndirect"].(bool); ok {
				allowIndirect = v
			}
		}

		msg := rule.RuleMessage{
			Id:          "unexpected",
			Description: "`eval` can be harmful.",
		}

		if allowIndirect {

			return rule.RuleListeners{
				ast.KindCallExpression: func(node *ast.Node) {
					call := node.AsCallExpression()

					if call.QuestionDotToken != nil {
						return
					}
					callee := call.Expression
					if callee != nil && ast.IsIdentifier(callee) && callee.AsIdentifier().Text == "eval" {
						ctx.ReportNode(callee, msg)
					}
				},
			}
		}

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()
				callee := call.Expression
				if callee != nil && ast.IsIdentifier(callee) && callee.AsIdentifier().Text == "eval" {
					ctx.ReportNode(callee, msg)
				}
			},
			ast.KindPropertyAccessExpression: func(node *ast.Node) {
				propAccess := node.AsPropertyAccessExpression()
				name := propAccess.Name()
				if name == nil || name.Text() != "eval" {
					return
				}

				obj := ast.SkipParentheses(propAccess.Expression)
				if obj == nil {
					return
				}

				if obj.Kind == ast.KindThisKeyword {
					if isThisReferringToGlobal(obj, ctx.SourceFile) {
						ctx.ReportNode(name, msg)
					}
					return
				}

				if isGlobalObjectChain(obj, ctx.TypeChecker) {
					ctx.ReportNode(name, msg)
				}
			},
			ast.KindElementAccessExpression: func(node *ast.Node) {
				elemAccess := node.AsElementAccessExpression()
				argExpr := elemAccess.ArgumentExpression
				if argExpr == nil {
					return
				}

				if utils.GetStaticStringValue(argExpr) != "eval" {
					return
				}

				obj := ast.SkipParentheses(elemAccess.Expression)
				if obj == nil {
					return
				}

				if obj.Kind == ast.KindThisKeyword {
					if isThisReferringToGlobal(obj, ctx.SourceFile) {
						ctx.ReportNode(argExpr, msg)
					}
					return
				}

				if isGlobalObjectChain(obj, ctx.TypeChecker) {
					ctx.ReportNode(argExpr, msg)
				}
			},
			ast.KindIdentifier: func(node *ast.Node) {
				if node.AsIdentifier().Text != "eval" {
					return
				}

				parent := node.Parent
				if parent == nil {
					return
				}

				if ast.IsCallExpression(parent) && parent.AsCallExpression().Expression == node {
					return
				}

				if ast.IsPropertyAccessExpression(parent) && parent.AsPropertyAccessExpression().Name() == node {
					return
				}

				if utils.IsNonReferenceIdentifier(node) {
					return
				}

				if utils.IsShadowed(node, "eval") {
					return
				}

				ctx.ReportNode(node, msg)
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-eval

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