Class: RuboCop::MagicComment Abstract

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

Overview

This class is abstract.

parent of three different magic comment handlers

Parse different formats of magic comments.

Direct Known Subclasses

EditorComment, SimpleComment

Defined Under Namespace

Classes: EditorComment, EmacsComment, SimpleComment, VimComment

Constant Summary collapse

TOKEN =

IRB’s pattern for matching magic comment tokens.

'(?<token>[[:alnum:]\-_]+)'
KEYWORDS =
{
  encoding: '(?:en)?coding',
  frozen_string_literal: 'frozen[_-]string[_-]literal',
  rbs_inline: 'rbs_inline',
  shareable_constant_value: 'shareable[_-]constant[_-]value',
  typed: 'typed'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ MagicComment



33
34
35
# File 'lib/rubocop/magic_comment.rb', line 33

def initialize(comment)
  @comment = comment
end

Class Method Details

.parse(comment) ⇒ RuboCop::MagicComment

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



24
25
26
27
28
29
30
31
# File 'lib/rubocop/magic_comment.rb', line 24

def self.parse(comment)
  case comment
  when EmacsComment::REGEXP then EmacsComment.new(comment)
  when VimComment::REGEXP   then VimComment.new(comment)
  else
    SimpleComment.new(comment)
  end
end

Instance Method Details

#any?Boolean



37
38
39
40
41
42
43
# File 'lib/rubocop/magic_comment.rb', line 37

def any?
  frozen_string_literal_specified? ||
    encoding_specified? ||
    rbs_inline_specified? ||
    shareable_constant_value_specified? ||
    typed_specified?
end

#encoding_specified?Boolean



110
111
112
# File 'lib/rubocop/magic_comment.rb', line 110

def encoding_specified?
  specified?(encoding)
end

#frozen_string_literalBoolean, ...

Expose the frozen_string_literal value coerced to a boolean if possible.



92
93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/magic_comment.rb', line 92

def frozen_string_literal
  return unless (setting = extract_frozen_string_literal)

  case setting.downcase
  when 'true'  then true
  when 'false' then false
  else
    setting
  end
end

#frozen_string_literal?Boolean

Does the magic comment enable the frozen string literal feature.

Test whether the frozen string literal value is true. Cannot just return frozen_string_literal since an invalid magic comment like ‘# frozen_string_literal: yes` is possible and the truthy value `’yes’‘ does not actually enable the feature



57
58
59
# File 'lib/rubocop/magic_comment.rb', line 57

def frozen_string_literal?
  frozen_string_literal == true
end

#frozen_string_literal_specified?Boolean

Was a magic comment for the frozen string literal found?



76
77
78
# File 'lib/rubocop/magic_comment.rb', line 76

def frozen_string_literal_specified?
  specified?(frozen_string_literal)
end

#rbs_inline_specified?Boolean



114
115
116
# File 'lib/rubocop/magic_comment.rb', line 114

def rbs_inline_specified?
  valid_rbs_inline_value?
end

#shareable_constant_valueString

Expose the shareable_constant_value value coerced to a boolean if possible.



106
107
108
# File 'lib/rubocop/magic_comment.rb', line 106

def shareable_constant_value
  extract_shareable_constant_value
end

#shareable_constant_value_specified?Boolean

Was a shareable_constant_value specified?



83
84
85
# File 'lib/rubocop/magic_comment.rb', line 83

def shareable_constant_value_specified?
  specified?(shareable_constant_value)
end

#typedObject



125
126
127
# File 'lib/rubocop/magic_comment.rb', line 125

def typed
  extract_typed
end

#typed_specified?Boolean

Was the Sorbet typed sigil specified?



121
122
123
# File 'lib/rubocop/magic_comment.rb', line 121

def typed_specified?
  specified?(extract_typed)
end

#valid?Boolean



45
46
47
# File 'lib/rubocop/magic_comment.rb', line 45

def valid?
  @comment.start_with?('#') && any?
end

#valid_literal_value?Boolean



61
62
63
# File 'lib/rubocop/magic_comment.rb', line 61

def valid_literal_value?
  [true, false].include?(frozen_string_literal)
end

#valid_rbs_inline_value?Boolean



65
66
67
# File 'lib/rubocop/magic_comment.rb', line 65

def valid_rbs_inline_value?
  %w[enabled disabled].include?(extract_rbs_inline_value)
end

#valid_shareable_constant_value?Boolean



69
70
71
# File 'lib/rubocop/magic_comment.rb', line 69

def valid_shareable_constant_value?
  %w[none literal experimental_everything experimental_copy].include?(shareable_constant_value)
end