From aa29c91cdc9c5dc737e93c1f91080aa174363bef Mon Sep 17 00:00:00 2001 From: Eddie Lebow Date: Sun, 12 Nov 2017 00:53:02 -0500 Subject: [PATCH] [eruby] Add erubi linter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Erubi is yet another parser for eRuby. This is the default parser in Rails as of version 5.1. It supports some additional syntax with similar behavior to Rails' extensions to the language, though incompatible. Rails currently still recommends their own syntax, so GetCommand still has to do the translation introduced in https://github.com/w0rp/ale/pull/1114 . Erubi does not supply an executable—It is intended to be invoked only from within a Ruby program. In this case, a one-liner on the command line. --- README.md | 2 +- ale_linters/eruby/erubi.vim | 35 +++++++++++++++++++ doc/ale-eruby.txt | 12 +++---- doc/ale.txt | 2 +- .../test_erubi_command_callback.vader | 31 ++++++++++++++++ 5 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 ale_linters/eruby/erubi.vim create mode 100644 test/command_callback/test_erubi_command_callback.vader diff --git a/README.md b/README.md index 73827899..598c3cb3 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ formatting. | Dockerfile | [hadolint](https://github.com/lukasmartinelli/hadolint) | | Elixir | [credo](https://github.com/rrrene/credo), [dogma](https://github.com/lpil/dogma) !! | | Elm | [elm-format](https://github.com/avh4/elm-format), [elm-make](https://github.com/elm-lang/elm-make) | -| Erb | [erb](https://github.com/jeremyevans/erubi), [erubis](https://github.com/kwatch/erubis) | +| Erb | [erb](https://apidock.com/ruby/ERB), [erubi](https://github.com/jeremyevans/erubi), [erubis](https://github.com/kwatch/erubis) | | Erlang | [erlc](http://erlang.org/doc/man/erlc.html), [SyntaxErl](https://github.com/ten0s/syntaxerl) | | Fortran | [gcc](https://gcc.gnu.org/) | | FusionScript | [fusion-lint](https://github.com/RyanSquared/fusionscript) | diff --git a/ale_linters/eruby/erubi.vim b/ale_linters/eruby/erubi.vim new file mode 100644 index 00000000..6f2d3ac6 --- /dev/null +++ b/ale_linters/eruby/erubi.vim @@ -0,0 +1,35 @@ +" Author: Eddie Lebow https://github.com/elebow +" Description: eruby checker using `erubi` + +function! ale_linters#eruby#erubi#CheckErubi(buffer) abort + return 'ruby -r erubi/capture_end -e ' . ale#Escape('""') +endfunction + +function! ale_linters#eruby#erubi#GetCommand(buffer, check_erubi_output) abort + let l:rails_root = ale#ruby#FindRailsRoot(a:buffer) + + if (!empty(a:check_erubi_output)) + " The empty command in CheckErubi returns nothing if erubi runs and + " emits an error if erubi is not present + return '' + endif + + if empty(l:rails_root) + return 'ruby -r erubi/capture_end -e ' . ale#Escape('puts Erubi::CaptureEndEngine.new($stdin.read).src') . '< %t | ruby -c' + endif + + " Rails-flavored eRuby does not comply with the standard as understood by + " Erubi, so we'll have to do some substitution. This does not reduce the + " effectiveness of the linter---the translated code is still evaluated. + return 'ruby -r erubi/capture_end -e ' . ale#Escape('puts Erubi::CaptureEndEngine.new($stdin.read.gsub(%{<%=},%{<%}), nil, %{-}).src') . '< %t | ruby -c' +endfunction + +call ale#linter#Define('eruby', { +\ 'name': 'erubi', +\ 'executable': 'ruby', +\ 'command_chain': [ +\ {'callback': 'ale_linters#eruby#erubi#CheckErubi'}, +\ {'callback': 'ale_linters#eruby#erubi#GetCommand', 'output_stream': 'stderr'}, +\ ], +\ 'callback': 'ale#handlers#ruby#HandleSyntaxErrors', +\}) diff --git a/doc/ale-eruby.txt b/doc/ale-eruby.txt index bfbe9ad2..a0f6f4f8 100644 --- a/doc/ale-eruby.txt +++ b/doc/ale-eruby.txt @@ -1,17 +1,15 @@ =============================================================================== ALE Eruby Integration *ale-eruby-options* -There are two linters for `eruby` files: +There are three linters for `eruby` files: - `erb` - `erubis` +- `erubi` -If you don't know which one your project uses, it's probably `erb`. -To selectively enable one or the other, see |g:ale_linters|. - -(Note that ALE already disables linters if the executable for that linter is -not found; thus, there's probably no need to disable one of these if you're -using the other one.) +`erb` is in the Ruby standard library and is mostly universal. `erubis` is the +default parser in Rails between 3.0 and 5.1. `erubi` is the default in Rails +5.1 and later. To selectively enable a subset, see |g:ale_linters|. =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 63991113..be577688 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -286,7 +286,7 @@ Notes: * Dockerfile: `hadolint` * Elixir: `credo`, `dogma`!! * Elm: `elm-format, elm-make` -* Erb: `erb`, `erubis` +* Erb: `erb`, `erubi`, `erubis` * Erlang: `erlc`, `SyntaxErl` * Fortran: `gcc` * FusionScript: `fusion-lint` diff --git a/test/command_callback/test_erubi_command_callback.vader b/test/command_callback/test_erubi_command_callback.vader new file mode 100644 index 00000000..1953d763 --- /dev/null +++ b/test/command_callback/test_erubi_command_callback.vader @@ -0,0 +1,31 @@ +Before: + runtime ale_linters/eruby/erubi.vim + call ale#test#SetDirectory('/testplugin/test/command_callback') + +After: + call ale#linter#Reset() + call ale#test#RestoreDirectory() + +Execute(Executable should not contain any filter code by default): + call ale#test#SetFilename('../ruby_fixtures/not_a_rails_app/file.rb') + + AssertEqual + \ 'ruby -r erubi/capture_end -e ' . ale#Escape('puts Erubi::CaptureEndEngine.new($stdin.read).src') . '< %t | ruby -c', + \ ale_linters#eruby#erubi#GetCommand(bufnr(''), []) + +Execute(Executable should filter invalid eRuby when inside a Rails project): + call ale#test#SetFilename('../ruby_fixtures/valid_rails_app/app/views/my_great_view.html.erb') + + AssertEqual + \ 'ruby -r erubi/capture_end -e ' . ale#Escape('puts Erubi::CaptureEndEngine.new($stdin.read.gsub(%{<%=},%{<%}), nil, %{-}).src') . '< %t | ruby -c', + \ ale_linters#eruby#erubi#GetCommand(bufnr(''), []) + +Execute(Command should be blank if the first command in the chain return output): + let output_lines = [ + \ "/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- erubi/capture_end (LoadError)", + \ " from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'", + \] + + AssertEqual + \ '', + \ ale_linters#eruby#erubi#GetCommand(bufnr(''), output_lines)