Skip to content

Commit c626229

Browse files
authored
Merge pull request #12218 from koic/fix_a_false_positive_for_lint_ordered_magic_comments
[Fix #12213] Fix a false positive for `Lint/OrderedMagicComments`
2 parents 3e92160 + a2c1100 commit c626229

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12213](https://github.com/rubocop/rubocop/issues/12213): Fix a false positive for `Lint/OrderedMagicComments` when comment text `# encoding: ISO-8859-1` is embedded within example code as source code comment. ([@koic][])

lib/rubocop/magic_comment.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module RuboCop
77
class MagicComment
88
# IRB's pattern for matching magic comment tokens.
99
# @see https://github.com/ruby/ruby/blob/b4a55c1/lib/irb/magic-file.rb#L5
10-
TOKEN = /[[:alnum:]\-_]+/.freeze
10+
TOKEN = '(?<token>[[:alnum:]\-_]+)'
1111
KEYWORDS = {
1212
encoding: '(?:en)?coding',
1313
frozen_string_literal: 'frozen[_-]string[_-]literal',
@@ -129,7 +129,7 @@ def specified?(value)
129129
# @return [String] if pattern matched
130130
# @return [nil] otherwise
131131
def extract(pattern)
132-
@comment[pattern, 1]
132+
@comment[pattern, :token]
133133
end
134134

135135
# Parent to Vim and Emacs magic comment handling.
@@ -157,10 +157,10 @@ def without(type)
157157
# @return [String] extracted value if it is found
158158
# @return [nil] otherwise
159159
def match(keyword)
160-
pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*(#{TOKEN})\z/
160+
pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*#{TOKEN}\z/
161161

162162
tokens.each do |token|
163-
next unless (value = token[pattern, 1])
163+
next unless (value = token[pattern, :token])
164164

165165
return value.downcase
166166
end
@@ -188,7 +188,7 @@ def tokens
188188
# @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
189189
# @see https://github.com/ruby/ruby/blob/3f306dc/parse.y#L6873-L6892 Emacs handling in parse.y
190190
class EmacsComment < EditorComment
191-
REGEXP = /-\*-(.+)-\*-/.freeze
191+
REGEXP = /-\*-(?<token>.+)-\*-/.freeze
192192
FORMAT = '# -*- %s -*-'
193193
SEPARATOR = ';'
194194
OPERATOR = ':'
@@ -216,7 +216,7 @@ def extract_typed; end
216216
#
217217
# comment.encoding # => 'ascii-8bit'
218218
class VimComment < EditorComment
219-
REGEXP = /#\s*vim:\s*(.+)/.freeze
219+
REGEXP = /#\s*vim:\s*(?<token>.+)/.freeze
220220
FORMAT = '# vim: %s'
221221
SEPARATOR = ', '
222222
OPERATOR = '='
@@ -259,9 +259,11 @@ def extract_typed; end
259259
# comment2.frozen_string_literal # => nil
260260
# comment2.encoding # => 'utf-8'
261261
class SimpleComment < MagicComment
262+
FSTRING_LITERAL_COMMENT = 'frozen_string_literal:\s*(true|false)'
263+
262264
# Match `encoding` or `coding`
263265
def encoding
264-
extract(/\A\s*\#.*\b#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
266+
extract(/\A\s*\#\s*(#{FSTRING_LITERAL_COMMENT})?\s*#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
265267
end
266268

267269
# Rewrite the comment without a given token type
@@ -283,15 +285,15 @@ def without(type)
283285
# Case-insensitive and dashes/underscores are acceptable.
284286
# @see https://github.com/ruby/ruby/blob/78b95b4/parse.y#L7134-L7138
285287
def extract_frozen_string_literal
286-
extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*(#{TOKEN})\s*\z/io)
288+
extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*#{TOKEN}\s*\z/io)
287289
end
288290

289291
def extract_shareable_constant_value
290-
extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*(#{TOKEN})\s*\z/io)
292+
extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*#{TOKEN}\s*\z/io)
291293
end
292294

293295
def extract_typed
294-
extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*(#{TOKEN})\s*\z/io)
296+
extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*#{TOKEN}\s*\z/io)
295297
end
296298
end
297299
end

spec/rubocop/cop/lint/ordered_magic_comments_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@
9696
puts x
9797
RUBY
9898
end
99+
100+
it 'does not register an offense when comment text `# encoding: ISO-8859-1` is embedded within ' \
101+
'example code as source code comment' do
102+
expect_no_offenses(<<~RUBY)
103+
# frozen_string_literal: true
104+
105+
# eval('# encoding: ISO-8859-1')
106+
RUBY
107+
end
99108
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy