question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

quotes: 'single'|'double' + allowTemplateLiterals should only allow template literals with interpolation

See original GitHub issue

What rule do you want to change?

The quotes rule in combination with { "allowTemplateLiterals": true }

Does this change cause the rule to produce more or fewer warnings?

More warnings

How will the change be implemented? (New option, new default behavior, etc.)?

Please provide some example code that this change will affect:

/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/

const a = 'foo';
const b = `bar`;
const c = `baz ${42}`;

What does the rule currently do for this code?

The rule currently allows all of these with the given configuration.

What will the rule do after it’s changed?

Per the configuration, single quotes are preferred but template literals are allowed. Ideally, that would mean that single quotes should be used UNLESS string interpolation is used. That is, template literals should be allowed ONLY when there is an ${expression} within the template literal. If not, then the use of template literals does not add any value, and only results in an inconsistent coding style despite the use of the eslint quotes rule.

In the above example, a and c would be allowed, but b would result in an error (since a template literal is used frivolously without there being any ${}).

Are you willing to submit a pull request to implement this change?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mathiasbynenscommented, Mar 20, 2020

@mdjermanovic { "allowTemplateLiterals": false } does the trick, thanks! I agree a redesign of these options (as proposed in #12156) would be nice.

1reaction
mdjermanoviccommented, Feb 28, 2020

That is, template literals should be allowed ONLY when there is an ${expression} within the template literal.

That’s how { "allowTemplateLiterals": false } (default) works.

/*eslint quotes: ["error", "single", { "allowTemplateLiterals": false }]*/

const a = 'foo'; // ok
const b = `bar`; // error
const c = `baz ${42}`; // ok

quotes, regardless of the configuration, never disallows these:

  • Tagged template literals.
  • Template literals with ${expression}
  • Multi-line template literals.

Use case for { "allowTemplateLiterals": true } should be only to allow the use of backticks instead of the other quotes to avoid escaping the preferred quotes:

/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/

const a = `'foo'`; // ok
const b = "'bar'"; // error

Though, the option allows all template literals, not just those with the preferred quote inside (I believe that wasn’t the intention, but it would be a breaking change now).

Read more comments on GitHub >

github_iconTop Results From Across the Web

quotes - ESLint - Pluggable JavaScript Linter
JavaScript allows you to define strings in one of three ways: double quotes, single quotes ... "allowTemplateLiterals": true allows strings to use backticks....
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
Is there a downside to using ES6 template literals syntax ...
You can already use single quotes or double quotes to make strings. Choosing which one is largely arbitrary, and you just stick with...
Read more >
Understanding Template Literals in JavaScript - Tania Rascia
In this article, you will go over the differences between single/double-quoted strings and template literals, running through the various ...
Read more >
What is the difference between using single, double quotes ...
They allow for embedded expressions such as, for example, variables: ... then using double quotes will save you from having to escape the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found