Skip to content

Add consistent-template-literal-escape rule#2866

Merged
sindresorhus merged 4 commits into
sindresorhus:mainfrom
costajohnt:consistent-template-literal-escape
Feb 19, 2026
Merged

Add consistent-template-literal-escape rule#2866
sindresorhus merged 4 commits into
sindresorhus:mainfrom
costajohnt:consistent-template-literal-escape

Conversation

@costajohnt
Copy link
Copy Markdown
Contributor

Fixes #2835

Adds a new consistent-template-literal-escape rule that enforces escaping the dollar sign (\${) instead of the opening brace ($\{) or both (\$\{) in template literals.

Summary

There are three ways to escape ${ in a template literal:

  • \${ — escape the dollar sign (enforced by this rule)
  • $\{ — escape the opening brace
  • \$\{ — escape both (redundant)

This rule reports and auto-fixes the latter two patterns.

Details

  • Listens on TemplateElement AST nodes
  • Skips String.raw tagged templates
  • Handles edge cases with preceding escaped backslashes
  • Auto-fixable via --fix
  • Enabled in recommended config
  • Snapshot-based tests covering valid/invalid cases including multiple occurrences and escaped backslash contexts

@costajohnt costajohnt force-pushed the consistent-template-literal-escape branch from 0c4ff61 to f16bd9b Compare February 8, 2026 04:23
@sindresorhus
Copy link
Copy Markdown
Owner

I think there is one important safety issue here: the auto-fix currently rewrites non-String.raw tagged templates too. That can change runtime behavior for tags that read strings.raw, even when cooked output looks the same. I’d suggest reporting them without a fixer.

The auto-fix changes `strings.raw` values, which can alter runtime
behavior for tag functions that read raw strings. Report the
inconsistency without a fixer so developers can decide manually.
@costajohnt
Copy link
Copy Markdown
Contributor Author

Good catch — you're right that changing the escape style alters strings.raw for tag functions that inspect raw values.

Updated the rule to only auto-fix untagged template literals. For tagged templates (other than String.raw, which is already skipped entirely), the inconsistency is still reported but without a fixer, so developers can decide manually.

@sindresorhus sindresorhus merged commit 55444b4 into sindresorhus:main Feb 19, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule proposal: consistent-template-literal-escape

3 participants