Skip to content

Commit 174c3ec

Browse files
committed
MLHSAddStar -> MLHS + ArgStar
1 parent 50f9fc1 commit 174c3ec

File tree

2 files changed

+21
-69
lines changed

2 files changed

+21
-69
lines changed

lib/syntax_tree.rb

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ def on_args_add_block(arguments, block)
830830
# method(*arguments)
831831
#
832832
class ArgStar
833-
# [untyped] the expression being splatted
833+
# [nil | untyped] the expression being splatted
834834
attr_reader :value
835835

836836
# [Location] the location of this node
@@ -3855,7 +3855,7 @@ def on_fndptn(constant, left, values, right)
38553855
# end
38563856
#
38573857
class For
3858-
# [MLHS | MLHSAddStar | VarField] the variable declaration being used to
3858+
# [MLHS | VarField] the variable declaration being used to
38593859
# pull values out of the object being enumerated
38603860
attr_reader :index
38613861

@@ -3903,7 +3903,7 @@ def to_json(*opts)
39033903

39043904
# :call-seq:
39053905
# on_for: (
3906-
# (MLHS | MLHSAddStar | VarField) value,
3906+
# (MLHS | VarField) value,
39073907
# untyped collection,
39083908
# Statements statements
39093909
# ) -> For
@@ -5238,8 +5238,8 @@ def on_method_add_block(call, block)
52385238
# first, second, third = value
52395239
#
52405240
class MLHS
5241-
# Array[ARefField | Field | Ident | MlhsParen | VarField] the parts of
5242-
# the left-hand side of a multiple assignment
5241+
# Array[ARefField | ArgStar | Field | Ident | MlhsParen | VarField] the
5242+
# parts of the left-hand side of a multiple assignment
52435243
attr_reader :parts
52445244

52455245
# [boolean] whether or not there is a trailing comma at the end of this
@@ -5276,14 +5276,14 @@ def to_json(*opts)
52765276
# (ARefField | Field | Ident | MlhsParen | VarField) part
52775277
# ) -> MLHS
52785278
def on_mlhs_add(mlhs, part)
5279-
if mlhs.parts.empty?
5280-
MLHS.new(parts: [part], location: part.location)
5281-
else
5282-
MLHS.new(
5283-
parts: mlhs.parts << part,
5284-
location: mlhs.location.to(part.location)
5285-
)
5286-
end
5279+
location =
5280+
if mlhs.parts.empty?
5281+
part.location
5282+
else
5283+
mlhs.location.to(part.location)
5284+
end
5285+
5286+
MLHS.new(parts: mlhs.parts << part, location: location)
52875287
end
52885288

52895289
# MLHSAddPost represents adding another set of variables onto a list of
@@ -5327,7 +5327,7 @@ def to_json(*opts)
53275327
end
53285328

53295329
# :call-seq:
5330-
# on_mlhs_add_post: (MLHSAddStar star, MLHS mlhs) -> MLHSAddPost
5330+
# on_mlhs_add_post: (MLHS star, MLHS mlhs) -> MLHSAddPost
53315331
def on_mlhs_add_post(star, mlhs)
53325332
MLHSAddPost.new(
53335333
star: star,
@@ -5336,61 +5336,20 @@ def on_mlhs_add_post(star, mlhs)
53365336
)
53375337
end
53385338

5339-
# MLHSAddStar represents a splatted variable inside of a multiple assignment
5340-
# on the left hand side.
5341-
#
5342-
# first, *rest = values
5343-
#
5344-
class MLHSAddStar
5345-
# [MLHS] the values before the starred expression
5346-
attr_reader :mlhs
5347-
5348-
# [nil | ARefField | Field | Ident | VarField] the expression being
5349-
# splatted
5350-
attr_reader :star
5351-
5352-
# [Location] the location of this node
5353-
attr_reader :location
5354-
5355-
def initialize(mlhs:, star:, location:)
5356-
@mlhs = mlhs
5357-
@star = star
5358-
@location = location
5359-
end
5360-
5361-
def pretty_print(q)
5362-
q.group(2, '(', ')') do
5363-
q.text('mlhs_add_star')
5364-
5365-
q.breakable
5366-
q.pp(mlhs)
5367-
5368-
q.breakable
5369-
q.pp(star)
5370-
end
5371-
end
5372-
5373-
def to_json(*opts)
5374-
{ type: :mlhs_add_star, mlhs: mlhs, star: star, loc: location }.to_json(
5375-
*opts
5376-
)
5377-
end
5378-
end
5379-
53805339
# :call-seq:
53815340
# on_mlhs_add_star: (
53825341
# MLHS mlhs,
53835342
# (nil | ARefField | Field | Ident | VarField) part
5384-
# ) -> MLHSAddStar
5343+
# ) -> MLHS
53855344
def on_mlhs_add_star(mlhs, part)
53865345
beginning = find_token(Op, '*')
53875346
ending = part || beginning
53885347

5389-
MLHSAddStar.new(
5390-
mlhs: mlhs,
5391-
star: part,
5392-
location: beginning.location.to(ending.location)
5393-
)
5348+
location = beginning.location.to(ending.location)
5349+
arg_star = ArgStar.new(value: part, location: location)
5350+
5351+
location = mlhs.location.to(location) unless mlhs.parts.empty?
5352+
MLHS.new(parts: mlhs.parts << arg_star, location: location)
53945353
end
53955354

53965355
# :call-seq:

test/syntax_tree_test.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,17 +663,10 @@ def test_mlhs
663663
def test_mlhs_add_post
664664
source = 'left, *middle, right = values'
665665

666-
at = location(chars: 6..20)
666+
at = location(chars: 0..20)
667667
assert_node(MLHSAddPost, 'mlhs_add_post', source, at: at, &:target)
668668
end
669669

670-
def test_mlhs_add_star
671-
source = 'first, *rest = values'
672-
673-
at = location(chars: 7..12)
674-
assert_node(MLHSAddStar, 'mlhs_add_star', source, at: at, &:target)
675-
end
676-
677670
def test_mlhs_paren
678671
source = '(left, right) = value'
679672

0 commit comments

Comments
 (0)