cypher / git-ruby-syntax-check

Checks the syntax of all changed ruby files is valid before allowing a commit
MIT License
70 stars 16 forks source link

ERB syntax checking fails for Rails 3 #2

Closed benzado closed 12 years ago

benzado commented 12 years ago

Rails 3 uses a variant of ERB that is not 100% compatible with the version that ships with Ruby. Specifically, this construct

<%= form_for :thing do |f| %>

results in errors under the erb tool but is OK under Rails.

More http://timelessrepo.com/block-helpers-in-rails3

As a result, the pre-commit hook finds errors where there are none.

cypher commented 12 years ago

Not sure how to fix this. Detect if erubis is installed, and then use that to validate the not-really-ERB?

benzado commented 12 years ago

Checking for erubis isn't enough; apparently Rails 3 is using a customized version of erubis.

A dude named Zach Powell pointed out on Stack Overflow that if you just replace all instances of <%= with <% then the problems go away, and the resulting code is equivalent as far as the syntax checker is concerned. That would mean replacing line 32 with

"sed 's/<%=/<%/g' #{file} | erb -xT - | #{compiler_ruby} -wc"

My first impulse was to make this an option, using git config, but considering it does no harm for non-Rails-3 ERB, maybe it should just be the default behavior.

benzado commented 12 years ago

Turns out the above hack (replacing <%= with <%) isn't harmless; expressions like <%= value %> become <% value %> and result in the warning "possibly useless use of a variable in void context."

As a workaround I'm removing -w from the command line; merely doing a plain syntax check.

cypher commented 12 years ago

I think checking for erubis is probably the way to go. I've implemented a completely untested version of this in 1c3e09f, if you'd like to try it out.

benzado commented 12 years ago

Cool! It (mostly) works. I'm submitting a change request with my further patches.