Skip to content

Instantly share code, notes, and snippets.

@billhorsman
Last active December 18, 2015 17:29
Show Gist options
  • Save billhorsman/5818994 to your computer and use it in GitHub Desktop.
Save billhorsman/5818994 to your computer and use it in GitHub Desktop.
Use ruby_parser gem to list all ruby files that produce an error. I'm using this to highlight Ruby files that might not be parsed successfully by Code Climate.
namespace :code do
desc "Parse with ruby_parser and list files producing errors"
task :parse do
bad = []
Dir.glob("**/*.rb").each do |filename|
if system("ruby_parse_extract_error #{filename} > /dev/null 2>&1")
print "."
else
bad << filename
print "x"
end
STDOUT.flush
end
puts
puts "Found #{bad.size} bad file(s):"
bad.each do |filename|
puts " #{filename}"
end
end
end
@billhorsman
Copy link
Author

As @zenspider says, it's better to use ruby_parse_extract_error rather than rely on the output of ruby_parse. Note that with ruby_parse_extract_error the exit code will indicate success but with ruby_parse it returns success regardless of how well it was parsed.

I am using this taks because my project is in Ruby 2 but Code Climate only supports Ruby 1.9.3 at the moment (they are working on it). I'm happy to forego Ruby 2 syntax in favour of awesome reports from Code Climate about how awful my code is :)

As @codeclimate says, this task will indicate any parse failures, not just ones relating to Ruby 2 syntax in Ruby 1.9.3. Hopefully your test suite spots the other failures though... I guess this is like the simplest, most lenient test suite you could have :)

Finally, don't assume that ruby_parse_extract_error will always fail to parse Ruby 2 syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment