Class: RuboCop::MagicComment Abstract
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | lib/rubocop/magic_comment.rb |
Overview
parent of three different magic comment handlers
Parse different formats of magic comments.
Constant Summary
-
KEYWORDS =
# File 'lib/rubocop/magic_comment.rb', line 11{ 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 -
TOKEN =
# File 'lib/rubocop/magic_comment.rb', line 10
IRB’s pattern for matching magic comment tokens.
'(?<token>[[:alnum:]\-_]+)'
Class Method Summary
- .new(comment) ⇒ MagicComment constructor
-
.parse(comment) ⇒ RuboCop::MagicComment
Detect magic comment format and pass it to the appropriate wrapper.
Instance Attribute Summary
- #any? ⇒ Boolean readonly
- #encoding_specified? ⇒ Boolean readonly
-
#frozen_string_literal? ⇒ Boolean
readonly
Does the magic comment enable the frozen string literal feature.
-
#frozen_string_literal_specified? ⇒ Boolean
readonly
Was a magic comment for the frozen string literal found?
- #rbs_inline_specified? ⇒ Boolean readonly
-
#shareable_constant_value_specified? ⇒ Boolean
readonly
Was a shareable_constant_value specified?
-
#typed_specified? ⇒ Boolean
readonly
Was the Sorbet #typed sigil specified?
- #valid? ⇒ Boolean readonly
- #valid_literal_value? ⇒ Boolean readonly
- #valid_rbs_inline_value? ⇒ Boolean readonly
- #valid_shareable_constant_value? ⇒ Boolean readonly
-
#warn_indent_specified? ⇒ Boolean
readonly
Was a warn_indent specified?
Instance Method Summary
-
#frozen_string_literal ⇒ Boolean, ...
readonly
Expose the #frozen_string_literal value coerced to a boolean if possible.
-
#shareable_constant_value ⇒ String
Expose the #shareable_constant_value value coerced to a boolean if possible.
- #typed
-
#warn_indent ⇒ String
Expose the #warn_indent value.
-
#extract(pattern) ⇒ String?
private
Match the entire comment string with a pattern and take the first capture.
- #specified?(value) ⇒ Boolean private
Constructor Details
.new(comment) ⇒ MagicComment
# File 'lib/rubocop/magic_comment.rb', line 34
def initialize(comment) @comment = comment end
Class Method Details
.parse(comment) ⇒ MagicComment
Detect magic comment format and pass it to the appropriate wrapper.
# 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 Attribute Details
#any? ⇒ Boolean (readonly)
[ GitHub ]
# 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 (readonly)
[ GitHub ]
# File 'lib/rubocop/magic_comment.rb', line 126
def encoding_specified? specified?(encoding) end
#frozen_string_literal? ⇒ Boolean (readonly)
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
# File 'lib/rubocop/magic_comment.rb', line 59
def frozen_string_literal? frozen_string_literal == true end
#frozen_string_literal_specified? ⇒ Boolean (readonly)
Was a magic comment for the frozen string literal found?
# File 'lib/rubocop/magic_comment.rb', line 78
def frozen_string_literal_specified? specified?(frozen_string_literal) end
#rbs_inline_specified? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/rubocop/magic_comment.rb', line 130
def rbs_inline_specified? valid_rbs_inline_value? end
#typed_specified? ⇒ Boolean (readonly)
Was the Sorbet #typed sigil specified?
# File 'lib/rubocop/magic_comment.rb', line 137
def typed_specified? specified?(extract_typed) end
#valid? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/rubocop/magic_comment.rb', line 47
def valid? @comment.start_with?('#') && any? end
#valid_literal_value? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/rubocop/magic_comment.rb', line 63
def valid_literal_value? [true, false].include?(frozen_string_literal) end
#valid_rbs_inline_value? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/rubocop/magic_comment.rb', line 67
def valid_rbs_inline_value? %w[enabled disabled].include?(extract_rbs_inline_value) end
#warn_indent_specified? ⇒ Boolean (readonly)
Was a warn_indent specified?
# File 'lib/rubocop/magic_comment.rb', line 85
def warn_indent_specified? specified?(warn_indent) end
Instance Method Details
#extract(pattern) ⇒ String? (private)
Match the entire comment string with a pattern and take the first capture.
# File 'lib/rubocop/magic_comment.rb', line 157
def extract(pattern) @comment[pattern, :token] end
#frozen_string_literal ⇒ Boolean, ... (readonly)
Expose the frozen_string_literal value coerced to a boolean if possible.
# 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
#specified?(value) ⇒ Boolean (private)
# File 'lib/rubocop/magic_comment.rb', line 147
def specified?(value) !value.nil? end
#typed
[ GitHub ]# File 'lib/rubocop/magic_comment.rb', line 141
def typed extract_typed end
#warn_indent ⇒ String
Expose the warn_indent value.
# File 'lib/rubocop/magic_comment.rb', line 115
def warn_indent extract_warn_indent end