Merge branch 'fix-prettier-eslint-fixer'

This commit is contained in:
w0rp 2017-08-30 22:49:55 +01:00
commit 1d86a724f2
14 changed files with 138 additions and 26 deletions

View File

@ -99,7 +99,7 @@ formatting.
| HTML | [HTMLHint](http://htmlhint.com/), [proselint](http://proselint.com/), [tidy](http://www.html-tidy.org/) | | HTML | [HTMLHint](http://htmlhint.com/), [proselint](http://proselint.com/), [tidy](http://www.html-tidy.org/) |
| Idris | [idris](http://www.idris-lang.org/) | | Idris | [idris](http://www.idris-lang.org/) |
| Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) | | Java | [checkstyle](http://checkstyle.sourceforge.net), [javac](http://www.oracle.com/technetwork/java/javase/downloads/index.html) |
| JavaScript | [eslint](http://eslint.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [flow](https://flowtype.org/), [prettier](https://github.com/prettier/prettier), prettier-eslint, prettier-standard, [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo) | JavaScript | [eslint](http://eslint.org/), [jscs](http://jscs.info/), [jshint](http://jshint.com/), [flow](https://flowtype.org/), [prettier](https://github.com/prettier/prettier), prettier-eslint >= 4.2.0, prettier-standard, [standard](http://standardjs.com/), [xo](https://github.com/sindresorhus/xo)
| JSON | [jsonlint](http://zaa.ch/jsonlint/) | | JSON | [jsonlint](http://zaa.ch/jsonlint/) |
| Kotlin | [kotlinc](https://kotlinlang.org) !!, [ktlint](https://ktlint.github.io) !! see `:help ale-integration-kotlin` for configuration instructions | Kotlin | [kotlinc](https://kotlinlang.org) !!, [ktlint](https://ktlint.github.io) !! see `:help ale-integration-kotlin` for configuration instructions
| LaTeX | [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/) | | LaTeX | [chktex](http://www.nongnu.org/chktex/), [lacheck](https://www.ctan.org/pkg/lacheck), [proselint](http://proselint.com/) |

View File

@ -1,25 +1,58 @@
" Author: tunnckoCore (Charlike Mike Reagent) <mameto2011@gmail.com>, " Author: tunnckoCore (Charlike Mike Reagent) <mameto2011@gmail.com>,
" w0rp <devw0rp@gmail.com> " w0rp <devw0rp@gmail.com>, morhetz (Pavel Pertsev) <morhetz@gmail.com>
" Description: Integration between Prettier and ESLint. " Description: Integration between Prettier and ESLint.
call ale#Set('javascript_prettier_eslint_executable', 'prettier-eslint') function! ale#fixers#prettier_eslint#SetOptionDefaults() abort
call ale#Set('javascript_prettier_eslint_use_global', 0) call ale#Set('javascript_prettier_eslint_executable', 'prettier-eslint')
call ale#Set('javascript_prettier_eslint_options', '') call ale#Set('javascript_prettier_eslint_use_global', 0)
call ale#Set('javascript_prettier_eslint_options', '')
call ale#Set('javascript_prettier_eslint_legacy', 0)
endfunction
call ale#fixers#prettier_eslint#SetOptionDefaults()
function! s:FindConfig(buffer) abort
for l:filename in [
\ '.eslintrc.js',
\ '.eslintrc.yaml',
\ '.eslintrc.yml',
\ '.eslintrc.json',
\ '.eslintrc',
\ 'package.json',
\]
let l:config = ale#path#FindNearestFile(a:buffer, l:filename)
if !empty(l:config)
return l:config
endif
endfor
return ''
endfunction
function! ale#fixers#prettier_eslint#GetExecutable(buffer) abort function! ale#fixers#prettier_eslint#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'javascript_prettier_eslint', [ return ale#node#FindExecutable(a:buffer, 'javascript_prettier_eslint', [
\ 'node_modules/prettier-eslint-cli/index.js', \ 'node_modules/prettier-eslint-cli/dist/index.js',
\ 'node_modules/.bin/prettier-eslint', \ 'node_modules/.bin/prettier-eslint',
\]) \])
endfunction endfunction
function! ale#fixers#prettier_eslint#Fix(buffer, lines) abort function! ale#fixers#prettier_eslint#Fix(buffer) abort
let l:options = ale#Var(a:buffer, 'javascript_prettier_eslint_options') let l:options = ale#Var(a:buffer, 'javascript_prettier_eslint_options')
let l:executable = ale#fixers#prettier_eslint#GetExecutable(a:buffer)
let l:config = !ale#Var(a:buffer, 'javascript_prettier_eslint_legacy')
\ ? s:FindConfig(a:buffer)
\ : ''
let l:eslint_config_option = !empty(l:config)
\ ? ' --eslint-config-path ' . ale#Escape(l:config)
\ : ''
return { return {
\ 'command': ale#Escape(ale#fixers#prettier_eslint#GetExecutable(a:buffer)) \ 'command': ale#Escape(l:executable)
\ . ' %t' \ . ' %t'
\ . ' ' . l:options \ . l:eslint_config_option
\ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' --write', \ . ' --write',
\ 'read_temporary_file': 1, \ 'read_temporary_file': 1,
\} \}

View File

@ -86,7 +86,8 @@ g:ale_javascript_prettier_use_global *g:ale_javascript_prettier_use_global*
See |ale-integrations-local-executables| See |ale-integrations-local-executables|
g:ale_javascript_prettier_use_local_config *g:ale_javascript_prettier_use_local_config* g:ale_javascript_prettier_use_local_config
*g:ale_javascript_prettier_use_local_config*
*b:ale_javascript_prettier_use_local_config* *b:ale_javascript_prettier_use_local_config*
Type: |Number| Type: |Number|
Default: `0` Default: `0`
@ -96,16 +97,10 @@ g:ale_javascript_prettier_use_local_config *g:ale_javascript_prettier_use_lo
=============================================================================== ===============================================================================
prettier-eslint *ale-javascript-prettier-eslint* prettier-eslint *ale-javascript-prettier-eslint*
ALE supports `prettier-eslint` for easy integration with projects, but it is ALE supports `prettier-eslint` >= 4.2.0. Using lower version is not recommended
not recommended for new projects. ALE instead recommends configuring because it cannot be configured to use the ESLint configuration file for input
|g:ale_fixers| to run `'prettier'` and `'eslint'` in a sequence like so: > given via stdin. However ALE could be set up on your own risk with older
versions with |g:ale_javascript_prettier_eslint_legacy|
let g:ale_fixers = {'javascript': ['prettier', 'eslint']}
<
This is because `prettier-eslint` cannot be configured to use the ESLint
configuration file for input given via stdin, which is how ALE integrates with
the tool.
g:ale_javascript_prettier_eslint_executable g:ale_javascript_prettier_eslint_executable
*g:ale_javascript_prettier_eslint_executable* *g:ale_javascript_prettier_eslint_executable*
@ -133,6 +128,14 @@ g:ale_javascript_prettier_eslint_use_global
See |ale-integrations-local-executables| See |ale-integrations-local-executables|
g:ale_javascript_prettier_eslint_legacy
*g:ale_javascript_prettier_eslint_legacy*
*b:ale_javascript_prettier_eslint_legacy*
Type: |Number|
Default: `0`
Fallback option for `prettier-eslint` < 4.2.0
=============================================================================== ===============================================================================
prettier-standard *ale-javascript-prettier-standard* prettier-standard *ale-javascript-prettier-standard*

View File

@ -215,7 +215,7 @@ Notes:
* HTML: `HTMLHint`, `proselint`, `tidy` * HTML: `HTMLHint`, `proselint`, `tidy`
* Idris: `idris` * Idris: `idris`
* Java: `checkstyle`, `javac` * Java: `checkstyle`, `javac`
* JavaScript: `eslint`, `jscs`, `jshint`, `flow`, `prettier`, `prettier-eslint`, `prettier-standard`, `standard`, `xo` * JavaScript: `eslint`, `jscs`, `jshint`, `flow`, `prettier`, `prettier-eslint` >= 4.2.0, `prettier-standard`, `standard`, `xo`
* JSON: `jsonlint` * JSON: `jsonlint`
* Kotlin: `kotlinc`, `ktlint` * Kotlin: `kotlinc`, `ktlint`
* LaTeX (tex): `chktex`, `lacheck`, `proselint` * LaTeX (tex): `chktex`, `lacheck`, `proselint`

View File

View File

@ -0,0 +1,76 @@
Before:
call ale#test#SetDirectory('/testplugin/test/fixers')
Save g:ale_javascript_prettier_eslint_executable
Save g:ale_javascript_prettier_eslint_use_global
Save g:ale_javascript_prettier_eslint_options
Save g:ale_javascript_prettier_eslint_legacy
unlet! g:ale_javascript_prettier_eslint_executable
unlet! g:ale_javascript_prettier_eslint_use_global
unlet! g:ale_javascript_prettier_eslint_options
unlet! g:ale_javascript_prettier_eslint_legacy
call ale#fixers#prettier_eslint#SetOptionDefaults()
After:
Restore
unlet! b:ale_javascript_prettier_eslint_executable
unlet! b:ale_javascript_prettier_eslint_use_global
unlet! b:ale_javascript_prettier_eslint_options
unlet! b:ale_javascript_prettier_eslint_legacy
call ale#test#RestoreDirectory()
Execute(The default command should be correct):
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command':
\ ale#Escape('prettier-eslint')
\ . ' %t'
\ . ' --write'
\ },
\ ale#fixers#prettier_eslint#Fix(bufnr(''))
Execute(Additional options should be used when set):
let b:ale_javascript_prettier_eslint_options = '--foobar'
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command':
\ ale#Escape('prettier-eslint')
\ . ' %t'
\ . ' --foobar --write'
\ },
\ ale#fixers#prettier_eslint#Fix(bufnr(''))
Execute(Configuration files should be detected):
call ale#test#SetFilename('eslint-test-files/react-app/foo/bar.js')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command':
\ ale#Escape('prettier-eslint')
\ . ' %t'
\ . ' --eslint-config-path ' . ale#Escape(g:dir . '/eslint-test-files/react-app/.eslintrc.js')
\ . ' --write'
\ },
\ ale#fixers#prettier_eslint#Fix(bufnr(''))
Execute(Configuration files should be disabled if the legacy option is on):
call ale#test#SetFilename('eslint-test-files/react-app/foo/bar.js')
let b:ale_javascript_prettier_eslint_legacy = 1
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command':
\ ale#Escape('prettier-eslint')
\ . ' %t'
\ . ' --write'
\ },
\ ale#fixers#prettier_eslint#Fix(bufnr(''))