prettier / prettier

Prettier is an opinionated code formatter.
https://prettier.io
MIT License
48.42k stars 4.15k forks source link

Change the default value for singleQuote to true #4102

Open lipis opened 6 years ago

lipis commented 6 years ago

From https://github.com/prettier/prettier/issues/3503#issuecomment-359863688

mvolkmann commented 6 years ago

Every JavaScript survey I've ever seen shows that most developers prefer single quotes for literal strings.

yvele commented 6 years ago

Most of popular languages (go, C#, java, C, C++, rust) strictly uses double quotes, why should there be an exception? šŸ¤” (python uses both)

Other languages

Language Double quotes Single quotes
go Yes
Python Yes Yes
C# Yes
java Yes
C Yes
C++ Yes
rust Yes
Lua Yes Yes

Other code formatters

Name Language Quotes
Black Python Double quotes (discussion)

Document languages

Language Double quotes Single quotes
CSS Yes Yes
HTML Yes Yes

Data related languages

Language Double quotes Single quotes
JSON Yes
YAML Yes Yes
GraphQL Yes
XML Yes

JavaScript related Guidelines

Guidelines Quotes
Airbnb Single quotes
Google Single quotes
React Single quotes but Double quotes for JSX properties
TypeScript Double quotes (for Contributors)
Vues.js Don't mind
jQuery Double quotes
Node.js Single quotes (for Contributors)
Deno Double quotes

Console

See also how console outputs

Console Quotes
Chrome Double quotes
Firefox Double quotes
Edge/IE Double quotes
Safari Double quotes
Node.js Single quotes
Deno Double quotes
j-f1 commented 6 years ago

Because single quotes are less noisy.

lipis commented 6 years ago

At least 50% less noisy :)

alo commented 6 years ago

This is from the Google JavaScript Style Guide

Use single quotes

