一個Ruby靜態代碼分析器:rubocop
這是一個Ruby靜態代碼分析器,基于社區Ruby編碼風格指南Ruby Style Guide。除了報告代碼中的問題,RuboCop還可以自動為你修復一些問題。
安裝
RuboCop的安裝是非常標準:
$ gem install rubocop
If you'd rather install RuboCop usingbundler, don't require it in yourGemfile:
gem 'rubocop', require: false
基本用法
不帶參數運行rubocop會檢查當前目錄下的所有Ruby源文件:
$ rubocop
另外,您可以通過rubocop指定文件和目錄進行檢查:
$ rubocop app spec lib/something.rb
Here's RuboCop in action. Consider the following Ruby source code:
def badName if something test end end
Running RuboCop on it (assuming it's in a file namedtest.rb) would produce the following report:
Inspecting 1 file WOffenses:
test.rb:1:5: C: Use snake_case for method names. def badName ^^^^^^^ test.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression. if something ^^ test.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. if something ^^ test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2 end ^^^
1 file inspected, 4 offenses detected</pre>
For more details check the available command-line options:
$ rubocop -h
Command flag | Description | </tr> </tbody>|||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
-v/--version | Displays the current version and exits. | </tr>|||||||||||||||||||||||||
-V/--verbose-version | Displays the current version plus the version of Parser and Ruby. | </tr>|||||||||||||||||||||||||
-F/--fail-fast | Inspects in modification time order and stops after first file with offenses. | </tr>|||||||||||||||||||||||||
-d/--debug | Displays some extra debug output. | </tr>|||||||||||||||||||||||||
-D/--display-cop-names | Displays cop names in offense messages. | </tr>|||||||||||||||||||||||||
-c/--config | Run with specified config file. | </tr>|||||||||||||||||||||||||
-f/--format | Choose a formatter. | </tr>|||||||||||||||||||||||||
-o/--out | Write output to a file instead of STDOUT. | </tr>|||||||||||||||||||||||||
-r/--require | Require Ruby file (see Loading Extensions). | </tr>|||||||||||||||||||||||||
-R/--rails | Run extra Rails cops. | </tr>|||||||||||||||||||||||||
-l/--lint | Run only lint cops. | </tr>|||||||||||||||||||||||||
-a/--auto-correct | Auto-correct certain offenses. Note: Experimental - use with caution. | </tr>|||||||||||||||||||||||||
--only | Run only the specified cop(s) and/or cops in the specified departments. | </tr>|||||||||||||||||||||||||
--except | Run all cops enabled by configuration except the specified cop(s) and/or departments. | </tr>|||||||||||||||||||||||||
--auto-gen-config | Generate a configuration file acting as a TODO list. | </tr>|||||||||||||||||||||||||
--show-cops | Shows available cops and their configuration. | </tr>|||||||||||||||||||||||||
--fail-level | Minimum severity for exit with error code. Full severity name or upper case initial can be given. Normally, auto-corrected offenses are ignored. UseAorautocorrectif you'd like them to trigger failure. | </tr> </tbody> </table>