no_require_imports

package
v0.1.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoRequireImportsRule = rule.CreateRule(rule.Rule{
	Name: "no-require-imports",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := NoRequireImportsOptions{
			Allow:         []string{},
			AllowAsImport: false,
		}

		if options != nil {
			var optsMap map[string]interface{}
			var ok bool

			if optArray, isArray := options.([]interface{}); isArray && len(optArray) > 0 {
				optsMap, ok = optArray[0].(map[string]interface{})
			} else {

				optsMap, ok = options.(map[string]interface{})
			}

			if ok {
				if allow, ok := optsMap["allow"].([]interface{}); ok {
					for _, pattern := range allow {
						if str, ok := pattern.(string); ok {
							opts.Allow = append(opts.Allow, str)
						}
					}
				}
				if allowAsImport, ok := optsMap["allowAsImport"].(bool); ok {
					opts.AllowAsImport = allowAsImport
				}
			}
		}

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

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

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

				// Check if this is a require call or require?.() call
				var isRequireCall bool

				if ast.IsIdentifier(callExpr.Expression) {
					identifier := callExpr.Expression.AsIdentifier()
					if identifier.Text == "require" {
						isRequireCall = true
					}
				} else if callExpr.QuestionDotToken != nil {

					if ast.IsIdentifier(callExpr.Expression) {
						identifier := callExpr.Expression.AsIdentifier()
						if identifier != nil && identifier.Text == "require" {
							isRequireCall = true
						}
					}
				}

				if !isRequireCall {
					return
				}

				if !isGlobalRequire(ctx, node) {
					return
				}

				if len(callExpr.Arguments.Nodes) > 0 && isStringOrTemplateLiteral(callExpr.Arguments.Nodes[0]) {
					if argValue, ok := getStaticStringValue(callExpr.Arguments.Nodes[0]); ok {
						if isImportPathAllowed(argValue) {
							return
						}
					}
				}

				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "noRequireImports",
					Description: "A `require()` style import is forbidden.",
				})
			},

			ast.KindExternalModuleReference: func(node *ast.Node) {
				extModRef := node.AsExternalModuleReference()

				if isStringOrTemplateLiteral(extModRef.Expression) {
					if argValue, ok := getStaticStringValue(extModRef.Expression); ok {
						if isImportPathAllowed(argValue) {
							return
						}
					}
				}

				if opts.AllowAsImport && node.Parent != nil &&
					node.Parent.Kind == ast.KindImportEqualsDeclaration {
					return
				}

				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "noRequireImports",
					Description: "A `require()` style import is forbidden.",
				})
			},
		}
	},
})

Functions

This section is empty.

Types

type NoRequireImportsOptions

type NoRequireImportsOptions struct {
	Allow         []string `json:"allow"`
	AllowAsImport bool     `json:"allowAsImport"`
}

Jump to

Keyboard shortcuts

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