Invoca / magic_frozen_string_literal

easily add '# frozen_string_literal: true' comments to the top of all your project's Ruby files
MIT License
159 stars 16 forks source link

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

Open PikachuEXE opened 8 years ago

PikachuEXE commented 8 years ago

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
roryokane commented 8 years ago

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.

PikachuEXE commented 8 years ago

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

roryokane commented 8 years ago

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.