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.

Add option to the `prefer-template` rule to only warn on concat with variables

See original GitHub issue
~/dev
❯ eslint --version
v2.13.1

~/dev
❯ echo "var key = 'fooBar';var foo = '--' + key.replace(/[A-Z]/g, '-$&').toLowerCase();" | eslint --stdin --rule='prefer-template:2'

<text>
  1:30  error  Unexpected string concatenation  prefer-template

✖ 1 problem (1 error, 0 warnings)

Problem

The following:

var key = 'fooBar';
var foo = '--' + key.replace(/[A-Z]/g, '-$&').toLowerCase();

Would be very unreadable in a template string:

var key = 'fooBar';
var foo = `--${key.replace(/[A-Z]/g, '-$&').toLowerCase()}`;

Request

I would like an option to only have template literals enforced when concatenating a string with variables, like:

var key = 'fooBar';
var foo = '--' + key;

⬇️

var key = 'fooBar';
var foo = `--${key}`;

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
platinumazurecommented, Jul 4, 2016

@sindresorhus I’m not really convinced by the new example-- seems to me that this is quite readable:

var foo = `${chalk.red.bold('I')} like ${chalk.blue.bold('unicorns')} and ${chalk.yellow.bold('ponies')}`;

Seems to me it’s honestly really case by case (because I agree your first example can be pretty hard to read). I just feel like assuming “variables” should be the only thing that should go in templates might be overly simplistic, certainly subjective (e.g., why not MemberExpressions, or maybe CallExpressions with no arguments?). I honestly still think at this point that your best bet is to use // eslint-disable-line prefer-template in those cases where you think a template is less readable.

1reaction
platinumazurecommented, Jul 1, 2016

Between @vitorbal’s suggestion of using an intermediate variable, the existence of inline config comments, and our new rule change guideline “Is the rule clearly incomplete without the requested enhancement?”, I’m 👎 on this proposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-template - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Unexpected string concatenation - Stack Overflow
You are only seeing an error because your linter rules have been configured to prefer template strings over string concatenation.
Read more >
prefer-template - Rule
Rule : prefer-template. Prefer a template expression over string literal concatenation. Config. If allow-single-concat is specified, then a single ...
Read more >
A Better Way To Concatenate Text Strings In Power Apps
Strings are an awesome new way to concatenate text strings in Power Apps by writing curly braces { } to denote a function...
Read more >
Airbnb JavaScript Style Guide()
const and let only exist in the blocks they are defined in. ... 5]].reduce((acc, item, index) => { const flatten = acc.concat(item); acc[index]...
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