Skip to content

Commit

Permalink
[Fix #6115] Fix a false positive for Lint/OrderedMagicComments
Browse files Browse the repository at this point in the history
Fixes #6115.

This PR fixes a false positive for `Lint/OrderedMagicComments`
when using `encoding: Encoding::SJIS` Hash notation after
`frozen_string_literal` magic comment.

There was a false positive to misunderstanding the following
Hash notation as an encoding magic comment.

```ruby
# frozen_string_literal: true

x = { encoding: Encoding::SJIS }
puts x
```

This PR changes the pattern match of the magic encoding comment
so that beginning of line always starts with `#`.
  • Loading branch information
koic authored and bbatsov committed Jul 23, 2018
1 parent 58f2f8d commit 05d9673
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#6103](https://github.com/rubocop-hq/rubocop/pull/6103): Fix a false positive for `Layout/IndentationWidth` when multiple modifiers are used in a block and a method call is made at end of the block. ([@koic][])
* [#6084](https://github.com/rubocop-hq/rubocop/issues/6084): Fix `Naming/MemoizedInstanceVariableName` cop to allow methods to have leading underscores. ([@kenman345][])
* [#6098](https://github.com/rubocop-hq/rubocop/issues/6098): Fix an error for `Layout/ClassStructure` when there is a comment in the macro method to be auto-correct. ([@koic][])
* [#6115](https://github.com/rubocop-hq/rubocop/issues/6115): Fix a false positive for `Lint/OrderedMagicComments` when using `{ encoding: Encoding::SJIS }` hash object after `frozen_string_literal` magic comment. ([@koic][])

## 0.58.1 (2018-07-10)

Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/cop/mixin/frozen_string_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ def frozen_string_literals_enabled?
end

def leading_comment_lines
processed_source[0..2].compact
comments = processed_source.comments

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

leading_comments << comment.text
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/magic_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def frozen_string_literal; end
class SimpleComment < MagicComment
# Match `encoding` or `coding`
def encoding
extract(/\#* \b(?:en)?coding: (#{TOKEN})/i)
extract(/\A\#.*\b(?:en)?coding: (#{TOKEN})/i)
end

private
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/ordered_magic_comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
RUBY
end

it 'does not register an offense when using ' \
'`encoding: Encoding::SJIS` Hash notation after' \
'`frozen_string_literal` magic comment' do
expect_no_offenses(<<-RUBY.strip_indent)
# frozen_string_literal: true
x = { encoding: Encoding::SJIS }
puts x
RUBY
end

it 'autocorrects ordered magic comments' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
# frozen_string_literal: true
Expand Down

0 comments on commit 05d9673

Please sign in to comment.