diff options
author | Eddie Lebow <elebow@users.noreply.github.com> | 2017-05-05 05:05:53 -0400 |
---|---|---|
committer | w0rp <w0rp@users.noreply.github.com> | 2017-05-05 10:05:53 +0100 |
commit | ba7999dae093c2c9b9f924c9bff8fb9fdea167fc (patch) | |
tree | 59655bdc243e1c0bc7ccb2c47153b07aad5b4192 /ale_linters | |
parent | 14f3fc777fc4a5e1fd1e1a7b75f4edf84b0a9af0 (diff) | |
download | ale-ba7999dae093c2c9b9f924c9bff8fb9fdea167fc.zip |
[RFC] Add Brakeman for Ruby on Rails (references #385) (#509)
* Add brakeman for Ruby on Rails
Diffstat (limited to 'ale_linters')
-rw-r--r-- | ale_linters/ruby/brakeman.vim | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/ale_linters/ruby/brakeman.vim b/ale_linters/ruby/brakeman.vim new file mode 100644 index 00000000..3cc5b77d --- /dev/null +++ b/ale_linters/ruby/brakeman.vim @@ -0,0 +1,72 @@ +" Author: Eddie Lebow https://github.com/elebow +" Description: Brakeman, a static analyzer for Rails security + +let g:ale_ruby_brakeman_options = +\ get(g:, 'ale_ruby_brakeman_options', '') + +function! ale_linters#ruby#brakeman#Handle(buffer, lines) abort + let l:result = json_decode(join(a:lines, '')) + + let l:output = [] + + for l:warning in l:result.warnings + " Brakeman always outputs paths relative to the Rails app root + let l:rails_root = s:FindRailsRoot(a:buffer) + let l:warning_file = l:rails_root . '/' . l:warning.file + + if !ale#path#IsBufferPath(a:buffer, l:warning_file) + continue + endif + + let l:text = l:warning.warning_type . ' ' . l:warning.message . ' (' . l:warning.confidence . ')' + let l:line = l:warning.line != v:null ? l:warning.line : 1 + + call add(l:output, { + \ 'lnum': l:line, + \ 'type': 'W', + \ 'text': l:text, + \}) + endfor + + return l:output +endfunction + +function! ale_linters#ruby#brakeman#GetCommand(buffer) abort + let l:rails_root = s:FindRailsRoot(a:buffer) + + if l:rails_root ==? '' + return '' + endif + + return 'brakeman -f json -q ' + \ . ale#Var(a:buffer, 'ruby_brakeman_options') + \ . ' -p ' . l:rails_root +endfunction + +function! s:FindRailsRoot(buffer) abort + " Find the nearest dir contining "app", "db", and "config", and assume it is + " the root of a Rails app. + for l:name in ['app', 'config', 'db'] + let l:dir = fnamemodify( + \ ale#path#FindNearestDirectory(a:buffer, l:name), + \ ':h:h' + \) + + if l:dir !=# '.' + \&& isdirectory(l:dir . '/app') + \&& isdirectory(l:dir . '/config') + \&& isdirectory(l:dir . '/db') + return l:dir + endif + endfor + + return '' +endfunction + +call ale#linter#Define('ruby', { +\ 'name': 'brakeman', +\ 'executable': 'brakeman', +\ 'command_callback': 'ale_linters#ruby#brakeman#GetCommand', +\ 'callback': 'ale_linters#ruby#brakeman#Handle', +\ 'lint_file': 1, +\}) |