diff options
-rw-r--r-- | ale_linters/ruby/reek.vim | 58 | ||||
-rw-r--r-- | test/command_callback/test_reek_command_callback.vader | 34 |
2 files changed, 76 insertions, 16 deletions
diff --git a/ale_linters/ruby/reek.vim b/ale_linters/ruby/reek.vim index a11b9cf8..aa5d8d70 100644 --- a/ale_linters/ruby/reek.vim +++ b/ale_linters/ruby/reek.vim @@ -4,21 +4,27 @@ call ale#Set('ruby_reek_show_context', 0) call ale#Set('ruby_reek_show_wiki_link', 0) -function! ale_linters#ruby#reek#Handle(buffer, lines) abort - let l:output = [] +function! ale_linters#ruby#reek#VersionCheck(buffer) abort + " If we have previously stored the version number in a cache, then + " don't look it up again. + if ale#semver#HasVersion('reek') + " Returning an empty string skips this command. + return '' + endif - for l:error in ale#util#FuzzyJSONDecode(a:lines, []) - for l:location in l:error.lines - call add(l:output, { - \ 'lnum': l:location, - \ 'type': 'W', - \ 'text': s:BuildText(a:buffer, l:error), - \ 'code': l:error.smell_type, - \}) - endfor - endfor + return 'reek --version' +endfunction - return l:output +function! ale_linters#ruby#reek#GetCommand(buffer, version_output) abort + let l:version = ale#semver#GetVersion('reek', a:version_output) + + " Tell reek what the filename is if the version of reek is new enough. + let l:display_name_args = ale#semver#GTE(l:version, [5, 0, 0]) + \ ? ' --stdin-filename %s' + \ : '' + + return 'reek -f json --no-progress --no-color' + \ . l:display_name_args endfunction function! s:BuildText(buffer, error) abort @@ -37,9 +43,29 @@ function! s:BuildText(buffer, error) abort return join(l:parts, ' ') endfunction +function! ale_linters#ruby#reek#Handle(buffer, lines) abort + let l:output = [] + + for l:error in ale#util#FuzzyJSONDecode(a:lines, []) + for l:location in l:error.lines + call add(l:output, { + \ 'lnum': l:location, + \ 'type': 'W', + \ 'text': s:BuildText(a:buffer, l:error), + \ 'code': l:error.smell_type, + \}) + endfor + endfor + + return l:output +endfunction + call ale#linter#Define('ruby', { -\ 'name': 'reek', -\ 'executable': 'reek', -\ 'command': 'reek -f json --no-progress --no-color', +\ 'name': 'reek', +\ 'executable': 'reek', +\ 'command_chain': [ +\ {'callback': 'ale_linters#ruby#reek#VersionCheck'}, +\ {'callback': 'ale_linters#ruby#reek#GetCommand'}, +\ ], \ 'callback': 'ale_linters#ruby#reek#Handle', \}) diff --git a/test/command_callback/test_reek_command_callback.vader b/test/command_callback/test_reek_command_callback.vader new file mode 100644 index 00000000..41e749c1 --- /dev/null +++ b/test/command_callback/test_reek_command_callback.vader @@ -0,0 +1,34 @@ +Before: + runtime ale_linters/ruby/reek.vim + call ale#test#SetDirectory('/testplugin/test/command_callback') + +After: + call ale#test#RestoreDirectory() + call ale#linter#Reset() + call ale#semver#ResetVersionCache() + +Execute(The reek callbacks should return the correct default values): + AssertEqual + \ 'reek --version', + \ ale_linters#ruby#reek#VersionCheck(bufnr('')) + AssertEqual + \ 'reek -f json --no-progress --no-color --stdin-filename %s', + \ ale_linters#ruby#reek#GetCommand(bufnr(''), ['reek 5.0.0']) + + " Try with older versions. + call ale#semver#ResetVersionCache() + AssertEqual + \ 'reek -f json --no-progress --no-color', + \ ale_linters#ruby#reek#GetCommand(bufnr(''), ['reek 4.8.2']) + +Execute(The reek version check should be cached): + AssertEqual + \ 'reek -f json --no-progress --no-color --stdin-filename %s', + \ ale_linters#ruby#reek#GetCommand(bufnr(''), ['reek 5.0.0']) + + AssertEqual + \ '', + \ ale_linters#ruby#reek#VersionCheck(bufnr('')) + AssertEqual + \ 'reek -f json --no-progress --no-color --stdin-filename %s', + \ ale_linters#ruby#reek#GetCommand(bufnr(''), []) |