Ordinary string literals are delimited with single quotes ('), rather than double quotes (").

a118n commented 6 years ago

Both Airbnb and Google style guides use single quotes. Switching this to default would be quite sensible, since a lot of companies/devs use their guides.

craigmorrison commented 6 years ago

A large reason for single quotes being common in JS compared to other langauges is that it reads well when quoting HTML and some selectors.

For example:

document.querySelector('[data-foo="bar"]')

The alternatives are to:

yvele commented 6 years ago

@a118n alright I've updated my post with those informations šŸ˜‰

TakahiRoyte commented 6 years ago

In addition to @craigmorrison 's comment, big JavaScript frameworks, such as Angular and Vue, often contains JavaScript implementation inside HTLM attributes.

// Vue example
<button @click="submit('string value')">Submit</button>

// Angular example
<button (click)="service.submit('string value')">Submit</button>

For such a case single quotes must be used in order to avoid interference with HTML's double quotation. The important part is how JavaScript is used, not what other languages use.

I strongly recommend single quotes to be used.

kachkaev commented 6 years ago

Prettier supports plugins for python, php, swift, elm, java, pg, ruby, etc. Most of the languages use double quotes (see comment above), so won't a switch cause more harm and ambiguity than benefits?

I agree that single quotes are 50% less noisy and was preferring them for a couple of years after looking at the airbnb style guide. I now use double quotes in JS and TS files again simply because that's Prettier's default and can't say it's terrible. On a positive side, the quotes are the same in JS and JSON, which simplifies string search (e.g. package names).

Airbnb and Google style guides are JavaScript-only, while Prettier is slowly becoming a cross-language tool. IMO this needs to be taken into account when making the final decision for 2.0. And, of course, the diffs that zero-config users would have is also something worth thinking about.

brocoli commented 6 years ago

There's also the "avoidEscape" setting in eslint, which is pretty useful for readability, be it with single or double quotes.

j-f1 commented 6 years ago

Thatā€™s already what we do @brocoli and I agree that itā€™s helpful.

lydell commented 5 years ago

The new Python formatter "black" uses double quotes: https://github.com/ambv/black/#strings. Discussion: https://github.com/ambv/black/issues/118

j-f1 commented 5 years ago

@iamclaytonray Donā€™t worry ā€” weā€™d never intentionality break your code. If the language only supports double quotes, weā€™ll print double quotes. Since Prettier isnā€™t a general formatter (we need to manually add support for each new language), there shouldnā€™t be issues with quotes being changed illegally.

dandonahoe commented 5 years ago

So is there a way to override the double quote default? I'm tearing my hair out trying to get this working. It totally breaks all of our style rules.

mvolkmann commented 5 years ago

Yes. See https://prettier.io/docs/en/options.html#quotes.

dandonahoe commented 5 years ago

@mvolkmann Thanks for the quick response. I've been toying with that but I'm not seeing it work. Here's my setup, with the ugly red circles being points of configuration. All packages are up to date and listed at the bottom. If you could take a look, I'd really appreciate it. Anything look funky here?

screen shot 2019-03-06 at 7 29 07 pm

"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"prettier": "1.14.3",
"tslint-config-prettier": "^1.18.0"
hawkrives commented 5 years ago

@dandonahoe Could you open a new issue for your problem? Ideally with a reproduction on the playground (https://prettier.io)

mvolkmann commented 5 years ago

@dandonahoe I've never seen the approach you are using where reading your .prettierrc file inside your .eslintrc file. I thought it was enough for the .prettierrc file to be present.

I have a pretty detailed description of the way I configure ESLint and Prettier at https://github.com/mvolkmann/talks/blob/master/eslint-prettier-2019.key.pdf. Maybe that will help.

dandonahoe commented 5 years ago

@mvolkmann That approach is meant to solve two use cases.

1 - Allow the IDE to read the .prettierrc 2 - I have a pre-commit git hook that lints staged files, but that only reads the .eslintrc.js, so sucking in the .prettierrc means I only have to add prettier rules in one spot.

Not my idea, but based off this boilerplate: https://github.com/react-boilerplate/react-boilerplate

mvolkmann commented 5 years ago

I wonder why the IDE can't get the Prettier settings from .prettierrc. I use VS Code and it does that.

On Thu, Mar 7, 2019 at 8:14 AM Dan Donahoe notifications@github.com wrote:

@mvolkmann https://github.com/mvolkmann That approach is meant to solve two use cases.

1 - Allow the IDE to read the .prettierrc 2 - I have a pre-commit git hook that lints staged files, but that only reads the .eslintrc.js, so sucking in the .prettierrc means I only have to add prettier rules in one spot.

Not my idea, but based off this boilerplate: https://github.com/react-boilerplate/react-boilerplate

ā€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/prettier/prettier/issues/4102#issuecomment-470540813, or mute the thread https://github.com/notifications/unsubscribe-auth/AAE10MqdDBf63hSqBtq7ovPS84Xljqa9ks5vUR6ugaJpZM4ScMrl .

-- R. Mark Volkmann Object Computing, Inc.

dandonahoe commented 5 years ago

@mvolkmann I use VS Code as well, but as you mentioned, I'll open a separate issue so I don't pollute this one. Thanks again.

tkrotoff commented 4 years ago

https://github.com/airbnb/javascript/issues/269#issuecomment-107319162:

Holding down the shift key every time you want a string kinda stinks. Your carpal tunnel will thank you for using single quotes.

lydell commented 4 years ago

@tkrotoff Thatā€™s not a good argument in the context of Prettier. Because with Prettier you can type either " or ', whichever you want, and then when you run Prettier the quotes are automatically changed.

karlhorky commented 4 years ago

I guess this is not a candidate for inclusion in 2.0 (#6888), right?

kachkaev commented 4 years ago

@karlhorky looking at the comments, there seems to be no consensus on this one. Guess it would not be wise to get such a massive change through in the last minute before 2.0 is out.

karlhorky commented 4 years ago

it would not be wise to get such a massive change through in the last minute before 2.0 is out

Hmm, there are still a number of open PRs that will land before 2.0 (which are also big changes to defaults), so I'm unsure that this argument holds water.

there seems to be no consensus on this one

This is more of an issue, surely, although there are a lot of positive signs already:

  1. The votes show the clearest picture so far (šŸ‘ 106 šŸ‘Ž 17)
  2. A few of the Prettier team have voiced their opinions so far:

šŸ‘ @j-f1 šŸ‘ @lipis

  1. The reason that the default was double quotes is because @jlongster used them at the time of creation of Prettier. The team decided that this was not a good default afterwards. (@vjeux "we want to switch the default" in https://github.com/prettier/prettier/issues/1105#issuecomment-290594825)

Maybe more of the Prettier team can chime in on changing the default on this one. cc @suchipi @thorn0 @evilebottnawi @lydell @prettier/core

alexander-akait commented 4 years ago

:+1: on single by default

ExE-Boss commented 4 years ago

Iā€™d prefer if all programming languages converged on doubleā€‘quotes, including JavaScript.

karlhorky commented 4 years ago

I would personally prefer consistency across languages too, but that's a bigger fish to fry - JavaScript (including the ecosystem) is different in many ways to other languages, and a JavaScript tool needs to embrace that.

karlhorky commented 4 years ago

I've opened a PR at #7466, just in case this will get the approvals that it needs.

lydell commented 4 years ago

So Iā€™d say we follow blackā€™s lead and stick with the double quotes. Personally, I program a lot in Elm and F# these days which do not allow single quotes for strings, so Iā€™m really used to double quotes now. Before I learned JS I learned HTML and CSS where double quotes is really common.

Shinigami92 commented 4 years ago
  • trailingComma es5: Makes code easier to edit and produces better git diff.
  • arrowParens always: Makes code easier to edit. Especially good for TypeScript.
  • eol lf: Helps collaboration across OS:es.
  • singleQuote: 100% bike shedding.

So Iā€™d say we follow blackā€™s lead and stick with the double quotes. Personally, I program a lot in Elm and F# these days which do not allow single quotes for strings, so Iā€™m really used to double quotes now. Before I learned JS I learned HTML and CSS where double quotes is really common.

Always keep in mind that this changes the default setting and does not force you to change it in the configuration file

lydell commented 4 years ago

How would you motivate the change in the 2.0 blog post?

karlhorky commented 4 years ago

How would you motivate the change in the 2.0 blog post?

@lydell I've started a bit of the "motivation" explanation in the unreleased changelog file in the PR here:

The JavaScript ecosystem has largely standardized on single quotes, with it being the most popular style seen in the wild.

https://github.com/prettier/prettier/pull/7466/files#diff-6afbd7a88bf7df26bba2e36de87663ffR16

I also further expanded on this in the answer to you in the PR:

https://github.com/prettier/prettier/pull/7466#issuecomment-579875882

If this should be fleshed out more for the 2.0 blog post, maybe some others could help out? I could also do some further writing here.

karlhorky commented 4 years ago

Ok, so maybe the supporters here can pull together and help me with some statistics:

@lydell has asked for some justification of the "most common" statement in the other pull request, so let's pull together and get some examples!

Here are my ideas for some examples of sources:

I will also take a look this evening.

karlhorky commented 4 years ago

Popular JavaScript Projects (combined with "GitHub Usage")

  1. Byte Archer
Project Dominant quote style
async 'Ā - 76% of quotes
express 'Ā - 92% of quotes
lodash 'Ā - 73% of quotes
request 'Ā - 97% of quotes
underscore 'Ā - 78% of quotes
angular.js 'Ā - 58% of quotes
react 'Ā - 52% of quotes

Double

Public Library Documentation

I went to the documentation websites of many popular JavaScript libraries to see what kinds of quotes that I would find in the examples.

I included both older, popular libraries as well as newer ones.

Single

Show Screenshots ![Screen Shot 2020-01-29 at 21 51 10](https://user-images.githubusercontent.com/1935696/73398667-335f0d00-42e6-11ea-969c-732d20848a20.png)
![Screen Shot 2020-01-29 at 22 18 04](https://user-images.githubusercontent.com/1935696/73398980-b4b69f80-42e6-11ea-8c04-f190e17da83e.png)
![Screen Shot 2020-01-29 at 21 49 39](https://user-images.githubusercontent.com/1935696/73398645-26dab480-42e6-11ea-824d-569b15ff6238.png)
![Screen Shot 2020-01-29 at 22 11 35](https://user-images.githubusercontent.com/1935696/73398808-6bfee680-42e6-11ea-981d-83ea045330db.png)
![Screen Shot 2020-01-29 at 21 55 41](https://user-images.githubusercontent.com/1935696/73398722-4376ec80-42e6-11ea-8399-6cedbffa0589.png)
![Screen Shot 2020-01-29 at 21 58 13](https://user-images.githubusercontent.com/1935696/73398778-5db0ca80-42e6-11ea-9151-34661dd6ff00.png)
![Screen Shot 2020-01-29 at 21 42 41](https://user-images.githubusercontent.com/1935696/73398557-03176e80-42e6-11ea-829e-2a9061be28da.png)
![Screen Shot 2020-01-29 at 21 45 48](https://user-images.githubusercontent.com/1935696/73398615-1cb8b600-42e6-11ea-8e33-9aa7d482c02c.png)
![Screen Shot 2020-01-29 at 22 11 59](https://user-images.githubusercontent.com/1935696/73398851-7c16c600-42e6-11ea-88b2-2f6a78eb54ae.png)
![Screen Shot 2020-01-29 at 21 46 25](https://user-images.githubusercontent.com/1935696/73399344-5ccc6880-42e7-11ea-92c2-5013b2161e22.png)
![Screen Shot 2020-01-29 at 22 09 20](https://user-images.githubusercontent.com/1935696/73399428-7e2d5480-42e7-11ea-9904-06d2a874a2c5.png)
![Screen Shot 2020-01-29 at 21 46 55](https://user-images.githubusercontent.com/1935696/73399511-a87f1200-42e7-11ea-8695-28a3dd3abb93.png)
![Screen Shot 2020-01-29 at 21 48 04](https://user-images.githubusercontent.com/1935696/73399539-b339a700-42e7-11ea-848e-f3062bcb2383.png)
![Screen Shot 2020-01-29 at 21 50 36](https://user-images.githubusercontent.com/1935696/73399674-f6941580-42e7-11ea-89aa-65fec686474e.png)
![Screen Shot 2020-01-29 at 22 38 41](https://user-images.githubusercontent.com/1935696/73399853-44108280-42e8-11ea-9d36-c09f6e77e780.png)
![Screen Shot 2020-01-29 at 22 12 34](https://user-images.githubusercontent.com/1935696/73399840-3e1aa180-42e8-11ea-9843-5af2c9fbf55a.png)
![Screen Shot 2020-01-29 at 22 10 43](https://user-images.githubusercontent.com/1935696/73400060-a6698300-42e8-11ea-9be9-c76223d50c6d.png)
![Screen Shot 2020-01-29 at 22 11 20](https://user-images.githubusercontent.com/1935696/73400080-b719f900-42e8-11ea-8bf2-1c9f8c5ff69d.png)
![Screen Shot 2020-01-29 at 21 57 45](https://user-images.githubusercontent.com/1935696/73400213-f8120d80-42e8-11ea-8525-451eff6fcd33.png)
![Screen Shot 2020-01-29 at 21 52 49](https://user-images.githubusercontent.com/1935696/73400254-0e1fce00-42e9-11ea-9e3a-7437d5075d32.png)
![Screen Shot 2020-01-29 at 21 53 54](https://user-images.githubusercontent.com/1935696/73400278-20017100-42e9-11ea-935f-6d56707fd2ad.png)
![Screen Shot 2020-01-29 at 21 55 13](https://user-images.githubusercontent.com/1935696/73400337-3ad3e580-42e9-11ea-8e28-3ce40f14dcf6.png)
![Screen Shot 2020-01-29 at 21 55 57](https://user-images.githubusercontent.com/1935696/73400377-47583e00-42e9-11ea-8288-caa2cb79d604.png)

Mixed (backticks, double quotes, single quotes)

Show Screenshots ![Screen Shot 2020-01-29 at 22 16 06](https://user-images.githubusercontent.com/1935696/73398882-8cc73c00-42e6-11ea-874f-45431d910748.png) ![Screen Shot 2020-01-29 at 22 17 12](https://user-images.githubusercontent.com/1935696/73398907-981a6780-42e6-11ea-828f-0ca0b6d1c14d.png) ![Screen Shot 2020-01-29 at 22 17 20](https://user-images.githubusercontent.com/1935696/73398926-a10b3900-42e6-11ea-998d-1bb76c3567da.png)

Double

Public Service Documentation

TODO

Public Blog Posts

TODO

Public Forums

TODO

Developer Surveys

TODO

Style Guides

Single

Double

mvolkmann commented 4 years ago

I tried to find survey results on this and struck out. The closest I could find is this: https://2ality.com/2012/09/javascript-quotes.html


R. Mark Volkmann Object Computing, Inc.

kachkaev commented 4 years ago

@mvolkmann that's 2012, which is nearly a decade ago. Using references of this quality, we can also 'prove' that no-semicolons is better than semicolons. That's what standard.js does in their docs:

Screenshot 2020-01-29 at 20 27 34

ā†“

šŸ™‚

mvolkmann commented 4 years ago

Maybe Prettier should run its own 2020 poll on this and any other topics being considered.

Shinigami92 commented 4 years ago

Maybe Prettier should run its own 2020 poll on this and any other topics being considered.

image

karlhorky commented 4 years ago

I tried to find survey results on this and struck out.

@mvolkmann All good, we might find something if we keep looking.

If not, there are enough other sources to be overwhelmingly convincing.

lydell commented 4 years ago

Some more documentation sites:

karlhorky commented 4 years ago

@lydell Thanks, added.

kachkaev commented 4 years ago

@Shinigami92 I donā€™t think that referring to the reactions for this issue is the right way to decide if the change should be done in v2.0.

kachkaev commented 4 years ago

In general, I donā€™t think that Prettier should pick defaults just based on whatā€™s currently popular. Itā€™s mission is not to follow some fashion, but rather to decide whatā€™s fashionable. None of the other default value options were changed based on stats. Instead, the guiding principle was a better developer experience, aiming for fewer ugly edge cases. Arrow function parentheses, semicolons and new lines are great examples here.

I donā€™t see how changing default quotes to single would improve DX (except for resulting less ā€˜inkā€™, which is good, but not enough to qualify). Objectively, itā€™s probably the opposite:

If you are a completely new developer who just starts coding, would you really appreciate the ā€˜cleannessā€™ of single quotes? Or would it just give you an extra headache while learning?

If the entire community is doing something for many years, it does not mean that this cannot be challenged or that it does not need to change. This can apply to Prettierā€™s defaults, but can also mean questioning widely seen code styling preferences.

To sum up, Iā€™d suggest postponing the decision and call for a proper research, which would not just involve votes by JS ā€˜veteransā€™ (who are experienced enough to discover the github issue or start their own open-source repo l). This is still quite a small proportion of the entire community of devs.

Shinigami92 commented 4 years ago

jsxSingleQuotes is its own attribute! And it should stick to default false!!!

const greeting = 'Hello, world!';
const element = (
  <h1 className="greeting">
    {greeting}
  </h1>
);
Shinigami92 commented 4 years ago

@Shinigami92 I donā€™t think that referring to the reactions for this issue is the right way to decide if the change should be done in v2.0.

  • The poll is nearly two years old and some opinions could have changed
  • People may be generally pro single quotes, but think that the right time for a change is v3.0 and not v2.0. At the time of writing this comment, the milestone for this issue is v3.0.

Sorry, that was actually a bit trolly

But I think we have now found some statistics and repositories that use single quotes

kachkaev commented 4 years ago

@Shinigami92 imagine Iā€™m a new developer, today is my first lesson on JSX. I feel pretty overwhelmed because even JavaScript alone feels too much for me. I also heard of JSON, but Iā€™m not sure what that is yet.

Could you please explain why you have a mix of quotes in your above example? Which ones are right where? Note that the simplicity and clarity of your answer may affect my whole career. Iā€™m still not sure if Iā€™m ā€˜smart enoughā€™ to do programming.

Shinigami92 commented 4 years ago

If we are targeting v3.0 I would prefer something like

{
  "quoteStyle": {
    "single": ["js", "ts", ...],
    "double": ["html", "pug", "jsx", "tsx", ...]
  }
}