Rule idea: String call instead of template literal
See original GitHub issueHi. I have an idea for the rule that will make reading the code more convenient. Instead of template literal with one value and without regular string around, String
call looks more clearly.
Example of bad code:
// prefer-string-call: "error"
const stringCount = `${getCount()}`; // Expected call of String() instead of template literal
Example of good code:
// prefer-string-call: "error"
const stringCount = String(getCount);
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Restrict template literal interpolation expressions to strings
I think, if you have a custom toString method, it's okay to call it in this context instead of intransparently breaking code. 8...
Read more >8. Template literals - Exploring JS
Template literals are string literals with support for interpolation and multiple lines. Tagged template literals (short: tagged templates): are function calls ...
Read more >Template literals (Template strings) - JavaScript | MDN
Template literals are enclosed by backtick ( ` ) characters instead of double or single quotes. Along with having normal strings, template ...
Read more >Template Literals are Strictly Better Strings - Ponyfoo
This article explores how template literals are strictly better than strings, and how they too should become the new default of a post-ES6 ......
Read more >Can ES6 template literals be substituted at runtime (or reused)?
The idea is to let the template parser split out the constant strings from the variable "slots", and then return a function that...
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 FreeTop 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
Top GitHub Comments
Hi @krutoo, thanks for the issue.
Personally, this feels like a subjective preference and I could see people going either way on this. Also, this covers a pretty small subset of strings. So I’m mildly opposed to adding this to core, but I will go along with the team consensus.
It sounds like this would be a great case for a custom rule for your codebase.
When template literals are available, nobody should be using
String()
, becauseglobal.String = function () { return 42; }
will break that code.The reason
'' + foo
is bad is because it callsvalueOf
first, not because it’s implicit (it’s not) - a template literal, likeString()
, callstoString
first, which makes it the best approach hands down.This rule should not exist, nor should it be an option on an existing rule.