summaryrefslogtreecommitdiff
path: root/autoload/ale/fixers/eslint.vim
blob: c9535cb0ac3b86f1d40d1c8f11f107c13a8d8678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
" Author: w0rp <devw0rp@gmail.com>
" Description: Fixing files with eslint.

function! ale#fixers#eslint#Fix(buffer) abort
    let l:executable = ale#handlers#eslint#GetExecutable(a:buffer)
    let l:command = ale#node#Executable(a:buffer, l:executable)
    \   . ' --version'

    return ale#semver#RunWithVersionCheck(
    \   a:buffer,
    \   l:executable,
    \   l:command,
    \   function('ale#fixers#eslint#ApplyFixForVersion'),
    \)
endfunction

function! ale#fixers#eslint#ProcessFixDryRunOutput(buffer, output) abort
    for l:item in ale#util#FuzzyJSONDecode(a:output, [])
        return split(get(l:item, 'output', ''), "\n")
    endfor

    return []
endfunction

function! ale#fixers#eslint#ProcessEslintDOutput(buffer, output) abort
    " If the output is an error message, don't use it.
    for l:line in a:output[:10]
        if l:line =~# '\v^Error:|^Could not connect'
            return []
        endif
    endfor

    return a:output
endfunction

function! ale#fixers#eslint#ApplyFixForVersion(buffer, version) abort
    let l:executable = ale#handlers#eslint#GetExecutable(a:buffer)
    let l:options = ale#Var(a:buffer, 'javascript_eslint_options')

    " Use the configuration file from the options, if configured.
    if l:options =~# '\v(^| )-c|(^| )--config'
        let l:config = ''
        let l:has_config = 1
    else
        let l:config = ale#handlers#eslint#FindConfig(a:buffer)
        let l:has_config = !empty(l:config)
    endif

    if !l:has_config
        return 0
    endif

    " Use --fix-to-stdout with eslint_d
    if l:executable =~# 'eslint_d$' && ale#semver#GTE(a:version, [3, 19, 0])
        return {
        \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
        \   'command': ale#node#Executable(a:buffer, l:executable)
        \       . ale#Pad(l:options)
        \       . ' --stdin-filename %s --stdin --fix-to-stdout',
        \   'process_with': 'ale#fixers#eslint#ProcessEslintDOutput',
        \}
    endif

    " 4.9.0 is the first version with --fix-dry-run
    if ale#semver#GTE(a:version, [4, 9, 0])
        return {
        \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
        \   'command': ale#node#Executable(a:buffer, l:executable)
        \       . ale#Pad(l:options)
        \       . ' --stdin-filename %s --stdin --fix-dry-run --format=json',
        \   'process_with': 'ale#fixers#eslint#ProcessFixDryRunOutput',
        \}
    endif

    return {
    \   'cwd': ale#handlers#eslint#GetCwd(a:buffer),
    \   'command': ale#node#Executable(a:buffer, l:executable)
    \       . ale#Pad(l:options)
    \       . (!empty(l:config) ? ' -c ' . ale#Escape(l:config) : '')
    \       . ' --fix %t',
    \   'read_temporary_file': 1,
    \}
endfunction