summaryrefslogtreecommitdiff
path: root/ale_linters/ruby
diff options
context:
space:
mode:
authorEddie Lebow <elebow@users.noreply.github.com>2017-07-12 05:43:47 -0400
committerw0rp <w0rp@users.noreply.github.com>2017-07-12 10:43:47 +0100
commitbc32e24203159945dcf3906a5e08261ccb06e065 (patch)
treeef1ddaefe8aee1a7ca37c156a11c447753dee17f /ale_linters/ruby
parent400580e4e8fc330166658c689d1abf05db38ab3b (diff)
downloadale-bc32e24203159945dcf3906a5e08261ccb06e065.zip
Add rails_best_practices handler (resolves #655) (#751)
* Move FindRailsRoot() to more general location * Add rails_best_practices handler (resolves #655) * Update documentation for rails_best_practices Also add brakeman to *ale* documentation. * rails_best_practices: allow overriding the executable * rails_best_practices: format help correctly * rails_best_practices: capture tool output on Windows
Diffstat (limited to 'ale_linters/ruby')
-rw-r--r--ale_linters/ruby/brakeman.vim24
-rw-r--r--ale_linters/ruby/rails_best_practices.vim59
2 files changed, 61 insertions, 22 deletions
diff --git a/ale_linters/ruby/brakeman.vim b/ale_linters/ruby/brakeman.vim
index 5ea531f1..269c18f0 100644
--- a/ale_linters/ruby/brakeman.vim
+++ b/ale_linters/ruby/brakeman.vim
@@ -15,7 +15,7 @@ function! ale_linters#ruby#brakeman#Handle(buffer, lines) abort
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:rails_root = ale#ruby#FindRailsRoot(a:buffer)
let l:warning_file = l:rails_root . '/' . l:warning.file
if !ale#path#IsBufferPath(a:buffer, l:warning_file)
@@ -36,7 +36,7 @@ function! ale_linters#ruby#brakeman#Handle(buffer, lines) abort
endfunction
function! ale_linters#ruby#brakeman#GetCommand(buffer) abort
- let l:rails_root = s:FindRailsRoot(a:buffer)
+ let l:rails_root = ale#ruby#FindRailsRoot(a:buffer)
if l:rails_root ==? ''
return ''
@@ -47,26 +47,6 @@ function! ale_linters#ruby#brakeman#GetCommand(buffer) abort
\ . ' -p ' . ale#Escape(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',
diff --git a/ale_linters/ruby/rails_best_practices.vim b/ale_linters/ruby/rails_best_practices.vim
new file mode 100644
index 00000000..a3d9f7a2
--- /dev/null
+++ b/ale_linters/ruby/rails_best_practices.vim
@@ -0,0 +1,59 @@
+" Author: Eddie Lebow https://github.com/elebow
+" Description: rails_best_practices, a code metric tool for rails projects
+
+let g:ale_ruby_rails_best_practices_options =
+\ get(g:, 'ale_ruby_rails_best_practices_options', '')
+
+function! ale_linters#ruby#rails_best_practices#Handle(buffer, lines) abort
+ if len(a:lines) == 0
+ return []
+ endif
+
+ let l:result = json_decode(join(a:lines, ''))
+
+ let l:output = []
+
+ for l:warning in l:result
+ if !ale#path#IsBufferPath(a:buffer, l:warning.filename)
+ continue
+ endif
+
+ call add(l:output, {
+ \ 'lnum': l:warning.line_number + 0,
+ \ 'type': 'W',
+ \ 'text': l:warning.message,
+ \})
+ endfor
+
+ return l:output
+endfunction
+
+function! ale_linters#ruby#rails_best_practices#GetCommand(buffer) abort
+ let l:executable = ale#handlers#rails_best_practices#GetExecutable(a:buffer)
+ let l:exec_args = l:executable =~? 'bundle$'
+ \ ? ' exec rails_best_practices'
+ \ : ''
+
+ let l:rails_root = ale#ruby#FindRailsRoot(a:buffer)
+
+ if l:rails_root ==? ''
+ return ''
+ endif
+
+ let l:output_file = ale#Has('win32') ? '%t ' : '/dev/stdout '
+ let l:cat_file = ale#Has('win32') ? '; type %t' : ''
+
+ return ale#Escape(l:executable) . l:exec_args
+ \ . ' --silent -f json --output-file ' . l:output_file
+ \ . ale#Var(a:buffer, 'ruby_rails_best_practices_options')
+ \ . ale#Escape(l:rails_root)
+ \ . l:cat_file
+endfunction
+
+call ale#linter#Define('ruby', {
+\ 'name': 'rails_best_practices',
+\ 'executable_callback': 'ale#handlers#rails_best_practices#GetExecutable',
+\ 'command_callback': 'ale_linters#ruby#rails_best_practices#GetCommand',
+\ 'callback': 'ale_linters#ruby#rails_best_practices#Handle',
+\ 'lint_file': 1,
+\})