How can I syntax check (not render) a Rails 3 ERB template file?

benzado picture benzado · Mar 14, 2012 · Viewed 7.3k times · Source

I'm trying to have a git pre-commit hook perform a syntax check on all Ruby code; there is one on GitHub at https://github.com/cypher/git-ruby-syntax-check.

It attempts to check .erb files by erb -x to translate them into Ruby code and then passes the output to ruby -c for syntax checking. Unfortunately, Rails 3 introduced a custom ERB parser that is incompatible with Ruby's standard ERB, and so the pre-commit hook is finding errors where there are none.

Is there some equivalent to erb -x that will output Ruby code from a Rails 3 ERB file?

Answer

ScottJShea picture ScottJShea · Mar 14, 2012

I have not dug much into either of these but you might try rails-erb-check (Git project) or this blog entry. I agree with shingara but the Blog Post describes a situation where this is useful and I wonder if you are in a similar position:

Diaspora is pretty fluid right now. This means we are have some green tests, some missing tests, and other tests that check intent (not implementation). In an ideal world, I suppose test cases would cover all of our bases...

Until then, I've added a new task to my fork, check_syntax:all. This breaks down further to the subtasks check_syntax:erb, check_syntax:haml, check_syntax:haml_ruby, check_syntax:sass, and check_syntax:yaml.