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.

Rule idea: String call instead of template literal

See original GitHub issue

Hi. 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
platinumazurecommented, Jul 16, 2019

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.

1reaction
ljharbcommented, Jul 16, 2019

When template literals are available, nobody should be using String(), because global.String = function () { return 42; } will break that code.

The reason '' + foo is bad is because it calls valueOf first, not because it’s implicit (it’s not) - a template literal, like String(), calls toString first, which makes it the best approach hands down.

This rule should not exist, nor should it be an option on an existing rule.

Read more comments on GitHub >

github_iconTop 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 >

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