diff --git a/README.md b/README.md index c039982..edf7b7e 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,12 @@ Here is a list of formatprograms that are supported by default, and thus will be It can be installed by running `npm install -g standard` (`nodejs` is required). No more configuration needed. More information about the style guide can be found here: http://standardjs.com/. +* `ESlint (local)` for __Javascript__. http://eslint.org/ + It can be installed by running `npm install eslint`. The linter is then installed locally at ```node_modules/.bin/eslint``` + When running formatter vim will walk up from the current file to search for such local installation and a + ESLint configuration file (either .eslintrc or eslintrc.json). When both are found eslint is executed with the --fix argument. + Currently only working on *nix like OS (Linux, MacOS etc.) requires OS to provide sh like shell syntax + * `xo` for __Javascript__. It can be installed by running `npm install -g xo` (`nodejs` is required). Here is the link to the repository: https://github.com/sindresorhus/xo. @@ -213,7 +219,7 @@ Here is a list of formatprograms that are supported by default, and thus will be It can be installed using [`cabal`](https://www.haskell.org/cabal/) build tool. Installation instructions are available at https://github.com/jaspervdj/stylish-haskell#installation * `remark` for __Markdown__. - A Javascript based markdown processor that can be installed with `npm install -g remark`. More info is available at https://github.com/wooorm/remark. + A Javascript based markdown processor that can be installed with `npm install -g remark-cli`. More info is available at https://github.com/wooorm/remark. * `fprettify` for modern __Fortran__. Download from [official repository](https://github.com/pseewald/fprettify). Install with `./setup.py install` or `./setup.py install --user`. diff --git a/plugin/defaults.vim b/plugin/defaults.vim index 01897f5..4085f02 100644 --- a/plugin/defaults.vim +++ b/plugin/defaults.vim @@ -160,8 +160,43 @@ if !exists('g:formatdef_xo_javascript') let g:formatdef_xo_javascript = '"xo --fix --stdin"' endif +" Setup ESLint local. Setup is done on formatter execution if ESLint and +" corresponding config is found they are used, otherwiese the formatter fails. +" No windows support at the moment. +if !exists('g:formatdef_eslint_local') + function! g:BuildESLintLocalCmd() + let l:path = fnamemodify(expand('%'), ':p') + let verbose = &verbose || g:autoformat_verbosemode == 1 + if has('win32') + return "(>&2 echo 'ESLint Local not supported on win32')" + endif + " find formatter & config file + let l:prog = findfile('node_modules/.bin/eslint', l:path.";") + let l:cfg = findfile('.eslintrc.json', l:path.";") + if empty(l:cfg) + let l:cfg = findfile('.eslintrc', l:path.";") + endif + if (empty(l:cfg) || empty(l:prog)) + if verbose + return "(>&2 echo 'No local ESLint program and/or config found')" + endif + return + endif + + " This formatter uses a temporary file as ESLint has not option to print + " the formatted source to stdout without modifieing the file. + let l:eslint_js_tmp_file = fnameescape(tempname().".js") + let content = getline('1', '$') + call writefile(content, l:eslint_js_tmp_file) + return l:prog." -c ".l:cfg." --fix ".l:eslint_js_tmp_file." 1> /dev/null; exit_code=$? + \ cat ".l:eslint_js_tmp_file."; rm -f ".l:eslint_js_tmp_file."; exit $exit_code" + endfunction + let g:formatdef_eslint_local = "g:BuildESLintLocalCmd()" +endif + if !exists('g:formatters_javascript') let g:formatters_javascript = [ + \ 'eslint_local', \ 'jsbeautify_javascript', \ 'pyjsbeautify_javascript', \ 'jscs', @@ -170,7 +205,6 @@ if !exists('g:formatters_javascript') \ ] endif - " JSON if !exists('g:formatdef_jsbeautify_json') if filereadable('.jsbeautifyrc')