@@ -7,7 +7,7 @@ module RuboCop
7
7
class MagicComment
8
8
# IRB's pattern for matching magic comment tokens.
9
9
# @see https://github.com/ruby/ruby/blob/b4a55c1/lib/irb/magic-file.rb#L5
10
- TOKEN = / [[:alnum:]\- _]+/ . freeze
10
+ TOKEN = '(?<token> [[:alnum:]\-_]+)'
11
11
KEYWORDS = {
12
12
encoding : '(?:en)?coding' ,
13
13
frozen_string_literal : 'frozen[_-]string[_-]literal' ,
@@ -129,7 +129,7 @@ def specified?(value)
129
129
# @return [String] if pattern matched
130
130
# @return [nil] otherwise
131
131
def extract ( pattern )
132
- @comment [ pattern , 1 ]
132
+ @comment [ pattern , :token ]
133
133
end
134
134
135
135
# Parent to Vim and Emacs magic comment handling.
@@ -157,10 +157,10 @@ def without(type)
157
157
# @return [String] extracted value if it is found
158
158
# @return [nil] otherwise
159
159
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 /
161
161
162
162
tokens . each do |token |
163
- next unless ( value = token [ pattern , 1 ] )
163
+ next unless ( value = token [ pattern , :token ] )
164
164
165
165
return value . downcase
166
166
end
@@ -188,7 +188,7 @@ def tokens
188
188
# @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
189
189
# @see https://github.com/ruby/ruby/blob/3f306dc/parse.y#L6873-L6892 Emacs handling in parse.y
190
190
class EmacsComment < EditorComment
191
- REGEXP = /-\* -(.+)-\* -/ . freeze
191
+ REGEXP = /-\* -(?<token> .+)-\* -/ . freeze
192
192
FORMAT = '# -*- %s -*-'
193
193
SEPARATOR = ';'
194
194
OPERATOR = ':'
@@ -216,7 +216,7 @@ def extract_typed; end
216
216
#
217
217
# comment.encoding # => 'ascii-8bit'
218
218
class VimComment < EditorComment
219
- REGEXP = /#\s *vim:\s *(.+)/ . freeze
219
+ REGEXP = /#\s *vim:\s *(?<token> .+)/ . freeze
220
220
FORMAT = '# vim: %s'
221
221
SEPARATOR = ', '
222
222
OPERATOR = '='
@@ -259,9 +259,11 @@ def extract_typed; end
259
259
# comment2.frozen_string_literal # => nil
260
260
# comment2.encoding # => 'utf-8'
261
261
class SimpleComment < MagicComment
262
+ FSTRING_LITERAL_COMMENT = 'frozen_string_literal:\s*(true|false)'
263
+
262
264
# Match `encoding` or `coding`
263
265
def encoding
264
- extract ( /\A \s *\# .* \b #{ KEYWORDS [ :encoding ] } : (#{ TOKEN } )/io )
266
+ extract ( /\A \s *\# \s *( #{ FSTRING_LITERAL_COMMENT } )? \s * #{ KEYWORDS [ :encoding ] } : (#{ TOKEN } )/io )
265
267
end
266
268
267
269
# Rewrite the comment without a given token type
@@ -283,15 +285,15 @@ def without(type)
283
285
# Case-insensitive and dashes/underscores are acceptable.
284
286
# @see https://github.com/ruby/ruby/blob/78b95b4/parse.y#L7134-L7138
285
287
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 )
287
289
end
288
290
289
291
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 )
291
293
end
292
294
293
295
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 )
295
297
end
296
298
end
297
299
end
0 commit comments