no_var_requires

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoVarRequiresRule = rule.CreateRule(rule.Rule{
	Name: "no-var-requires",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := &Options{}
		if options != nil {
			if optMap, ok := options.(map[string]interface{}); ok {
				if allowList, exists := optMap["allow"]; exists {
					if allowSlice, ok := allowList.([]interface{}); ok {
						for _, item := range allowSlice {
							if str, ok := item.(string); ok {
								opts.Allow = append(opts.Allow, str)
							}
						}
					} else if allowStrSlice, ok := allowList.([]string); ok {
						opts.Allow = allowStrSlice
					}
				}
			} else if o, ok := options.(*Options); ok {
				opts = o
			}
		}

		// Compile allow patterns into regexes
		var allowPatterns []*regexp.Regexp
		for _, pattern := range opts.Allow {
			if re, err := regexp.Compile(pattern); err == nil {
				allowPatterns = append(allowPatterns, re)
			}
		}

		isImportPathAllowed := func(importPath string) bool {
			for _, pattern := range allowPatterns {
				if pattern.MatchString(importPath) {
					return true
				}
			}
			return false
		}

		isStringOrTemplateLiteral := func(node *ast.Node) bool {
			if node == nil {
				return false
			}
			if node.Kind == ast.KindStringLiteral {
				return true
			}
			if node.Kind == ast.KindTemplateExpression || node.Kind == ast.KindNoSubstitutionTemplateLiteral {
				return true
			}
			return false
		}

		isInVariableDeclaration := func(node *ast.Node) bool {
			current := node
			for current != nil {
				if current.Kind == ast.KindVariableDeclaration ||
					current.Kind == ast.KindVariableDeclarationList {
					return true
				}
				current = current.Parent
			}
			return false
		}

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				callExpr := node.AsCallExpression()
				if callExpr == nil {
					return
				}

				callee := callExpr.Expression
				if callee == nil || callee.Kind != ast.KindIdentifier {
					return
				}

				identifier := callee.AsIdentifier()
				if identifier == nil || identifier.Text != "require" {
					return
				}

				symbol := identifier.Symbol()
				if symbol != nil && len(symbol.Declarations) > 0 {

					return
				}

				args := callExpr.Arguments
				if args != nil && len(args.Nodes) > 0 && isStringOrTemplateLiteral(args.Nodes[0]) {

					arg := args.Nodes[0]
					var argValue string
					if arg.Kind == ast.KindStringLiteral {
						stringLiteral := arg.AsStringLiteral()
						if stringLiteral != nil {

							text := stringLiteral.Text
							if len(text) >= 2 && ((text[0] == '"' && text[len(text)-1] == '"') ||
								(text[0] == '\'' && text[len(text)-1] == '\'')) {
								argValue = text[1 : len(text)-1]
							} else {
								argValue = text
							}
						}
					}
					if argValue != "" && isImportPathAllowed(argValue) {
						return
					}
				}

				parent := node.Parent
				if parent != nil && parent.Kind == ast.KindPropertyAccessExpression {

					parent = parent.Parent
				}

				if parent == nil {
					return
				}

				if parent.Kind == ast.KindImportEqualsDeclaration {
					return
				}

				if parent.Kind == ast.KindExpressionStatement {
					return
				}

				if isInVariableDeclaration(node) ||
					parent.Kind == ast.KindCallExpression ||
					parent.Kind == ast.KindPropertyAccessExpression ||
					parent.Kind == ast.KindNewExpression ||
					parent.Kind == ast.KindAsExpression ||
					parent.Kind == ast.KindTypeAssertionExpression {
					ctx.ReportNode(node, rule.RuleMessage{
						Description: "Require statement not part of import statement.",
						Id:          "noVarReqs",
					})
				}
			},
		}
	},
})

Functions

This section is empty.

Types

type Options

type Options struct {
	Allow []string `json:"allow"`
}

Jump to

Keyboard shortcuts

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