Syntax Video
Lint Like a Senior Developer w/ eslint + husky + lint staged + github actions
- 0:00 intro
- 0:37 vite project setup
- 1:26 antfu/eslint-config
- 3:06 eslint stylistic
- 3:49 vs code editor settings
- 4:56 customize eslint settings
- 5:32 lint:fix task
- 6:52 commit hooks with husky
- 8:45 lint-staged setup
- 11:16 github actions lint setup
- 14:17 run github actions locally with act
- 16:37 bypassing commit hooks with --no-verify
- 18:00 squashing commits
- 18:59 push vs code settings to github
- 19:52 thanks
intro
linting our code bases is the start of really good code hygiene and in this video we're going to talk about linting from end to end so not only will we have eslint set up inside of our editor and our codebase we'll also set it up so that our code will lint on Save and we'll lint before we commit with some pre-commit hooks we'll even take it a step further so that whenever we make a pool request to our repo the linter is run there just to make sure the code is in a good State before we merge it in in this video I'm going to show you how to set all of the that up so let's get started my name is CJ welcome to [Music] syntax so for this example we're just going to start up a vit and react app if you head to the V site they have a command we can run I'm going to use pnpm and we'll set up a new vit app I'm going to call this my V react lint example and we'll choose react we'll also choose typescript now let's head into that directory and then install dependencies now if we open this up with VSS code you
vite project setup
can see we have our default feat and react project here and it actually comes generated with an eslint config and this has all of the defaults and recommended rules that you would want for working with react but I like to take this one step further so there are various lint rules that I like to enable and also some plugins I like to add and some stylistic rules that I like to enable as well now you could set all of this up from scratch and your config file would start to get very lengthy but I actually like to use a tool from Ant Fu called es lint config and this is essentially a preconfigured eslint config that has a bunch of plugins already enabled so you can just change the settings if you'd like to use them I use this for every single one of my projects so much so that I actually have a default ant Fu config with all of my preferred settings and plug-in rules so this is what I'm going to enable here I'll first add it as a Dev dependency and then I'm going to grab my preconfigured settings here and then update my eslint config now I'm
antfu/eslint-config
going to rename this MJS so that it behaves as a module and I'm actually going to replace the entire existing eslint config and because this is a react app an Fu's eslint config has a react option and we can set that to true now if we open up the eslint output Channel you should see that ant fu is trying to install various plugins and it's not going to be able to do that inside of vs code so what we'll do is run the linter and that should prompt us to install those plugins and if we look in our package.json it comes preconfigured with this lint task so from the command line if I do pnpm lint should prompt me to install these plugins awesome and now with those new ant Fu settings we actually run into a lot of linter errors and so if we look at our package.json we now have all of the plugins that that config requires including these react plugins now another thing I like to enable is lint on Save and if you head to ant Fu's repo he actually has some settings that we can use if you check out vs code support
these are settings we can add to our settings. Json file that have some sensible defaults so it enables lint on save so anytime I save my file it automatically applies the linter and this config is actually using eslint stylistic so these are essentially formatting rules that give you a similar experience to let's say prettier but it's actually just built in directly to eslint and in the FAQs of his repo here he has this link to a blog post called why I don't use prettier and he kind of talks about the limitations of prettier and one of the main things is he doesn't like the automatic line wrapping cuz it creates a lot of noise in pull requests so esit stylistic can still format your code in that way but it has some more sensible defaults so it's not changing a bunch of code based on print width and the other thing is typically if you are using prettier you have to find some kind of plugin that make eslint and prettier work together so I prefer just to use eslint with eslint stylistic like anfu has set up now to get all of these settings we're just going to copy this settings file here and inside of VSS code we'll create a folder called vs
eslint stylistic
vs code editor settings
code and create a settings. Json inside of there and you'll notice that this disables prettier so even if you have the prettier extension installed inside of this project we don't want to use it we're only going to be using ES lint and like I said this does lint on Save now another thing that this enables is some sensible defaults because it won't show linter errors for some of those stylistic rules so if you're missing a semicolon or your white spaces off typically you don't want to see a red squiggly under those things especially because they're going to be fixed on save so this disables all of that so you don't have too much noise inside of your edit editor we also enable the linter for all of the supported file types and these settings are great so if I just do a Reload of vs code and let's say we head into our main. TSX you'll notice we do have a linter rule because there's some import sorting rules built into his config but if we save it automatically adds all of our stylistic settings as well so it added semicolons changed to double quotes and you can see it sorted
the Imports here so in Fu's config is really great I highly recommend you check out his document and go modify his config to your heart's content um these are my preferred settings but you might have some other preferred editor settings but his config is flexible enough that it can be enabled or disabled for all of your preferences now looking at some of the rules that I like to enable I actually add this setting here for sort Imports because if you're using typescript with Alias Imports that setting is needed so that way they're sorted in the right order and then I also like to restrict file name casing so I want to make make sure that every file in my code base is using Kebab case so next let's actually just lint our whole project because it was generating using different style settings we should apply our current style settings to everything so in our package.json let's add a lint fix command that also fixes all the autof fixable problems you'll notice that because I enabled formatters with antus config it also is trying to format this
customize eslint settings
lint:fix task
Json file so the moment I save it it sorts the properties here in the Json file according to that rule now let's run lint fix so it'll fix all of the autof fixable things in our codebase so it fixed most things but you can see because I have that file name casing rule it's erroring out for our app files because they're not using Kebab case so I actually need to rename these to be lowercased and yeah the import was updated I just need to reload vs code to make sure that it can detect the casing name change and with that we should be good it looks like we have some warnings yeah but these warnings are good because I mean it is just the default react app but I'm going to go ahead and add this re here on any of the links to get rid of that warning and then we're also missing the type attribute on our button component yeah so we can just give this a type of button so if we run our linter again we should be good to go so that's it for the first step of getting linting into end configure your linter enable all of your rules and then also make sure you have VSS code integration next
commit hooks with husky
up we're going to set up pre-commit hooks so right now this isn't a git repo but if we init it and then stage all of our files one thing I like to do is run the linter before allowing a commit to happen now there's some controversy around whether or not you should enable this it can be disabled for one-off commits and I really like it because I don't want to have to wait for my CI server to tell me that my code isn't passing linting so I wanted to catch that before I actually push up to GitHub so let's enable that now for this we're going to use a tool called Husky and it essentially just configures our pre-commit hooks for us we'll use the command here to install it with pnpm and then we can run init and it will generate the Husky folder for us so if we take a look at our editor we now have this husky folder and there's this file in here called pre-commit and this can list any command that you want to run before a commit happens so by default it's running this test command we don't have any tests in this codebase but we actually want to run our linter before
allowing us to commit so if we do pnpm lint there it'll run that every time before we commit so let me actually just introduce a linter error like I'll remove the semicolon here um it's actually going to break because I have uh Flint on save so I'm going to disable lint on Save and make sure that that semicolon doesn't exist we'll stage everything but now that we have this pre-commit Hook when I attempt to commit it's going to run the linter first so if we just try to commit this like so it runs the linter first and if the linter fails it does not allow the commit to happen so this is my workflow so that's great now another thing we would like to do is only run the linter on files that have actually changed so let me go ahead and get this committed I'll push it up to GitHub and then we can work on making sure that the linter only runs for the changed files instead of running against the whole codebase so our codes on GitHub now I'm going to go ahead and check out a branch and we're going to
lint-staged setup
call this lint staged setup and the tool we're going to be using is called lint staged so right now every time before we commit it's going to run lint and that runs across our entire code base so you can imagine as this code base gets larger this lint command is going to take longer and longer to run what lint stage does is it only looks at the files that have been staged for commit and it will only run the linter for those specific stage files you can also use it to only run test for your stage files and this makes it so that if you only changed one file you don't have to wait for the whole codebase to lint you just wait for that one changed file to lint so we'll install this as a Dev dependency we'll do it with pnpm and if you check their docs they have a configuration section now you can configure lint staged with its own separate file but you can also just add it to your package.json and that's what I like to do because I don't want another config file hanging around so you can take that config and then in your package.json add it down here and
then specify the command you want to run for our case we want to run the lint command so here we're going to say pnpm lint and so now if we just want to test this we can do pnpm exec lint staged and you'll see that right now we don't have any staged files so let's actually do what we did earlier let's introduce a linting change and let's say just like remove a semicolon here and then we'll stage those files and now run exec lint stage and you'll see that it detects that there are only three changed files and then runs the linter on only those three files instead of the whole code base and you can see we have a couple of warnings here let's just fix our whole code base and then if we now stage the files that have been linted and then run lint staged again it should pass this time because we don't have any linter errors great so now instead of running just lint for our commit hook we actually want to run Lynch stage so if
we head to our pre-commit file instead of doing pnpm lint we'll do pnpm exec lint Stag and so now anytime before commit it's going to run lint staged so let's add all of this and then we'll commit it we'll say add lint staged and then before the commit it's going to see we have files changed run the lter on them and then everything's good to go so this is definitely something you're going to want to set up if you're using pre-commit hooks because like I said you don't want to have to run that linter on your whole code base before every single commit now one more thing we can do is set up linting on a CI server so anytime I make a pull request to this repo here we can actually run the linter and if there are any linting errors that will actually show up in the pull request and so whoever made that pull request will know that they need to fix their code now we're going to do this with GitHub actions if you head to the actions page they have a bunch of preconfigured actions you can use we're just going to set it up from scratch cuz it's not too hard if you head to their docs there is a section called building and testing
github actions lint setup
nodejs now we're in a react project but when we run the build or when we run the linter that's all using things inside of nodejs so we're going to want to follow the instructions here for setting up a workflow with nodejs they also have instructions for all kinds of other types of environments that you'd want to run it in now one of the nice things is you actually can set up a matrix of versions so if your GitHub workflow is maybe running tests you could actually run tests against multiple versions of node we're just worried about a single version of node so we're going to copy this config right here and then in our codebase we'll create a folder called GitHub inside the GitHub folder we'll create a folder called workflows and inside of there I'll create lint DOL and now we'll get it set up so the name here is just going to be lint and and this is going to run inside of a container it'll run on an Ubuntu container and you can see they have some stuff set up for us so this actions SL checkout we'll check out the latest version of our code and then set up node we'll set up this container to run nodejs and then we
specify our node version here now this currently is saying use node version 20 I want to use the LTS or the long-term support version the current LTS is version 22 and then our linter wants us to specify it like that now after that you can run any number of commands inside of this container and for our purposes we actually want to do a pnpm install and then after pnpm dependencies have been installed then we want to run pnpm lint now in order to use pnpm in this action we need to set up this container to have pnpm installed if you check out the pnpm docs there's a section here on continuous integration and they show you how to set this up with GitHub actions they actually have a preconfigured action here that will install pnpm in the container that runs on the workflow so I'm going to copy this section here and then just put it right above our node.js setup so now our container will have pnpm so when we run install that command will exist now the other thing is we want to set this up to only run on pull requests right now this
will run on every push to the repo but we want to set it up for pull requests to the main branch if you check out their docs for triggering a workflow you can see all the different types of scenarios that could potentially cause a GitHub action to run like when a new issue is opened or when you Fork repo or when a lab is created there's a lot of different types of events what we care about are filters and you could do like when a push happens to a specific Branch but what we care about is pull requests so when a pull request happens on the main branch that's when we want to run our linter here and so that should be the setting that we need now typically in order to test this out you would need to push it to GitHub and then see the action run and see if it actually works but there's a way to run these GitHub actions locally and that's with this tool called act so they have a lot of different ways to install it I'm on a Mac so I just used Homebrew to install it and then we can use it to run these GitHub actions now this is built on top of Docker because that's what's happening in the cloud with GitHub actions is actually spinning up
run github actions locally with act
containers so you'll need Docker running on your machine as well but to test this out we can use act pull request and then it will run any actions that should run on pull request now I have act installed locally and I have Docker running in the background so if I do act pull request it will run any workflow that should respond to a pull request now I have already run this before so it's already has all the containers locally the first time it runs it'll take a little bit of time because it does have to download those containers now we actually got an error that it can't find the package esent plugin format I thought we had this installed let's look at our package.json apparently we don't but that's a good thing because we didn't have to push our code to GitHub to see that it was going to fail so I'm going to install this as a Dev dependency and then we'll try running act again okay we have a bunch of ler errors let's see why and it's looking at files in this pnpm store now I don't know if this same folder will get created for GitHub actions but we should ignore this because if it does exist we don't want
to run our linter against it so if we head into our lint config we can actually add an ignores section here and we'll just put pnpm store slst star so ignore any file in that directory if it exists so let's try running it again so it worked but actually do have a linter error uh because I disabled lint on Save earlier so let's reenable that and then save this file that adds the trailing comma we can run this again to make sure it's going to work great so now I'm pretty confident that if I push this action up to GitHub it should work as intended so let's go ahead and add everything and we'll make a commit and lint stage is going to run against those four staged files it passed we'll push this up to that branch and check to see if the linter runs on GitHub we'll open up a new pull request against Maine and if we give it a second the linter should kick in there it is all right moment of truth isn't going to going to pass and we have a successful build so that's awesome this is how I want things to exist any pull request to this repo it's going to automatically run the linter uh but let's test to see what happens if
bypassing commit hooks with --no-verify
there is a linter error now one of the things you can do is make a commit without running the gith hooks and really this is one of the main complaints a lot of people have is like I just want to commit whenever I want I don't want to run the linter every time with all of this enabled you can still disable the pre-commit hooks so if you just pass Dash in whenever you do a get commit that will skip any hooks that could have run you can also do D- no verify so let's do that right now push up and then see the linter fail on GitHub so let's disable auto lint on Save remove a semicolon from somewhere okay so let's commit this file that has a linter error we'll say this should break and we'll pass in dash in for no verify look at that our pre-commit hooks did not run and then I'll just push this up to that branch and we should see a failing run over on the pool request so the new commit was received we'll see the previous one passed and this one's running now and it failed of course we were expecting it to fail but we were
missing that semicolon so now if I just fix all my codes commit that and then we'll say this should work and because I didn't pass Dash in it's going to run lint stage which was really fast because I only had one changed file and then we can push this up and again that'll trigger another action run and it passed so that's great and now we can actually merge this in now one thing I like to do when merging in pull requests is to squash any commits that happened on the PO request because if we merg this in without squashing all four of these commits would show up on the main branch and if we look at the main branch it only has one commit right now which was our initial commit and if I were to merge this in as is we then have four commits one of them is broken and I want to keep the main commit history clean so if we do a squash and merge it's going to take all four of these commits turn them into a single commit and then on our main branch we're only going to see that one commit that has all of these changes in it so I'm going to do a
squashing commits
squash and merge that's great another good thing to do is when you're done with a branch on GitHub go ahead and delete it so you don't have a bunch of stale branches hanging around but now if we look at the main branch commit history we just have those two commits and if we look at this Commit This has all of the changes from that P requests but nice and concise in a single commit so I'm going to head back to the main branch pull all those Chang is in and one thing I noticed is that these GitHub settings are actually ignored by default I actually like to commit these to my repo so first of all I'm going to reenable uh lint on Save which actually got committed and if we look in our get ignore yeah this is ignoring everything in the vs code folder I actually want this settings file to exist on GitHub so when other people pull my repo down they have all of these same editor settings so I'm going to manually add that ignored file you'll see that by default it's like hey this is in your get ignor do you really want to do that I do because I want others to have my same vs code settings and so now this file will actually exist in the repo so I'm going
push vs code settings to github
to commit this and I'll just push directly up to the main branch awesome so now we have full in toin linting we're linting in our editor we have lint on Save we're linting before a commit happens with lint Stag and whenever a pull request happens we're running the linter on our CI server so that's it for this video but if you enjoyed this definitely leave a comment leave any questions if you have them and if you enjoy this definitely subscribe because I'll be publishing more videos soon where I actually working on a full stack application where I'm using this exact same flow I show you how to work with GitHub issues and GitHub projects set up your linter and lint stage and everything in the same way and just have a nice editor experience so thanks so much for watching and I'll see you in the next one
thanks
Quote it with credit and a link. Quoting Syntax