Closed pheara closed 6 years ago
The three big ones seem to be:
https://www.npmjs.com/package/prettier
https://jlongster.com/A-Prettier-Formatter
a year old, now at 1.3M downloads (2018-05-15)
has a VSCode plugin
styles JavaScript, Flow, TypeScript, CSS, SCSS, Less, JSX, Vue, GraphQL, JSON, Markdown
due to it's age, some features are still under development (e.g. comment-formatting)
https://www.npmjs.com/package/standard
least widely used at 0.13M downloads
there's a VSCode plugin
styles javascript
https://github.com/airbnb/javascript
seems to be a bit more modular: there's eslint-config-airbnb, eslint-config-airbnb-base, airbnb-js-shims, babel-preset-airbnb, and quite a handful more on npm.
eslint-config-airbnb-base has 0.9M downloads, eslint-config-airbnb 0.7M.
there's no plugin for VSCode
styles javascript, jsx(?), css, ruby
There's also the google-js-styleguide and idiomatic-js, but there's no editor-plugins or automatisation scripts that we could find with a reasonable web-search.
We'll use Prettier as it's got support for all languages we use, a plugin for the editor most of use, should have a straightforwared setup and seems to be the style that enjoys the widest usage of the researched ones.
Prettier gives us a handful of options with rather sane defaults. However, we'd argue for activating trailing commas as it makes for better commits/git-blame. We might use 4 space intendation instead of the 2 that are the default.
The prettier-documentation recommends using husky (docs) to automatically setup git-hooks when running npm install
.
The documentation mentions 5 options for the hook:
pip
and thus a full python setup..git/hooks/pre-commit
: the fallback if nothing else worksAs such, the first two (husky + linst-staged or husky + pretty-quick) are the most interesting options for us. The first is somewhat preferable, as we probably want to use linting in the future. There's a pre-commit.js (not to be confused with the python tool above) that we'll need for the npm task that installs the hook.
Issue: "Currently there is a limitation where if you stage specific lines this approach will stage the whole file after formatting." A slightly more complex task to fix this problem: https://github.com/okonet/lint-staged/issues/62#issuecomment-286830310. A somwewhat simpler solution: https://github.com/okonet/lint-staged/issues/62#issuecomment-381432450
Even simpler: switch to "no. 4: husky + precise-commits" as a solution.
On the other hand, we only need formatting for the entire file anyway.
Note, that when the husky major-version upgrade hits npm (and if we choose to follow), we'll need to slightly change our config, as described here: https://github.com/typicode/husky#upgrading-from-014
If you want to already format on save, for vscode there's the "Prettier - Code Formatter" plugin. Additionally you want to add "editor.formatOnSave": true
to your workspace settings (Ctrl+,
then go to tab labelled "workspace settings"). There's also a build-hook that triggers when building and a commit-hook that makes sure everything is formatted and linted when committing.
ESLint is the standard, has integration with prettier and vscode. We used that in (almost) default settings and also included it in the pre-commit hook.
Issue: we have mixed tabs, 2-space, 4-space intendation, as well as a lot of other formatting and linting-issues.
Plan: