]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/prettier.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / fixers / prettier.vim
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.
4
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', '')
8
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',
14     \])
15 endfunction
16
17 function! ale#fixers#prettier#Fix(buffer) abort
18     return ale#semver#RunWithVersionCheck(
19     \   a:buffer,
20     \   ale#fixers#prettier#GetExecutable(a:buffer),
21     \   '%e --version',
22     \   function('ale#fixers#prettier#ApplyFixForVersion'),
23     \)
24 endfunction
25
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:'
30             return []
31         endif
32     endfor
33
34     return a:output
35 endfunction
36
37 function! ale#fixers#prettier#GetCwd(buffer) abort
38     let l:config = ale#path#FindNearestFile(a:buffer, '.prettierignore')
39
40     " Fall back to the directory of the buffer
41     return !empty(l:config) ? fnamemodify(l:config, ':h') : '%s:h'
42 endfunction
43
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')
47     let l:parser = ''
48
49     let l:filetypes = split(getbufvar(a:buffer, '&filetype'), '\.')
50
51     if index(l:filetypes, 'handlebars') > -1
52         let l:parser = 'glimmer'
53     endif
54
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'
63         else
64             let l:parser = 'babylon'
65         endif
66
67         let l:prettier_parsers = {
68         \    'typescript': 'typescript',
69         \    'css': 'css',
70         \    'less': 'less',
71         \    'scss': 'scss',
72         \    'json': 'json',
73         \    'json5': 'json5',
74         \    'graphql': 'graphql',
75         \    'markdown': 'markdown',
76         \    'vue': 'vue',
77         \    'svelte': 'svelte',
78         \    'yaml': 'yaml',
79         \    'openapi': 'yaml',
80         \    'html': 'html',
81         \    'ruby': 'ruby',
82         \    'astro': 'astro',
83         \}
84
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]
88                 break
89             endif
90         endfor
91     endif
92
93     if !empty(l:parser)
94         let l:options = (!empty(l:options) ? l:options . ' ' : '') . '--parser ' . l:parser
95     endif
96
97     " Special error handling needed for prettier_d
98     if l:executable =~# 'prettier_d$'
99         return {
100         \   'cwd': '%s:h',
101         \   'command':ale#Escape(l:executable)
102         \       . (!empty(l:options) ? ' ' . l:options : '')
103         \       . ' --stdin-filepath %s --stdin',
104         \   'process_with': 'ale#fixers#prettier#ProcessPrettierDOutput',
105         \}
106     endif
107
108     " 1.4.0 is the first version with --stdin-filepath
109     if ale#semver#GTE(a:version, [1, 4, 0])
110         return {
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',
115         \}
116     endif
117
118     return {
119     \   'command': ale#Escape(l:executable)
120     \       . ' %t'
121     \       . (!empty(l:options) ? ' ' . l:options : '')
122     \       . ' --write',
123     \   'read_temporary_file': 1,
124     \}
125 endfunction