Skip to content

Commit

Permalink
Fix encoding method of RuboCop::MagicComment::SimpleComment
Browse files Browse the repository at this point in the history
�$B!.�(BLint/OrderedMagicComments�$B!.�(B cop extracts codes including "encoding:" that is not comment.
e.g. `CSV.generate(encoding: Encoding::UTF_8) do |csv|`

I think that is solved by adding the condition of "# " before "encoding:".
  • Loading branch information
htwroclau authored and bbatsov committed Mar 10, 2018
1 parent 1553689 commit dba6b75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#5633](https://github.com/bbatsov/rubocop/pull/5633): Fix broken `--fail-fast`. ([@mmyoji][])
* [#5630](https://github.com/bbatsov/rubocop/issues/5630): Fix false positive for `Style/FormatStringToken` when using placeholder arguments in `format` method. ([@koic][])
* [#5651](https://github.com/bbatsov/rubocop/pull/5651): Fix NoMethodError when specified config file that does not exist. ([@onk][])
* [#5647](https://github.com/bbatsov/rubocop/pull/5647): Fix encoding method of RuboCop::MagicComment::SimpleComment. ([@htwroclau][])

### Changes

Expand Down Expand Up @@ -3242,3 +3243,4 @@
[@colorbox]: https://github.com/colorbox
[@mmyoji]: https://github.com/mmyoji
[@unused]: https://github.com/unused
[@htwroclau]: https://github.com/htwroclau
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(/\#* \b(?:en)?coding: (#{TOKEN})/i)
end

private
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/magic_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
encoding: 'ascii-8bit',
frozen_string_literal: nil

include_examples 'magic comment',
' CSV.generate(encoding: Encoding::UTF_8) do |csv|',
encoding: nil,
frozen_string_literal: nil

include_examples(
'magic comment',
'# -*- encoding: ASCII-8BIT; frozen_string_literal: true -*-',
Expand Down

0 comments on commit dba6b75

Please sign in to comment.