Is Rubocop an add-on to the "ruby -c" syntax?

We had a test that found every Ruby file in our application and ran ruby -c

. We introduced Rubocop and made it check the same list of files.

Is the test that was running ruby -c

now useless, or is there an example of a failure mode that would have been caught ruby -c

but not by Rubocop?

The documentation for ruby -c

states:

Forces Ruby to check the script syntax and exit without executing. If there are no syntax errors, Ruby will print "Syntax OK" to standard output.

This is an example of a syntax problem to be caught by either:

% echo "puts 'hello world" > hot_script.rb
% ruby -c hot_script.rb
hot_script.rb:1: unterminated string meets end of file

% rubocop hot_script.rb
Inspecting 1 file
F

Offenses:

hot_script.rb:1:6: F: unterminated string meets end of file
(Using Ruby 1.9 parser; configure using TargetRubyVersion parameter, under AllCops)
puts 'hello world
     ^

1 file inspected, 1 offense detected

      

Rubocop even catches some of the same warnings, although I haven't had ruby -c

one to catch them before, and so I'm more interested in errors. Here's an example of relative parity when handling a warning:

% cat unused_var.rb
def hot_method
  a = 1
  b = 2
  puts b
end

% ruby -cwW2 unused_var.rb
unused_var.rb:2: warning: assigned but unused variable - a
Syntax OK

% rubocop unused_var.rb
Inspecting 1 file
W

Offenses:

unused_var.rb:2:3: W: Lint/UselessAssignment: Useless assignment to variable - a.
  a = 1
  ^

1 file inspected, 1 offense detected

      

I searched with

but I may be doing it wrong. The test was slower in Ruby 1.9 than it was in Ruby 1.8, so the answer to this question is really valuable to me. And you have to admit that you are curious, right?

+3


source to share


2 answers


The answer is "most of the time." RuboCop is based on a parser, which is a standalone Ruby parser that mimics, more or less, an MRI parser. RuboCop wraps the parser's syntax checks and reports problems correctly. However, as stated in the GitHub parser:

Unfortunately, MRI often changes syntax in patch level versions [...] there is no easy way to track these changes.

This policy makes it nearly impossible to make Parser exactly compatible with the Ruby MRI parser.



In addition, the parser supports the latest minor version of whatever release you are using and does not support minor versions. Therefore, if you are using Ruby 2.4.0, RuboCop will use the version of the parser that supports the 2.4.1 syntax.

For all intents and purposes, the parser is equivalent to the official MRI parser, and unless you have a specific reason to use both methods, using RuboCop should suffice.

+3


source


Rubocop also identifies and reports syntax errors, as the code cannot be parsed correctly if it does, so neither is necessary.



+2


source







All Articles