File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed
lib/yard/parser/ruby/legacy Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -656,7 +656,7 @@ def lex_init()
656
656
if @lex_state != EXPR_END && @lex_state != EXPR_CLASS &&
657
657
( @lex_state != EXPR_ARG || @space_seen )
658
658
c = peek ( 0 )
659
- tk = identify_here_document if /[-\w \" \' \` ]/ =~ c
659
+ tk = identify_here_document if /[-~ \w \" \' \` ]/ =~ c
660
660
end
661
661
if !tk
662
662
@lex_state = EXPR_BEG
@@ -1063,6 +1063,8 @@ def identify_here_document
1063
1063
ch = getc
1064
1064
if ch == "-"
1065
1065
ch = getc
1066
+ elsif ch == "~"
1067
+ ch = getc
1066
1068
indent = true
1067
1069
end
1068
1070
if /['"`]/ =~ ch # '
@@ -1096,9 +1098,12 @@ def identify_here_document
1096
1098
str = String . new
1097
1099
while ( l = gets )
1098
1100
l . chomp!
1099
- l . strip! if indent
1100
- break if l == quoted
1101
- str << l . chomp << "\n "
1101
+ if l == quoted
1102
+ str = dedent ( str ) if indent
1103
+ break
1104
+ else
1105
+ str << l . chomp << "\n "
1106
+ end
1102
1107
end
1103
1108
1104
1109
@reader . divert_read_from ( reserve )
@@ -1108,6 +1113,16 @@ def identify_here_document
1108
1113
Token ( Ltype2Token [ lt ] , str ) . set_text ( str . dump )
1109
1114
end
1110
1115
1116
+ def dedent ( str )
1117
+ lines = str . split ( "\n " , -1 )
1118
+ dedent_amt = lines . map do |line |
1119
+ line =~ /\S / ? line . match ( /^ */ ) . offset ( 0 ) [ 1 ] : nil
1120
+ end . compact . min || 0
1121
+ return str if dedent_amt . zero?
1122
+
1123
+ lines . map { |line | line =~ /\S / ? line . gsub ( /^ {#{ dedent_amt } }/ , "" ) : line } . join ( "\n " )
1124
+ end
1125
+
1111
1126
def identify_quotation ( initial_char )
1112
1127
ch = getc
1113
1128
if lt = PERCENT_LTYPE [ ch ]
Original file line number Diff line number Diff line change @@ -294,6 +294,12 @@ class B; end
294
294
it "converts heredoc to string" do
295
295
src = "<<-XML\n foo\n \n XML"
296
296
s = stmt ( src )
297
- expect ( s . source ) . to eq '"foo\n\n"'
297
+ expect ( s . source ) . to eq '" foo\n\n"'
298
+ end
299
+
300
+ it "converts squiggly heredoc to string" do
301
+ src = "<<~XML\n bar\n \n XML"
302
+ s = stmt ( src )
303
+ expect ( s . source ) . to eq '"bar\n\n"'
298
304
end
299
305
end
You can’t perform that action at this time.
0 commit comments