2017-05-27 17:31:52 +00:00
|
|
|
" Author: tunnckoCore (Charlike Mike Reagent) <mameto2011@gmail.com>,
|
2017-08-29 08:46:53 +00:00
|
|
|
" w0rp <devw0rp@gmail.com>, morhetz (Pavel Pertsev) <morhetz@gmail.com>
|
2017-05-27 17:31:52 +00:00
|
|
|
" Description: Integration of Prettier with ALE.
|
2017-05-21 22:03:33 +00:00
|
|
|
|
2017-05-27 17:31:52 +00:00
|
|
|
call ale#Set('javascript_prettier_executable', 'prettier')
|
2018-04-09 18:11:20 +00:00
|
|
|
call ale#Set('javascript_prettier_use_global', get(g:, 'ale_use_global_executables', 0))
|
2017-06-07 14:33:41 +00:00
|
|
|
call ale#Set('javascript_prettier_options', '')
|
2017-05-21 22:03:33 +00:00
|
|
|
|
2017-06-07 15:02:05 +00:00
|
|
|
function! ale#fixers#prettier#GetExecutable(buffer) abort
|
2021-05-26 21:43:30 +00:00
|
|
|
return ale#path#FindExecutable(a:buffer, 'javascript_prettier', [
|
2017-08-29 08:46:53 +00:00
|
|
|
\ 'node_modules/.bin/prettier_d',
|
2017-05-27 17:31:52 +00:00
|
|
|
\ 'node_modules/prettier-cli/index.js',
|
|
|
|
\ 'node_modules/.bin/prettier',
|
|
|
|
\])
|
2017-05-21 22:03:33 +00:00
|
|
|
endfunction
|
|
|
|
|
2017-06-07 15:02:05 +00:00
|
|
|
function! ale#fixers#prettier#Fix(buffer) abort
|
2019-04-07 13:58:06 +00:00
|
|
|
return ale#semver#RunWithVersionCheck(
|
|
|
|
\ a:buffer,
|
|
|
|
\ ale#fixers#prettier#GetExecutable(a:buffer),
|
|
|
|
\ '%e --version',
|
|
|
|
\ function('ale#fixers#prettier#ApplyFixForVersion'),
|
|
|
|
\)
|
2017-11-22 13:46:11 +00:00
|
|
|
endfunction
|
|
|
|
|
2018-08-09 16:56:53 +00:00
|
|
|
function! ale#fixers#prettier#ProcessPrettierDOutput(buffer, output) abort
|
|
|
|
" If the output is an error message, don't use it.
|
|
|
|
for l:line in a:output[:10]
|
|
|
|
if l:line =~# '^\w*Error:'
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return a:output
|
|
|
|
endfunction
|
|
|
|
|
2021-03-01 20:11:10 +00:00
|
|
|
function! ale#fixers#prettier#GetCwd(buffer) abort
|
2020-08-28 07:14:50 +00:00
|
|
|
let l:config = ale#path#FindNearestFile(a:buffer, '.prettierignore')
|
|
|
|
|
|
|
|
" Fall back to the directory of the buffer
|
2021-03-01 20:11:10 +00:00
|
|
|
return !empty(l:config) ? fnamemodify(l:config, ':h') : '%s:h'
|
2020-08-28 07:14:50 +00:00
|
|
|
endfunction
|
|
|
|
|
2019-04-07 13:58:06 +00:00
|
|
|
function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) abort
|
2017-11-22 13:46:11 +00:00
|
|
|
let l:executable = ale#fixers#prettier#GetExecutable(a:buffer)
|
2017-05-21 22:03:33 +00:00
|
|
|
let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
|
2018-06-18 21:40:57 +00:00
|
|
|
let l:parser = ''
|
|
|
|
|
2019-06-10 22:11:14 +00:00
|
|
|
let l:filetypes = split(getbufvar(a:buffer, '&filetype'), '\.')
|
2019-01-22 10:24:15 +00:00
|
|
|
|
2019-06-10 22:11:14 +00:00
|
|
|
if index(l:filetypes, 'handlebars') > -1
|
|
|
|
let l:parser = 'glimmer'
|
|
|
|
endif
|
2018-06-18 21:40:57 +00:00
|
|
|
|
2019-06-10 22:11:14 +00:00
|
|
|
" 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')) && l:parser is# '' && 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'
|
2019-06-06 16:06:23 +00:00
|
|
|
else
|
2019-06-10 22:11:14 +00:00
|
|
|
let l:parser = 'babylon'
|
|
|
|
endif
|
2019-06-06 16:06:23 +00:00
|
|
|
|
2019-06-10 22:11:14 +00:00
|
|
|
let l:prettier_parsers = {
|
|
|
|
\ 'typescript': 'typescript',
|
|
|
|
\ 'css': 'css',
|
|
|
|
\ 'less': 'less',
|
|
|
|
\ 'scss': 'scss',
|
|
|
|
\ 'json': 'json',
|
|
|
|
\ 'json5': 'json5',
|
|
|
|
\ 'graphql': 'graphql',
|
|
|
|
\ 'markdown': 'markdown',
|
|
|
|
\ 'vue': 'vue',
|
2021-03-20 22:23:27 +00:00
|
|
|
\ 'svelte': 'svelte',
|
2019-06-10 22:11:14 +00:00
|
|
|
\ 'yaml': 'yaml',
|
2019-10-26 15:16:23 +00:00
|
|
|
\ 'openapi': 'yaml',
|
2019-06-10 22:11:14 +00:00
|
|
|
\ 'html': 'html',
|
2021-02-18 07:33:02 +00:00
|
|
|
\ 'ruby': 'ruby',
|
2019-06-10 22:11:14 +00:00
|
|
|
\}
|
|
|
|
|
|
|
|
for l:filetype in l:filetypes
|
|
|
|
if has_key(l:prettier_parsers, l:filetype)
|
|
|
|
let l:parser = l:prettier_parsers[l:filetype]
|
|
|
|
break
|
2018-06-18 21:40:57 +00:00
|
|
|
endif
|
2019-06-10 22:11:14 +00:00
|
|
|
endfor
|
2018-06-18 21:40:57 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
if !empty(l:parser)
|
|
|
|
let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser
|
|
|
|
endif
|
2017-11-22 13:46:11 +00:00
|
|
|
|
2018-08-09 16:56:53 +00:00
|
|
|
" Special error handling needed for prettier_d
|
|
|
|
if l:executable =~# 'prettier_d$'
|
|
|
|
return {
|
2021-03-01 20:11:10 +00:00
|
|
|
\ 'cwd': '%s:h',
|
|
|
|
\ 'command':ale#Escape(l:executable)
|
2018-08-09 16:56:53 +00:00
|
|
|
\ . (!empty(l:options) ? ' ' . l:options : '')
|
|
|
|
\ . ' --stdin-filepath %s --stdin',
|
|
|
|
\ 'process_with': 'ale#fixers#prettier#ProcessPrettierDOutput',
|
|
|
|
\}
|
|
|
|
endif
|
|
|
|
|
2017-11-22 13:46:11 +00:00
|
|
|
" 1.4.0 is the first version with --stdin-filepath
|
2019-04-07 13:58:06 +00:00
|
|
|
if ale#semver#GTE(a:version, [1, 4, 0])
|
2017-11-22 13:46:11 +00:00
|
|
|
return {
|
2021-03-01 20:11:10 +00:00
|
|
|
\ 'cwd': ale#fixers#prettier#GetCwd(a:buffer),
|
|
|
|
\ 'command': ale#Escape(l:executable)
|
2017-11-22 13:46:11 +00:00
|
|
|
\ . (!empty(l:options) ? ' ' . l:options : '')
|
|
|
|
\ . ' --stdin-filepath %s --stdin',
|
|
|
|
\}
|
2017-09-06 14:21:26 +00:00
|
|
|
endif
|
2017-05-21 22:03:33 +00:00
|
|
|
|
|
|
|
return {
|
2017-11-22 13:46:11 +00:00
|
|
|
\ 'command': ale#Escape(l:executable)
|
2017-05-21 22:03:33 +00:00
|
|
|
\ . ' %t'
|
2017-08-29 15:05:19 +00:00
|
|
|
\ . (!empty(l:options) ? ' ' . l:options : '')
|
2017-05-21 22:03:33 +00:00
|
|
|
\ . ' --write',
|
|
|
|
\ 'read_temporary_file': 1,
|
|
|
|
\}
|
|
|
|
endfunction
|