Railsã¡ã¢(22) : RuboCopã§éçã³ã¼ã解æãè¡ã
RuboCopã¨ããgemã使ç¨ããã¨éçã³ã¼ã解æãè¡ããããã«ãªãï¼READMEã®åé ã«ããã³ããã®ã»ãªããæ¸ãã¦ããããã«RoboCopã¨Rubyãããã¦ããã®ãï¼ã
ãã¼ã¹ã«ãªã£ã¦ããã¹ã¿ã¤ã«ã¬ã¤ãã¯ä¸è¨ãã¼ã¸ï¼æ¥æ¬èªè¨³ï¼ãã確èªã§ããã
RuboCopã®ã¤ã³ã¹ãã¼ã«
Gemfile
ã«ä¸è¨è¡ã追å ãã¦bundle install
ããã
ããã§rubocop
ã³ãã³ãã使ããããã«ãªãã¤ã³ã¹ãã¼ã«ã¯å®äºã
group :development do gem 'rubocop', require: false end
RuboCopã®ä½¿ãæ¹
ã¨ããããã¢ããªã®ãã£ã¬ã¯ããªã«ç§»åãã¦ä½ãèããã«rubocop
ã³ãã³ããå®è¡ãã¦ã¿ãã¨ãã«ã¬ã³ããã£ã¬ã¯ããªä»¥ä¸ã®ãã¡ã¤ã«ã«å¯¾ããéçã³ã¼ã解æã®çµæãåºåãããã
çµæã«ããã¨45ãã¡ã¤ã«ããã§ãã¯ãã¦167ã®éåãè¦ã¤ãã£ãããã ãååã«æ¸ãã¦ããC
ã¨ãW
ã¨ãã¯åãã¡ã¤ã«ã«å¯¾ããçµæã§ãããããä¸è¨å
容ã示ãã¦ããF
ãæãæ·±å»ã
ï¼ã¤ãã¤çµæã確èªãã¦ããã°ããã®ã ãããæ°ãå¤ããèªåã§çæãããã³ã¼ãã対象ã«ãã¦ãããã許容ãã¦ãããã¹ã¿ã¤ã«ãããã®ã§è¨å®ãå¤æ´ãã¦ã¿ãã
æå | æå³ |
. | Cleanï¼åé¡ãªãï¼ |
C | Conventionï¼æ £ç¿éåï¼ |
W | Warning |
E | Error |
F | Fatal |
$ rubocop Inspecting 45 files .CCCCCCCCCCCCCCWWW.CCCCWWCCC..CCCC.C.CC.C.C.C Offenses: Gemfile:3:1: C: Extra blank line detected. Gemfile:47:81: C: Line is too long. [83/80] ãâ¦çç¥â¦ config/routes.rb:9:81: C: Line is too long. [84/80] # The priority is based upon order of creation: first created -> highest priority. ^^^^ config/routes.rb:21:81: C: Line is too long. [81/80] # Example resource route (maps HTTP verbs to controller actions automatically): ^ 45 files inspected, 167 offenses detected
RuboCopã®è¨å®
RuboCopã®è¨å®ã¯ã¢ããªã®ãã£ã¬ã¯ããªã«.rubocop.yml
ã¨ãããã¡ã¤ã«åã§è¨è¿°ããã
ã²ã¨ã¾ãä»åã¯æ°ãå¤ãã¦ç¡è¦ãã¦ãããããªãã®ãè¨å®ãã¦åå®è¡ãã¦ã¿ãã
ï¼ãªããããã©ã«ãã®è¨å®ã¯ä¸è¨ãªã³ã¯ãåç
§ï¼
AllCops: # é¤å¤ãããã£ã¬ã¯ããªï¼èªåçæããããã¡ã¤ã«ï¼ # ããã©ã«ãè¨å®ã«ãã"vendor/**/*"ãç¡å¹åãããªãããã«è¨è¿° Exclude: - bin/* - db/schema.rb - db/migrate/* - vendor/**/* # Railsåãã®Rails copsãå®è¡ã"rubocop -R"ã¨åã RunRailsCops: true # "Missing top-level class documentation comment."ãç¡å¹ Style/Documentation: Enabled: false # "Prefer single-quoted strings when you don't need string interpolation or special symbols."ãç¡å¹ Style/StringLiterals: Enabled: false # "Line is too long"ãç¡å¹ Metrics/LineLength: Enabled: false
$ rubocop ã⦠35 files inspected, 17 offenses detected
éåã17ã«ã¾ã§æ¸ã£ãã®ã§å¾ã¯1ã¤ãã¤ãã§ãã¯ãã¦ã¿ãã
RuboCopã®éåå 容ã®ä¾
éåå 容ã®ä¾ã¨ãã¦ã¯ä»¥ä¸ã®ãããªãã®ãããã
C: Extra blank line detected.
ä½åãªç©ºç½è¡ãããã
C: 1 trailing blank lines detected.
ãã¡ã¤ã«æ«å°¾ã«ä½åãªç©ºç½è¡ãããã
C: Do not use parentheses for method calls with no arguments. f.close() ^
å¼æ°ããªãã¡ã½ããã§ã¯()ã¯ä½¿ç¨ããªãã
C: Missing space after #.
ã³ã¡ã³ãã®#æåã®å¾ã«ç©ºç½ããªãã
C: Keep a blank line before and after private.
"private"ã®åå¾ã«ã¯ç©ºç½è¡ãå ¥ããã
C: Prefer each over for.
"for"ãã"each"ã¡ã½ããã®æ¹ãããã
ãªã©ãªã©â¦
ä¸éãä¿®æ£ãã¦rubocop
ãå®è¡ããã¨ä»¥ä¸ã®ããã«éåã0ã«ãªã£ãã
RuboCopã®ãããã§Rubyãããæ¸ãæ¹ãããã£ããããã®ã§ä¾¿å©ã ããç´°ããããã¨æããã¨ãããã®ã§ãã®å ´åã¯è¨å®ã§ç¡å¹åãããªã©ããã°ããã
$ rubocop Inspecting 35 files ................................... 35 files inspected, no offenses detected
.rubocop_todo.yml
ããã¢ããªéçºã®éä¸ã§RuboCopã追å ãããªã©ãã¦éåã®æ°ã極端ã«å¤ãå ´åã¯.rubocop_todo.yml
ã使ãã®ããããããã
ã¾ãã--auto-gen-config
ãªãã·ã§ã³ãæå®ãã¦.rubocop_todo.yml
ãçæããï¼ä¾ã¯éåã1ã¤ã ãï¼ã
$ rubocop --auto-gen-config Inspecting 35 files ..................................C Offenses: config/routes.rb:3:3: C: Missing space after #. #get 'artists/show' ^^^^^^^^^^^^^^^^^^^ 35 files inspected, 1 offense detected Created .rubocop_todo.yml. Run `rubocop --config .rubocop_todo.yml`, or add inherit_from: .rubocop_todo.yml in a .rubocop.yml file.
çæããããã¡ã¤ã«ã¯ç¾ç¶ã®éåãå ¨ã¦ç¡å¹åããè¨å®ãè¨è¿°ããã¦ããã
$ cat .rubocop_todo.yml # This configuration was generated by `rubocop --auto-gen-config` # on 2015-08-17 21:07:31 +0900 using RuboCop version 0.32.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. # Offense count: 1 # Cop supports --auto-correct. Style/LeadingCommentSpace: Enabled: false
ãã®ãã¡ã¤ã«ãinherit_from
ã§.rubocop.yml
ã«è¿½å ããã
inherit_from: .rubocop_todo.yml AllCops: ãâ¦
ããããã¨éåã0ã«ãªãã®ã§ãå¾ã¯æéãããã¨ãã«ã§ã1ã¤ãã¤è¨å®ãå¤æ´ãã¦éåãä¿®æ£ãã¦ããã¨ãã使ç¨æ¹æ³ãããã
$ rubocop Inspecting 35 files ................................... 35 files inspected, no offenses detected
ããã½ãã㯠å¯éåã¯ã¤ã¤ã¬ã¹ããããã³ ãã©ã㯠RP-WF7-K
- åºç社/ã¡ã¼ã«ã¼: ããã½ããã¯
- çºå£²æ¥: 2012/03/06
- ã¡ãã£ã¢: ã¨ã¬ã¯ãããã¯ã¹
- è³¼å ¥: 3人 ã¯ãªãã¯: 42å
- ãã®ååãå«ãããã° (2件) ãè¦ã