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:
- Created 7 years ago
- Reactions:5
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sindresorhus I’m not really convinced by the new example-- seems to me that this is quite readable:
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.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.