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',
  shareable_constant_value: 'shareable[_-]constant[_-]value',
  typed: 'typed'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ MagicComment

Returns a new instance of MagicComment.



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

def initialize(comment)
  @comment = comment
end

Class Method Details

.parse(comment) ⇒ RuboCop::MagicComment

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

Parameters:

Returns:



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

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

Returns:

  • (Boolean)


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

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

#encoding_specified?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rubocop/magic_comment.rb', line 104

def encoding_specified?
  specified?(encoding)
end

#frozen_string_literalBoolean, ...

Expose the ‘frozen_string_literal` value coerced to a boolean if possible.

Returns:

  • (Boolean)

    if value is ‘true` or `false` in any case

  • (nil)

    if frozen_string_literal comment isn’t found

  • (String)

    if comment is found but isn’t true or false



86
87
88
89
90
91
92
93
94
95
# File 'lib/rubocop/magic_comment.rb', line 86

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubocop/magic_comment.rb', line 55

def frozen_string_literal?
  frozen_string_literal == true
end

#frozen_string_literal_specified?Boolean

Was a magic comment for the frozen string literal found?

Returns:

  • (Boolean)


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

def frozen_string_literal_specified?
  specified?(frozen_string_literal)
end

#shareable_constant_valueString

Expose the ‘shareable_constant_value` value coerced to a boolean if possible.

Returns:

  • (String)

    for shareable_constant_value config



100
101
102
# File 'lib/rubocop/magic_comment.rb', line 100

def shareable_constant_value
  extract_shareable_constant_value
end

#shareable_constant_value_specified?Boolean

Was a shareable_constant_value specified?

Returns:

  • (Boolean)


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

def shareable_constant_value_specified?
  specified?(shareable_constant_value)
end

#typedObject



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

def typed
  extract_typed
end

#typed_specified?Boolean

Was the Sorbet ‘typed` sigil specified?

Returns:

  • (Boolean)


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

def typed_specified?
  specified?(extract_typed)
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubocop/magic_comment.rb', line 43

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

#valid_literal_value?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rubocop/magic_comment.rb', line 59

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

#valid_shareable_constant_value?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubocop/magic_comment.rb', line 63

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