123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::MagicComment::EditorComment Abstract

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::MagicComment
Defined in: lib/rubocop/magic_comment.rb

Overview

This class is abstract.

Parent to Vim and Emacs magic comment handling.

Constant Summary

::RuboCop::MagicComment - Inherited

KEYWORDS, TOKEN

Class Method Summary

::RuboCop::MagicComment - Inherited

.new,
.parse

Detect magic comment format and pass it to the appropriate wrapper.

Instance Attribute Summary

::RuboCop::MagicComment - Inherited

#any?, #encoding_specified?,
#frozen_string_literal?

Does the magic comment enable the frozen string literal feature.

#frozen_string_literal_specified?

Was a magic comment for the frozen string literal found?

#shareable_constant_value_specified?

Was a shareable_constant_value specified?

#typed_specified?

Was the Sorbet typed sigil specified?

#valid?, #valid_literal_value?, #valid_shareable_constant_value?

Instance Method Summary

::RuboCop::MagicComment - Inherited

#frozen_string_literal

Expose the frozen_string_literal value coerced to a boolean if possible.

#shareable_constant_value

Expose the shareable_constant_value value coerced to a boolean if possible.

#typed,
#extract

Match the entire comment string with a pattern and take the first capture.

#specified?

Constructor Details

This class inherits a constructor from RuboCop::MagicComment

Instance Method Details

#encoding

[ GitHub ]

  
# File 'lib/rubocop/magic_comment.rb', line 139

def encoding
  match(self.class::KEYWORDS[:encoding])
end

#match(keyword) ⇒ String? (private)

Find a token starting with the provided keyword and extract its value.

Parameters:

Returns:

  • (String)

    extracted value if it is found

  • (nil)

    otherwise

[ GitHub ]

  
# File 'lib/rubocop/magic_comment.rb', line 159

def match(keyword)
  pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*#{TOKEN}\z/

  tokens.each do |token|
    next unless (value = token[pattern, :token])

    return value.downcase
  end

  nil
end

#tokensArray<String> (private)

Individual tokens composing an editor specific comment string.

[ GitHub ]

  
# File 'lib/rubocop/magic_comment.rb', line 174

def tokens
  extract(self.class::REGEXP).split(self.class::SEPARATOR).map(&:strip)
end

#without(type)

Rewrite the comment without a given token type

[ GitHub ]

  
# File 'lib/rubocop/magic_comment.rb', line 144

def without(type)
  remaining = tokens.grep_v(/\A#{self.class::KEYWORDS[type.to_sym]}/)
  return '' if remaining.empty?

  self.class::FORMAT % remaining.join(self.class::SEPARATOR)
end