Invoca/magic_frozen_string_literal

Add option to add comment with value `false` if comment absent

PikachuEXE opened this issue · 3 comments

I want a slow transition
So I don't want to turn it on for every file in my projects at once
But I want a comment at a top so I remember to change it to true

# frozen_string_literal: false

You could implement this in a non-general way by cloning this gem’s source, modifying lib/add_magic_comment.rb, and running bin/magic_frozen_string_literal by passing its full path. This is the only change needed in lib/add_magic_comment.rb:

-MAGIC_COMMENT         = "#{MAGIC_COMMENT_PREFIX}: true"
+MAGIC_COMMENT         = "#{MAGIC_COMMENT_PREFIX}: false"

However, this will not properly ignore any existing occurrences of # frozen_string_literal: true – it will overwrite them and set them all to # frozen_string_literal: false.

I don't want the true to be overwritten :S

You could further disable the removal of existing comments, whether true or false, by also removing add_magic_comment.rb lines 29–31:

while lines.first && lines.first.match(MAGIC_COMMENT_PATTERN)
  lines.shift
end

Or you could have the program remove only existing false comments by editing MAGIC_COMMENT_PATTERN, which is used in the deletion code above:

-MAGIC_COMMENT_PATTERN = /^(-|(<%))?#\s*#{MAGIC_COMMENT_PREFIX}\s*(%>)?/
+MAGIC_COMMENT_PATTERN = /^(-|(<%))?#\s*#{MAGIC_COMMENT_PREFIX}:\s*false\s*(%>)?/

Of course, it would be better if the gem itself supported this.