no_string_refs

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT, MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoStringRefsRule = rule.Rule{
	Name: "react/no-string-refs",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		detectTemplateLiterals := false
		if optsMap := utils.GetOptionsMap(options); optsMap != nil {
			if v, ok := optsMap["noTemplateLiterals"].(bool); ok {
				detectTemplateLiterals = v
			}
		}

		pragma := reactutil.GetReactPragma(ctx.Settings)
		createClass := reactutil.GetReactCreateClass(ctx.Settings)

		checkRefsUsage := reactutil.ReactVersionLessThan(ctx.Settings, 18, 3, 0)

		reportStringRef := func(node *ast.Node) {
			ctx.ReportNode(node, rule.RuleMessage{
				Id:          "stringInRefDeprecated",
				Description: "Using string literals in ref attributes is deprecated.",
			})
		}

		return rule.RuleListeners{
			ast.KindPropertyAccessExpression: func(node *ast.Node) {
				if !checkRefsUsage {
					return
				}
				prop := node.AsPropertyAccessExpression()
				if ast.SkipParentheses(prop.Expression).Kind != ast.KindThisKeyword {
					return
				}
				name := prop.Name()
				if name == nil || name.Kind != ast.KindIdentifier || name.AsIdentifier().Text != "refs" {
					return
				}
				if !reactutil.IsInsideReactComponent(node, pragma, createClass) {
					return
				}
				ctx.ReportNode(node, rule.RuleMessage{
					Id:          "thisRefsDeprecated",
					Description: "Using this.refs is deprecated.",
				})
			},

			ast.KindJsxAttribute: func(node *ast.Node) {
				attr := node.AsJsxAttribute()
				name := attr.Name()
				if name == nil || name.Kind != ast.KindIdentifier || name.AsIdentifier().Text != "ref" {
					return
				}
				init := attr.Initializer
				if init == nil {
					return
				}
				switch init.Kind {
				case ast.KindStringLiteral:
					reportStringRef(node)
				case ast.KindJsxExpression:
					expr := init.AsJsxExpression().Expression
					if expr == nil {
						return
					}

					expr = ast.SkipParentheses(expr)
					switch expr.Kind {
					case ast.KindStringLiteral:
						reportStringRef(node)
					case ast.KindNoSubstitutionTemplateLiteral, ast.KindTemplateExpression:
						if detectTemplateLiterals {
							reportStringRef(node)
						}
					}
				}
			},
		}
	},
}

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