Skip to content

Instantly share code, notes, and snippets.

@jonheslop
Last active July 6, 2021 17:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonheslop/3ed40a9c20a867e39028384eb446095a to your computer and use it in GitHub Desktop.
Save jonheslop/3ed40a9c20a867e39028384eb446095a to your computer and use it in GitHub Desktop.
My workings on how best to integrate XO (ESLint) and Prettier into a project.

Looking into Formatters & Linters

So after a brief discussion on #nodejs it looks like people would like:

  • To use Prettier to format all our code automagically so it all sticks to one consistent style
  • To use XO as a wrapper on ESLint to enforce code-quality rules
  • To ditch Standard because as it turns out people like semicolons.

What I did first

Read the docs on:

I ran $ xo and got over 4000 errors mostly around variable naming and var/let that should have been const

I ran $ xo --fix and it down to 200 which were around filenames not using the right convention and stuff it doesn't want to auto fix.

But because I worry this would make a huge PR that no one wants to look through I thought we can go introduce the rules incrementally with a precommit hook. So…

I looking harder at [Zeit’s]https://github.com/zeit/now-desktop/issues/266) I noticed they run on pre-commit, Prettier actually has docs on this too.

So I installed Husky and now run prettier --write and xo --fix on pre-commit on any files that have been staged for commit.

Instructions for other repos

  1. npm i -D xo prettier eslint-config-prettier husky lint-staged
  2. Add the xo config to your package.json
{
  "xo": {
    "extends": [
      "prettier"
    ],
    "ignore": [
      "node_modules/**/*.*"
    ]
  },
}
  1. Add the job that runs precommit:
{
  "lint-staged": {
    "*.js": [
      "xo --fix",
      "npm run lint",
      "prettier --write",
      "git add"
    ],
    "*.css": [
      "prettier --write",
      "git add"
    ]
  },
}
  1. Add the precommit job to scrips
{
	"scripts":{
		"precommit": "lint-staged"
	}
}

Then when you do a commit this happens

❯ git commit -m "commit message"
husky > npm run -s precommit (node v6.11.1)

 ❯ Running tasks for *.js
   ✖ xo --fix
     → 1 error
     npm run lint
     prettier --write
     git add
 ↓ Running tasks for *.css [skipped]
   → No staged files match *.css
✖ xo --fix found some errors. Please fix them and try committing again.

app/utils/email_tools.js:1:1
✖  1:1  Filename is not in kebab case. Rename it to email-tools.js.  unicorn/filename-case

1 error

husky > pre-commit hook failed (add --no-verify to bypass)

So now you have your code listed and styled automatically on commit but it's nice to not have your commit rejected so I'd recommend install a linter plugin to your editor so that you get inline highlighting.

XO has plugins for most of the text editors out there. If you use an IDE you can use an ESLint plugin instead.

To make it really easy to get going I've created a boilerplate repo with nothing but a package.json and an index.js so that you can just run npm i and get everything. — https://github.com/jonheslop/nodejs-starter

The pros of this are, repos would graudally meet style rather than having to one big change PR which reformatted everything.

@Nooshu
Copy link

Nooshu commented Nov 17, 2017

Nice work @jonheslop. What's the consensus in terms of linting for SCSS / CSS? Initially I was thinking https://stylelint.io/, seems to have a tonne of options and a good community. But it looks like Prettier can deal with this too? @36degrees / @NickColley looping you in too.

@NickColley
Copy link

NickColley commented Nov 20, 2017

We use https://github.com/alphagov/govuk-lint, but we're keen to have a more general approach that's not tied to Ruby that we can use alongside the community.

@NickColley
Copy link

NickColley commented Nov 20, 2017

I'm worried with this spike that there's not an easy path for people to use in their editors etc.

Seems to be lots of moving parts, we could publish our own package that consolidates them like gouvk-lint but we'd have to be ok maintaining that.

Normally I like to commit linting changes separately to a regular change so that reviewers can read what I've changed, is this possible?

@jonheslop
Copy link
Author

jonheslop commented Nov 20, 2017

@Nooshu @NickColley well Prettier can lint scss (and I made it do so in my repo thing)so I don’t see why we wouldn't use it. We could tweak the config if we don’t agree with all of it.

Re editors, I didn't have to do anything to get this working with my install of Sublime Text as I was already running SublimeLinter it just read the rules from Prettier/xo automatically, would just need some guidance that people should install plugins to their editors.

@sebacruz
Copy link

Thanks for share, it saved me a lot of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment