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

Improving maintainability: ESLint and Prettier #193

Merged

Conversation

mrauhu
Copy link
Contributor

@mrauhu mrauhu commented Jan 5, 2022

Hello.

This pull request is part of #188.

Done:

  • Move the Prettier configuration from package.json to .prettierrc.
  • Synchronize .editorconfig with the Prettier standard.
  • Add ESLint with Prettier config.
  • Add the TypeScript plugin to ESlint.
  • Run Prettier before ESLint

@eirslett, @IanVS

Best wishes,
Sergey.

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
.nvmrc Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
.editorconfig Show resolved Hide resolved
@mrauhu mrauhu requested a review from joshwooding January 5, 2022 17:07
@mrauhu mrauhu force-pushed the improving-maintainability/eslint-prettier branch from 12279cd to 22c3b98 Compare January 5, 2022 17:32
.eslintrc.js Outdated Show resolved Hide resolved
.eslintrc.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
Added `--max-warnings=0` argument for returning error with ESLint `only-warn` plugin.
@mrauhu mrauhu mentioned this pull request Jan 5, 2022
3 tasks
@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

I published a small portion of changes, and I got a lot of questions.

Spoilers:

Configuration of ESLint with Prettier plugins is based on the best practices from the Vue and Vite community.

@eirslett
Copy link
Collaborator

eirslett commented Jan 6, 2022

It's looking very good so far!

Code style PRs are always a PITA since everybody has so strong opinions about code style! I couldn't care less if it's 2 or 4 spaces or tabs or whatever, 80 characters, 120 characters, I just really like going with the default/recommended settings on everything. That's what I always do on greenfield projects.

The trade-off in this case is for example the indent size, which will lead to merge conflicts in the other 10 open PRs, and git blame will be a bit less useful unless you ignore whitespace diffs (but only "power users" of the git tools do that, in my experience).

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

The trade-off in this case is for example the indent size, which will lead to merge conflicts in the other 10 open PRs

@eirslett

Problem is, right now there is no code style, because in every file you can find different indent size, formatting, etc.

After this pull request and next #195 is merged, you need run once for each pull request:

yarn lint

Then commit and push changes.

mrauhu added 3 commits January 6, 2022 03:35
* `@types/node` for LTS version;
* `@typescript-eslint/eslint-plugin`;
* `@typescript-eslint/parser`;
* `typescript`.

# Conflicts:
#	package.json
#	yarn.lock
@IanVS
Copy link
Member

IanVS commented Jan 6, 2022

I checked out the branch, ran yarn lint, and got a number of errors and warnings, including errors like this:

/Users/ianvs/code/storybook/storybook-builder-vite/packages/storybook-builder-vite/index.js
  25:1  warning  This line has a length of 124. Maximum allowed is 120  max-len

I believe these are due to long comments and strings which prettier does not break into multiple lines.

There are also a number of parsing errors in .jsx files, like this:

/Users/ianvs/code/storybook/storybook-builder-vite/packages/example-workspaces/packages/app/stories/Button.jsx
  12:9  error  Parsing error: Unexpected token <

Lastly, there are a number of files which prettier wants to format but which are not being formatted by the current yarn lint command. This can cause a problem if maintainers have prettier enabled in their editors, since formatting changes will be made for unrelated lines whenever anyone works in those files.

