useless brackets when format the string under the template literal
See original GitHub issueinput:
```javascript
`模板字符串可以直接插入单引号'和双引号"`;
'单引号字符串可以插入反引号`';
```
output:
```javascript
`模板字符串可以直接插入单引号'和双引号"`;
('单引号字符串可以插入反引号`');
```
expect: no brackets wrap the string:
'单引号字符串可以插入反引号`'
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >
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 Free
Top 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
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:
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.
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.