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

Commit 4a9a7c6

Browse files
committed
Fold Def and DefEndless
1 parent a99d081 commit 4a9a7c6

File tree

7 files changed

+33
-138
lines changed

7 files changed

+33
-138
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1515
- `Yield0` is no longer a node. Instead if has been folded into the `Yield` node. The `Yield` node can now have its `arguments` field be `nil`. Consequently, the `visit_yield0` method has been removed from the visitor interface. If you were previously using this method, you should now use `visit_yield` instead.
1616
- `FCall` is no longer a node. Instead it has been folded into the `Call` node. The `Call` node can now have its `receiver` and `operator` fields be `nil`. Consequently, the `visit_fcall` method has been removed from the visitor interface. If you were previously using this method, you should now use `visit_call` instead.
1717
- `Dot2` and `Dot3` are no longer nodes. Instead they have become a single new `RangeLiteral` node. This node looks the same as `Dot2` and `Dot3`, except that it additionally has an `operator` field that contains the operator that created the node. Consequently, the `visit_dot2` and `visit_dot3` methods have been removed from the visitor interface. If you were previously using these methods, you should now use `visit_range_literal` instead.
18-
- `DefEndless` has been folded into the `Def` and `Defs` nodes.
18+
- `DefEndless` and `Defs` have both been folded into the `Def` node. The `Def` node now has the `target` and `operator` fields which originally came from `Defs` which can both be `nil`. It also now has an `endless?` method on it to tell if the original node was found in the endless form. Finally the `bodystmt` field can now either be a `BodyStmt` as it was or any other kind of node since that was the body of the `DefEndless` node. The `visit_defs` and `visit_def_endless` methods on the visitor have therefore been removed.
1919

2020
## [4.3.0] - 2022-10-28
2121

lib/syntax_tree/node.rb

Lines changed: 21 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ def align(q, node, &block)
31083108
part = parts.first
31093109

31103110
case part
3111-
when Def, Defs
3111+
when Def
31123112
q.text(" ")
31133113
yield
31143114
when IfOp
@@ -3535,8 +3535,15 @@ def format(q)
35353535
# Def represents defining a regular method on the current self object.
35363536
#
35373537
# def method(param) result end
3538+
# def object.method(param) result end
35383539
#
35393540
class Def < Node
3541+
# [nil | untyped] the target where the method is being defined
3542+
attr_reader :target
3543+
3544+
# [nil | Op | Period] the operator being used to declare the method
3545+
attr_reader :operator
3546+
35403547
# [Backtick | Const | Ident | Kw | Op] the name of the method
35413548
attr_reader :name
35423549

@@ -3549,7 +3556,9 @@ class Def < Node
35493556
# [Array[ Comment | EmbDoc ]] the comments attached to this node
35503557
attr_reader :comments
35513558

3552-
def initialize(name:, params:, bodystmt:, location:)
3559+
def initialize(target:, operator:, name:, params:, bodystmt:, location:)
3560+
@target = target
3561+
@operator = operator
35533562
@name = name
35543563
@params = params
35553564
@bodystmt = bodystmt
@@ -3562,13 +3571,15 @@ def accept(visitor)
35623571
end
35633572

35643573
def child_nodes
3565-
[name, params, bodystmt]
3574+
[target, operator, name, params, bodystmt]
35663575
end
35673576

35683577
alias deconstruct child_nodes
35693578

35703579
def deconstruct_keys(_keys)
35713580
{
3581+
target: target,
3582+
operator: operator,
35723583
name: name,
35733584
params: params,
35743585
bodystmt: bodystmt,
@@ -3581,6 +3592,12 @@ def format(q)
35813592
q.group do
35823593
q.group do
35833594
q.text("def ")
3595+
3596+
if target
3597+
q.format(target)
3598+
q.format(CallOperatorFormatter.new(operator), stackable: false)
3599+
end
3600+
35843601
q.format(name)
35853602

35863603
case params
@@ -3666,115 +3683,6 @@ def format(q)
36663683
end
36673684
end
36683685

3669-
# Defs represents defining a singleton method on an object.
3670-
#
3671-
# def object.method(param) result end
3672-
#
3673-
class Defs < Node
3674-
# [untyped] the target where the method is being defined
3675-
attr_reader :target
3676-
3677-
# [Op | Period] the operator being used to declare the method
3678-
attr_reader :operator
3679-
3680-
# [Backtick | Const | Ident | Kw | Op] the name of the method
3681-
attr_reader :name
3682-
3683-
# [nil | Params | Paren] the parameter declaration for the method
3684-
attr_reader :params
3685-
3686-
# [BodyStmt | untyped] the expressions to be executed by the method
3687-
attr_reader :bodystmt
3688-
3689-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
3690-
attr_reader :comments
3691-
3692-
def initialize(
3693-
target:,
3694-
operator:,
3695-
name:,
3696-
params:,
3697-
bodystmt:,
3698-
location:,
3699-
comments: []
3700-
)
3701-
@target = target
3702-
@operator = operator
3703-
@name = name
3704-
@params = params
3705-
@bodystmt = bodystmt
3706-
@location = location
3707-
@comments = []
3708-
end
3709-
3710-
def accept(visitor)
3711-
visitor.visit_defs(self)
3712-
end
3713-
3714-
def child_nodes
3715-
[target, operator, name, params, bodystmt]
3716-
end
3717-
3718-
alias deconstruct child_nodes
3719-
3720-
def deconstruct_keys(_keys)
3721-
{
3722-
target: target,
3723-
operator: operator,
3724-
name: name,
3725-
params: params,
3726-
bodystmt: bodystmt,
3727-
location: location,
3728-
comments: comments
3729-
}
3730-
end
3731-
3732-
def format(q)
3733-
q.group do
3734-
q.group do
3735-
q.text("def ")
3736-
q.format(target)
3737-
q.format(CallOperatorFormatter.new(operator), stackable: false)
3738-
q.format(name)
3739-
3740-
case params
3741-
when Paren
3742-
q.format(params)
3743-
when Params
3744-
q.format(params) if !params.empty? || params.comments.any?
3745-
end
3746-
end
3747-
3748-
if endless?
3749-
q.text(" =")
3750-
q.group do
3751-
q.indent do
3752-
q.breakable_space
3753-
q.format(bodystmt)
3754-
end
3755-
end
3756-
else
3757-
unless bodystmt.empty?
3758-
q.indent do
3759-
q.breakable_force
3760-
q.format(bodystmt)
3761-
end
3762-
end
3763-
3764-
q.breakable_force
3765-
q.text("end")
3766-
end
3767-
end
3768-
end
3769-
3770-
# Returns true if the method was found in the source in the "endless" form,
3771-
# i.e. where the method body is defined using the `=` operator after the
3772-
# method name and parameters.
3773-
def endless?
3774-
!bodystmt.is_a?(BodyStmt)
3775-
end
3776-
end
3777-
37783686
# DoBlock represents passing a block to a method call using the +do+ and +end+
37793687
# keywords.
37803688
#
@@ -6919,8 +6827,7 @@ def format(q)
69196827
return
69206828
end
69216829

6922-
case q.parent
6923-
when Def, Defs
6830+
if q.parent.is_a?(Def)
69246831
q.nest(0) do
69256832
q.text("(")
69266833
q.group do

lib/syntax_tree/parser.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ def on_def(name, params, bodystmt)
12241224
)
12251225

