fix `Missing frozen string literal comment` issue
Asked Answered
L

4

21

I created a new migration, it looks like this one:

class AddCommentsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :comments, :text
  end
end

Now with Code Climate I am warned of an issue: Missing frozen string literal comment.

I tried to fix it like this:

# frozen_string_literal: true
class AddCommentsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :comments, :text
  end
end

But I still have the same issue. How can I solve it? Thanks.

Livelihood answered 9/5, 2016 at 18:27 Comment(1)
Looks correct to me. Have you investigated locally with the Code Climate CLI or rubocop?Oratory
C
14

I experienced the same problem. Rubocop was working fine before but suddenly it started acting up. I read through their configuration options on github and saw the particular property that is messing with your code. The property can be found here: FrozenStringLiteral.

To silence this warning, you only need to add this to your rubocop.yml file

Style/FrozenStringLiteralComment:
  Enabled: false
Caffrey answered 27/7, 2016 at 23:27 Comment(2)
This change just silences the warning. The linked page describes the right solution: Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default..Middle
Disagree with @Middle . This is well explained in this blog post. Basically this magic comment was to support a change that was never adopted, and never will be adopted. Disabling this rubocop is the correct fix.Wo
V
4

Adding an empty line below the string literal line fixed it for me.

# frozen_string_literal: true

module FooBar
end
Villegas answered 31/5, 2022 at 7:42 Comment(0)
S
0

Make sure you added your changes to the staging area before trying to run Rubocop again. I had the same problem and that solved it for me.

Solander answered 29/4, 2022 at 8:26 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Naphtha
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewInsubordinate
F
0

Run bundle exec rubocop --parallel -A (AUTO FIX)

AND

RUN AGAIN bundle exec rubocop --parallel OR rubocop

Try....

Foreleg answered 8/1 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.