From b8a1038a411e266c07a7e91c6561126680854bef Mon Sep 17 00:00:00 2001 From: Alvin Chan Date: Mon, 18 Jun 2018 14:40:57 -0700 Subject: Set `--parser` option in Prettier's fixer (#1620) * Set `--parser` option in Prettier's fixer * Add expected `--parser` option to tests * Disable Prettier `--parser` detection if file extension exists * Manually default Prettier `--parser` to "babylon" * Add `--parser` test for TypeScript * Add tests for Prettier `--parser` * Add JSON5 to the suggested fixer for Prettier --- autoload/ale/fix/registry.vim | 2 +- autoload/ale/fixers/prettier.vim | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'autoload') diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 7b55acfc..e99b8d2e 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -51,7 +51,7 @@ let s:default_registry = { \ }, \ 'prettier': { \ 'function': 'ale#fixers#prettier#Fix', -\ 'suggested_filetypes': ['javascript', 'typescript', 'json', 'css', 'scss', 'less', 'markdown', 'graphql', 'vue'], +\ 'suggested_filetypes': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'json5', 'graphql', 'markdown', 'vue'], \ 'description': 'Apply prettier to a file.', \ }, \ 'prettier_eslint': { diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index dcc35813..e8f4e92e 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -30,8 +30,26 @@ 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:version = ale#semver#GetVersion(l:executable, a:version_output) + let l:parser = '' + + " 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' + + for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.') + if index(l:prettier_parsers, l:filetype) > -1 + let l:parser = l:filetype + break + endif + endfor + endif + + if !empty(l:parser) + let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser + endif " 1.4.0 is the first version with --stdin-filepath if ale#semver#GTE(l:version, [1, 4, 0]) -- cgit v1.2.3