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.

useless brackets when format the string under the template literal

See original GitHub issue

input:

​```javascript
`模板字符串可以直接插入单引号'和双引号"`;
'单引号字符串可以插入反引号`';
​```

output:

​```javascript
`模板字符串可以直接插入单引号'和双引号"`;
('单引号字符串可以插入反引号`');
​```

expect: no brackets wrap the string:

'单引号字符串可以插入反引号`'

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
bakkotcommented, Jan 15, 2020

This is intentional. Prettier wraps any string literal which is in statement position in parentheses because otherwise such strings would get interpreted as directives if they occurred at the top of a function or program, which can change the behavior of the program.

To illustrate:

function f() {
  'use strict';
  return this === void 0;
}

function g() {
  ('use strict');
  return this === void 0;
}

f(); // true
g(); // false

Now, it is not technically necessary to do this for strings which are not on the first line of a function or program. But doing so anyway is more consistent, and can highlight bugs. See for example this thread on twitter.

0reactions
bakkotcommented, Jan 16, 2020

The reason it wraps strings in parentheses is because it is sometimes necessary to do so to preserve the behavior of the program (and the AST), as in my example, which is one of the most important goals for prettier. Since it is sometimes necessary, for consistency it always does it, consistency being another important goal. The fact that it sometimes highlights bugs is just a bonus.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
Include Parentheses () in the Template literals? - Stack Overflow
How to include Parentheses () in the string when using Template literals? Code: let type = "Yes"; let string = `Test ${type? (type)...
Read more >
507518 – Nested brackets in template literals breaks ... - Bugs
I think that the problem is not the brackets, but the "grave accent" (`)-character used to surround the value of variable instead of...
Read more >
Create Strings using Template Literals: is it JUST a bug?
have a comma separating each template literal string (before the line break and after the backtick) and,; the entire output should be flanked...
Read more >
What does this ${} syntax do? - JavaScript FAQ
Sometimes called template literals or template strings, this format is another way to type out a string making use of `` (called a...
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