▶ yarn prettier --check .
Checking formatting...
[warn] packages/example-react/.storybook/main.js
[warn] packages/example-react/package.json
[warn] packages/example-react/stories/button.css
[warn] packages/example-react/stories/Button.jsx
[warn] packages/example-react/stories/Button.stories.jsx
[warn] packages/example-react/stories/EnvironmentVariables.jsx
[warn] packages/example-react/stories/EnvironmentVariables.stories.jsx
[warn] packages/example-react/stories/header.css
[warn] packages/example-react/stories/Header.jsx
[warn] packages/example-react/stories/Header.stories.jsx
[warn] packages/example-react/stories/Introduction.stories.mdx
[warn] packages/example-react/stories/mdx-in-stories/Example.docs.mdx
[warn] packages/example-react/stories/mdx-in-stories/Example.stories.jsx
[warn] packages/example-react/stories/page.css
[warn] packages/example-react/stories/Page.jsx
[warn] packages/example-react/stories/Page.stories.jsx
[warn] packages/example-svelte/.storybook/main.js
[warn] packages/example-svelte/.storybook/preview.js
[warn] packages/example-svelte/package.json
[warn] packages/example-svelte/stories/button.css
[warn] packages/example-svelte/stories/header.css
[warn] packages/example-svelte/stories/Introduction.stories.mdx
[warn] packages/example-svelte/stories/page.css
[warn] packages/example-vue/.storybook/main.js
[warn] packages/example-vue/.storybook/preview.js
[warn] packages/example-vue/package.json
[warn] packages/example-vue/stories/button.css
[warn] packages/example-vue/stories/Button.vue
[warn] packages/example-vue/stories/EnvironmentVariables.vue
[warn] packages/example-vue/stories/header.css
[warn] packages/example-vue/stories/Header.vue
[warn] packages/example-vue/stories/Introduction.stories.mdx
[warn] packages/example-vue/stories/page.css
[warn] packages/example-vue/stories/Page.vue
[warn] packages/example-workspaces/package.json
[warn] packages/example-workspaces/packages/app/stories/button.css
[warn] packages/example-workspaces/packages/app/stories/Button.jsx
[warn] packages/example-workspaces/packages/app/stories/Button.stories.jsx
[warn] packages/example-workspaces/packages/app/stories/EnvironmentVariables.jsx
[warn] packages/example-workspaces/packages/app/stories/EnvironmentVariables.stories.jsx
[warn] packages/example-workspaces/packages/app/stories/header.css
[warn] packages/example-workspaces/packages/app/stories/Header.jsx
[warn] packages/example-workspaces/packages/app/stories/Header.stories.jsx
[warn] packages/example-workspaces/packages/app/stories/Introduction.stories.mdx
[warn] packages/example-workspaces/packages/app/stories/mdx-in-stories/Example.docs.mdx
[warn] packages/example-workspaces/packages/app/stories/mdx-in-stories/Example.stories.jsx
[warn] packages/example-workspaces/packages/app/stories/page.css
[warn] packages/example-workspaces/packages/app/stories/Page.jsx
[warn] packages/example-workspaces/packages/app/stories/Page.stories.jsx
[warn] packages/example-workspaces/packages/catalog/.storybook/main.js
[warn] packages/example-workspaces/packages/catalog/package.json
[warn] packages/storybook-builder-vite/input/iframe.html
[warn] packages/storybook-builder-vite/package.json
[warn] packages/storybook-builder-vite/README.md
[warn] README.md
[warn] Code style issues found in the above file(s). Forgot to run Prettier?

@IanVS
Copy link
Member

IanVS commented Jan 6, 2022

git blame will be a bit less useful unless you ignore whitespace diffs

It's not just whitespace changes which will be made when we run formatting. I appreciate that the formatting is not being done in this PR, because if we do all of the formatting in a separate PR, we can use git-ignore-revs-fie to hide the formatting commit from git blame.

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

I checked out the branch, ran yarn lint, and got a number of errors and warnings ...

Yes, because all formatting is done in #195.

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

This can cause a problem if maintainers have prettier enabled in their editors

@IanVS I'm already working on the CONTRIBUTING.md, so there will be no problems.

@joshwooding
Copy link
Member

joshwooding commented Jan 6, 2022

This can cause a problem if maintainers have prettier enabled in their editors

@IanVS I'm already working on the CONTRIBUTING.md, so there will be no problems.

I'm just curious why we wouldn't want our IDEs to run prettier? It's an easy workflow which almost always ensures formatting is kept consistent.

package.json Outdated Show resolved Hide resolved
@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

I'm just curious why we wouldn't want our IDEs to run prettier? It's an easy workflow which almost always ensures formatting is kept consistent.

It's simple:

Linters usually contain not only code quality rules, but also stylistic rules. Most stylistic rules are unnecessary when using Prettier, but worse – they might conflict with Prettier!
...
Luckily it’s easy to turn off rules that conflict or are unnecessary with Prettier, by using these pre-made configs:

eslint-config-prettier

https://prettier.io/docs/en/integrating-with-linters.html

Easy workflow which almost always ensures formatting is kept consistent:

image

@joshwooding
Copy link
Member

joshwooding commented Jan 6, 2022

I'm just curious why we wouldn't want our IDEs to run prettier? It's an easy workflow which almost always ensures formatting is kept consistent.

It's simple:

Linters usually contain not only code quality rules, but also stylistic rules. Most stylistic rules are unnecessary when using Prettier, but worse – they might conflict with Prettier!

...

Luckily it’s easy to turn off rules that conflict or are unnecessary with Prettier, by using these pre-made configs:

eslint-config-prettier

https://prettier.io/docs/en/integrating-with-linters.html

Easy workflow which almost always ensures formatting is kept consistent:

image

If we want to start quoting the integrating with linters page we can't skip over:

image

Which mentions that eslint-plugin-prettier is not generally recommended with a list of downsides :)

I'm still not convinced we shouldn't just make the traditional separation of Ealing handling code quality and prettier handle styling.

@IanVS
Copy link
Member

IanVS commented Jan 6, 2022

@mrauhu Yes, you're using the config that disables eslint style rules, which is great. But eslint does not run on every file that prettier can run on. I think that's the main challenge here. I'm still unclear on why you prefer to run prettier via eslint. I haven't heard any reasons other than mentioning it's done in vue and vite, but maybe they have different needs.

