From cb8ad9fbd82df92993e526a8e48d4400ba08936b Mon Sep 17 00:00:00 2001 From: Paul Reimer Date: Thu, 2 Aug 2018 12:20:19 -0700 Subject: [PATCH] Javascript prettier filetype detect fix (#1769) Support fixing YAML and HTML files with prettier for files without file extensions. --- autoload/ale/fixers/prettier.vim | 19 +++++++++++++++---- .../fixers/test_prettier_fixer_callback.vader | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index e8f4e92e..9a4a5d8e 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -36,12 +36,23 @@ 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 - let l:prettier_parsers = ['typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue'] - let l:parser = 'babylon' + let l:prettier_parsers = { + \ 'typescript': 'typescript', + \ 'css': 'css', + \ 'less': 'less', + \ 'scss': 'scss', + \ 'json': 'json', + \ 'json5': 'json5', + \ 'graphql': 'graphql', + \ 'markdown': 'markdown', + \ 'vue': 'vue', + \ 'yaml': 'yaml', + \} + let l:parser = '' for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.') - if index(l:prettier_parsers, l:filetype) > -1 - let l:parser = l:filetype + if has_key(l:prettier_parsers, l:filetype) + let l:parser = l:prettier_parsers[l:filetype] break endif endfor diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader index 2018c3a6..130941d1 100644 --- a/test/fixers/test_prettier_fixer_callback.vader +++ b/test/fixers/test_prettier_fixer_callback.vader @@ -222,6 +222,20 @@ Execute(Should set --parser based on filetype, Vue): \ }, \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0']) +Execute(Should set --parser based on filetype, YAML): + call ale#test#SetFilename('../prettier-test-files/testfile') + + set filetype=yaml + + AssertEqual + \ { + \ 'command': 'cd ' . ale#Escape(expand('%:p:h')) . ' && ' + \ . ale#Escape(g:ale_javascript_prettier_executable) + \ . ' --parser yaml' + \ . ' --stdin-filepath %s --stdin', + \ }, + \ ale#fixers#prettier#ApplyFixForVersion(bufnr(''), ['1.6.0']) + Execute(Should set --parser based on first filetype of multiple filetypes): call ale#test#SetFilename('../prettier-test-files/testfile')