standard: Disable backtick quotes if it is not a template
Now you could write either
let a = 'hello'
or
let a = `hello`
And it would pass standard linter for both cases, even if in the second case the template doesn’t have any interpolated variables and in fact is just a different form of a string. So you could mix both forms, something conceptually similar to mixing " and ' quotes.
ESLint allows us to prevent this behavior by adding a rule
"quotes": [2, "single"]
This rule makes ESLint to throw an error for this
let a = `hello`
but don’t throw any errors for this
let subject = 'world'
let a = `hello, ${world}`
Also see this issue in ESLint.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 21 (11 by maintainers)
There are also a couple of other arguments against making backtick quotes preferred for all strings:
${console}, then it should be properly escaped in order to avoid runtime errors or perhaps even vulnerabilities. It is also not always possible for any linter to automatically determine the cases when an expression should be escaped and when it is not, like in this example with${console}.standardto enforce style rules.And what about this case?
It seems to be legit, but it’s forbidden by the rule.
I also think this would make sense, such that
is enforced. Note that the implementation is very simple: Remove the
"allowTemplateLiterals": trueoption from thequotesrule. Despite the naming of the option,"allowTemplateLiterals": falseactually still allows template literals when their extra functionality is used, see examples in the doc for the quotes rule.Thanks for the input everyone!
I think that we should disallow template literals when placeholders or tagged template features are not used.
Only 1 ecosystem package (
webtorrent-desktop) needed to be updated, and it was just one line that needed the update: https://github.com/webtorrent/webtorrent-desktop/commit/c8f1e23b1a3908e3b616b457736fdc2a31af9502I’m going to ship this in v14.
I like having two types of quotes to indicate intent. Interpolating is rare enough that I appreciating having it explicitly called out by the
`character.