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

Commit a99d081

Browse files
committed
Fold DefEndless into Def and Defs nodes
1 parent 087350a commit a99d081

File tree

8 files changed

+70
-147
lines changed

8 files changed

+70
-147
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +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.
1819

1920
## [4.3.0] - 2022-10-28
2021

lib/syntax_tree/node.rb

Lines changed: 60 additions & 111 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, DefEndless
3111+
when Def, Defs
31123112
q.text(" ")
31133113
yield
31143114
when IfOp
@@ -3540,10 +3540,10 @@ class Def < Node
35403540
# [Backtick | Const | Ident | Kw | Op] the name of the method
35413541
attr_reader :name
35423542

3543-
# [Params | Paren] the parameter declaration for the method
3543+
# [nil | Params | Paren] the parameter declaration for the method
35443544
attr_reader :params
35453545

3546-
# [BodyStmt] the expressions to be executed by the method
3546+
# [BodyStmt | untyped] the expressions to be executed by the method
35473547
attr_reader :bodystmt
35483548

35493549
# [Array[ Comment | EmbDoc ]] the comments attached to this node
@@ -3583,112 +3583,41 @@ def format(q)
35833583
q.text("def ")
35843584
q.format(name)
35853585

3586-
if !params.is_a?(Params) || !params.empty? || params.comments.any?
3586+
case params
3587+
when Paren
35873588
q.format(params)
3589+
when Params
3590+
q.format(params) if !params.empty? || params.comments.any?
35883591
end
35893592
end
35903593

3591-
unless bodystmt.empty?
3592-
q.indent do
3593-
q.breakable_force
3594-
q.format(bodystmt)
3594+
if endless?
3595+
q.text(" =")
3596+
q.group do
3597+
q.indent do
3598+
q.breakable_space
3599+
q.format(bodystmt)
3600+
end
3601+
end
3602+
else
3603+
unless bodystmt.empty?
3604+
q.indent do
3605+
q.breakable_force
3606+
q.format(bodystmt)
3607+
end
35953608
end
3596-
end
35973609

3598-
q.breakable_force
3599-
q.text("end")
3610+
q.breakable_force
3611+
q.text("end")
3612+
end
36003613
end
36013614
end
3602-
end
3603-
3604-
# DefEndless represents defining a single-line method since Ruby 3.0+.
3605-
#
3606-
# def method = result
3607-
#
3608-
class DefEndless < Node
3609-
# [untyped] the target where the method is being defined
3610-
attr_reader :target
3611-
3612-
# [Op | Period] the operator being used to declare the method
3613-
attr_reader :operator
3614-
3615-
# [Backtick | Const | Ident | Kw | Op] the name of the method
3616-
attr_reader :name
3617-
3618-
# [nil | Params | Paren] the parameter declaration for the method
3619-
attr_reader :paren
3620-
3621-
# [untyped] the expression to be executed by the method
3622-
attr_reader :statement
3623-
3624-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
3625-
attr_reader :comments
3626-
3627-
def initialize(
3628-
target:,
3629-
operator:,
3630-
name:,
3631-
paren:,
3632-
statement:,
3633-
location:,
3634-
comments: []
3635-
)
3636-
@target = target
3637-
@operator = operator
3638-
@name = name
3639-
@paren = paren
3640-
@statement = statement
3641-
@location = location
3642-
@comments = []
3643-
end
3644-
3645-
def accept(visitor)
3646-
visitor.visit_def_endless(self)
3647-
end
36483615

3649-
def child_nodes
3650-
[target, operator, name, paren, statement]
3651-
end
3652-
3653-
alias deconstruct child_nodes
3654-
3655-
def deconstruct_keys(_keys)
3656-
{
3657-
target: target,
3658-
operator: operator,
3659-
name: name,
3660-
paren: paren,
3661-
statement: statement,
3662-
location: location,
3663-
comments: comments
3664-
}
3665-
end
3666-
3667-
def format(q)
3668-
q.group do
3669-
q.text("def ")
3670-
3671-
if target
3672-
q.format(target)
3673-
q.format(CallOperatorFormatter.new(operator), stackable: false)
3674-
end
3675-
3676-
q.format(name)
3677-
3678-
if paren
3679-
params = paren
3680-
params = params.contents if params.is_a?(Paren)
3681-
q.format(paren) unless params.empty?
3682-
end
3683-
3684-
q.text(" =")
3685-
q.group do
3686-
q.indent do
3687-
q.breakable_space
3688-
q.format(statement)
3689-
end
3690-
end
3691-
end
3616+
# Returns true if the method was found in the source in the "endless" form,
3617+
# i.e. where the method body is defined using the `=` operator after the
3618+
# method name and parameters.
3619+
def endless?
3620+
!bodystmt.is_a?(BodyStmt)
36923621
end
36933622
end
36943623

@@ -3751,10 +3680,10 @@ class Defs < Node
37513680
# [Backtick | Const | Ident | Kw | Op] the name of the method
37523681
attr_reader :name
37533682

3754-
# [Params | Paren] the parameter declaration for the method
3683+
# [nil | Params | Paren] the parameter declaration for the method
37553684
attr_reader :params
37563685

