Open lipis opened 6 years ago
Every JavaScript survey I've ever seen shows that most developers prefer single quotes for literal strings.
Most of popular languages (go, C#, java, C, C++, rust) strictly uses double quotes, why should there be an exception? š¤ (python uses both)
Language | Double quotes | Single quotes |
---|---|---|
go | Yes | |
Python | Yes | Yes |
C# | Yes | |
java | Yes | |
C | Yes | |
C++ | Yes | |
rust | Yes | |
Lua | Yes | Yes |
Name | Language | Quotes |
---|---|---|
Black | Python | Double quotes (discussion) |
Language | Double quotes | Single quotes |
---|---|---|
CSS | Yes | Yes |
HTML | Yes | Yes |
Language | Double quotes | Single quotes |
---|---|---|
JSON | Yes | |
YAML | Yes | Yes |
GraphQL | Yes | |
XML | Yes |
Guidelines | Quotes |
---|---|
Airbnb | Single quotes |
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 |
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 |
Because single quotes are less noisy.
At least 50% less noisy :)
This is from the Google JavaScript Style Guide
Use single quotes
Ordinary string literals are delimited with single quotes ('), rather than double quotes (").
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.
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:
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.
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.
There's also the "avoidEscape" setting in eslint, which is pretty useful for readability, be it with single or double quotes.
Thatās already what we do @brocoli and I agree that itās helpful.
The new Python formatter "black" uses double quotes: https://github.com/ambv/black/#strings. Discussion: https://github.com/ambv/black/issues/118
@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.
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 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?
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"prettier": "1.14.3",
"tslint-config-prettier": "^1.18.0"
@dandonahoe Could you open a new issue for your problem? Ideally with a reproduction on the playground (https://prettier.io)
@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.
@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
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.
@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.
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.
@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.
I guess this is not a candidate for inclusion in 2.0 (#6888), right?
@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.
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:
š @j-f1 š @lipis
Maybe more of the Prettier team can chime in on changing the default on this one. cc @suchipi @thorn0 @evilebottnawi @lydell @prettier/core
:+1: on single by default
Iād prefer if all programming languages converged on doubleāquotes, including JavaScript.
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.
I've opened a PR at #7466, just in case this will get the approvals that it needs.
git diff
.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.
- 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
How would you motivate the change in the 2.0 blog post?
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.
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.
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 |
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.
- moment: mixed
TODO
TODO
TODO
TODO
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.
@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:
ā
š
Maybe Prettier should run its own 2020 poll on this and any other topics being considered.
Maybe Prettier should run its own 2020 poll on this and any other topics being considered.
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 Thanks, added.
@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.
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.
jsxSingleQuotes
is its own attribute! And it should stick to default false
!!!
const greeting = 'Hello, world!';
const element = (
<h1 className="greeting">
{greeting}
</h1>
);
@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
@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.
If we are targeting v3.0 I would prefer something like
{
"quoteStyle": {
"single": ["js", "ts", ...],
"double": ["html", "pug", "jsx", "tsx", ...]
}
}
From https://github.com/prettier/prettier/issues/3503#issuecomment-359863688