Getting Started with NPM Scripts - Linting with Standard
And the Journey Continues...
Like last we will be starting from where we left off before.
If you would like to join in at this point you can get the code to begin with at Getting Started with NPM Scripts - Delete Things!.
Since most of what we are doing is similar to our previous steps I'm going to cut to the chase a bit. Install and save
standard as a dev dependency:
npm install --save-dev standard
Add a new line to the scripts section of your
New npm script
"lint:js": "standard src/**/*.js"
We pass in the path to our
src/folder so we only lint the code we are writing.
And Update the
watch:js to run
clean:js and the
"watch:js": "chokidar src/**/*.js -c "npm run lint:js && npm run clean:js && npm run build:js"",
And we are done.
If you run
watch:js now you may notice some new output when you make a change to a file:
Did you notice the errors?
'myApp' is assigned a value but never used. and such? These are generated from our new step
a Linter performs static analysis, examines the code without running it, and returns errors for things that may cause issue (or bugs)
I can correct these errors in my
src/ folder by removing the semicolons from
sub-app.js, adding a new line at the end of
app.js. I could also run
standard --fix and StandardJS would attempt to fix issues that are pretty straightforward. I imagine most of the errors we started with will be fixed except for the
'mySubApp' is assigned a value but never used. errors.
To make this easier I will add
--fix to the script we created previously so it will finally look like this:
"lint:js": "standard src/**/*.js --fix"
I picked StandardJS as the style to use so there was less room for interpretation. If some one joins the project they will have to conform to the style or there will be issues with each change they make. This will help keep a specific coding standard throughout the project and since StandardJS is pretty set there shouldn't be much time used debating each option that could be set in a more configurable linter.
All code used in this example can be found at BrettMN/npm-scripts-getting-started
Now that we have some coding standards to check things maybe we should add a test or at least the ability at add and run tests. Are there any other steps you would like to see? Let me know by leaving a comment below or sending an email to me at email@example.com and let me know.