Skip to content

Commit

Permalink
[Fix #7586] Fix infinite loop in FrozenStringLiteralComment
Browse files Browse the repository at this point in the history
It was detecting frozen_string_literal in the first three lines of the
file, while there's no guideline to separate magic comments with
newlines. If shebang and encoding were separated by a newline, that was
three lines already, and frozen_string_literal was added on line 4, and
not detected as such after auto-correction, triggering an infinite loop.
  • Loading branch information
pirj authored and bbatsov committed Dec 30, 2019
1 parent 75eb107 commit 8c792d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#7569](https://github.com/rubocop-hq/rubocop/issues/7569): Make `Style/YodaCondition` accept `__FILE__ == $0`. ([@koic][])
* [#7576](https://github.com/rubocop-hq/rubocop/issues/7576): Fix an error for `Gemspec/OrderedDependencies` when using a local variable in an argument of dependent gem. ([@koic][])
* [#7595](https://github.com/rubocop-hq/rubocop/issues/7595): Make `Style/NumericPredicate` aware of ignored methods when specifying ignored methods. ([@koic][])
* [#7607](https://github.com/rubocop-hq/rubocop/issues/7607): Fix `Style/FrozenStringLiteralComment` infinite loop when magic comments are newline-separated. ([@pirj][])

## 0.78.0 (2019-12-18)

Expand Down
8 changes: 1 addition & 7 deletions lib/rubocop/cop/mixin/frozen_string_literal.rb
Expand Up @@ -40,13 +40,7 @@ def frozen_string_literals_enabled?
end

def leading_comment_lines
comments = processed_source.comments

comments.each_with_object([]) do |comment, leading_comments|
next if comment.loc.line > 3

leading_comments << comment.text
end
processed_source.comments.first(3).map(&:text)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb
Expand Up @@ -117,6 +117,18 @@
RUBY
end

it 'accepts a frozen string literal comment below newline-separated ' \
'magic comments' do
expect_no_offenses(<<~RUBY)
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
puts 1
RUBY
end

it 'accepts a disabled frozen string literal comment below shebang and ' \
'encoding comments' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 8c792d1

Please sign in to comment.