summaryrefslogtreecommitdiff
path: root/ale_linters/javascript/flow.vim
blob: f0b81b1d8c2ce06b2b601963d80381a96e4b6209 (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
" Author: Zach Perrault -- @zperrault
" Description: FlowType checking for JavaScript files

let g:ale_javascript_flow_executable =
\   get(g:, 'ale_javascript_flow_executable', 'flow')

let g:ale_javascript_flow_use_global =
\   get(g:, 'ale_javascript_flow_use_global', 0)

function! ale_linters#javascript#flow#GetExecutable(buffer) abort
  if g:ale_javascript_flow_use_global
    return g:ale_javascript_flow_executable
  endif

  return ale#util#ResolveLocalPath(
  \   a:buffer,
  \   'node_modules/.bin/flow',
  \   g:ale_javascript_flow_executable
  \)
endfunction

function! ale_linters#javascript#flow#GetCommand(buffer) abort
  return ale_linters#javascript#flow#GetExecutable(a:buffer)
  \   . ' check-contents --respect-pragma --json --from ale %s'
endfunction

function! ale_linters#javascript#flow#Handle(buffer, lines) abort
  let l:str = join(a:lines, '')
  if l:str ==# ''
    return []
  endif
  let l:flow_output = json_decode(l:str)

  if has_key(l:flow_output, 'errors')
    let l:output = []

    for l:error in l:flow_output.errors
      " Each error is broken up into parts
      let l:text = ''
      let l:line = 0
      let l:col = 0
      for l:message in l:error.message
        " Comments have no line of column information
        if has_key(l:message, 'loc') && l:line ==# 0
          let l:line = l:message.loc.start.line + 0
          let l:col = l:message.loc.start.column + 0
        endif
        if l:text ==# ''
          let l:text = l:message.descr . ':'
        else
          let l:text = l:text . ' ' . l:message.descr
        endif
      endfor

      if has_key(l:error, 'operation')
        let l:text = l:text . ' See also: ' . l:error.operation.descr
      endif

      call add(l:output, {
      \   'bufnr': a:buffer,
      \   'lnum': l:line,
      \   'vcol': 0,
      \   'col': l:col,
      \   'text': l:text,
      \   'type': l:error.level ==# 'error' ? 'E' : 'W',
      \})
    endfor

    return l:output
  else
    return []
  endif
endfunction

call ale#linter#Define('javascript', {
\   'name': 'flow',
\   'executable_callback': 'ale_linters#javascript#flow#GetExecutable',
\   'command_callback': 'ale_linters#javascript#flow#GetCommand',
\   'callback': 'ale_linters#javascript#flow#Handle',
\})