no_namespace

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoNamespaceRule = rule.CreateRule(rule.Rule{
	Name: "no-namespace",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := defaultNoNamespaceOptions

		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 allowDeclarations, ok := optsMap["allowDeclarations"].(bool); ok {
					opts.AllowDeclarations = utils.Ref(allowDeclarations)
				}
				if allowDefinitionFiles, ok := optsMap["allowDefinitionFiles"].(bool); ok {
					opts.AllowDefinitionFiles = utils.Ref(allowDefinitionFiles)
				}
			}
		}

		return rule.RuleListeners{
			ast.KindModuleDeclaration: func(node *ast.Node) {
				moduleDecl := node.AsModuleDeclaration()
				if moduleDecl == nil {
					return
				}

				if moduleDecl.Keyword != ast.KindNamespaceKeyword {
					return
				}

				if opts.AllowDefinitionFiles != nil && *opts.AllowDefinitionFiles && strings.HasSuffix(ctx.SourceFile.FileName(), ".d.ts") {
					return
				}

				if opts.AllowDeclarations != nil && *opts.AllowDeclarations && utils.IncludesModifier(node, ast.KindDeclareKeyword) {
					return
				}

				ctx.ReportNode(moduleDecl.Name(), buildNoNamespaceMessage())
			},
		}
	},
})

rule instance check if the namespace is used

Functions

This section is empty.

Types

type NoNamespaceOptions

type NoNamespaceOptions struct {
	AllowDeclarations    *bool `json:"allowDeclarations"`
	AllowDefinitionFiles *bool `json:"allowDefinitionFiles"`
}

rule options

Jump to

Keyboard shortcuts

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