From 3e4b8ea466efca0d05c9952b80f00f96f5dd0e4e Mon Sep 17 00:00:00 2001 From: Cyrille David Date: Thu, 6 Jun 2019 18:06:23 +0200 Subject: [PATCH] prettier: Support experimental languages Such as handlebars --- autoload/ale/fixers/prettier.vim | 71 +++++++++++-------- doc/ale-handlebars.txt | 7 ++ doc/ale.txt | 1 + .../fixers/test_prettier_fixer_callback.vader | 14 ++++ 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/autoload/ale/fixers/prettier.vim b/autoload/ale/fixers/prettier.vim index b7f0ecd7..e798dfb3 100644 --- a/autoload/ale/fixers/prettier.vim +++ b/autoload/ale/fixers/prettier.vim @@ -41,36 +41,49 @@ function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) 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(a:version, [1, 16, 0]) - let l:parser = 'babel' - else - let l:parser = 'babylon' - endif - - let l:prettier_parsers = { - \ 'typescript': 'typescript', - \ 'css': 'css', - \ 'less': 'less', - \ 'scss': 'scss', - \ 'json': 'json', - \ 'json5': 'json5', - \ 'graphql': 'graphql', - \ 'markdown': 'markdown', - \ 'vue': 'vue', - \ 'yaml': 'yaml', - \ 'html': 'html', - \} - - for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.') - if has_key(l:prettier_parsers, l:filetype) - let l:parser = l:prettier_parsers[l:filetype] - break + if match(l:options, '--parser') == -1 + if empty(expand('#' . a:buffer . ':e')) + " 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(a:version, [1, 16, 0]) + let l:parser = 'babel' + else + let l:parser = 'babylon' endif - endfor + + let l:prettier_parsers = { + \ 'typescript': 'typescript', + \ 'css': 'css', + \ 'less': 'less', + \ 'scss': 'scss', + \ 'json': 'json', + \ 'json5': 'json5', + \ 'graphql': 'graphql', + \ 'markdown': 'markdown', + \ 'vue': 'vue', + \ 'yaml': 'yaml', + \ 'html': 'html', + \} + + for l:filetype in split(getbufvar(a:buffer, '&filetype'), '\.') + if has_key(l:prettier_parsers, l:filetype) + let l:parser = l:prettier_parsers[l:filetype] + break + endif + endfor + else + " Append a --parser flag. In cases the filetype has an exprimental + " support in Prettier and has to be appended manually. + let l:prettier_experimental_parsers = { + \ 'html.handlebars': 'glimmer', + \} + let l:filetype = getbufvar(a:buffer, '&filetype') + + if has_key(l:prettier_experimental_parsers, l:filetype) + let l:parser = l:prettier_experimental_parsers[l:filetype] + endif + endif endif if !empty(l:parser) diff --git a/doc/ale-handlebars.txt b/doc/ale-handlebars.txt index 061c5d3c..5daec5b3 100644 --- a/doc/ale-handlebars.txt +++ b/doc/ale-handlebars.txt @@ -2,6 +2,13 @@ ALE Handlebars Integration *ale-handlebars-options* +=============================================================================== +prettier *ale-handlebars-prettier* + +See |ale-javascript-prettier| for information about the available options. +Uses glimmer parser by default. + + =============================================================================== ember-template-lint *ale-handlebars-embertemplatelint* diff --git a/doc/ale.txt b/doc/ale.txt index f0820be6..767dff8a 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2034,6 +2034,7 @@ documented in additional help files. hackfmt...............................|ale-hack-hackfmt| hhast.................................|ale-hack-hhast| handlebars..............................|ale-handlebars-options| + prettier..............................|ale-handlebars-prettier| ember-template-lint...................|ale-handlebars-embertemplatelint| haskell.................................|ale-haskell-options| brittany..............................|ale-haskell-brittany| diff --git a/test/fixers/test_prettier_fixer_callback.vader b/test/fixers/test_prettier_fixer_callback.vader index 9be161ac..062ae8cf 100644 --- a/test/fixers/test_prettier_fixer_callback.vader +++ b/test/fixers/test_prettier_fixer_callback.vader @@ -280,6 +280,20 @@ Execute(Should set --parser based on first filetype of multiple filetypes): \ . ' --stdin-filepath %s --stdin', \ } +Execute(Should set --parser for experimental language, Handlebars): + call ale#test#SetFilename('../prettier-test-files/testfile.hbs') + + set filetype=html.handlebars + + GivenCommandOutput ['1.6.0'] + AssertFixer + \ { + \ 'command': ale#path#CdString(expand('%:p:h')) + \ . ale#Escape(g:ale_javascript_prettier_executable) + \ . ' --parser glimmer' + \ . ' --stdin-filepath %s --stdin', + \ } + Execute(The prettier_d post-processor should permit regular JavaScript content): AssertEqual \ [