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.
Direct Known Subclasses
Defined Under Namespace
Classes: EditorComment, EmacsComment, SimpleComment, VimComment
Constant Summary collapse
- TOKEN =
/[[:alnum:]\-_]+/.freeze
- KEYWORDS =
{ encoding: '(?:en)?coding', frozen_string_literal: 'frozen[_-]string[_-]literal', shareable_constant_value: 'shareable[_-]constant[_-]value' }.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_literal` value 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.
-
#shareable_constant_value ⇒ String
Expose the ‘shareable_constant_value` value coerced to a boolean if possible.
-
#shareable_constant_value_specified? ⇒ Boolean
Was a shareable_constant_value specified?.
- #valid? ⇒ Boolean
- #valid_literal_value? ⇒ Boolean
- #valid_shareable_constant_value? ⇒ Boolean
Constructor Details
#initialize(comment) ⇒ MagicComment
Returns a new instance of MagicComment.
30 31 32 |
# File 'lib/rubocop/magic_comment.rb', line 30 def initialize(comment) @comment = comment end |
Class Method Details
.parse(comment) ⇒ RuboCop::MagicComment
Detect magic comment format and pass it to the appropriate wrapper.
21 22 23 24 25 26 27 28 |
# File 'lib/rubocop/magic_comment.rb', line 21 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
34 35 36 |
# File 'lib/rubocop/magic_comment.rb', line 34 def any? frozen_string_literal_specified? || encoding_specified? || shareable_constant_value_specified? end |
#encoding_specified? ⇒ Boolean
99 100 101 |
# File 'lib/rubocop/magic_comment.rb', line 99 def encoding_specified? specified?(encoding) end |
#frozen_string_literal ⇒ Boolean, ...
Expose the ‘frozen_string_literal` value coerced to a boolean if possible.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rubocop/magic_comment.rb', line 81 def frozen_string_literal return unless (setting = extract_frozen_string_literal) case setting 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
50 51 52 |
# File 'lib/rubocop/magic_comment.rb', line 50 def frozen_string_literal? frozen_string_literal == true end |
#frozen_string_literal_specified? ⇒ Boolean
Was a magic comment for the frozen string literal found?
65 66 67 |
# File 'lib/rubocop/magic_comment.rb', line 65 def frozen_string_literal_specified? specified?(frozen_string_literal) end |
#shareable_constant_value ⇒ String
Expose the ‘shareable_constant_value` value coerced to a boolean if possible.
95 96 97 |
# File 'lib/rubocop/magic_comment.rb', line 95 def shareable_constant_value extract_shareable_constant_value end |
#shareable_constant_value_specified? ⇒ Boolean
Was a shareable_constant_value specified?
72 73 74 |
# File 'lib/rubocop/magic_comment.rb', line 72 def shareable_constant_value_specified? specified?(shareable_constant_value) end |
#valid? ⇒ Boolean
38 39 40 |
# File 'lib/rubocop/magic_comment.rb', line 38 def valid? @comment.start_with?('#') && any? end |
#valid_literal_value? ⇒ Boolean
54 55 56 |
# File 'lib/rubocop/magic_comment.rb', line 54 def valid_literal_value? [true, false].include?(frozen_string_literal) end |
#valid_shareable_constant_value? ⇒ Boolean
58 59 60 |
# File 'lib/rubocop/magic_comment.rb', line 58 def valid_shareable_constant_value? %w[none literal experimental_everything experimental_copy].include?(shareable_constant_value) end |