Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 919bda9

Browse files
committed
Remove escaped forward slashes from regular expression literals when converting to %r.
1 parent 875de00 commit 919bda9

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1010

1111
- The ability to "check" formatting by formatting the output of the first format.
1212
- Comments can now be attached to the `case` keyword.
13+
- Remove escaped forward slashes from regular expression literals when converting to `%r`.
1314

1415
### Changed
1516

lib/syntax_tree.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def on_END(statements)
612612
# some other content that is not executed by the program
613613
#
614614
class EndContent
615-
# [Array[ String ]] the content after the script
615+
# [String] the content after the script
616616
attr_reader :value
617617

618618
# [Location] the location of this node
@@ -635,25 +635,23 @@ def format(q)
635635
q.text("__END__")
636636
q.breakable(force: true)
637637

638-
q.seplist(value, -> { q.breakable(literal: true, force: true) }) do |line|
639-
q.text(line)
640-
end
638+
separator = -> { q.breakable(indent: false, force: true) }
639+
q.seplist(value.split(/\r?\n/, -1), separator) { |line| q.text(line) }
641640
end
642641

643642
def pretty_print(q)
644643
q.group(2, "(", ")") do
645644
q.text("__end__")
646645

647646
q.breakable
648-
q.pp(value.join("\n"))
647+
q.pp(value)
649648

650649
q.pp(Comment::List.new(comments))
651650
end
652651
end
653652

654653
def to_json(*opts)
655-
{ type: :__end__, value: value.join("\n"), loc: location, cmts: comments }
656-
.to_json(*opts)
654+
{ type: :__end__, value: value, loc: location, cmts: comments }.to_json(*opts)
657655
end
658656
end
659657

@@ -662,7 +660,7 @@ def to_json(*opts)
662660
def on___end__(value)
663661
@__end__ =
664662
EndContent.new(
665-
value: lines[lineno..-1],
663+
value: source[(char_pos + value.length)..-1],
666664
location: Location.token(line: lineno, char: char_pos, size: value.size)
667665
)
668666
end
@@ -9702,11 +9700,26 @@ def format(q)
97029700
q.format_each(parts)
97039701
q.text(ending)
97049702
end
9703+
elsif braces
9704+
q.group do
9705+
q.text("%r{")
9706+
9707+
parts.each do |part|
9708+
if part.is_a?(TStringContent)
9709+
q.text(part.value.gsub("\\/", "/"))
9710+
else
9711+
q.format(part)
9712+
end
9713+
end
9714+
9715+
q.text("}")
9716+
q.text(ending[1..-1])
9717+
end
97059718
else
97069719
q.group do
9707-
q.text(braces ? "%r{" : "/")
9720+
q.text("/")
97089721
q.format_each(parts)
9709-
q.text(braces ? "}" : "/")
9722+
q.text("/")
97109723
q.text(ending[1..-1])
97119724
end
97129725
end

test/fixtures/regexp_literal.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@
4949
foo %r{= bar}
5050
%
5151
foo(/ bar/)
52+
%
53+
/foo\/bar/
54+
-
55+
%r{foo/bar}

0 commit comments

Comments
 (0)