Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var JsxFilenameExtensionRule = rule.Rule{ Name: "react/jsx-filename-extension", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { extensions := []string{".jsx"} allow := "always" optsMap := utils.GetOptionsMap(options) ignoreFilesWithoutCode := false if optsMap != nil { if exts, ok := optsMap["extensions"]; ok { if extArr, ok := exts.([]interface{}); ok { extensions = nil for _, e := range extArr { if s, ok := e.(string); ok { extensions = append(extensions, s) } } } } if a, ok := optsMap["allow"].(string); ok { allow = a } if v, ok := optsMap["ignoreFilesWithoutCode"].(bool); ok { ignoreFilesWithoutCode = v } } isExtensionAllowed := func(ext string) bool { for _, e := range extensions { if e == ext { return true } } return false } fileName := ctx.SourceFile.FileName() if strings.HasPrefix(fileName, "<") { return rule.RuleListeners{} } ext := path.Ext(fileName) if allow == "as-needed" && isExtensionAllowed(ext) { hasJSX := false var walker ast.Visitor walker = func(node *ast.Node) bool { if hasJSX { return true } switch node.Kind { case ast.KindJsxElement, ast.KindJsxSelfClosingElement, ast.KindJsxFragment: hasJSX = true return true } node.ForEachChild(walker) return hasJSX } ctx.SourceFile.Node.ForEachChild(walker) if !hasJSX { if ignoreFilesWithoutCode && (ctx.SourceFile.Statements == nil || len(ctx.SourceFile.Statements.Nodes) == 0) { return rule.RuleListeners{} } ctx.ReportRange(core.NewTextRange(0, 0), rule.RuleMessage{ Id: "extensionOnlyForJSX", Description: fmt.Sprintf("Only files containing JSX may use the extension '%s'", ext), }) } return rule.RuleListeners{} } if !isExtensionAllowed(ext) { reported := false reportJSX := func(node *ast.Node) { if !reported { reported = true ctx.ReportNode(node, rule.RuleMessage{ Id: "noJSXWithExtension", Description: fmt.Sprintf("JSX not allowed in files with extension '%s'", ext), }) } } return rule.RuleListeners{ ast.KindJsxElement: reportJSX, ast.KindJsxSelfClosingElement: reportJSX, ast.KindJsxFragment: reportJSX, } } return rule.RuleListeners{} }, }
JsxFilenameExtensionRule restricts file extensions that may contain JSX.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.