class RuboCop::MagicComment::SimpleComment

Wrapper for regular magic comments not bound to an editor.

Simple comments can only specify one setting per comment.

@example frozen string literal comments

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

@example encoding comments

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

Constants

FSTRING_LITERAL_COMMENT

Public Instance Methods

encoding() click to toggle source

Match ‘encoding` or `coding`

# File lib/rubocop/magic_comment.rb, line 265
def encoding
  extract(/\A\s*\#\s*(#{FSTRING_LITERAL_COMMENT})?\s*#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
end
without(type) click to toggle source

Rewrite the comment without a given token type

# File lib/rubocop/magic_comment.rb, line 270
def without(type)
  if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/io)
    ''
  else
    @comment
  end
end

Private Instance Methods

extract_frozen_string_literal() click to toggle source

Extract ‘frozen_string_literal`.

The ‘frozen_string_literal` magic comment only works if it is the only text in the comment.

Case-insensitive and dashes/underscores are acceptable. @see github.com/ruby/ruby/blob/78b95b4/parse.y#L7134-L7138

# File lib/rubocop/magic_comment.rb, line 287
def extract_frozen_string_literal
  extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*#{TOKEN}\s*\z/io)
end
extract_shareable_constant_value() click to toggle source
# File lib/rubocop/magic_comment.rb, line 291
def extract_shareable_constant_value
  extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*#{TOKEN}\s*\z/io)
end
extract_typed() click to toggle source
# File lib/rubocop/magic_comment.rb, line 295
def extract_typed
  extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*#{TOKEN}\s*\z/io)
end