diff options
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/erblint.vim | 40 | ||||
-rw-r--r-- | test/fixers/test_erblint_fixer_callback.vader | 55 | ||||
-rw-r--r-- | test/test-files/eruby/dummy.html.erb | 0 |
4 files changed, 100 insertions, 0 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 0447bd3e..c6764daf 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -100,6 +100,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['nim'], \ 'description': 'Apply nimpretty to a file.', \ }, +\ 'erblint': { +\ 'function': 'ale#fixers#erblint#Fix', +\ 'suggested_filetypes': ['eruby'], +\ 'description': 'Apply erblint --autocorrect to a file.', +\ }, \ 'eslint': { \ 'function': 'ale#fixers#eslint#Fix', \ 'suggested_filetypes': ['javascript', 'typescript'], diff --git a/autoload/ale/fixers/erblint.vim b/autoload/ale/fixers/erblint.vim new file mode 100644 index 00000000..41aca0c8 --- /dev/null +++ b/autoload/ale/fixers/erblint.vim @@ -0,0 +1,40 @@ +" Author: Roeland Moors - https://github.com/roelandmoors +" Description: ERB Lint, support for https://github.com/Shopify/erb-lint + +call ale#Set('eruby_erblint_executable', 'erblint') +call ale#Set('eruby_erblint_options', '') + + +" Erblint fixer outputs diagnostics first and then the fixed +" output. These are delimited by something like this: +" ================ /path/to/demo.html.erb ================== +" We only need the output after this +function! ale#fixers#erblint#PostProcess(buffer, output) abort + let l:line = 0 + + for l:output in a:output + let l:line = l:line + 1 + + if l:output =~# "^=\\+.*=\\+$" + break + endif + endfor + + return a:output[l:line :] +endfunction + +function! ale#fixers#erblint#GetCommand(buffer) abort + let l:executable = ale#Var(a:buffer, 'eruby_erblint_executable') + let l:options = ale#Var(a:buffer, 'eruby_erblint_options') + + return ale#ruby#EscapeExecutable(l:executable, 'erblint') + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' --autocorrect --stdin %s' +endfunction + +function! ale#fixers#erblint#Fix(buffer) abort + return { + \ 'command': ale#fixers#erblint#GetCommand(a:buffer), + \ 'process_with': 'ale#fixers#erblint#PostProcess' + \} +endfunction diff --git a/test/fixers/test_erblint_fixer_callback.vader b/test/fixers/test_erblint_fixer_callback.vader new file mode 100644 index 00000000..7b56e3a9 --- /dev/null +++ b/test/fixers/test_erblint_fixer_callback.vader @@ -0,0 +1,55 @@ +Before: + Save g:ale_eruby_erblint_executable + Save g:ale_eruby_erblint_options + + " Use an invalid global executable, so we don't match it. + let g:ale_eruby_erblint_executable = 'xxxinvalid' + let g:ale_eruby_erblint_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The erblint callback should return the correct default values): + call ale#test#SetFilename('../test-files/eruby/dummy.html.erb') + + AssertEqual + \ { + \ 'process_with': 'ale#fixers#erblint#PostProcess', + \ 'command': ale#Escape(g:ale_eruby_erblint_executable) + \ . ' --autocorrect --stdin %s', + \ }, + \ ale#fixers#erblint#Fix(bufnr('')) + +Execute(The erblint callback should include custom erblint options): + let g:ale_eruby_erblint_options = '--lint-all' + call ale#test#SetFilename('../test-files/ruby/with_config/dummy.rb') + + AssertEqual + \ { + \ 'process_with': 'ale#fixers#erblint#PostProcess', + \ 'command': ale#Escape(g:ale_eruby_erblint_executable) + \ . ' --lint-all' + \ . ' --autocorrect --stdin %s', + \ }, + \ ale#fixers#erblint#Fix(bufnr('')) + +Execute(The erblint post-processor should remove diagnostics content): + AssertEqual + \ [ + \ '<div>', + \ '', + \ '</div>', + \ ], + \ ale#fixers#erblint#PostProcess(bufnr(''), [ + \ 'Linting 1 files with 11 autocorrectable linters...', + \ '', + \ '1 error(s) corrected in ERB files', + \ '================ /home/user/demo.html.erb ==================', + \ '<div>', + \ '', + \ '</div>', + \ ]) diff --git a/test/test-files/eruby/dummy.html.erb b/test/test-files/eruby/dummy.html.erb new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/test-files/eruby/dummy.html.erb |