Class: RuboCop::RSpec::ExpectOffense::AnnotatedSource
- Inherits:
-
Object
- Object
- RuboCop::RSpec::ExpectOffense::AnnotatedSource
- Defined in:
- lib/rubocop/rspec/expect_offense.rb
Overview
Parsed representation of code annotated with the ‘^^^ Message` style
Constant Summary collapse
- ANNOTATION_PATTERN =
/\A\s*\^+ /.freeze
Class Method Summary collapse
-
.parse(annotated_source) ⇒ AnnotatedSource
Separates annotation lines from source lines.
Instance Method Summary collapse
-
#initialize(lines, annotations) ⇒ AnnotatedSource
constructor
A new instance of AnnotatedSource.
-
#plain_source ⇒ String
Return the plain source code without annotations.
-
#to_s ⇒ String
Construct annotated source string (like what we parse).
-
#with_offense_annotations(offenses) ⇒ self
Annotate the source code with the RuboCop offenses provided.
Constructor Details
#initialize(lines, annotations) ⇒ AnnotatedSource
annotations are sorted so that reconstructing the annotation text via #to_s is deterministic
Returns a new instance of AnnotatedSource.
202 203 204 205 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 202 def initialize(lines, annotations) @lines = lines.freeze @annotations = annotations.sort.freeze end |
Class Method Details
.parse(annotated_source) ⇒ AnnotatedSource
Separates annotation lines from source lines. Tracks the real source line number that each annotation corresponds to.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 181 def self.parse(annotated_source) source = [] annotations = [] annotated_source.each_line do |source_line| if ANNOTATION_PATTERN.match?(source_line) annotations << [source.size, source_line] else source << source_line end end new(source, annotations) end |
Instance Method Details
#plain_source ⇒ String
Return the plain source code without annotations
243 244 245 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 243 def plain_source lines.join end |
#to_s ⇒ String
Construct annotated source string (like what we parse)
Reconstruct a deterministic annotated source string. This is useful for eliminating semantically irrelevant annotation ordering differences.
230 231 232 233 234 235 236 237 238 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 230 def to_s reconstructed = lines.dup annotations.reverse_each do |line_number, annotation| reconstructed.insert(line_number, annotation) end reconstructed.join end |
#with_offense_annotations(offenses) ⇒ self
Annotate the source code with the RuboCop offenses provided
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 252 def with_offense_annotations(offenses) offense_annotations = offenses.map do |offense| indent = ' ' * offense.column carets = '^' * offense.column_length [offense.line, "#{indent}#{carets} #{offense.}\n"] end self.class.new(lines, offense_annotations) end |