All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: tunnckoCore (Charlike Mike Reagent) <mameto2011@gmail.com>,
2 " w0rp <devw0rp@gmail.com>, morhetz (Pavel Pertsev) <morhetz@gmail.com>
3 " Description: Integration of Prettier with ALE.
5 call ale#Set('javascript_prettier_executable', 'prettier')
6 call ale#Set('javascript_prettier_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('javascript_prettier_options', '')
9 function! ale#fixers#prettier#GetExecutable(buffer) abort
10 return ale#path#FindExecutable(a:buffer, 'javascript_prettier', [
11 \ 'node_modules/.bin/prettier_d',
12 \ 'node_modules/prettier-cli/index.js',
13 \ 'node_modules/.bin/prettier',
17 function! ale#fixers#prettier#Fix(buffer) abort
18 return ale#semver#RunWithVersionCheck(
20 \ ale#fixers#prettier#GetExecutable(a:buffer),
22 \ function('ale#fixers#prettier#ApplyFixForVersion'),
26 function! ale#fixers#prettier#ProcessPrettierDOutput(buffer, output) abort
27 " If the output is an error message, don't use it.
28 for l:line in a:output[:10]
29 if l:line =~# '^\w*Error:'
37 function! ale#fixers#prettier#GetCwd(buffer) abort
38 let l:config = ale#path#FindNearestFile(a:buffer, '.prettierignore')
40 " Fall back to the directory of the buffer
41 return !empty(l:config) ? fnamemodify(l:config, ':h') : '%s:h'
44 function! ale#fixers#prettier#ApplyFixForVersion(buffer, version) abort
45 let l:executable = ale#fixers#prettier#GetExecutable(a:buffer)
46 let l:options = ale#Var(a:buffer, 'javascript_prettier_options')
49 let l:filetypes = split(getbufvar(a:buffer, '&filetype'), '\.')
51 if index(l:filetypes, 'handlebars') > -1
52 let l:parser = 'glimmer'
55 " Append the --parser flag depending on the current filetype (unless it's
56 " already set in g:javascript_prettier_options).
57 if empty(expand('#' . a:buffer . ':e')) && l:parser is# '' && match(l:options, '--parser') == -1
58 " Mimic Prettier's defaults. In cases without a file extension or
59 " filetype (scratch buffer), Prettier needs `parser` set to know how
60 " to process the buffer.
61 if ale#semver#GTE(a:version, [1, 16, 0])
62 let l:parser = 'babel'
64 let l:parser = 'babylon'
67 let l:prettier_parsers = {
68 \ 'typescript': 'typescript',
74 \ 'graphql': 'graphql',
75 \ 'markdown': 'markdown',
85 for l:filetype in l:filetypes
86 if has_key(l:prettier_parsers, l:filetype)
87 let l:parser = l:prettier_parsers[l:filetype]
94 let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser
97 " Special error handling needed for prettier_d
98 if l:executable =~# 'prettier_d$'
101 \ 'command':ale#Escape(l:executable)
102 \ . (!empty(l:options) ? ' ' . l:options : '')
103 \ . ' --stdin-filepath %s --stdin',
104 \ 'process_with': 'ale#fixers#prettier#ProcessPrettierDOutput',
108 " 1.4.0 is the first version with --stdin-filepath
109 if ale#semver#GTE(a:version, [1, 4, 0])
111 \ 'cwd': ale#fixers#prettier#GetCwd(a:buffer),
112 \ 'command': ale#Escape(l:executable)
113 \ . (!empty(l:options) ? ' ' . l:options : '')
114 \ . ' --stdin-filepath %s --stdin',
119 \ 'command': ale#Escape(l:executable)
121 \ . (!empty(l:options) ? ' ' . l:options : '')
123 \ 'read_temporary_file': 1,