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.

Bug: Probably wrong fix of ESLint's prefer-template warning

See original GitHub issue

Environment

Node version: 16.9.1 npm version: 7.24.0 Local ESLint version: 8.0.0.beta.2 Global ESLint version: none Operating System: Windows 10 21H1

What parser are you using?

Other

What did you do?

ESLint raises a warning ESLint: Unexpected string concatenation.(prefer-template):

for the given code:

res.setHeader("content-security-policy",
		"default-src 'self' https://*.google.com;"
		+ "script-src 'self' 'unsafe-eval' 'unsafe-inline' https://*.google.com;"
		+ "connect-src 'self' https://*.google.com;"
		+ "frame-ancestors 'none';"
		+ "report-to " + policyViolationReport
		+ ";");

What did you expect to happen?

When I click on ESLint: Fix: prefere-template in IDE (or eslint --config eslintrc.json --fix app.js), I expect to get:

res.setHeader("content-security-policy",
		`"default-src 'self' https://*.google.com;"`
		+ `"script-src 'self' 'unsafe-eval' 'unsafe-inline' https://*.google.com;"`
		+ `"connect-src 'self' https://*.google.com;"`
		+ `"frame-ancestors 'none';"`
		+ `"report-to "${policyViolationReport};`);

What actually happened?

The result in fact:

res.setHeader("content-security-policy",
		`${"default-src 'self' https://*.google.com;"
		+ "script-src 'self' 'unsafe-eval' 'unsafe-inline' https://*.google.com;"
		+ "connect-src 'self' https://*.google.com;"
		+ "frame-ancestors 'none';"
		+ "report-to "}${  policyViolationReport
		 };`);

The possible issues are:

  1. Unwanted space ${ policyViolationReport
  2. Unwanted line break for the closing clause }; of the last literal variable:
+ "report-to "}${  policyViolationReport
 };`);
  1. For some reason, "default-src 'self' https://*.google.com;" is represented as a placeholders (${expression}), while in fact it’s just a text, not a variable in contrast to the policyViolationReport, which really should be represented as a literal placeholder — ${policyViolationReport}.

  2. BTW, the line break with literals can be implemented without concatenation symbols +, e.g.:

`string text line 1
 string text line 2`

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

File for reproduction: app.js.txt

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Oct 30, 2021
  1. Unwanted space ${ policyViolationReport

That’s intentional because this rule doesn’t know user’s preferences about spacing in ${}. This spacing can be automatically fixed in subsequent iterations by template-curly-spacing and/or no-multi-spaces rules.

  1. Unwanted line break for the closing clause }; of the last literal variable:
+ "report-to "}${  policyViolationReport
 };`);

That’s intentional, it is assumed that the line breaks were there for readability.

  1. For some reason, "default-src 'self' https://*.google.com;" is represented as a placeholders (${expression}), while in fact it’s just a text, not a variable in contrast to the policyViolationReport, which really should be represented as a literal placeholder — ${policyViolationReport}.

This looks like a bug.

  1. BTW, the line break with literals can be implemented without concatenation symbols +, e.g.:
`string text line 1
 string text line 2`

It wouldn’t be the same string value, because line breaks in template literals contribute to the value.

1reaction
snitin315commented, Oct 9, 2021

I looked into it and found that it’s related to the placement of the variable in concatenation instead of multi-line concatenation:

  • code: var foo = "Hello" + test + "world" => fixed code: var foo = `Hello ${test} world`
  • code: var foo = "Hello " + "world" + test => fixed code: var foo = `${"Hello " + "world"}${test}`
  • Similarly for multi-line concatenation.

Check fixed code in online demo

The fixed code is not incorrect in any case, just the format used in the cases is not consistent.

I am looking more into it, if I can come up with a fix for this.

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 >
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 >
Reduce smelling code and detect JavaScript issues with ESLint
Problem statement: Smelling Code Have you ever had to fix an error in someone else's code - but you had to reformat the...
Read more >
Using Prettier and ESLint to automate formatting and fixing ...
Learn how to use ESLint and Prettier together to automatically format and fix JavaScript code in your projects.
Read more >
Invalid Hook Call Warning - React
You are probably here because you got the following error message: ... Bad: inside an event handler (to fix, move it outside!) const...
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