2016-11-01 05:00:08 -04:00
|
|
|
" Author: Zach Perrault -- @zperrault
|
2017-11-11 07:07:08 -05:00
|
|
|
" Author: Florian Beeres <yuuki@protonmail.com>
|
2017-11-21 09:37:01 -05:00
|
|
|
" Description: FlowType checking for JavaScript files
|
2017-11-11 07:07:08 -05:00
|
|
|
|
2017-05-27 12:11:03 -04:00
|
|
|
call ale#Set('javascript_flow_executable', 'flow')
|
2017-11-21 09:37:01 -05:00
|
|
|
call ale#Set('javascript_flow_use_home_config', 0)
|
2017-05-27 12:11:03 -04:00
|
|
|
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-11-21 09:37:01 -05:00
|
|
|
let l:flow_config = ale#path#FindNearestFile(a:buffer, '.flowconfig')
|
|
|
|
|
|
|
|
if empty(l:flow_config)
|
|
|
|
" Don't run Flow if we can't find a .flowconfig file.
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Don't run Flow with a configuration file from the home directory by
|
|
|
|
" default, which can eat all of your RAM.
|
|
|
|
if fnamemodify(l:flow_config, ':h') is? $HOME
|
|
|
|
\&& !ale#Var(a:buffer, 'javascript_flow_use_home_config')
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
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
|
|
|
|
|
2017-07-07 05:47:09 -04:00
|
|
|
function! ale_linters#javascript#flow#VersionCheck(buffer) abort
|
2017-11-21 09:37:01 -05:00
|
|
|
let l:executable = ale_linters#javascript#flow#GetExecutable(a:buffer)
|
|
|
|
|
|
|
|
if empty(l:executable)
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
|
|
|
return ale#Escape(l:executable) . ' --version'
|
2017-07-07 05:47:09 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#javascript#flow#GetCommand(buffer, version_lines) abort
|
2017-11-21 09:37:01 -05:00
|
|
|
let l:executable = ale_linters#javascript#flow#GetExecutable(a:buffer)
|
2017-02-10 17:47:56 -05:00
|
|
|
|
2017-11-21 09:37:01 -05:00
|
|
|
if empty(l:executable)
|
2017-02-10 17:47:56 -05:00
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
2017-11-09 18:42:54 -05:00
|
|
|
let l:version = ale#semver#GetVersion(l:executable, a:version_lines)
|
2017-07-07 05:47:09 -04:00
|
|
|
|
|
|
|
" If we can parse the version number, then only use --respect-pragma
|
|
|
|
" if the version is >= 0.36.0, which added the argument.
|
2017-11-09 18:42:54 -05:00
|
|
|
let l:use_respect_pragma = empty(l:version)
|
|
|
|
\ || ale#semver#GTE(l:version, [0, 36])
|
2017-07-07 05:47:09 -04:00
|
|
|
|
2017-11-09 18:42:54 -05:00
|
|
|
return ale#Escape(l:executable)
|
2017-07-07 05:47:09 -04:00
|
|
|
\ . ' check-contents'
|
|
|
|
\ . (l:use_respect_pragma ? ' --respect-pragma': '')
|
|
|
|
\ . ' --json --from ale %s'
|
2016-11-01 05:00:08 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-07-22 14:21:30 -04:00
|
|
|
" Filter lines of flow output until we find the first line where the JSON
|
|
|
|
" output starts.
|
|
|
|
function! s:GetJSONLines(lines) abort
|
|
|
|
let l:start_index = 0
|
|
|
|
|
|
|
|
for l:line in a:lines
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:line[:0] is# '{'
|
2017-07-22 14:21:30 -04:00
|
|
|
break
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:start_index += 1
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return a:lines[l:start_index :]
|
|
|
|
endfunction
|
|
|
|
|
2017-11-11 07:07:08 -05:00
|
|
|
function! s:ExtraErrorMsg(current, new) abort
|
|
|
|
let l:newMsg = ''
|
|
|
|
|
|
|
|
if a:current is# ''
|
|
|
|
" extra messages appear to already have a :
|
|
|
|
let l:newMsg = a:new
|
|
|
|
else
|
|
|
|
let l:newMsg = a:current . ' ' . a:new
|
|
|
|
endif
|
|
|
|
|
|
|
|
return l:newMsg
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:GetDetails(error) abort
|
|
|
|
let l:detail = ''
|
|
|
|
|
|
|
|
for l:extra_error in a:error.extra
|
|
|
|
|
|
|
|
if has_key(l:extra_error, 'message')
|
|
|
|
for l:extra_message in l:extra_error.message
|
|
|
|
let l:detail = s:ExtraErrorMsg(l:detail, l:extra_message.descr)
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(l:extra_error, 'children')
|
|
|
|
for l:child in l:extra_error.children
|
|
|
|
for l:child_message in l:child.message
|
|
|
|
let l:detail = l:detail . ' ' . l:child_message.descr
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:detail
|
|
|
|
endfunction
|
|
|
|
|
2017-01-22 09:54:57 -05:00
|
|
|
function! ale_linters#javascript#flow#Handle(buffer, lines) abort
|
2017-07-22 14:21:30 -04:00
|
|
|
let l:str = join(s:GetJSONLines(a:lines), '')
|
2016-11-01 05:00:08 -04:00
|
|
|
|
2017-07-22 14:21:30 -04:00
|
|
|
if empty(l:str)
|
2017-02-10 17:47:56 -05:00
|
|
|
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')
|
2017-08-08 03:39:13 -04:00
|
|
|
\&& l:line is# 0
|
2017-06-06 15:08:19 -04:00
|
|
|
\&& 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
|
|
|
|
|
2017-08-08 03:39:13 -04:00
|
|
|
if l:text is# ''
|
2017-02-10 17:47:56 -05:00
|
|
|
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
|
|
|
|
2017-11-11 07:07:08 -05:00
|
|
|
let l:errorToAdd = {
|
2017-02-10 17:47:56 -05:00
|
|
|
\ 'lnum': l:line,
|
|
|
|
\ 'col': l:col,
|
|
|
|
\ 'text': l:text,
|
2017-11-11 07:07:08 -05:00
|
|
|
\ 'type': has_key(l:error, 'level') && l:error.level is# 'error' ? 'E' : 'W',
|
|
|
|
\}
|
|
|
|
|
|
|
|
if has_key(l:error, 'extra')
|
|
|
|
let l:errorToAdd.detail = s:GetDetails(l:error)
|
|
|
|
endif
|
|
|
|
|
|
|
|
call add(l:output, l:errorToAdd)
|
|
|
|
|
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',
|
2017-07-07 05:47:09 -04:00
|
|
|
\ 'command_chain': [
|
|
|
|
\ {'callback': 'ale_linters#javascript#flow#VersionCheck'},
|
|
|
|
\ {'callback': 'ale_linters#javascript#flow#GetCommand'},
|
|
|
|
\ ],
|
2016-11-01 05:00:08 -04:00
|
|
|
\ 'callback': 'ale_linters#javascript#flow#Handle',
|
2017-07-18 06:46:59 -04:00
|
|
|
\ 'add_newline': !has('win32'),
|
2016-11-01 05:00:08 -04:00
|
|
|
\})
|