Skip to content

Proposed new rule: wrong-quotes-for-template-string #6186

@JamesMessinger

Description

@JamesMessinger

When does this rule warn? Please describe and show example code:
This rule would warn when a string appears to use ES6 template string variable substitution, but isn't using a backtick quotes. The goal is to eliminate an easy code mistake, such as the following:

This code would warn:

let name = "John Doe";             // <--- This is a normal string (not a template string)
let greeting = "Hello, ${name}";   // <--- This is ALSO a normal string, but the coder 
                                   //      probably meant for it to be a template string

This code would not warn:

let name = "John Doe";             // <--- This is a normal string (not a template string)
let greeting = `Hello, ${name}`;   // <--- This is a template string

Is this rule preventing an error or is it stylistic?
This rule aims to prevent a common ES6 coding mistake. It's easy to mistakenly use the wrong quotes (e.g. double-quotes instead of backticks) for a template string. When this happens, the code lints and runs perfectly fine, but at runtime, the string value will contain the raw "${name}" rather than the variable value "John Doe".

Why is this rule a candidate for inclusion instead of creating a custom rule?
This is a rule that would benefit everybody who is using ES6 template strings. It's not limited to a specific framework or platform.

Are you willing to create the rule yourself?
Maybe. Not sure how hard it would be.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions