Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier doesn't work for changing double quote to single quote in visual code

I work with EsLint and Prettier in visual code. Everything work well without singlequote and doublequote.

This is my .eslintrc file: { "parser": "babel-eslint", "plugins": ["prettier"], "rules": { "prettier/prettier": [ "error", { "singleQuote": true } ] } } When I use ctr+ shift+P and type "format Document". the double quote didn't change to single quote. If I use autofix, it changes but when I save it, the single quote turn into double quote.

How can I fix that problem.

like image 699
nguyễn khánh công Avatar asked Jul 16 '17 02:07

nguyễn khánh công


People also ask

How do you replace a double quote in a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.


2 Answers

As you already have singleQuote: true set in your prettier config, I suspect that you're seeing your single quotes converted to double quotes specifically inside your JSX tags. This is because Prettier has a separate rule for JSX:

"jsxSingleQuote": true

Without this, even with "singleQuote": true, Prettier will still convert single quotes within JSX to double.

According to Prettier, double quotes in JSX is the default because it's descended from HTML, where double quotes are more common.

Set "jsxSingleQuote": true wherever you have your Prettier config, and that should do the trick.

like image 191
Andrew Avatar answered Sep 22 '22 00:09

Andrew


This only happens if you have ' in your string e.g. ('it\'s a thing'). Prettier has this issue and there is apparently no solution to this.

My workaround is, using `` (string template syntax) for such strings and single quote for all other strings.

`it's a thing`

You don't have to escape this.

like image 43
Nitin Jadhav Avatar answered Sep 21 '22 00:09

Nitin Jadhav