Add support for prettier configuration file. (#886)

* Add support for prettier configuration file.

As of version 1.6.0, prettier allows passing a `--config` argument with
a path to a configuration file.

* Add test prettier configuration file.

* Add option to use local prettier configuration.

* Add description for new prettier option.

* Also check if the config is present before using it.
This commit is contained in:
aliou 2017-08-29 17:05:19 +02:00 committed by w0rp
parent 87fb72346a
commit b36882e72e
6 changed files with 88 additions and 1 deletions

View File

@ -4,8 +4,27 @@
call ale#Set('javascript_prettier_executable', 'prettier')
call ale#Set('javascript_prettier_use_global', 0)
call ale#Set('javascript_prettier_use_local_config', 0)
call ale#Set('javascript_prettier_options', '')
function! s:FindConfig(buffer) abort
for l:filename in [
\ '.prettierrc',
\ 'prettier.config.js',
\ '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#GetExecutable(buffer) abort
return ale#node#FindExecutable(a:buffer, 'javascript_prettier', [
\ 'node_modules/.bin/prettier_d',
@ -16,11 +35,15 @@ endfunction
function! ale#fixers#prettier#Fix(buffer) abort
let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
let l:config = s:FindConfig(a:buffer)
let l:use_config = ale#Var(a:buffer, 'javascript_prettier_use_local_config')
\ && !empty(l:config)
return {
\ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer))
\ . ' %t'
\ . ' ' . l:options
\ . (!empty(l:options) ? ' ' . l:options : '')
\ . (l:use_config ? ' --config ' . ale#Escape(l:config) : '')
\ . ' --write',
\ 'read_temporary_file': 1,
\}

View File

@ -86,6 +86,12 @@ g:ale_javascript_prettier_use_global *g:ale_javascript_prettier_use_global*
See |ale-integrations-local-executables|
g:ale_javascript_prettier_use_local_config *g:ale_javascript_prettier_use_local_config*
*b:ale_javascript_prettier_use_local_config*
Type: |Number|
Default: `0`
This variable can be set to use the local prettier configuration file.
===============================================================================
prettier-eslint *ale-javascript-prettier-eslint*

View File

@ -0,0 +1,58 @@
Before:
call ale#test#SetDirectory('/testplugin/test/fixers')
Save g:ale_javascript_prettier_executable
Save g:ale_javascript_prettier_options
" Use an invalid global executable, so we don't match it.
let g:ale_javascript_prettier_executable = 'xxxinvalid'
let g:ale_javascript_prettier_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
silent cd ..
silent cd command_callback
let g:dir = getcwd()
After:
let g:ale_has_override = {}
call ale#test#RestoreDirectory()
Execute(The prettier callback should return the correct default values):
call ale#test#SetFilename('../prettier-test-files/testfile.js')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t'
\ . ' --write',
\ },
\ ale#fixers#prettier#Fix(bufnr(''))
Execute(The prettier callback should include configuration files when the option is set):
let g:ale_javascript_prettier_use_local_config = 1
call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t'
\ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
\ . ' --write',
\ },
\ ale#fixers#prettier#Fix(bufnr(''))
Execute(The prettier callback should include custom prettier options):
let g:ale_javascript_prettier_options = '--no-semi'
call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
\ . ' %t'
\ . ' --no-semi'
\ . ' --config ' . ale#Escape(simplify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
\ . ' --write',
\ },
\ ale#fixers#prettier#Fix(bufnr(''))

View File