Our goal, here, is threefold:
- use Vim's built-in features to their fullest,
- be a good project citizen even if we don't use $EDITOR_DU_JOUR,
- have a minimal but beneficial impact on the infrastructure of the project we work on.
We will add two scripts to our package.json
: lint
and lint:compact
.
Users can run the lint
script interactively to get the usual detailed output:
$ npm run lint src
while tools (editors, CI, Git hooks, etc.) can be set up to use the more machine-friendly lint:compact
script:
$ npm run lint:compact
-
Install the required modules:
$ npm install --only=dev eslint
-
Configure ESLint:
$ ./node_modules/.bin/eslint --init
or:
$ npx eslint --init
-
Edit the
"scripts"
section ofpackage.json
:"scripts": { "lint": "eslint", "lint:compact": "eslint --format compact" },
Standard is a relatively popular ESLint wrapper that comes with its own rules. It defaults to ESLint's --format compact
, which is fine from a tooling standpoint, but we must add yet another dependency to get a human-readable output.
NOTE: Standard being an ESLint wrapper, it is an either or situation: ESLint or Standard.
-
Install the required modules:
$ npm install --only=dev standard snazzy
-
Edit the
"scripts"
section ofpackage.json
:"scripts": { "lint": "standard | snazzy", "lint:compact": "standard", },
Add this line to $MYVIMRC
:
set errorformat+=%f:\ line\ %l\\,\ col\ %c\\,\ %m,%-G%.%#
Reference:
:help 'errorformat'
Add this line to after/ftplugin/javascript.vim
for linting only:
setlocal makeprg=npm\ run\ --silent\ lint:compact
Or, for linting and fixing:
setlocal makeprg=npm\ run\ --silent\ lint:compact\ --\ --fix
Usage:
:make % | cwindow
:make ## | cwindow
Reference:
:help 'makeprg'
:help :make
:help quickfix
:help quickfix-window
:help :_%
:help :_##
Add this snippet to after/ftplugin/javascript.vim
:
augroup JS
autocmd! * <buffer>
autocmd BufWritePost <buffer> silent make <afile> | silent redraw!
augroup END
Reference:
:help autocommand
Add this snippet to after/ftplugin/javascript.vim
:
setlocal autoread
augroup JS
autocmd! * <buffer>
autocmd BufWritePost <buffer> silent make <afile> | checktime | silent redraw!
augroup END
Reference:
:help 'autoread'
:help :checktime
Thank you, @samundra.