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)

Commits related to this issue

Most upvoted comments

There are also a couple of other arguments against making backtick quotes preferred for all strings:

  1. Using backticks for strings without interpolated variables adds a burden of additional care about escaping, because if you are not planning to interpolate, but for some reason want to have in your string literally ${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}.
  2. Making backticked strings to be the only possible strings to use instead of single quoted ones would break all the codebases in pure ES5 that can’t use string template syntax, but use standard to enforce style rules.

And what about this case?

console.log(`I'd like to quote someone: "That will force manual escaping on me"`)

It seems to be legit, but it’s forbidden by the rule.

I also think this would make sense, such that

Single quotes for strings – except to avoid escaping

is enforced. Note that the implementation is very simple: Remove the "allowTemplateLiterals": true option from the quotes rule. Despite the naming of the option, "allowTemplateLiterals": false actually 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.

console.log('hello there')    // ✓ ok
console.log("hello there")    // ✗ avoid
console.log(`hello there`)    // ✗ avoid

$("<div class='box'>")        // ✓ ok
console.log(`hello ${name}`)  // ✓ ok

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/c8f1e23b1a3908e3b616b457736fdc2a31af9502

I’m going to ship this in v14.

it’d be simpler to just disable non-backtick quotes entirely

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.