forbid_elements

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ForbidElementsRule = rule.Rule{
	Name: "react/forbid-elements",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		indexed := parseOptions(options)
		if len(indexed) == 0 {
			return rule.RuleListeners{}
		}

		checkJsxTag := func(element *ast.Node) {
			tagName := reactutil.GetJsxTagName(element)
			if tagName == nil {
				return
			}
			reportIfForbidden(ctx, indexed, utils.TrimmedNodeText(ctx.SourceFile, tagName), tagName)
		}

		pragma := reactutil.GetReactPragma(ctx.Settings)

		return rule.RuleListeners{
			ast.KindJsxOpeningElement:     checkJsxTag,
			ast.KindJsxSelfClosingElement: checkJsxTag,
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()

				if !isCreateElementCall(call.Expression, pragma, ctx.TypeChecker) {
					return
				}
				if call.Arguments == nil || len(call.Arguments.Nodes) == 0 {
					return
				}

				arg := ast.SkipParentheses(call.Arguments.Nodes[0])
				switch arg.Kind {
				case ast.KindIdentifier:
					name := arg.AsIdentifier().Text
					if identifierStartRegex.MatchString(name) {
						reportIfForbidden(ctx, indexed, name, arg)
					}
				case ast.KindStringLiteral:
					text := arg.AsStringLiteral().Text
					if literalElementRegex.MatchString(text) {
						reportIfForbidden(ctx, indexed, text, arg)
					}
				case ast.KindTrueKeyword, ast.KindFalseKeyword, ast.KindNullKeyword:
					// In ESTree, `true` / `false` / `null` are `Literal` nodes;
					// upstream applies `String(argument.value)` which yields
					// `"true"` / `"false"` / `"null"` — all three match
					// `/^[a-z][^.]*$/`. tsgo splits these into dedicated
					// keyword kinds, so handle them explicitly to preserve
					// upstream's (degenerate but observable) behavior:
					// `React.createElement(true)` + `forbid: ['true']` reports.
					var text string
					switch arg.Kind {
					case ast.KindTrueKeyword:
						text = "true"
					case ast.KindFalseKeyword:
						text = "false"
					case ast.KindNullKeyword:
						text = "null"
					}
					reportIfForbidden(ctx, indexed, text, arg)
				case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:

					if ast.IsOptionalChain(arg) {
						return
					}
					reportIfForbidden(ctx, indexed, utils.TrimmedNodeText(ctx.SourceFile, arg), arg)
				}
			},
		}
	},
}

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