123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::MagicComment::SimpleComment

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::MagicComment
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

::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

Match encoding or coding

[ GitHub ]

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

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

#extract_frozen_string_literal (private)

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.

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# 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 (private)

[ GitHub ]

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

def extract_typed
  extract(/\A\s*#\s*#{KEYWORDS[:typed]}:\s*#{TOKEN}\s*\z/io)
end

#without(type)

Rewrite the comment without a given token type

[ GitHub ]

  
# 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