prefer_template

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 PreferTemplateRule = rule.Rule{
	Name: "prefer-template",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {

		reported := map[int]bool{}

		checkForStringConcat := func(node *ast.Node) {
			if !utils.IsStringLiteralOrTemplate(node) {
				return
			}

			parent := ast.WalkUpParenthesizedExpressions(node.Parent)
			if !utils.IsPlusBinaryExpression(parent) {
				return
			}
			top := getTopConcatBinaryExpression(parent)
			if reported[top.Pos()] {
				return
			}
			reported[top.Pos()] = true

			if !hasNonStringLiteral(top) {
				return
			}

			msg := rule.RuleMessage{
				Id:          "unexpectedStringConcatenation",
				Description: "Unexpected string concatenation.",
			}

			if hasOctalOrNonOctalDecimalEscape(ctx.SourceFile, top) {
				ctx.ReportNode(top, msg)
				return
			}

			fixed := toTemplateLiteral(ctx.SourceFile, top, "", "")
			ctx.ReportNodeWithFixes(top, msg, rule.RuleFixReplace(ctx.SourceFile, top, fixed))
		}

		return rule.RuleListeners{
			ast.KindStringLiteral:                 checkForStringConcat,
			ast.KindNoSubstitutionTemplateLiteral: checkForStringConcat,
			ast.KindTemplateExpression:            checkForStringConcat,
		}
	},
}

https://eslint.org/docs/latest/rules/prefer-template

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