Class: RuboCop::MagicComment Abstract
- Inherits:
-
Object
- Object
- RuboCop::MagicComment
- Defined in:
- lib/rubocop/magic_comment.rb
Overview
parent of three different magic comment handlers
Parse different formats of magic comments.
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', warn_indent: 'warn[_-]indent', shareable_constant_value: 'shareable[_-]constant[_-]value', typed: 'typed' }.freeze
Class Method Summary collapse
-
.parse(comment) ⇒ RuboCop::MagicComment
Detect magic comment format and pass it to the appropriate wrapper.
Instance Method Summary collapse
- #any? ⇒ Boolean
- #encoding_specified? ⇒ Boolean
-
#frozen_string_literal ⇒ Boolean, ...
Expose the
frozen_string_literalvalue coerced to a boolean if possible. -
#frozen_string_literal? ⇒ Boolean
Does the magic comment enable the frozen string literal feature.
-
#frozen_string_literal_specified? ⇒ Boolean
Was a magic comment for the frozen string literal found?.
-
#initialize(comment) ⇒ MagicComment
constructor
A new instance of MagicComment.
- #rbs_inline_specified? ⇒ Boolean
-
#shareable_constant_value ⇒ String
Expose the
shareable_constant_valuevalue coerced to a boolean if possible. -
#shareable_constant_value_specified? ⇒ Boolean
Was a shareable_constant_value specified?.
- #typed ⇒ Object
-
#typed_specified? ⇒ Boolean
Was the Sorbet
typedsigil specified?. - #valid? ⇒ Boolean
- #valid_literal_value? ⇒ Boolean
- #valid_rbs_inline_value? ⇒ Boolean
- #valid_shareable_constant_value? ⇒ Boolean
-
#warn_indent ⇒ String
Expose the
warn_indentvalue. -
#warn_indent_specified? ⇒ Boolean
Was a warn_indent specified?.
Constructor Details
#initialize(comment) ⇒ MagicComment
Returns a new instance of MagicComment.
34 35 36 |
# File 'lib/rubocop/magic_comment.rb', line 34 def initialize(comment) @comment = comment end |
Class Method Details
.parse(comment) ⇒ RuboCop::MagicComment
Detect magic comment format and pass it to the appropriate wrapper.
25 26 27 28 29 30 31 32 |
# File 'lib/rubocop/magic_comment.rb', line 25 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
38 39 40 41 42 43 44 45 |
# File 'lib/rubocop/magic_comment.rb', line 38 def any? frozen_string_literal_specified? || encoding_specified? || rbs_inline_specified? || warn_indent_specified? || shareable_constant_value_specified? || typed_specified? end |
#encoding_specified? ⇒ Boolean
126 127 128 |
# File 'lib/rubocop/magic_comment.rb', line 126 def encoding_specified? specified?(encoding) end |
#frozen_string_literal ⇒ Boolean, ...
Expose the frozen_string_literal value coerced to a boolean if possible.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubocop/magic_comment.rb', line 101 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
59 60 61 |
# File 'lib/rubocop/magic_comment.rb', line 59 def frozen_string_literal? frozen_string_literal == true end |
#frozen_string_literal_specified? ⇒ Boolean
Was a magic comment for the frozen string literal found?
78 79 80 |
# File 'lib/rubocop/magic_comment.rb', line 78 def frozen_string_literal_specified? specified?(frozen_string_literal) end |
#rbs_inline_specified? ⇒ Boolean
130 131 132 |
# File 'lib/rubocop/magic_comment.rb', line 130 def rbs_inline_specified? valid_rbs_inline_value? end |
#shareable_constant_value ⇒ String
Expose the shareable_constant_value value coerced to a boolean if possible.
122 123 124 |
# File 'lib/rubocop/magic_comment.rb', line 122 def shareable_constant_value extract_shareable_constant_value end |
#shareable_constant_value_specified? ⇒ Boolean
Was a shareable_constant_value specified?
92 93 94 |
# File 'lib/rubocop/magic_comment.rb', line 92 def shareable_constant_value_specified? specified?(shareable_constant_value) end |
#typed ⇒ Object
141 142 143 |
# File 'lib/rubocop/magic_comment.rb', line 141 def typed extract_typed end |
#typed_specified? ⇒ Boolean
Was the Sorbet typed sigil specified?
137 138 139 |
# File 'lib/rubocop/magic_comment.rb', line 137 def typed_specified? specified?(extract_typed) end |
#valid? ⇒ Boolean
47 48 49 |
# File 'lib/rubocop/magic_comment.rb', line 47 def valid? @comment.start_with?('#') && any? end |
#valid_literal_value? ⇒ Boolean
63 64 65 |
# File 'lib/rubocop/magic_comment.rb', line 63 def valid_literal_value? [true, false].include?(frozen_string_literal) end |
#valid_rbs_inline_value? ⇒ Boolean
67 68 69 |
# File 'lib/rubocop/magic_comment.rb', line 67 def valid_rbs_inline_value? %w[enabled disabled].include?(extract_rbs_inline_value) end |
#valid_shareable_constant_value? ⇒ Boolean
71 72 73 |
# File 'lib/rubocop/magic_comment.rb', line 71 def valid_shareable_constant_value? %w[none literal experimental_everything experimental_copy].include?(shareable_constant_value) end |
#warn_indent ⇒ String
Expose the warn_indent value.
115 116 117 |
# File 'lib/rubocop/magic_comment.rb', line 115 def warn_indent extract_warn_indent end |
#warn_indent_specified? ⇒ Boolean
Was a warn_indent specified?
85 86 87 |
# File 'lib/rubocop/magic_comment.rb', line 85 def warn_indent_specified? specified?(warn_indent) end |