Problem/Motivation
Currently, we are using a mixture of single quotes and double quotes in our JavaScript, and I can't see why. When I turn on eslint's quotes rule set to single quotes, it only returns 215 errors; pretty small compared to turning on the eslint rule and setting it to double quotes: 4013 errors.
Our PHP standards for quotes are pretty clear: https://www.drupal.org/coding-standards#quotes
single quote strings should be used by default. Their use is recommended except in two cases:
- Deliberate in-line variable interpolation, e.g. "<h2>$header</h2>".
- Translated strings where one can avoid escaping single quotes by enclosing the string in double quotes. One such string would be "He's a good person." It would be 'He\'s a good person.' with single quotes. Such escaping may not be handled properly by .pot file generators for text translation, and it's also somewhat awkward to read.
In JavaScript, there is no such thing as #1. But we do have Drupal.t() which can have strings with apostrophes.
So I think we should adopt the same rules for quotes in JavaScript as we already have for PHP.
Fortunately, eslint has a rule that will allow us to: always use single quotes, except when we use double quotes to avoid escaping a single quote or apostrophe in the string.
Proposed resolution
Let's update our .eslintrc file to add:
"quotes": [2, "single", "avoid-escape"]
Remaining tasks
- Add the new eslint rule.
- Fix the 211 linting errors by switching string from double quotes to single quotes.
- Update Drupal's JavaScript docs to mention the new recommendation. https://www.drupal.org/node/172169
Quotes
Single quote strings should be used by default. Their use is recommended except in the following case:
Translated strings where one can avoid escaping single quotes by enclosing the string in double quotes. One such string would be
Drupal.t("She's a good person.")It would beDrupal.t('She\'s a good person.')with single quotes. Such escaping may not be handled properly by .pot file generators for text translation, and it's also somewhat awkward to read.
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | core-js-eslint-quotes-2548195-22.patch | 68.61 KB | nod_ |
| #21 | only_use_single_quotes-2548195-21.patch | 68.17 KB | sriharsha.uppuluri |
| #12 | core-js-eslint-quotes-2548195-12.patch | 68.6 KB | madhavvyas |
| #11 | core-js-eslint-quotes-2548195-10.patch | 68.6 KB | madhavvyas |
| #8 | core-js-eslint-quotes-2548195-8.patch | 68.22 KB | nod_ |
Comments
Comment #2
johnalbinHere's a patch.
Comment #3
nod_Didn't have the courage to fill this issue but it's very much +1 from me.
Only thing is the change in active links. I could swear I needed the single quotes because querySelector didn't like some values of queryString but I can't remember I dislike double quotes so if I used them like this there is probably a reason.
The rest is fine.
Comment #4
johnalbinDo you want me to reverse that hunk? And add an eslint exclude comment?
I couldn't find anything in the docs for querySelectorAll() that hints that quotes make any difference. Neither https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll nor http://www.w3.org/TR/selectors-api/
Comment #5
nod_maybe it was with
<front>. Not sure, I'll try to remember, if I take too long let's go with it.Comment #6
nod_Ah! remembered. It's because queryString is a JSON value with
"all over the place. Faster to do it that way than escape everything. So we have to exclude that line from eslint check.Comment #7
johnalbinnew patch!
Comment #8
nod_Rerolled since the sec critical added a bunch of lines in ajax.js other than that, all good!
Comment #9
nod_When testing, know that eslint released a new version 1.3.0, it's bugged so use eslint 1.2.0 or wait for https://github.com/eslint/eslint/issues/3570 to be fixed.
Comment #10
webchickThis no longer applies after JSDoc-ageddeon.
This could also use a beta eval, as it seems like quite a disruptive change for little gain?
Comment #11
madhavvyas commentedPatch re-rolled comment #8
Comment #12
madhavvyas commentedUpdated patch file name, Not sure why Add test link not coming beside re-rolled patch.
Comment #13
nod_Comment #14
johnalbinI hadn't realized madhavvyas had created an updated patch, so I independently re-rolled my patch to the latest 8.0.x. When running with eslint 1.3.1, I was seeing new JS code fail the linters, so I fixed the new double-quoted JS and then created a new patch.
Then I noticed madhavvyas' patch. And our patches are identical! So his patch in #12 looks good to go.
Comment #15
nod_Agreed.
Comment #16
alexpottThis is exactly the sort of coding standards change that will be good to make during RC rather than just before it. Since it is disruptive to patches but undisruptive to codebase. Let's do it then.
Comment #17
droplet commentedIn case you don't know, it can perform autofix with this command:
eslint --fix core\**\*.jsComment #18
xjmThe tag for #16 is "rc target".
Comment #19
alexpottNeeds a reroll
Comment #20
naveenvalechaAdding tag
Comment #21
sriharsha.uppuluri commentedIts re-rolled.
Comment #22
nod_Missing one
Updated patch to fix this issue (another case of
"use strict";).Tried it, no js error or anything, everything looks good.
Comment #23
alexpottAs agreed this can be committed during RC.
Committed b0e82e5 and pushed to 8.0.x. Thanks!
Comment #25
johnalbinThanks, Alex!
And Sri, Théodore, and madhavvyas, too!
:-)