Skip to content

Commit

Permalink
Add Elvis handler for Erlang
Browse files Browse the repository at this point in the history
[Elvis][1] is an Erlang style reviewer.

[1]: https://github.com/inaka/elvis
  • Loading branch information
dmitrivereshchagin authored and motato1 committed Dec 29, 2020
1 parent 5daa59b commit cf91a6b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
39 changes: 39 additions & 0 deletions ale_linters/erlang/elvis.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
" Author: Dmitri Vereshchagin <dmitri.vereshchagin@gmail.com>
" Description: Elvis linter for Erlang files

call ale#Set('erlang_elvis_executable', 'elvis')

function! ale_linters#erlang#elvis#Handle(buffer, lines) abort
let l:pattern = '\v:(\d+):[^:]+:(.+)'
let l:loclist = []

for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:loclist, {
\ 'lnum': str2nr(l:match[1]),
\ 'text': s:AbbreviateMessage(l:match[2]),
\ 'type': 'W',
\})
endfor

return l:loclist
endfunction

function! s:AbbreviateMessage(text) abort
let l:pattern = '\v\c^(line \d+ is too long):.*$'

return substitute(a:text, l:pattern, '\1.', '')
endfunction

function! s:GetCommand(buffer) abort
let l:file = ale#Escape(expand('#' . a:buffer . ':.'))

return '%e rock --output-format=parsable ' . l:file
endfunction

call ale#linter#Define('erlang', {
\ 'name': 'elvis',
\ 'callback': 'ale_linters#erlang#elvis#Handle',
\ 'executable': {b -> ale#Var(b, 'erlang_elvis_executable')},
\ 'command': function('s:GetCommand'),
\ 'lint_file': 1,
\})
12 changes: 12 additions & 0 deletions doc/ale-erlang.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ g:ale_erlang_dialyzer_rebar3_profile *g:ale_erlang_dialyzer_rebar3_profile*
This variable can be changed to specify the profile that is used to
run dialyzer with rebar3.


-------------------------------------------------------------------------------
elvis *ale-erlang-elvis*

g:ale_erlang_elvis_executable *g:ale_erlang_elvis_executable*
*b:ale_erlang_elvis_executable*
Type: |String|
Default: `'elvis'`

This variable can be changed to specify the elvis executable.


-------------------------------------------------------------------------------
erlc *ale-erlang-erlc*

Expand Down
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Notes:
* `erubis`
* `ruumba`
* Erlang
* `elvis`!!
* `erlc`
* `SyntaxErl`
* Fish
Expand Down
1 change: 1 addition & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,7 @@ documented in additional help files.
elm-make..............................|ale-elm-elm-make|
erlang..................................|ale-erlang-options|
dialyzer..............................|ale-erlang-dialyzer|
elvis.................................|ale-erlang-elvis|
erlc..................................|ale-erlang-erlc|
syntaxerl.............................|ale-erlang-syntaxerl|
eruby...................................|ale-eruby-options|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ formatting.
* [erubis](https://github.com/kwatch/erubis)
* [ruumba](https://github.com/ericqweinstein/ruumba)
* Erlang
* [elvis](https://github.com/inaka/elvis) :floppy_disk:
* [erlc](http://erlang.org/doc/man/erlc.html)
* [SyntaxErl](https://github.com/ten0s/syntaxerl)
* Fish
Expand Down
16 changes: 16 additions & 0 deletions test/command_callback/test_erlang_elvis_command_callback.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Before:
let b:file = fnamemodify(bufname(''), ':.')
call ale#assert#SetUpLinterTest('erlang', 'elvis')

After:
call ale#assert#TearDownLinterTest()

Execute(Default command should be correct):
AssertLinter 'elvis',
\ ale#Escape('elvis') . ' rock --output-format=parsable ' . ale#Escape(b:file)

Execute(Executable should be configurable):
let b:ale_erlang_elvis_executable = '/path/to/elvis'

AssertLinter '/path/to/elvis',
\ ale#Escape('/path/to/elvis') . ' rock --output-format=parsable ' . ale#Escape(b:file)
37 changes: 37 additions & 0 deletions test/handler/test_erlang_elvis_handler.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Before:
runtime ale_linters/erlang/elvis.vim

After:
call ale#linter#Reset()

Execute(Warning messages should be handled):
AssertEqual
\ [
\ {
\ 'lnum': 11,
\ 'text': "Replace the 'if' expression on line 11 with a 'case' expression or function clauses.",
\ 'type': 'W',
\ },
\ {
\ 'lnum': 20,
\ 'text': 'Remove the debug call to io:format/1 on line 20.',
\ 'type': 'W',
\ },
\ ],
\ ale_linters#erlang#elvis#Handle(bufnr(''), [
\ "src/foo.erl:11:no_if_expression:Replace the 'if' expression on line 11 with a 'case' expression or function clauses.",
\ 'src/foo.erl:20:no_debug_call:Remove the debug call to io:format/1 on line 20.',
\ ])

Execute(Line length message shouldn't contain the line itself):
AssertEqual
\ [
\ {
\ 'lnum': 24,
\ 'text': 'Line 24 is too long.',
\ 'type': 'W',
\ },
\ ],
\ ale_linters#erlang#elvis#Handle(bufnr(''), [
\ 'src/foo.erl:24:line_length:Line 24 is too long: io:format("Look ma, too long!"),.',
\ ])

0 comments on commit cf91a6b

Please sign in to comment.