prettier: Support experimental languages

Such as handlebars
This commit is contained in:
Cyrille David 2019-06-06 18:06:23 +02:00 committed by Cyrille David
parent 7b78f2b846
commit 3e4b8ea466
4 changed files with 64 additions and 29 deletions

View File

@ -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)

View File

@ -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*

View File

@ -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|

View File

@ -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
\ [