forbid_dom_props

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ForbidDomPropsRule = rule.Rule{
	Name: "react/forbid-dom-props",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		cfg := parseOptions(options)

		if len(cfg.byProp) == 0 {
			return rule.RuleListeners{}
		}
		return rule.RuleListeners{
			ast.KindJsxAttribute: func(node *ast.Node) {
				parent := reactutil.GetJsxParentElement(node)
				if parent == nil {
					return
				}
				tagName := reactutil.GetJsxTagName(parent)
				if tagName == nil {
					return
				}

				if tagName.Kind != ast.KindIdentifier {
					return
				}
				tag := tagName.AsIdentifier().Text
				if !reactutil.IsCasedLowercaseFirstLetter(tag) {
					return
				}

				nameNode := node.AsJsxAttribute().Name()
				if nameNode == nil || nameNode.Kind != ast.KindIdentifier {
					return
				}
				prop := nameNode.AsIdentifier().Text
				if prop == "" {
					return
				}

				attr := node.AsJsxAttribute()
				if attr.Initializer == nil {
					return
				}
				propValue, hasValue := stringLiteralValue(node)
				forbidden, e := cfg.isForbidden(prop, tag, propValue, hasValue)
				if !forbidden {
					return
				}

				if e.message != "" {
					ctx.ReportNode(node, rule.RuleMessage{
						Description: e.message,
						Data:        map[string]string{"prop": prop, "propValue": propValue},
					})
					return
				}
				if e.hasDisallowedValues {
					desc := strings.ReplaceAll(msgPropIsForbiddenWithValue, "{{prop}}", prop)
					desc = strings.ReplaceAll(desc, "{{propValue}}", propValue)
					ctx.ReportNode(node, rule.RuleMessage{
						Id:          "propIsForbiddenWithValue",
						Description: desc,
						Data:        map[string]string{"prop": prop, "propValue": propValue},
					})
					return
				}
				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "propIsForbidden",
					Description: strings.ReplaceAll(msgPropIsForbidden, "{{prop}}", prop),
					Data:        map[string]string{"prop": prop, "propValue": propValue},
				})
			},
		}
	},
}

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