Rule Enhancement Proposal: autofix for prefer-template
See original GitHub issueprefer-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.
- If there is
or
${` in the string, escapes it:→ \
${
→\${
- 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}
- 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:
- Created 7 years ago
- Comments:12 (12 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 >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 >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
Looking at the examples, it seems inconsistent to do this:
Instead, maybe it would be better to keep all the whitespace around the plus sign:
Then
template-curly-spacing
can fix the whitespace if necessary.(Admittedly, most people will probably want their code to look more like
hello, ${name}
thanhello, ${ 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.)Yes.