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

Commit 5c6615e

Browse files
committed
Handle comments on keywords in rescue and else clauses
1 parent 4efb084 commit 5c6615e

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

lib/syntax_tree/node.rb

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,9 @@ class BodyStmt < Node
22332233
# [nil | Rescue] the optional rescue chain attached to the begin clause
22342234
attr_reader :rescue_clause
22352235

2236+
# [nil | Kw] the optional else keyword
2237+
attr_reader :else_keyword
2238+
22362239
# [nil | Statements] the optional set of statements inside the else clause
22372240
attr_reader :else_clause
22382241

@@ -2245,13 +2248,15 @@ class BodyStmt < Node
22452248
def initialize(
22462249
statements:,
22472250
rescue_clause:,
2251+
else_keyword:,
22482252
else_clause:,
22492253
ensure_clause:,
22502254
location:,
22512255
comments: []
22522256
)
22532257
@statements = statements
22542258
@rescue_clause = rescue_clause
2259+
@else_keyword = else_keyword
22552260
@else_clause = else_clause
22562261
@ensure_clause = ensure_clause
22572262
@location = location
@@ -2294,7 +2299,7 @@ def accept(visitor)
22942299
end
22952300

22962301
def child_nodes
2297-
[statements, rescue_clause, else_clause, ensure_clause]
2302+
[statements, rescue_clause, else_keyword, else_clause, ensure_clause]
22982303
end
22992304

23002305
alias deconstruct child_nodes
@@ -2324,10 +2329,13 @@ def format(q)
23242329
if else_clause
23252330
q.nest(-2) do
23262331
q.breakable(force: true)
2327-
q.text("else")
2332+
q.format(else_keyword)
2333+
end
2334+
2335+
unless else_clause.empty?
2336+
q.breakable(force: true)
2337+
q.format(else_clause)
23282338
end
2329-
q.breakable(force: true)
2330-
q.format(else_clause)
23312339
end
23322340

23332341
if ensure_clause
@@ -4711,13 +4719,17 @@ def quotes(q)
47114719
# end
47124720
#
47134721
class Else < Node
4722+
# [Kw] the else keyword
4723+
attr_reader :keyword
4724+
47144725
# [Statements] the expressions to be executed
47154726
attr_reader :statements
47164727

47174728
# [Array[ Comment | EmbDoc ]] the comments attached to this node
47184729
attr_reader :comments
47194730

4720-
def initialize(statements:, location:, comments: [])
4731+
def initialize(keyword:, statements:, location:, comments: [])
4732+
@keyword = keyword
47214733
@statements = statements
47224734
@location = location
47234735
@comments = comments
@@ -4728,18 +4740,23 @@ def accept(visitor)
47284740
end
47294741

47304742
def child_nodes
4731-
[statements]
4743+
[keyword, statements]
47324744
end
47334745

47344746
alias deconstruct child_nodes
47354747

47364748
def deconstruct_keys(keys)
4737-
{ statements: statements, location: location, comments: comments }
4749+
{
4750+
keyword: keyword,
4751+
statements: statements,
4752+
location: location,
4753+
comments: comments
4754+
}
47384755
end
47394756

47404757
def format(q)
47414758
q.group do
4742-
q.text("else")
4759+
q.format(keyword)
47434760

47444761
unless statements.empty?
47454762
q.indent do
@@ -8993,6 +9010,9 @@ def to_json(*opts)
89939010
# end
89949011
#
89959012
class Rescue < Node
9013+
# [Kw] the rescue keyword
9014+
attr_reader :keyword
9015+
89969016
# [RescueEx] the exceptions being rescued
89979017
attr_reader :exception
89989018

@@ -9006,12 +9026,14 @@ class Rescue < Node
90069026
attr_reader :comments
90079027

90089028
def initialize(
9029+
keyword:,
90099030
exception:,
90109031
statements:,
90119032
consequent:,
90129033
location:,
90139034
comments: []
90149035
)
9036+
@keyword = keyword
90159037
@exception = exception
90169038
@statements = statements
90179039
@consequent = consequent
@@ -9041,13 +9063,14 @@ def accept(visitor)
90419063
end
90429064

90439065
def child_nodes
9044-
[exception, statements, consequent]
9066+
[keyword, exception, statements, consequent]
90459067
end
90469068

90479069
alias deconstruct child_nodes
90489070

90499071
def deconstruct_keys(keys)
90509072
{
9073+
keyword: keyword,
90519074
exception: exception,
90529075
statements: statements,
90539076
consequent: consequent,
@@ -9058,10 +9081,10 @@ def deconstruct_keys(keys)
90589081

90599082
def format(q)
90609083
q.group do
9061-
q.text("rescue")
9084+
q.format(keyword)
90629085

90639086
if exception
9064-
q.nest("rescue ".length) { q.format(exception) }
9087+
q.nest(keyword.value.length + 1) { q.format(exception) }
90659088
else
90669089
q.text(" StandardError")
90679090
end

lib/syntax_tree/parser.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ def on_bodystmt(statements, rescue_clause, else_clause, ensure_clause)
682682
BodyStmt.new(
683683
statements: statements,
684684
rescue_clause: rescue_clause,
685+
else_keyword: else_clause && find_token(Kw, "else"),
685686
else_clause: else_clause,
686687
ensure_clause: ensure_clause,
687688
location: Location.fixed(line: lineno, char: char_pos)
@@ -1115,7 +1116,7 @@ def on_dyna_symbol(string_content)
11151116
# :call-seq:
11161117
# on_else: (Statements statements) -> Else
11171118
def on_else(statements)
1118-
beginning = find_token(Kw, "else")
1119+
keyword = find_token(Kw, "else")
11191120

11201121
# else can either end with an end keyword (in which case we'll want to
11211122
# consume that event) or it can end with an ensure keyword (in which case
@@ -1127,13 +1128,16 @@ def on_else(statements)
11271128

11281129
node = tokens[index]
11291130
ending = node.value == "end" ? tokens.delete_at(index) : node
1130-
# ending = node
11311131

1132-
statements.bind(beginning.location.end_char, ending.location.start_char)
1132+
statements.bind(
1133+
find_next_statement_start(keyword.location.end_char),
1134+
ending.location.start_char
1135+
)
11331136

11341137
Else.new(
1138+
keyword: keyword,
11351139
statements: statements,
1136-
location: beginning.location.to(ending.location)
1140+
location: keyword.location.to(ending.location)
11371141
)
11381142
end
11391143

@@ -2316,6 +2320,7 @@ def on_rescue(exceptions, variable, statements, consequent)
23162320
end
23172321

23182322
Rescue.new(
2323+
keyword: keyword,
23192324
exception: rescue_ex,
23202325
statements: statements,
23212326
consequent: consequent,

test/fixtures/bodystmt.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@
3434
ensure
3535
foo
3636
end
37+
%
38+
begin
39+
else # else
40+
end
41+
%
42+
begin
43+
ensure # ensure
44+
end
45+
%
46+
begin
47+
rescue # rescue
48+
else # else
49+
ensure # ensure
50+
end
51+
-
52+
begin
53+
rescue StandardError # rescue
54+
else # else
55+
ensure # ensure
56+
end

test/fixtures/else.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
else
1919
bar
2020
end
21+
%
22+
if foo
23+
else # bar
24+
end

0 commit comments

Comments
 (0)