12261226
Def.new(
1227+
target: nil,
1228+
operator: nil,
12271229
name: name,
12281230
params: params,
12291231
bodystmt: bodystmt,
@@ -1235,6 +1237,8 @@ def on_def(name, params, bodystmt)
12351237
statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
12361238

12371239
Def.new(
1240+
target: nil,
1241+
operator: nil,
12381242
name: name,
12391243
params: params,
12401244
bodystmt: statement,
@@ -1268,7 +1272,7 @@ def on_defined(value)
12681272
# (Backtick | Const | Ident | Kw | Op) name,
12691273
# (Params | Paren) params,
12701274
# BodyStmt bodystmt
1271-
# ) -> Defs
1275+
# ) -> Def
12721276
def on_defs(target, operator, name, params, bodystmt)
12731277
# Make sure to delete this token in case you're defining something
12741278
# like def class which would lead to this being a kw and causing all kinds
@@ -1307,7 +1311,7 @@ def on_defs(target, operator, name, params, bodystmt)
13071311
ending.location.start_column
13081312
)
13091313

1310-
Defs.new(
1314+
Def.new(
13111315
target: target,
13121316
operator: operator,
13131317
name: name,
@@ -1320,7 +1324,7 @@ def on_defs(target, operator, name, params, bodystmt)
13201324
# the statements list. Before, it was just the individual statement.
13211325
statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
13221326

1323-
Defs.new(
1327+
Def.new(
13241328
target: target,
13251329
operator: operator,
13261330
name: name,

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ class Visitor < BasicVisitor
122122
# Visit a Defined node.
123123
alias visit_defined visit_child_nodes
124124

125-
# Visit a Defs node.
126-
alias visit_defs visit_child_nodes
127-
128125
# Visit a DoBlock node.
129126
alias visit_do_block visit_child_nodes
130127

lib/syntax_tree/visitor/field_visitor.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ def visit_cvar(node)
315315

316316
def visit_def(node)
317317
node(node, "def") do
318+
field("target", node.target)
319+
field("operator", node.operator)
318320
field("name", node.name)
319321
field("params", node.params)
320322
field("bodystmt", node.bodystmt)
@@ -329,17 +331,6 @@ def visit_defined(node)
329331
end
330332
end
331333

332-
def visit_defs(node)
333-
node(node, "defs") do
334-
field("target", node.target)
335-
field("operator", node.operator)
336-
field("name", node.name)
337-
field("params", node.params)
338-
field("bodystmt", node.bodystmt)
339-
comments(node)
340-
end
341-
end
342-
343334
def visit_do_block(node)
344335
node(node, "do_block") do
345336
field("block_var", node.block_var) if node.block_var

lib/syntax_tree/visitor/with_environment.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def visit_def(node)
5656
with_new_environment { super }
5757
end
5858

59-
def visit_defs(node)
60-
with_new_environment { super }
61-
end
62-
6359
# Visit for keeping track of local arguments, such as method and block
6460
# arguments
6561
def visit_params(node)

test/node_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_defined
394394
end
395395

396396
def test_defs
397-
assert_node(Defs, "def object.method(param) result end")
397+
assert_node(Def, "def object.method(param) result end")
398398
end
399399

400400
def test_defs_paramless
@@ -403,7 +403,7 @@ def object.method
403403
end
404404
SOURCE
405405

406-
assert_node(Defs, source)
406+
assert_node(Def, source)
407407
end
408408

409409
def test_do_block

0 commit comments

Comments
 (0)