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.

`prefer-template` configuration to ignore if resulting line would be over a configurable length.

See original GitHub issue

What rule do you want to change? prefer-template Does this change cause the rule to produce more or fewer warnings? Fewer errors/warnings (depending on configuration) How will the change be implemented? (New option, new default behavior, etc.)? New option Please provide some example code that this change will affect:

const userInfoString = `theExpectedStart-${user.name}`
  + (user.hasPet ? `-${user.petName} : '')
  + (user.hasAddress ? `-${user.city}` : '')

What does the rule currently do for this code? It gives an error What will the rule do after it’s changed? Not give an error when the rule is configured to ignore string concatenation for lines which would be over the configured length Are you willing to submit a pull request to implement this change? If I have time.

prefer-template (used as a rule in popular style guides such as the airbnb style guide) often encourages very long lines (even greater than max-len when "ignoreTemplateLiterals": true is set)

For example, if we want to display user information in a kebab-case string, we might do something like

// works when the `max-len` rule has the `"ignoreTemplateLiterals": true` option:
const userInfoString = `theExpectedStart-${user.name}${user.hasPet ? `-${user.petName} : ''}${user.hasAddress ? `-${user.city}` : ''}`

This 135-character line constructs a string like theExpectedStart-harts-vancouver ( "ignoreTemplateLiterals": true is also part of the airbnb style guide)

The following would be much more readable, but currently not configurable to not throw a linting error:

const userInfoString = `theExpectedStart-${user.name}`
  + (user.hasPet ? `-${user.petName} : '')
  + (user.hasAddress ? `-${user.city}` : '')

There are currently some workarounds:

// eslint-disable-next-line prefer-template
const userInfoString = `theExpectedStart-${user.name}`
  + user.hasPet ? `-${user.petName} : ''
  + user.hasAddress ? `-${user.city}` : ''
// construct the string with multiple statements:
let userInfoString=`theExpectedStart-${user.name}`
if (user.hasPet) {
  userInfoString=`${userInfoString}-${user.petName}`
}
if (user.hasAddress) {
  userInfoString=`${userInfoString}-${user.city}`
}

But a configurable option to prefer-template would be preferable. Something like "ignoreForLinesOverLength": 80, for example, which would disable the rule for situations like

const userInfoString = `theExpectedStart-${user.name}`
  + (user.hasPet ? `-${user.petName} : '')
  + (user.hasAddress ? `-${user.city}` : '')

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
btmillscommented, Apr 30, 2021

To add yet another alternative, this is how Prettier wraps long template strings:

const userInfoString = `theExpectedStart-${user.name}${
  user.hasPet ? `-${user.petName}` : ""
}${user.hasAddress ? `-${user.city}` : ""}`;

In my experience, the nudge to refactor long template strings with complex expressions inside leads to more legible code once I’ve rearranged it. When there’s no better alternative, a disable comment is appropriate. I think the rule is doing the right thing here, and line length isn’t its responsibility.

0reactions
nzakascommented, May 6, 2021

There isn’t enough support on the team to add an additional option, so closing.

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 >
System Error Messages Guide For Access and Edge ... - Cisco
This is an information message which occurs when NAT is configured and a frame is dropped due to the default maximum entries limit....
Read more >
Configuring ESLint - Breword 文档集合
ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix...
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 >
Diff - 402e1b6e55e9041dfd1a93580e45e5c5dba1db55^!
You may reproduce and distribute copies of the + Work or Derivative ... +* c5cbc1b Docs: Add rule config above each example in...
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