diff options
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/fixers/prettier.vim | 67 |
1 files changed, 25 insertions, 42 deletions
diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index d66e00f0..d75299eb 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -4,31 +4,8 @@ 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', - \ '.prettierrc.json', - \ '.prettierrc.yaml', - \ '.prettierrc.yml', - \ '.prettierrc.js', - \ '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', @@ -38,32 +15,38 @@ function! ale#fixers#prettier#GetExecutable(buffer) abort endfunction function! ale#fixers#prettier#Fix(buffer) abort + let l:executable = ale#fixers#prettier#GetExecutable(a:buffer) + + let l:command = ale#semver#HasVersion(l:executable) + \ ? '' + \ : ale#Escape(l:executable) . ' --version' + + return { + \ 'command': l:command, + \ 'chain_with': 'ale#fixers#prettier#ApplyFixForVersion', + \} +endfunction + +function! ale#fixers#prettier#ApplyFixForVersion(buffer, version_output) abort + let l:executable = ale#fixers#prettier#GetExecutable(a:buffer) 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) - let l:filetype = getbufvar(a:buffer, '&filetype') - " Append the --parser flag depending on the current filetype (unless it's - " already set in g:javascript_prettier_options). - if match(l:options, '--parser') == -1 - if l:filetype is# 'typescript' - let l:parser = 'typescript' - elseif l:filetype =~# 'css\|scss\|less' - let l:parser = 'postcss' - elseif l:filetype is# 'json' - let l:parser = 'json' - else - let l:parser = 'babylon' - endif - let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser + let l:version = ale#semver#GetVersion(l:executable, a:version_output) + + " 1.4.0 is the first version with --stdin-filepath + if ale#semver#GTE(l:version, [1, 4, 0]) + return { + \ 'command': ale#path#BufferCdString(a:buffer) + \ . ale#Escape(l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' --stdin-filepath %s --stdin', + \} endif return { - \ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer)) + \ 'command': ale#Escape(l:executable) \ . ' %t' \ . (!empty(l:options) ? ' ' . l:options : '') - \ . (l:use_config ? ' --config ' . ale#Escape(l:config) : '') \ . ' --write', \ 'read_temporary_file': 1, \} |