Proposed new rule: wrong-quotes-for-template-string
See original GitHub issueWhen 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:13 (12 by maintainers)
Top GitHub Comments
I will give 👍 for this. There are 5 issues (#6143, #5408, #5376, #4286, #2170) about this in this repo, and my coworker also had this mistake before. Plus, autofixing for this is usefull to me. I cannot modify both quotes by one step.
Yes 😃 I’ll try to take a stab at it tonight.