rubocop 你如何修复丢失的魔法评论
rubocop how do you fix Missing magic comment
我有一个 ruby 代码库,为了查找缺陷,我 运行
$ rubocop
然后我得到
$ rubocop
Inspecting 153 files
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCWWCCCCC
Offenses:
Gemfile:1:1: C: Missing magic comment # frozen_string_literal: true.
source "https://rubygems.org"
我的 Gemfile 需要做哪些修改才能使 rubocop 不抱怨?
只需添加
# frozen_string_literal: true
到每个 Ruby 文件的第一行。或者 运行
rubocop -a
允许 Rubocop 自动修复它能够修复的所有违规行为。
顺便说一句。我喜欢 Rubocop 并自己使用它,但我不会将它发现的东西称为 缺陷。我看到该列表更像是与我的同事讨论的建议或原因。
使用 -D
选项尝试 运行 Rubocop:
rubocop -D
Inspecting 1 file
C
Offenses:
spec/rails_helper.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
require 'spec_helper'
^
添加 -D
将导致 Rubocop 打印被侵犯的警察的姓名,在本例中为 Style/FrozenStringLiteralComment
。然后您可以在 Rubocop 文档中搜索该警察:
http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FrozenStringLiteralComment
This cop is designed to help upgrade to Ruby 3.0. It will add the comment # frozen_string_literal: true
to the top of files to enable frozen string literals. Frozen string literals will be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.
如果你想更具体,运行 rubocop
仅 # frozen_string_literal: true
你可以使用 --only
标志选项:
Run only the specified cop(s) and/or cops in the specified departments.
要查看这些文件:
rubocop --only Style/FrozenStringLiteralComment
要自动更正这些特定文件,请使用 -A
标志(如前一个答案中所述):
rubocop --only Style/FrozenStringLiteralComment -A
您可以查看有关自动更正的更多信息here。
如果您想忽略它,请添加到您的 .rubocop.yml
Style/FrozenStringLiteralComment:
Enabled: false
但您可能想知道 ,尤其是当您使用 Ruby 2.x
时
我有一个 ruby 代码库,为了查找缺陷,我 运行
$ rubocop
然后我得到
$ rubocop
Inspecting 153 files
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCWCCCCCCCWWCCCCC
Offenses:
Gemfile:1:1: C: Missing magic comment # frozen_string_literal: true.
source "https://rubygems.org"
我的 Gemfile 需要做哪些修改才能使 rubocop 不抱怨?
只需添加
# frozen_string_literal: true
到每个 Ruby 文件的第一行。或者 运行
rubocop -a
允许 Rubocop 自动修复它能够修复的所有违规行为。
顺便说一句。我喜欢 Rubocop 并自己使用它,但我不会将它发现的东西称为 缺陷。我看到该列表更像是与我的同事讨论的建议或原因。
使用 -D
选项尝试 运行 Rubocop:
rubocop -D
Inspecting 1 file
C
Offenses:
spec/rails_helper.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.
require 'spec_helper'
^
添加 -D
将导致 Rubocop 打印被侵犯的警察的姓名,在本例中为 Style/FrozenStringLiteralComment
。然后您可以在 Rubocop 文档中搜索该警察:
http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/FrozenStringLiteralComment
This cop is designed to help upgrade to Ruby 3.0. It will add the comment
# frozen_string_literal: true
to the top of files to enable frozen string literals. Frozen string literals will be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.
如果你想更具体,运行 rubocop
仅 # frozen_string_literal: true
你可以使用 --only
标志选项:
Run only the specified cop(s) and/or cops in the specified departments.
要查看这些文件:
rubocop --only Style/FrozenStringLiteralComment
要自动更正这些特定文件,请使用 -A
标志(如前一个答案中所述):
rubocop --only Style/FrozenStringLiteralComment -A
您可以查看有关自动更正的更多信息here。
如果您想忽略它,请添加到您的 .rubocop.yml
Style/FrozenStringLiteralComment:
Enabled: false
但您可能想知道