Add babylon
as default Prettier parser (#2220)
* Mimic Prettier's default parser by setting it to `babylon` * Add tests to check default Prettier `parser` * Set Prettier default parser based on version * Update the comment to explain the reason for an explicit default
This commit is contained in:
parent
d0284f22ea
commit
f12d312aa4
2 changed files with 37 additions and 1 deletions
|
@ -47,6 +47,15 @@ function! ale#fixers#prettier#ApplyFixForVersion(buffer, version_output) abort
|
|||
" Append the --parser flag depending on the current filetype (unless it's
|
||||
" already set in g:javascript_prettier_options).
|
||||
if empty(expand('#' . a:buffer . ':e')) && match(l:options, '--parser') == -1
|
||||
" Mimic Prettier's defaults. In cases without a file extension or
|
||||
" filetype (scratch buffer), Prettier needs `parser` set to know how
|
||||
" to process the buffer.
|
||||
if ale#semver#GTE(l:version, [1, 16, 0])
|
||||
let l:parser = 'babel'
|
||||
else
|
||||
let l:parser = 'babylon'
|
||||
endif
|
||||
|
||||
let l:prettier_parsers = {
|
||||
\ 'typescript': 'typescript',
|
||||
\ 'css': 'css',
|
||||
|
@ -60,7 +69,6 @@ function! ale#fixers#prettier#ApplyFixForVersion(buffer, version_output) abort
|
|||
\ 'yaml': 'yaml',
|
||||
\ 'html': 'html',
|
||||
\}
|
||||
let l:parser = ''
|
||||
|
||||
for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.')
|
||||
if has_key(l:prettier_parsers, l:filetype)
|
||||
|
|
|
@ -96,6 +96,34 @@ Execute(The version number should be cached):
|
|||
\ },
|
||||
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), [])
|
||||
|
||||
Execute(Should set --parser to `babylon` by default, < 1.16.0):
|
||||
call ale#test#SetFilename('../prettier-test-files/testfile')
|
||||
|
||||
set filetype=javascript
|
||||
|
||||
AssertEqual
|
||||
\ {
|
||||
\ 'command': ale#path#CdString(expand('%:p:h'))
|
||||
\ . ale#Escape(g:ale_javascript_prettier_executable)
|
||||
\ . ' --parser babylon'
|
||||
\ . ' --stdin-filepath %s --stdin',
|
||||
\ },
|
||||
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0'])
|
||||
|
||||
Execute(Should set --parser to `babel` by default, >= 1.16.0):
|
||||
call ale#test#SetFilename('../prettier-test-files/testfile')
|
||||
|
||||
set filetype=javascript
|
||||
|
||||
AssertEqual
|
||||
\ {
|
||||
\ 'command': ale#path#CdString(expand('%:p:h'))
|
||||
\ . ale#Escape(g:ale_javascript_prettier_executable)
|
||||
\ . ' --parser babel'
|
||||
\ . ' --stdin-filepath %s --stdin',
|
||||
\ },
|
||||
\ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.16.0'])
|
||||
|
||||
Execute(Should set --parser based on filetype, TypeScript):
|
||||
call ale#test#SetFilename('../prettier-test-files/testfile')
|
||||
|
||||
|
|
Reference in a new issue