#1095 - Use --stdin-filepath for prettier, where available
This commit is contained in:
parent
3f70f1cbf1
commit
520541cd2d
@ -4,31 +4,8 @@
|
|||||||
|
|
||||||
call ale#Set('javascript_prettier_executable', 'prettier')
|
call ale#Set('javascript_prettier_executable', 'prettier')
|
||||||
call ale#Set('javascript_prettier_use_global', 0)
|
call ale#Set('javascript_prettier_use_global', 0)
|
||||||
call ale#Set('javascript_prettier_use_local_config', 0)
|
|
||||||
call ale#Set('javascript_prettier_options', '')
|
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
|
function! ale#fixers#prettier#GetExecutable(buffer) abort
|
||||||
return ale#node#FindExecutable(a:buffer, 'javascript_prettier', [
|
return ale#node#FindExecutable(a:buffer, 'javascript_prettier', [
|
||||||
\ 'node_modules/.bin/prettier_d',
|
\ 'node_modules/.bin/prettier_d',
|
||||||
@ -38,32 +15,38 @@ function! ale#fixers#prettier#GetExecutable(buffer) abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! ale#fixers#prettier#Fix(buffer) abort
|
function! ale#fixers#prettier#Fix(buffer) abort
|
||||||
let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
|
let l:executable = ale#fixers#prettier#GetExecutable(a:buffer)
|
||||||
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
|
let l:command = ale#semver#HasVersion(l:executable)
|
||||||
" already set in g:javascript_prettier_options).
|
\ ? ''
|
||||||
if match(l:options, '--parser') == -1
|
\ : ale#Escape(l:executable) . ' --version'
|
||||||
if l:filetype is# 'typescript'
|
|
||||||
let l:parser = 'typescript'
|
return {
|
||||||
elseif l:filetype =~# 'css\|scss\|less'
|
\ 'command': l:command,
|
||||||
let l:parser = 'postcss'
|
\ 'chain_with': 'ale#fixers#prettier#ApplyFixForVersion',
|
||||||
elseif l:filetype is# 'json'
|
\}
|
||||||
let l:parser = 'json'
|
endfunction
|
||||||
else
|
|
||||||
let l:parser = 'babylon'
|
function! ale#fixers#prettier#ApplyFixForVersion(buffer, version_output) abort
|
||||||
endif
|
let l:executable = ale#fixers#prettier#GetExecutable(a:buffer)
|
||||||
let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser
|
let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
|
||||||
|
|
||||||
|
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
|
endif
|
||||||
|
|
||||||
return {
|
return {
|
||||||
\ 'command': ale#Escape(ale#fixers#prettier#GetExecutable(a:buffer))
|
\ 'command': ale#Escape(l:executable)
|
||||||
\ . ' %t'
|
\ . ' %t'
|
||||||
\ . (!empty(l:options) ? ' ' . l:options : '')
|
\ . (!empty(l:options) ? ' ' . l:options : '')
|
||||||
\ . (l:use_config ? ' --config ' . ale#Escape(l:config) : '')
|
|
||||||
\ . ' --write',
|
\ . ' --write',
|
||||||
\ 'read_temporary_file': 1,
|
\ 'read_temporary_file': 1,
|
||||||
\}
|
\}
|
||||||
|
@ -167,14 +167,6 @@ 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*
|
|
||||||
*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*
|
prettier-eslint *ale-javascript-prettier-eslint*
|
||||||
|
@ -14,7 +14,9 @@ Before:
|
|||||||
|
|
||||||
After:
|
After:
|
||||||
let g:ale_has_override = {}
|
let g:ale_has_override = {}
|
||||||
|
|
||||||
call ale#test#RestoreDirectory()
|
call ale#test#RestoreDirectory()
|
||||||
|
call ale#semver#ResetVersionCache()
|
||||||
|
|
||||||
Execute(The prettier callback should return the correct default values):
|
Execute(The prettier callback should return the correct default values):
|
||||||
call ale#test#SetFilename('../prettier-test-files/testfile.js')
|
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,
|
\ 'read_temporary_file': 1,
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' %t'
|
||||||
\ . ' --parser babylon'
|
|
||||||
\ . ' --write',
|
\ . ' --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
|
let g:ale_javascript_prettier_use_local_config = 1
|
||||||
call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js')
|
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,
|
\ 'read_temporary_file': 1,
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' %t'
|
||||||
\ . ' --parser babylon'
|
|
||||||
\ . ' --config ' . ale#Escape(ale#path#Winify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
|
|
||||||
\ . ' --write',
|
\ . ' --write',
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), [])
|
||||||
|
|
||||||
Execute(The prettier callback should include custom prettier options):
|
Execute(The prettier callback should include custom prettier options):
|
||||||
let g:ale_javascript_prettier_options = '--no-semi'
|
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,
|
\ 'read_temporary_file': 1,
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' %t'
|
||||||
\ . ' --no-semi --parser babylon'
|
\ . ' --no-semi'
|
||||||
\ . ' --config ' . ale#Escape(ale#path#Winify(g:dir . '/../prettier-test-files/with_config/.prettierrc'))
|
|
||||||
\ . ' --write',
|
\ . ' --write',
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), [])
|
||||||
|
|
||||||
Execute(Append '--parser typescript' for filetype=typescript):
|
Execute(The version check should be correct):
|
||||||
set filetype=typescript
|
call ale#test#SetFilename('../prettier-test-files/testfile.js')
|
||||||
call ale#test#SetFilename('../prettier-test-files/testfile.ts')
|
|
||||||
|
|
||||||
AssertEqual
|
AssertEqual
|
||||||
\ {
|
\ {
|
||||||
\ 'read_temporary_file': 1,
|
\ 'chain_with': 'ale#fixers#prettier#ApplyFixForVersion',
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' --version',
|
||||||
\ . ' --parser typescript'
|
|
||||||
\ . ' --write',
|
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
\ ale#fixers#prettier#Fix(bufnr(''))
|
||||||
|
|
||||||
Execute(Append '--parser json' for filetype=json):
|
Execute(--stdin-filepath should be used when prettier is new enough):
|
||||||
set filetype=json
|
let g:ale_javascript_prettier_options = '--no-semi'
|
||||||
call ale#test#SetFilename('../prettier-test-files/testfile.json')
|
call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js')
|
||||||
|
|
||||||
AssertEqual
|
AssertEqual
|
||||||
\ {
|
\ {
|
||||||
\ 'read_temporary_file': 1,
|
\ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ . ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' --no-semi'
|
||||||
\ . ' --parser json'
|
\ . ' --stdin-filepath %s --stdin',
|
||||||
\ . ' --write',
|
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
|
||||||
|
|
||||||
Execute(Append '--parser postcss' for filetype=scss):
|
Execute(The version number should be cached):
|
||||||
set filetype=scss
|
call ale#test#SetFilename('../prettier-test-files/with_config/testfile.js')
|
||||||
call ale#test#SetFilename('../prettier-test-files/testfile.scss')
|
|
||||||
|
|
||||||
|
" 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
|
AssertEqual
|
||||||
\ {
|
\ {
|
||||||
\ 'read_temporary_file': 1,
|
\ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && '
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
\ . ale#Escape(g:ale_javascript_prettier_executable)
|
||||||
\ . ' %t'
|
\ . ' --stdin-filepath %s --stdin',
|
||||||
\ . ' --parser postcss'
|
|
||||||
\ . ' --write',
|
|
||||||
\ },
|
\ },
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), [])
|
||||||
|
|
||||||
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(Append '--parser postcss' for filetype=less):
|
|
||||||
set filetype=less
|
|
||||||
call ale#test#SetFilename('../prettier-test-files/testfile.less')
|
|
||||||
|
|
||||||
AssertEqual
|
|
||||||
\ {
|
|
||||||
\ 'read_temporary_file': 1,
|
|
||||||
\ 'command': ale#Escape(g:ale_javascript_prettier_executable)
|
|
||||||
\ . ' %t'
|
|
||||||
\ . ' --parser postcss'
|
|
||||||
\ . ' --write',
|
|
||||||
\ },
|
|
||||||
\ ale#fixers#prettier#Fix(bufnr(''))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user