Class: RuboCop::Cop::Style::MagicCommentFormat::CommentRange

Inherits:
Object
  • Object
show all
Extended by:
SimpleForwardable
Defined in:
lib/rubocop/cop/style/magic_comment_format.rb

Overview

Value object to extract source ranges for the different parts of a magic comment

Constant Summary collapse

DIRECTIVE_REGEXP =
Regexp.union(MagicComment::KEYWORDS.map do |_, v|
  Regexp.new(v, Regexp::IGNORECASE)
end).freeze
VALUE_REGEXP =
Regexp.new("(?:#{DIRECTIVE_REGEXP}:\s*)(.*?)(?=;|$)")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ CommentRange

Returns a new instance of CommentRange.



119
120
121
# File 'lib/rubocop/cop/style/magic_comment_format.rb', line 119

def initialize(comment)
  @comment = comment
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



117
118
119
# File 'lib/rubocop/cop/style/magic_comment_format.rb', line 117

def comment
  @comment
end

Instance Method Details

#directivesObject

A magic comment can contain one directive (normal style) or multiple directives (emacs style)



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rubocop/cop/style/magic_comment_format.rb', line 125

def directives
  @directives ||= begin
    matches = []

    text.scan(DIRECTIVE_REGEXP) do
      offset = Regexp.last_match.offset(0)
      matches << loc.expression.adjust(begin_pos: offset.first)
                    .with(end_pos: loc.expression.begin_pos + offset.last)
    end

    matches
  end
end

#valuesObject

A magic comment can contain one value (normal style) or multiple directives (emacs style)



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rubocop/cop/style/magic_comment_format.rb', line 141

def values
  @values ||= begin
    matches = []

    text.scan(VALUE_REGEXP) do
      offset = Regexp.last_match.offset(1)
      matches << loc.expression.adjust(begin_pos: offset.first)
                    .with(end_pos: loc.expression.begin_pos + offset.last)
    end

    matches
  end
end