Skip to content

Commit

Permalink
[Fix #12738] Fix an error for Style/Encoding
Browse files Browse the repository at this point in the history
Fixees #12738.

This PR fixes an error for `Style/Encoding`
when magic encoding with mixed case present.
  • Loading branch information
koic authored and bbatsov committed Mar 4, 2024
1 parent 1a73d64 commit 6674cc8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12738](https://github.com/rubocop/rubocop/issues/12738): Fix an error for `Style/Encoding` when magic encoding with mixed case present. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/magic_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def encoding

# Rewrite the comment without a given token type
def without(type)
if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/)
if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/io)
''
else
@comment
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/style/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def foo() end
RUBY
end

it 'registers an offense when magic encoding with mixed case present' do
expect_offense(<<~RUBY)
# Encoding: UTF-8
^^^^^^^^^^^^^^^^^ Unnecessary utf-8 encoding comment.
RUBY

expect_correction('')
end

it 'registers an offense when encoding present on 2nd line after shebang' do
expect_offense(<<~RUBY)
#!/usr/bin/env ruby
Expand Down

0 comments on commit 6674cc8

Please sign in to comment.