]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/fixers/eslint.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 / eslint.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Fixing files with eslint.
3
4 function! ale#fixers#eslint#Fix(buffer) abort
5     let l:executable = ale#handlers#eslint#GetExecutable(a:buffer)
6     let l:command = ale#node#Executable(a:buffer, l:executable)
7     \   . ' --version'
8
9     return ale#semver#RunWithVersionCheck(
10     \   a:buffer,
11     \   l:executable,
12     \   l:command,
13     \   function('ale#fixers#eslint#ApplyFixForVersion'),
14     \)
15 endfunction
16
17 function! ale#fixers#eslint#ProcessFixDryRunOutput(buffer, output) abort
18     for l:item in ale#util#FuzzyJSONDecode(a:output, [])
19         return split(get(l:item, 'output', ''), "\n")
20     endfor
21
22     return []
23 endfunction
24
25 function! ale#fixers#eslint#ProcessEslintDOutput(buffer, output) abort
26     " If the output is an error message, don't use it.
27     for l:line in a:output[:10]
28         if l:line =~# '\v^Error:|^Could not connect'
29             return []
30         endif
31     endfor
32
33     return a:output
34 endfunction
35
36 function! ale#fixers#eslint#ApplyFixForVersion(buffer, version) abort
37     let l:executable = ale#handlers#eslint#GetExecutable(a:buffer)
38     let l:options = ale#Var(a:buffer, 'javascript_eslint_options')
39
40     " Use the configuration file from the options, if configured.
41     if l:options =~# '\v(^| )-c|(^| )--config'
42         let l:config = ''
43         let l:has_config = 1
44     else
45         let l:config = ale#handlers#eslint#FindConfig(a:buffer)
46         let l:has_config = !empty(l:config)
47     endif
48
49     if !l:has_config
50         return 0
51     endif
52
53     " Use --fix-to-stdout with eslint_d
54     if l:executable =~# 'eslint_d$' && ale#semver#GTE(a:version, [3, 19, 0])
55         return {
56         \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
57         \   'command': ale#node#Executable(a:buffer, l:executable)
58         \       . ale#Pad(l:options)
59         \       . ' --stdin-filename %s --stdin --fix-to-stdout',
60         \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
61         \}
62     endif
63
64     " 4.9.0 is the first version with --fix-dry-run
65     if ale#semver#GTE(a:version, [4, 9, 0])
66         return {
67         \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
68         \   'command': ale#node#Executable(a:buffer, l:executable)
69         \       . ale#Pad(l:options)
70         \       . ' --stdin-filename %s --stdin --fix-dry-run --format=json',
71         \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
72         \}
73     endif
74
75     return {
76     \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
77     \   'command': ale#node#Executable(a:buffer, l:executable)
78     \       . ale#Pad(l:options)
79     \       . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '')
80     \       . ' --fix %t',
81     \   'read_temporary_file': 1,
82     \}
83 endfunction