Problem/Motivation
Core JavaScript has many different stylistic formats. Each patch can be formatted differently depending on implementer or reviewer.
Proposed resolution
Use Prettier for formatting our JavaScript. This should only apply to our .es6.js files.
Remaining tasks
Accept these changes, then create another patch with all the changes from Prettier applied.
| Comment | File | Size | Author |
|---|---|---|---|
| #35 | prettier_eslint-2978964-35.patch | 6.04 KB | corbacho |
| #31 | 2978964-31.patch | 5.94 KB | ApacheEx |
Comments
Comment #2
GrandmaGlassesRopeMan- add prettier
- update eslint and dependencies.
- updates .eslintrc.json with correct extends key
Comment #3
dawehnerNo c++ reference anymore, how sad.
If I understood you correctly, we need to update eslint to get the prettier eslint config working. Seems reasonable given it is a dev configuration.
For the follow up, how about you creating the ticket, tag it as novice, and review it by yourself. I'm quite sure you get quicker endresults :)
Comment #4
GrandmaGlassesRopeManI've just created the followup.
Comment #5
alexpottSomething is blocking us :( #2981652-4: [Aug. 9 2018] Format core JavaScript using recently add Prettier - running prettier shouldn't result in linting issues.
Comment #6
ApacheEx commentedHere is updated patch.
What I did is just add the prettier without upgrading other packages (at least it's not required for getting prettier work).
p.s. I think upgrading eslint packages should be done in follow-up, because it introduces extra lint issues.
Comment #7
ApacheEx commentedHere is follow-up for updating packages: #2982345: Update eslint packages to most recent major versions
Comment #8
ApacheEx commentedHere is one big patch which includes patches from followups:
- #2982345: Update eslint packages to most recent major versions
- #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier
Comment #9
ApacheEx commentedComment #10
GrandmaGlassesRopeManRe-upload of #2. After talking with @lauriii, we believe that it makes the most sense to take the approach of using this smaller patch without the prettier code-style changes and actually do that on commit. Additionally a followup patch to fix the minor issues introduced by Prettier.
---
- add prettier
- update eslint and dependencies.
- updates .eslintrc.json with correct extends key
Comment #11
GrandmaGlassesRopeMan- fix the small comment placement issue.
Comment #13
ApacheEx commentedbut why not to take the patch from #6 which is just ~6KB that introduces prettier without any issues?
updating deps - we have followup.
reformatting by prettier - we have followup.
Up
but if all child issues are RTBC - then the big patch (#8) should be taken (it's just merge of all patches with no conflicts)
Comment #14
GrandmaGlassesRopeMan- rerolled since #2982345: Update eslint packages to most recent major versions was merged.
Comment #15
ApacheEx commented#14 looks clean and nice (flex)
Comment #17
droplet commentedI love Prettier and used it in each of my own projects. :p
I also considered it before push ESLint to Core (one step to the final). One of the problems I've seen, few of other huge projects outside Drupal also experienced it but they restored to ESLint at final (or dropped ESLint). Prettier is not as stable as it seems sometimes (at version update or whatever it can be wired, you will lose control and have to adapt their way), it will change pretty a lot of code. I'm not sure if Drupal would tolerate such an amount of new changes and to go back and fore.
A simple example, sometimes you change a LETTER but Prettier will rewrite a few lines.
Prettier isn't 100% always print IDENTICAL code. Reviewers can't distinguish Preitter or your own changes from a patch file.
Comment #18
GrandmaGlassesRopeMan@droplet 👋
I had run the prettier transition on the es6 files and then rebuilt everything and didn't observe any changes. It's probably a good point though to check and see if this does happen. Mostly with this change I'm trying to get out of the business of reviewing for code-style on each JS patch.
Comment #19
samuel.mortensonTwo questions:
Comment #20
GrandmaGlassesRopeMan@samuel.mortenson - Thanks. I think #19.1 should be addressed in a change record. In practice, you should run
lint:core-js-passingandprettierbefore making a patch.Comment #21
samuel.mortensonAdding tag.
Comment #22
GrandmaGlassesRopeManComment #23
samuel.mortensonThis looks good to me - the change record mentions that the branch this is introduced in is "8.6.x", which relies on a backport from 8.7.x, but from talking with framework managers it sounds like there's a good chance of this getting into 8.6 before beta.
Comment #24
droplet commentedshould be double quotes for Windows.
It's better to combine 2 commands (or as the new command). Prettier run first and then linting tools. lint-staged also a good tool and time-saver.
Comment #25
alexpottFor #24.
Comment #26
ApacheEx commentedHere is
lint-stagedsolution which I really like.But, let me know if we should go with it in this task, otherwise I will provide a patch to fix double quotes only.
Props:
- less chance to forget something to run
- prettier will process only staged files
- you can't commit if
lint:core-js-passingfails- and many more...
Cons (but not sure):
- you need to commit changes (via
git commit) to run lint-staged commands automatically (e.g prettier)- you can also add changes (via
git add) and then run lint-staged manually viayarn run precommitComment #27
xjmIf this issue is completed in time to be backported to 8.6.x as per the alpha phase allowed changes, I would recommend backporting it and then scheduling #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier for during the beta phase.
Comment #28
GrandmaGlassesRopeManI think that the changes in #26 are out of scope right now. Considering that the patch workflow that is currently used makes this difficult to enforce, git hooks anyone? I'd suggest just switching to double quotes for windows envs and getting this in. At a later date if we want to address a more automated approach that's probably best.
Comment #29
ApacheEx commentedas I thought :) Here is updated patch.
Comment #30
ApacheEx commentedRerolled with 8.7.x
I've also added this rule
"prettier/prettier": "off"to.eslintrc.passing.jsonThis will allow not to have lint issues from the prettier when
yarn run lint:core-js-passingit should be removed in #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier
Comment #31
ApacheEx commentedOh, forgot to upload the patch :)
Comment #32
tedbowI applied #31
I then followed the instructions in the change record: https://www.drupal.org/node/2986680
I got a bunch errors when I ran
yarn run lint:core-js-passingMost of them where like this
62:20 error '?' should be placed at the end of the line operator-linebreakThis is because we added the line break rule in #2983365: JS codestyle: operator-linebreak.
But when run prettier it will make lines depending on it's own internal rules so we can't a rule on this and expect it to pass. So we should remove this rule.
The others where
In this case it we are concatenating lines which prettier changes to all on the single so it becomes a useless concat.
This could be fixed in #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier or on commit if the committer is going to run prettier.
The offending code is
We are using a comment here to disable linting a particular line.
When prettier is run the code will look something like
So here the disabling of eslint no longer works because it was pushed down 1 line.
Again this can be fixed in #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier or on commit if the committer is going to run prettier.
We would simply need to move the comment down 1 line.
This is the problem @alexpott referred to #5
So I think the change is to remove the
no-useless-concatrule which won't make sense prettier.Comment #33
ApacheEx commentedThanks for review, but it seems you forgot to run
yarn installin the ./core folder.here is my result after applying patch and running
yarn install && yarn run lint:core-js-passing:https://www.dropbox.com/s/20hq3wal1iwzuch/Screenshot%202018-08-02%2013.2...
Comment #34
tedbow@ApacheEx thanks for all your work on this issue!
I did the
yarn installLooking at your screen shot you didn't actually run `
yarn run prettier` from the change record instructions: https://www.drupal.org/node/2986680The patch itself doesn't change any *.es6.js files so it will not have any effect on the output of
yarn run lint:core-js-passing.To commit this issue we have to make sure that the changes that `
yarn run prettier` will make to the files are compatible with our eslint rules so that we don't always get errors withyarn run lint:core-js-passingThis was @alexpott's point in #5
I copied in the wrong rule here. I meant
operator-linebreakThe main point I was making in #32 is that
operator-linebreakis not compatible with the new prettier changes.yarn run prettierwill always produce code formatting that will not pass this rule.Turning this rule off in
.eslintrc.passing.jsonin reduces the errors afteryarn run prettieris run from 99 to 3. #32 explains why we have these existing errors.So we have to turn off
operator-linebreakin this issue.BTW I would do this change myself but I would rather not make any changes to this issue so I am free to RTBC this issue!(@ApacheEx and @drpal have already worked this issue a bunch so probably can't RTBC)
Comment #35
corbacho commentedI confirm comments from @tedbow
I got 99 problems https://gist.github.com/dcorb/bdc8b04a649286d316b4e0723cecec21
but I think I got a solution
Instead of totally removing this line:
"operator-linebreak": ["error", "after"]I changed it to
"operator-linebreak": ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" } }](It seems is quite common to make an exception for ? and :, since the rule is provided in the documentation of eslint rule https://eslint.org/docs/rules/operator-linebreak )
Also I upgraded Prettier to 1.4.0 (the other 2 packages were latest)
There are still 3 eslint problems left, like Ted reported too: https://monosnap.com/file/F5PPdy7xgaWmrtc61DxTEVNdI5fX6w
But agree with @tedbow to apply necessary changes along with the issue that prettifies the files 🦄
Comment #36
lauriiiIt seems like prettier does some pretty interesting changes for our theme functions. Posting a patch containing all changes prettier does so we can confirm that there's no other instances where something would go wrong. In the instance that was failing due to eslint prettier did some changes that probably would have led into breaks even in the functionality. We should report that in the upstream repository.
I also applied a patch that fixes the eslint failures which can be applied after running prettier.
Comment #38
ApacheEx commentedAbout failure test, see https://www.drupal.org/project/drupal/issues/2981652#comment-12664645 for more details
So, what you need to do is revert
./core/modules/locale/tests/locale_test.es6.jsand add it to.prettierignoreComment #39
corbacho commentedGreat ApacheEx. So, that means we are good to go with #35 patch and then we can move into #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier, that is about "applying prettier". Patch from laurii in #36 can be moved there too. Nice fixes
@tedbow would you do the honours to give the final RTBC :)
Comment #40
tedbow@drpal & @ApacheEx thanks for all the work on this!
RTBC!🎉For the patch in #35
Comment #42
lauriiiComment #45
lauriiiCommitted #35 979164b and pushed to 8.7.x. Cherry picked 9a9a04b and pushed to 8.6.x. Thanks!
Comment #46
xjmWe include a brief list of coding standards changes in our release notes, since they affect core, contrib, and custom projects that use our standards.
Comment #48
andrewmacpherson commentedThe change record https://www.drupal.org/node/2986680 is a bit sparse. I don't understand what I'm supposed to be doing with it.
The change record doesn't say. The prettier website isn't much help either.It's something to do with code formatting.Do the commands have to be run in a particular directory?Run theyarncommands incore/directory. Runyarn installfirst.Update: answered some of my own questions. We could do with a handbook page summarizing the various JS dev tools we now have, The details are scattered around various change records. I'm still sketchy on what prettier actually does though?
Comment #49
.jch commentedIs this lib referenced in core not installed when a d8 site is built with composer?
ESLint library loaded from: ...\web\themes\custom\motif\node_modules\eslint\lib\api.js
Cannot find module 'eslint-plugin-prettier' Referenced from: ...\web\core\.eslintrc.json Referenced from: ...\web\.eslintrc.json
ESLint library loaded from: C:\Users\James\AppData\Roaming\npm\node_modules\eslint\lib\api.js
Comment #50
droplet commented#48,
Actually, I'm against this commit while we able to improve the whole experience with just a few mins or an hour max.
Basically, we should look into this #24. And the #2879072: Improve ES6 patching experience, it can output a friendly error message instead docs and it will be always up-to-date.
#49,
Run `yarn install` in CORE dir first.