2016-11-01 05:00:08 -04:00
|
|
|
" Author: Zach Perrault -- @zperrault
|
|
|
|
" Description: FlowType checking for JavaScript files
|
|
|
|
|
2017-05-27 12:11:03 -04:00
|
|
|
call ale#Set('javascript_flow_executable', 'flow')
|
|
|
|
call ale#Set('javascript_flow_use_global', 0)
|
2016-11-01 05:00:08 -04:00
|
|
|
|
|
|
|
function! ale_linters#javascript#flow#GetExecutable(buffer) abort
|
2017-05-27 12:11:03 -04:00
|
|
|
return ale#node#FindExecutable(a:buffer, 'javascript_flow', [
|
2017-02-10 17:47:56 -05:00
|
|
|
\ 'node_modules/.bin/flow',
|
2017-05-27 12:11:03 -04:00
|
|
|
\])
|
2016-11-01 05:00:08 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#javascript#flow#GetCommand(buffer) abort
|
2017-04-17 18:29:02 -04:00
|
|
|
let l:flow_config = ale#path#FindNearestFile(a:buffer, '.flowconfig')
|
2017-02-10 17:47:56 -05:00
|
|
|
|
|
|
|
if empty(l:flow_config)
|
|
|
|
" Don't run Flow if we can't find a .flowconfig file.
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
2017-05-12 04:20:16 -04:00
|
|
|
return ale#Escape(ale_linters#javascript#flow#GetExecutable(a:buffer))
|
2017-02-10 17:47:56 -05:00
|
|
|
\ . ' check-contents --respect-pragma --json --from ale %s'
|
2016-11-01 05:00:08 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-01-22 09:54:57 -05:00
|
|
|
function! ale_linters#javascript#flow#Handle(buffer, lines) abort
|
2017-02-10 17:47:56 -05:00
|
|
|
let l:str = join(a:lines, '')
|
2016-11-01 05:00:08 -04:00
|
|
|
|
2017-02-10 17:47:56 -05:00
|
|
|
if l:str ==# ''
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:flow_output = json_decode(l:str)
|
2016-11-01 05:00:08 -04:00
|
|
|
let l:output = []
|
|
|
|
|
2017-02-10 17:47:56 -05:00
|
|
|
for l:error in get(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
|
2017-04-16 06:45:42 -04:00
|
|
|
" Comments have no line of column information, so we skip them.
|
|
|
|
" In certain cases, `l:message.loc.source` points to a different path
|
|
|
|
" than the buffer one, thus we skip this loc information too.
|
2017-06-06 15:08:19 -04:00
|
|
|
if has_key(l:message, 'loc')
|
|
|
|
\&& l:line ==# 0
|
|
|
|
\&& ale#path#IsBufferPath(a:buffer, l:message.loc.source)
|
2017-02-10 17:47:56 -05:00
|
|
|
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
|
2016-11-01 05:00:08 -04:00
|
|
|
endif
|
2017-02-10 17:47:56 -05:00
|
|
|
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'lnum': l:line,
|
|
|
|
\ 'col': l:col,
|
|
|
|
\ 'text': l:text,
|
|
|
|
\ 'type': l:error.level ==# 'error' ? 'E' : 'W',
|
|
|
|
\})
|
2016-11-01 05:00:08 -04:00
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
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',
|
|
|
|
\})
|