冒险村02 - Begin from linter(2)

02 - Begin from linter : rubocop

延续上篇的 rails_best_practices 後,这篇来介绍 linter Rubocop

Rubocop 是一个可以统一规定、自订规则及忽略规则,达到完全的客制化的 linter,把团队的开发 coding style 都遵循在这个基石上继续建立,可以提升专案开发效率。

如果专案没有一个基本的规范,在工程师的协作中,想必一定常常遇到该写单引号还是双引号(战)?注解该写中文还是英文、TODO 该怎麽注释?括号怎麽断行、def 的写法、程序行数限制是否该重构写法?该如何断行?等等的情况。

废话不多说,直接来看要怎麽使用这个强大的 linter 吧!

注:除了使用 rubocop 官方 的规范外,也可以参考 GithubShopify 等各家的 Ruby Style Guide 来使用,这部分就要看团队讨论决定。

gem install

  # Gemfile
  group :development, :test do
    gem "rubocop", require: false
  end

Usage

  # At the root directory of a Rails app
  bundle exec rubocop

  # for specific filename
  bundle exec rubocop app/models/users.rb

Code style checker

  # app/models/users.rb
  class User < ApplicationRecord
    has_many :posts
  end

透过 rubocop 执行检查一下档案是否符合规范!

  Inspecting 1 file
  C

  Offenses:

  app/models/user.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
  class User < ApplicationRecord
  ^

  1 file inspected, 1 offense detected, 1 offense auto-correctable

  Tip: Based on detected gems, the following RuboCop extension libraries might be helpful:
    * rubocop-rails (https://github.com/rubocop/rubocop-rails)

  You can opt out of this message by adding the following to your config (see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions for more options):
    AllCops:
      SuggestExtensions: false

从错误的讯息中可以看到不符合规定的程序码,不只告诉侦测档案的数量、哪ㄧ行、错误的原因,以及修改的建议。

Modify with suggest

Class: RuboCop::Cop::Style::FrozenStringLiteralComment

  # frozen_string_literal: true

  class User < ApplicationRecord
    has_many :posts
  end

  # bundle exec rubocop app/models/user.rb
  # 1 file inspected, no offenses detected

Generate config

产生规则以及需要待修改的档案

  bundle exec rubocop --auto-gen-config

Setting Rule

前面提到的单引号、双引号的使用,也是满常需要规范的部分,不然有时候找 string 搜寻真的挺不方便,default 开启预设为 single_quotes

但也可以直接选择不开启,不管三七二十一随便都用

  # .rubocop.yml

  #################### Style ####################
  Style/StringLiterals:
    Enabled: false

改为 doueble_quotes 的规范

  # .rubocop.yml

  #################### Style ####################
  Style/StringLiterals:
    Enabled: true
    EnforcedStyle: double_quotes

Exclude file

可以将特定的档案,不受到 rubocop 的侦测,将档案排除在这个规则下的限制

  # .rubocop.yml

  Metrics:
    Exclude:
      - '.git/**/*'
      - 'db/*.rb'
      # Exclude for specific file
      - 'app/models/user.rb'

Auto fix

如前面的例子,不想要排除原本的规则,但又想要自动产生 frozen_string_literal 在每个档案前该怎麽做?(以前专案还不习惯的时候常常用,因为都忘记加,但後来每次新增的时候就习惯先去 copy 过来,後来也就很少用了)

  # At the root directory of a Rails app
  bundle exec rubocop --auto-correct --only Style/FrozenStringLiteralComment

最後,如果有些 file 真的不想修或者是以前有些人都没好好照规范,中间才把 cop 规范起来,然後有一堆要重新修改又懒得改的话,不妨就通通先放在 todo 等人来修正吧(误

Visual Studio Code Extension

可以透过 extension 先安装 ruby-rubocop 外挂,在编辑档案时就可以先知道哪行程序码不符合规范(外挂会在编辑器程序码中下方标示小蝌蚪),可以省去跑指令才会出现错误,不过都要等存档後一阵子才会出现!

https://marketplace.visualstudio.com/items?itemName=misogi.ruby-rubocop

Require

另外如果关於 code performance checks & enforcing Rails best practices and coding conventions 也可以基於 rubocop 再加入 rubocop-performance & rubocop-rails,详细就不多赘述了!

  # .rubocop.yml
  require:
    - rubocop-performance
    - rubocop-rails

RubyGems

参考来源

My blog


<<:  [前端暴龙机,Vue2.x 进化 Vue3 ] Day8. v-model 修饰符 -- 省下自己写 JS 处理的时间

>>:  GKE (一)

Day 27 Filebeat with multiple module and ELK Dashboard

Day 27 Filebeat with multiple module and ELK Dashb...

30天打造品牌特色电商网站 Day.1 网站介面基础知识

处在疫情时代,电商已然成为时下的热门趋势,电商平台多元且方便,简单几步骤就能轻松开店创业,但如何在高...

Day 14 - 安装与执行 YOLO

Day 14 - 安装与执行 YOLO 在 介绍影像辨识的处理流程 - Day 10 有提到 YOL...

寝室的秘密授课(四):测试覆盖率 Test Coverage

「为什麽要写这麽多测试案例啊?加减乘除不是四个就够了?而且除法测试里面还多放了一个assertFai...

Day1 Why Go?

What is Golang? Golang又简称为Go,Golang是一个始於2009年由Goog...