diff options
-rw-r--r-- | autoload/ale/fixers/prettier.vim | 67 | ||||
-rw-r--r-- | doc/ale-javascript.txt | 8 | ||||
-rw-r--r-- | test/fixers/test_prettier_fixer_callback.vader | 89 |
3 files changed, 53 insertions, 111 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, \} diff --git a/doc/ale-javascript.txt b/doc/ale-javascript.txt index 5a2969a1..365dfa62 100644 --- a/doc/ale-javascript.txt +++ b/doc/ale-javascript.txt @@ -167,14 +167,6 @@ 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* diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader index 471a8632..3480b41d 100644 --- a/test/fixers/test_prettier_fixer_callback.vader +++ b/test/fixers/test_prettier_fixer_callback.vader @@ -14,7 +14,9 @@ Before: After: let g:ale_has_override = {} + call ale#test#RestoreDirectory() + call ale#semver#ResetVersionCache() Execute(The prettier callback should return the correct default values): call ale#test#SetFilename('../prettier-test-files/testfile.js') @@ -24,12 +26,11 @@ Execute(The prettier callback should return the correct default values): \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' - \ . ' --parser babylon' \ . ' --write', \ }, - \ ale#fixers#prettier#Fix(bufnr('')) + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), []) -Execute(The prettier callback should include configuration files when the option is set): +Execute(The --config option should not be set automatically): let g:ale_javascript_prettier_use_local_config = 1 call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js') @@ -38,11 +39,9 @@ Execute(The prettier callback should include configuration files when the option \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' - \ . ' --parser babylon' - \ . ' --config ' . ale#Escape(ale#path#Winify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) \ . ' --write', \ }, - \ ale#fixers#prettier#Fix(bufnr('')) + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), []) Execute(The prettier callback should include custom prettier options): let g:ale_javascript_prettier_options = '--no-semi' @@ -53,78 +52,46 @@ Execute(The prettier callback should include custom prettier options): \ 'read_temporary_file': 1, \ 'command': ale#Escape(g:ale_javascript_prettier_executable) \ . ' %t' - \ . ' --no-semi --parser babylon' - \ . ' --config ' . ale#Escape(ale#path#Winify(g:dir . '/../prettier-test-files/with_config/.prettierrc')) - \ . ' --write', - \ }, - \ ale#fixers#prettier#Fix(bufnr('')) - -Execute(Append '--parser typescript' for filetype=typescript): - set filetype=typescript - call ale#test#SetFilename('../prettier-test-files/testfile.ts') - - AssertEqual - \ { - \ 'read_temporary_file': 1, - \ 'command': ale#Escape(g:ale_javascript_prettier_executable) - \ . ' %t' - \ . ' --parser typescript' + \ . ' --no-semi' \ . ' --write', \ }, - \ ale#fixers#prettier#Fix(bufnr('')) + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), []) -Execute(Append '--parser json' for filetype=json): - set filetype=json - call ale#test#SetFilename('../prettier-test-files/testfile.json') +Execute(The version check should be correct): + call ale#test#SetFilename('../prettier-test-files/testfile.js') AssertEqual \ { - \ 'read_temporary_file': 1, + \ 'chain_with': 'ale#fixers#prettier#ApplyFixForVersion', \ 'command': ale#Escape(g:ale_javascript_prettier_executable) - \ . ' %t' - \ . ' --parser json' - \ . ' --write', + \ . ' --version', \ }, \ ale#fixers#prettier#Fix(bufnr('')) -Execute(Append '--parser postcss' for filetype=scss): - set filetype=scss - call ale#test#SetFilename('../prettier-test-files/testfile.scss') +Execute(--stdin-filepath should be used when prettier is new enough): + 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' - \ . ' --parser postcss' - \ . ' --write', + \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . ale#Escape(g:ale_javascript_prettier_executable) + \ . ' --no-semi' + \ . ' --stdin-filepath %s --stdin', \ }, - \ ale#fixers#prettier#Fix(bufnr('')) + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0']) -Execute(Append '--parser postcss' for filetype=css): - set filetype=css - call ale#test#SetFilename('../prettier-test-files/testfile.css') - - AssertEqual - \ { - \ 'read_temporary_file': 1, - \ 'command': ale#Escape(g:ale_javascript_prettier_executable) - \ . ' %t' - \ . ' --parser postcss' - \ . ' --write', - \ }, - \ ale#fixers#prettier#Fix(bufnr('')) +Execute(The version number should be cached): + call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js') -Execute(Append '--parser postcss' for filetype=less): - set filetype=less - call ale#test#SetFilename('../prettier-test-files/testfile.less') + " Call the second callback with the version output. + call ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0']) + " Call it again without the vesrion output. We should use the newer command. AssertEqual \ { - \ 'read_temporary_file': 1, - \ 'command': ale#Escape(g:ale_javascript_prettier_executable) - \ . ' %t' - \ . ' --parser postcss' - \ . ' --write', + \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . ale#Escape(g:ale_javascript_prettier_executable) + \ . ' --stdin-filepath %s --stdin', \ }, - \ ale#fixers#prettier#Fix(bufnr('')) + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), []) |