Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single quotes are sometimes replaced with double quotes even when "prettier.singleQuote: true" is specified #2338

Closed
alexjlockwood opened this issue Jun 28, 2017 · 11 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@alexjlockwood
Copy link

alexjlockwood commented Jun 28, 2017

I am using the vscode extension and am having an issue with the prettier.singleQuote setting. The only settings I have customized are:

"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"prettier.printWidth": 100

Specifically, when prettier is run on a string like this:

'You\'ll lose any unsaved changes.'

the string is changed to:

"You'll lose any unsaved changes."

The expected behavior (I think) is to continue using single quotes since the setting prohibits the use of double quotes.

My temporary workaround is to change the string to:

`You'll lose any unsaved changes.`
@josephfrazier
Copy link
Collaborator

This is as intended. From https://github.com/prettier/prettier/tree/21733e441dc01f7d85b483edee92b7e7507bfd9a#quotes:

  • If the number of quotes outweighs the other quote, the quote which is less used will be used to format the string - Example: "I'm double quoted" results in "I'm double quoted" and "This \"example\" is single quoted" results in 'This "example" is single quoted'.

Maybe we should change the preceding description "Use single quotes instead of double quotes." to something like "Prefer single quotes over double quotes.".

@lydell
Copy link
Member

lydell commented Jun 28, 2017

FYI, this is how Prettier handles quotes:

  1. JSX always uses double quotes.
  2. The quote that results in the least number of escapes is chosen.
  3. Finally, the singleQuote option is used to choose quote.

See also #973.

@alexjlockwood
Copy link
Author

The reason I reported is because prettier modifies the code to use double quotes, which results in an eslint error. Is there any way this can be avoided?

@lydell
Copy link
Member

lydell commented Jun 28, 2017

Yes! Disable the ESLint rule. It gains you nothing.

@alexjlockwood
Copy link
Author

Ahh, good point! Thanks for the advice and the great tool. :)

@oliamb
Copy link

oliamb commented Jul 5, 2017

Instead of disabling it entirely, you can use avoidEscape option (http://eslint.org/docs/rules/quotes).

@lydell
Copy link
Member

lydell commented Jul 5, 2017

@oliamb Even if you do, the rule still doesn't gain you anything, does it? It only makes ESLint take a little longer to run?

@oliamb
Copy link

oliamb commented Jul 5, 2017

I have at least one interesting use case that can justify this rule. When using string constants, it is useful to be able to search and replace using 'myStringConstant' rather than myStringConstant or /['"]myStringConstant['"]/. I was wondering if the rule in prettier will forbad me to do so anymore but it is not the case as it is still deterministic ('it is green' VS "it's green")

NB: let's maybe not dive into whether it is a good idea to use string constants ;-)

@brocoli
Copy link

brocoli commented Apr 27, 2018

Code style is all about consistency, it doesn't need to "gain something" beyond consistency for it to be useful.

Still, using avoidEscape in eslint seems like a good idea, and the correct resolution to this issue anyway.

drbonzo added a commit to drbonzo/regexp_flow_react that referenced this issue May 10, 2018
Also check: prettier/prettier#2338
when Prettier was using double quotes, instead single quotes (as in config: `"singleQuote": true`)
@neossoftware
Copy link

neossoftware commented May 22, 2018

My .prettierrc looks :

{ "useTabs": false, "printWidth": 80, "tabWidth": 2, "singleQuote": true, "trailingComma": "none", "jsxBracketSameLine": false, "parser": "typescript" }

and it works perfect, I had the problem that I wanted to format my typescript clases but when I was using Prettier it always changed the single quote to double, I only modify the parser to Typescript.

@ConnorChristie
Copy link

ConnorChristie commented Jun 19, 2018

FYI the tslint rule for avoid escape is here:
https://github.com/palantir/tslint/blob/46098f7b7f0f63f8d04b63e9a3598d1fc467b35f/test/rules/quotemark/double-avoid-escape/tslint.json

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Sep 17, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Sep 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

7 participants