Class: RuboCop::MagicComment::SimpleComment

Inherits:
MagicComment
  • Object
show all
Defined in:
lib/rubocop/magic_comment.rb

Overview

Wrapper for regular magic comments not bound to an editor.

Simple comments can only specify one setting per comment.

Examples:

frozen string literal comments

comment1 = RuboCop::MagicComment.parse('# frozen_string_literal: true')
comment1.frozen_string_literal # => true
comment1.encoding              # => nil

encoding comments

comment2 = RuboCop::MagicComment.parse('# encoding: utf-8')
comment2.frozen_string_literal # => nil
comment2.encoding              # => 'utf-8'

Constant Summary collapse

FSTRING_LITERAL_COMMENT =
'frozen_string_literal:\s*(true|false)'

Instance Method Summary collapse

Instance Method Details

#encoding ⇒ Object

Match encoding or coding



308
309
310
# File 'lib/rubocop/magic_comment.rb', line 308

def encoding
  extract(/\A\s*\#\s*(#{FSTRING_LITERAL_COMMENT})?\s*#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
end

#new_frozen_string_literal(value) ⇒ Object



321
322
323
# File 'lib/rubocop/magic_comment.rb', line 321

def new_frozen_string_literal(value)
  "# frozen_string_literal: #{value}"
end

#without(type) ⇒ Object

Rewrite the comment without a given token type



313
314
315
316
317
318
319
# File 'lib/rubocop/magic_comment.rb', line 313

def without(type)
  if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/io)
    ''
  else
    @comment
  end
end