jsx_no_target_blank

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JsxNoTargetBlankRule = rule.Rule{
	Name: "react/jsx-no-target-blank",
	Run: func(ctx rule.RuleContext, rawOptions any) rule.RuleListeners {
		opts := parseOptions(rawOptions)
		linkComponents := reactutil.ReadComponentsFromSettings(ctx.Settings, "linkComponents", "linkAttribute", "href", reactutil.DefaultLinkComponents())
		formComponents := reactutil.ReadComponentsFromSettings(ctx.Settings, "formComponents", "formAttribute", "action", reactutil.DefaultFormComponents())

		messageId := "noTargetBlankWithoutNoreferrer"
		description := msgNoreferrer
		relValue := "noreferrer"
		if opts.allowReferrer {
			messageId = "noTargetBlankWithoutNoopener"
			description = msgNoopener
			relValue = "noopener"
		}

		check := func(node *ast.Node) {

			tagName := reactutil.GetJsxTagName(node)
			if tagName == nil || tagName.Kind != ast.KindIdentifier {
				return
			}
			name := tagName.AsIdentifier().Text
			isLink := opts.links && linkComponents[name] != nil
			isForm := opts.forms && formComponents[name] != nil
			if !isLink && !isForm {
				return
			}
			attrs := reactutil.GetJsxElementAttributes(node)
			if len(attrs) == 0 {
				return
			}

			targetIdx := findLastIndex(attrs, func(a *ast.Node) bool { return attrHasName(a, "target") })
			spreadIdx := findLastIndex(attrs, func(a *ast.Node) bool { return a.Kind == ast.KindJsxSpreadAttribute })

			shouldProceed := func() bool {
				var targetAttr *ast.Node
				if targetIdx != -1 {
					targetAttr = attrs[targetIdx]
				}
				if attributeValuePossiblyBlank(targetAttr) {
					return true
				}
				return opts.warnOnSpreadAttributes && spreadIdx >= 0
			}

			if isLink {
				if shouldProceed() {
					componentAttrs := linkComponents[name]
					dangerous := hasExternalLink(attrs, componentAttrs, opts.warnOnSpreadAttributes, spreadIdx) ||
						(opts.enforceDynamicLinks == "always" && hasDynamicLink(attrs, componentAttrs))
					if dangerous && !hasSecureRel(attrs, opts.allowReferrer, opts.warnOnSpreadAttributes, spreadIdx) {
						reportWithOptionalFix(ctx, node, messageId, description,
							buildRelFix(ctx.SourceFile, attrs, targetIdx, spreadIdx, relValue))
						return
					}
				}
			}
			if isForm {

				if !shouldProceed() {
					return
				}
				if hasSecureRel(attrs, false, false, -1) {
					return
				}
				formAttrs := formComponents[name]
				if hasExternalLink(attrs, formAttrs, false, -1) ||
					(opts.enforceDynamicLinks == "always" && hasDynamicLink(attrs, formAttrs)) {
					reportWithOptionalFix(ctx, node, messageId, description, nil)
				}
			}
		}

		return rule.RuleListeners{
			ast.KindJsxOpeningElement:     check,
			ast.KindJsxSelfClosingElement: check,
		}
	},
}

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