3757-
# [BodyStmt] the expressions to be executed by the method
3686+
# [BodyStmt | untyped] the expressions to be executed by the method
37583687
attr_reader :bodystmt
37593688

37603689
# [Array[ Comment | EmbDoc ]] the comments attached to this node
@@ -3808,22 +3737,42 @@ def format(q)
38083737
q.format(CallOperatorFormatter.new(operator), stackable: false)
38093738
q.format(name)
38103739

3811-
if !params.is_a?(Params) || !params.empty? || params.comments.any?
3740+
case params
3741+
when Paren
38123742
q.format(params)
3743+
when Params
3744+
q.format(params) if !params.empty? || params.comments.any?
38133745
end
38143746
end
38153747

3816-
unless bodystmt.empty?
3817-
q.indent do
3818-
q.breakable_force
3819-
q.format(bodystmt)
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
38203762
end
3821-
end
38223763

3823-
q.breakable_force
3824-
q.text("end")
3764+
q.breakable_force
3765+
q.text("end")
3766+
end
38253767
end
38263768
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
38273776
end
38283777

38293778
# DoBlock represents passing a block to a method call using the +do+ and +end+
@@ -6971,7 +6920,7 @@ def format(q)
69716920
end
69726921

69736922
case q.parent
6974-
when Def, Defs, DefEndless
6923+
when Def, Defs
69756924
q.nest(0) do
69766925
q.text("(")
69776926
q.group do

lib/syntax_tree/parser.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ def on_cvar(value)
11811181
# (Backtick | Const | Ident | Kw | Op) name,
11821182
# (nil | Params | Paren) params,
11831183
# untyped bodystmt
1184-
# ) -> Def | DefEndless
1184+
# ) -> Def
11851185
def on_def(name, params, bodystmt)
11861186
# Make sure to delete this token in case you're defining something like
11871187
# def class which would lead to this being a kw and causing all kinds of
@@ -1234,12 +1234,10 @@ def on_def(name, params, bodystmt)
12341234
# the statements list. Before, it was just the individual statement.
12351235
statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
12361236

1237-
DefEndless.new(
1238-
target: nil,
1239-
operator: nil,
1237+
Def.new(
12401238
name: name,
1241-
paren: params,
1242-
statement: statement,
1239+
params: params,
1240+
bodystmt: statement,
12431241
location: beginning.location.to(bodystmt.location)
12441242
)
12451243
end
@@ -1322,12 +1320,12 @@ def on_defs(target, operator, name, params, bodystmt)
13221320
# the statements list. Before, it was just the individual statement.
13231321
statement = bodystmt.is_a?(BodyStmt) ? bodystmt.statements : bodystmt
13241322

1325-
DefEndless.new(
1323+
Defs.new(
13261324
target: target,
13271325
operator: operator,
13281326
name: name,
1329-
paren: params,
1330-
statement: statement,
1327+
params: params,
1328+
bodystmt: statement,
13311329
location: beginning.location.to(bodystmt.location)
13321330
)
13331331
end

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ class Visitor < BasicVisitor
119119
# Visit a Def node.
120120
alias visit_def visit_child_nodes
121121

122-
# Visit a DefEndless node.
123-
alias visit_def_endless visit_child_nodes
124-
125122
# Visit a Defined node.
126123
alias visit_defined visit_child_nodes
127124

lib/syntax_tree/visitor/field_visitor.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,6 @@ def visit_def(node)
322322
end
323323
end
324324

325-
def visit_def_endless(node)
326-
node(node, "def_endless") do
327-
if node.target
328-
field("target", node.target)
329-
field("operator", node.operator)
330-
end
331-
332-
field("name", node.name)
333-
field("paren", node.paren) if node.paren
334-
field("statement", node.statement)
335-
comments(node)
336-
end
337-
end
338-
339325
def visit_defined(node)
340326
node(node, "defined") do
341327
field("value", node.value)

lib/syntax_tree/visitor/with_environment.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ def visit_defs(node)
6060
with_new_environment { super }
6161
end
6262

63-
def visit_def_endless(node)
64-
with_new_environment { super }
65-
end
66-
6763
# Visit for keeping track of local arguments, such as method and block
6864
# arguments
6965
def visit_params(node)

test/fixtures/def_endless.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ def foo = bar
44
def foo(bar) = baz
55
%
66
def foo() = bar
7-
-
8-
def foo = bar
97
% # >= 3.1.0
108
def foo = bar baz
119
% # >= 3.1.0
@@ -14,8 +12,6 @@ def self.foo = bar
1412
def self.foo(bar) = baz
1513
% # >= 3.1.0
1614
def self.foo() = bar
17-
-
18-
def self.foo = bar
1915
% # >= 3.1.0
2016
def self.foo = bar baz
2117
%

test/node_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ def method
379379

380380
guard_version("3.0.0") do
381381
def test_def_endless
382-
assert_node(DefEndless, "def method = result")
382+
assert_node(Def, "def method = result")
383383
end
384384
end
385385

386386
guard_version("3.1.0") do
387387
def test_def_endless_command
388-
assert_node(DefEndless, "def method = result argument")
388+
assert_node(Def, "def method = result argument")
389389
end
390390
end
391391

0 commit comments

Comments
 (0)