The downsides of this approach are:

  • Formatting is only applied to files that eslint is run on, not .css, .md, etc.
  • It's an extra eslint plugin that we need to use.
  • Prettier itself discourages this approach by saying:

These plugins were especially useful when Prettier was new. By running Prettier inside your linters, you didn’t have to set up any new infrastructure and you could re-use your editor integrations for the linters. But these days you can run prettier --check . and most editors have Prettier support.

The downsides of those plugins are:

  • You end up with a lot of red squiggly lines in your editor, which gets annoying. Prettier is supposed to make you forget about formatting – and not be in your face about it!
  • They are slower than running Prettier directly.
  • They’re yet one layer of indirection where things may break.

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

I made all suggested changes.

Please, merge it.

@IanVS
Copy link
Member

IanVS commented Jan 6, 2022

Please, merge it.

I appreciate all the hard work you're putting into this, @mrauhu, and I hope that I'm not coming off as too demanding or nit-picky. But I would like to be sure that we are not causing trouble for ourselves down the line. @joshwooding and I both work in this repo fairly often, and we have experience working in other projects as well. In fact, I was one of the early maintainers of ESLint back around its 1.0 timeframe. So I think we are not speaking out of ignorance.

I'm going to leave the final say here to @eirslett, since this is his repo. But here are my main concerns:

  • I think PRs should stand alone and contain a complete changeset. As it is, if we merge this PR without Improving maintainability: TypeScript #195, we will have a broken eslint config.
    • What I would like to see: This PR should contain all the necessary config such that a small subsequent PR should only need to fix lint warnings and formatting (not config). The commit SHA of that subsequent PR can then be added to a file and we can use --ignore-revs-file to exclude those changes from git blame.
  • I have a strong preference for running prettier apart from eslint, for all the reasons I and @joshwooding mentioned above. I haven't heard any good reasons here for tying prettier into eslint.
    • What I would like to see: eslint-plugin-prettier should be removed, scripts should be added for prettier --write and prettier --check.
  • The eslint config should be the same no matter the NODE_ENV. If you don't want to see certain errors in your editor, you should configure your editor thusly.
    • What I would like to see: Remove the rules for 'no-console' and 'no-debugger' from the eslint config

There are other decisions here which I don't agree with, but I have expressed them and don't care enough about them to press those points. But without the changes I've listed above, I won't be approving this PR, but I won't block it either, since like I said, I defer to @eirslett.

Thanks again for all your work here, and your efforts to improve the codebase.

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 6, 2022

@IanVS thank you for clarification.

@mrauhu mrauhu mentioned this pull request Jan 6, 2022
3 tasks
@mrauhu mrauhu force-pushed the improving-maintainability/eslint-prettier branch from a8a5355 to fdd592e Compare January 6, 2022 20:25
Copy link
Member

@IanVS IanVS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrauhu thank you for understanding and for making the changes I requested. I think this is in a great spot now.

@eirslett
Copy link
Collaborator

eirslett commented Jan 7, 2022

if we merge this PR without Improving maintainability: TypeScript #195, we will have a broken eslint config.

That's fine, we can live with a somewhat broken eslint config as long as it is temporary (for a few hours or days).

Are there any blockers to merging this PR now, as it is? It looks complete to me.

The only thing is that there are 28 commits (as of now) and many of these commits are simple reverts of one another, so the history is confusing. @mrauhu could you please squash all the changes into 1 commit, with a good commit message? Then we'll get this stuff merged!

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 7, 2022

@eirslett

if we merge this PR without Improving maintainability: TypeScript #195, we will have a broken eslint config.

That's fine, we can live with a somewhat broken eslint config as long as it is temporary (for a few hours or days).

Right now there is nothing broken, I moved the ESLint config changes from #195 to #193.

Aslo, I reorder commits between three pull requests:

  1. Improving maintainability: ESLint and Prettier #193 — this pull request;
  2. Improving maintainability: run lint #196 — on top of 193, ran lint and fixed warnings in the package directory;
  3. Improving maintainability: TypeScript #195 — on top of 196, converted codebase from JavaScript to TypeScript.

I'm working on typization for #195 draft and soon convert it to pull request.

... could you please squash all the changes into 1 commit, with a good commit message?

I can, but these pull requests are on top of each other, can you merge them one by one without squashing?

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 7, 2022

@mrauhu
Copy link
Contributor Author

mrauhu commented Jan 7, 2022

The only thing is that there are 28 commits (as of now) and many of these commits are simple reverts of one another, so the history is confusing.

I think history of commits provide a better understanding for a developer what is changed and why.

It works great with an IDE, because you can view commit history for every file and line.

@eirslett eirslett merged commit 090ca75 into storybookjs:main Jan 7, 2022
@eirslett
Copy link
Collaborator

eirslett commented Jan 7, 2022

Ok, there we go! Thanks a lot for the work! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants