Skip to content

Commit 617fd40

Browse files
authored
Merge pull request #13734 from dvandersluis/fix-expect-offense
Fix `ExpectOffense` when a source line contains an escaped caret
2 parents 3e23024 + a3e8d21 commit 617fd40

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/rubocop/rspec/expect_offense.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ def expect_no_corrections
190190
def expect_no_offenses(source, file = nil)
191191
offenses = inspect_source(source, file)
192192

193-
# Carets given in `expect_no_offenses` should not be treated as annotations so are escaped
194-
expected_annotations = AnnotatedSource.parse(source.gsub('^', '\^'))
193+
# Since source given `expect_no_offenses` does not have annotations, we do not need to parse
194+
# for them, and can just build an `AnnotatedSource` object from the source lines.
195+
# This also prevents treating source lines that begin with a caret as an annotation.
196+
expected_annotations = AnnotatedSource.new(source.each_line.to_a, [])
195197
actual_annotations = expected_annotations.with_offense_annotations(offenses)
196198
expect(actual_annotations.to_s).to eq(source)
197199
end
@@ -240,7 +242,7 @@ def self.parse(annotated_source)
240242
if ANNOTATION_PATTERN.match?(source_line)
241243
annotations << [source.size, source_line]
242244
else
243-
source << source_line.gsub('\^', '^')
245+
source << source_line
244246
end
245247
end
246248
annotations.each { |a| a[0] = 1 } if source.empty?

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy