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 Enhancement Proposal: autofix for prefer-template

See original GitHub issue

prefer-template is warning concatenations of between strings and expressions. If the rule supports autofix, it’s very helpful to us since we need many steps to fix the code.

  1. If there is or${` in the string, escapes it:
    • → \
    • ${\${
  2. Replaces the concatenation by a template curly:
    • " + expression + "${expression}
    • " + expression${expression}"
    • expression + ""${expression}
    • If there are line terminators around the +s, should keep those to inside of template curlies.
    • If the expression is enclosed parentheses, should remove the parentheses.
      • " + (a + 1) + "${a + 1}
  3. Replaces the enclosing "/' by `.

Examples:

"hello, " + name + "."
// ↓
`hello, ${name}.`
"hello, " + name
// ↓
`hello, ${name}`
"favorites: " + favorites.map(f => {
    return f.name
}) + ";"
// ↓
`favorites: ${favorites.map(f => {
    return f.name
})};`
"favorites: " + 
    favorites.map(f => {
        return f.name
    }) + 
    ";"
// ↓
`favorites: ${
    favorites.map(f => {
        return f.name
    })
};`

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Sep 16, 2016

Looking at the examples, it seems inconsistent to do this:

"hello, " + name + "."
// ↓
`hello, ${name}.` // the whitespace before and after the + sign is removed

"hello, " +
  name + "."
// ↓
`hello, ${
  name}.` // the newline after the + sign is not removed

Instead, maybe it would be better to keep all the whitespace around the plus sign:

"hello, " + name + "."
// ↓
`hello, ${  name  }.`

Then template-curly-spacing can fix the whitespace if necessary.

(Admittedly, most people will probably want their code to look more like hello, ${name} than hello, ${ name }, but in my opinion keeping the whitespace seems like the more consistent thing to do. Alternatively, I guess we could make an exception for spaces surrounding plus signs since they’re a fairly common case.)

1reaction
mysticateacommented, Sep 15, 2016

Yes.

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 >
How To Enable Linting on Save with Visual Studio Code and ...
Learn how to lint your code on every file save using ESLint rules and VS Code settings.
Read more >
BQU - River Thames Conditions
Voip phone adapter linksys, Harpy eagle video, Bransby law firm, Case study format for b.ed? Bloom's friseur steingasse mainz, Pasaport pizza burdur, ...
Read more >
node_modules/eslint/CHANGELOG.md ... - GitLab
9152417 Fix: deprecation warning in RuleTester using Node v11 (#11009) (Teddy Katz); e349a03 Docs: Update issue templates to ask for PRs (#11012) (Nicholas ......
Read more >
Tutorial: Write your first analyzer and code fix - Microsoft Learn
An analyzer contains code that recognizes violations of your rule. ... The analyzer with code fix template creates five projects:.
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