Skip to content

Commit 31da8dc

Browse files
committed
Simplify, simplify
1 parent 8ad14cb commit 31da8dc

File tree

2 files changed

+45
-182
lines changed

2 files changed

+45
-182
lines changed

lib/syntax_tree.rb

Lines changed: 39 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,7 @@ def to_json(*opts)
11911191
# :call-seq:
11921192
# on_assoc_new: (untyped key, untyped value) -> Assoc
11931193
def on_assoc_new(key, value)
1194-
Assoc.new(
1195-
key: key,
1196-
value: value,
1197-
location: key.location.to(value.location)
1198-
)
1194+
Assoc.new(key: key, value: value, location: key.location.to(value.location))
11991195
end
12001196

12011197
# AssocSplat represents double-splatting a value into a hash (either a hash
@@ -1236,48 +1232,9 @@ def on_assoc_splat(value)
12361232
AssocSplat.new(value: value, location: operator.location.to(value.location))
12371233
end
12381234

1239-
# AssocListFromArgs represents the key-value pairs of a hash literal. Its
1240-
# parent node is always a hash.
1241-
#
1242-
# { key1: value1, key2: value2 }
1243-
#
1244-
class AssocListFromArgs
1245-
# [Array[ AssocNew | AssocSplat ]]
1246-
attr_reader :assocs
1247-
1248-
# [Location] the location of this node
1249-
attr_reader :location
1250-
1251-
def initialize(assocs:, location:)
1252-
@assocs = assocs
1253-
@location = location
1254-
end
1255-
1256-
def pretty_print(q)
1257-
q.group(2, '(', ')') do
1258-
q.text('assoclist_from_args')
1259-
q.breakable
1260-
q.group(2, '(', ')') { q.seplist(assocs) { |assoc| q.pp(assoc) } }
1261-
end
1262-
end
1263-
1264-
def to_json(*opts)
1265-
{ type: :assoclist_from_args, assocs: assocs, loc: location }.to_json(
1266-
*opts
1267-
)
1268-
end
1269-
end
1270-
1271-
# :call-seq:
1272-
# on_assoclist_from_args: (
1273-
# Array[AssocNew | AssocSplat] assocs
1274-
# ) -> AssocListFromArgs
1275-
def on_assoclist_from_args(assocs)
1276-
AssocListFromArgs.new(
1277-
assocs: assocs,
1278-
location: assocs[0].location.to(assocs[-1].location)
1279-
)
1280-
end
1235+
# def on_assoclist_from_args(assocs)
1236+
# assocs
1237+
# end
12811238

12821239
# Backref represents a global variable referencing a matched value. It comes
12831240
# in the form of a $ followed by a positive integer.
@@ -3977,53 +3934,41 @@ def on_gvar(value)
39773934
# { key => value }
39783935
#
39793936
class HashLiteral
3980-
# [nil | AssocListFromArgs] the contents of the hash
3981-
attr_reader :contents
3937+
# [Array[ AssocNew | AssocSplat ]] the optional contents of the hash
3938+
attr_reader :assocs
39823939

39833940
# [Location] the location of this node
39843941
attr_reader :location
39853942

3986-
def initialize(contents:, location:)
3987-
@contents = contents
3943+
def initialize(assocs:, location:)
3944+
@assocs = assocs
39883945
@location = location
39893946
end
39903947

39913948
def pretty_print(q)
39923949
q.group(2, '(', ')') do
39933950
q.text('hash')
39943951

3995-
q.breakable
3996-
q.pp(contents)
3952+
if assocs.any?
3953+
q.breakable
3954+
q.group(2, '(', ')') { q.seplist(assocs) { |assoc| q.pp(assoc) } }
3955+
end
39973956
end
39983957
end
39993958

40003959
def to_json(*opts)
4001-
{ type: :hash, cnts: contents, loc: location }.to_json(*opts)
3960+
{ type: :hash, assocs: assocs, loc: location }.to_json(*opts)
40023961
end
40033962
end
40043963

40053964
# :call-seq:
4006-
# on_hash: ((nil | AssocListFromArgs) contents) -> HashLiteral
4007-
def on_hash(contents)
3965+
# on_hash: ((nil | Array[AssocNew | AssocSplat]) assocs) -> HashLiteral
3966+
def on_hash(assocs)
40083967
lbrace = find_token(LBrace)
40093968
rbrace = find_token(RBrace)
40103969

4011-
if contents
4012-
# Here we're going to expand out the location information for the contents
4013-
# node so that it can grab up any remaining comments inside the hash.
4014-
location =
4015-
Location.new(
4016-
start_line: contents.location.start_line,
4017-
start_char: lbrace.location.end_char,
4018-
end_line: contents.location.end_line,
4019-
end_char: rbrace.location.start_char
4020-
)
4021-
4022-
contents = contents.class.new(assocs: contents.assocs, location: location)
4023-
end
4024-
40253970
HashLiteral.new(
4026-
contents: contents,
3971+
assocs: assocs || [],
40273972
location: lbrace.location.to(rbrace.location)
40283973
)
40293974
end
@@ -5270,11 +5215,7 @@ def to_json(*opts)
52705215
# ) -> MLHS
52715216
def on_mlhs_add(mlhs, part)
52725217
location =
5273-
if mlhs.parts.empty?
5274-
part.location
5275-
else
5276-
mlhs.location.to(part.location)
5277-
end
5218+
mlhs.parts.empty? ? part.location : mlhs.location.to(part.location)
52785219

52795220
MLHS.new(parts: mlhs.parts << part, location: location)
52805221
end
@@ -5460,112 +5401,42 @@ def on_mrhs_new
54605401
# :call-seq:
54615402
# on_mrhs_add: (MRHS mrhs, untyped part) -> MRHS
54625403
def on_mrhs_add(mrhs, part)
5463-
if mrhs.is_a?(MRHSNewFromArgs)
5464-
MRHS.new(
5465-
parts: [*mrhs.arguments.parts, part],
5466-
location: mrhs.location.to(part.location)
5467-
)
5468-
elsif mrhs.parts.empty?
5469-
MRHS.new(parts: [part], location: mrhs.location)
5470-
else
5471-
MRHS.new(parts: mrhs.parts << part, loc: mrhs.location.to(part.location))
5472-
end
5473-
end
5474-
5475-
# MRHSAddStar represents using the splat operator to expand out a value on the
5476-
# right hand side of a multiple assignment.
5477-
#
5478-
# values = first, *rest
5479-
#
5480-
class MRHSAddStar
5481-
# [MRHS | MRHSNewFromArgs] the values before the splatted expression
5482-
attr_reader :mrhs
5483-
5484-
# [untyped] the splatted expression
5485-
attr_reader :star
5486-
5487-
# [Location] the location of this node
5488-
attr_reader :location
5489-
5490-
def initialize(mrhs:, star:, location:)
5491-
@mrhs = mrhs
5492-
@star = star
5493-
@location = location
5494-
end
5495-
5496-
def pretty_print(q)
5497-
q.group(2, '(', ')') do
5498-
q.text('mrhs_add_star')
5499-
5500-
q.breakable
5501-
q.pp(mrhs)
5502-
5503-
q.breakable
5504-
q.pp(star)
5404+
location =
5405+
if mrhs.parts.empty?
5406+
mrhs.location
5407+
else
5408+
mrhs.location.to(part.location)
55055409
end
5506-
end
55075410

5508-
def to_json(*opts)
5509-
{ type: :mrhs_add_star, mrhs: mrhs, star: star, loc: location }.to_json(
5510-
*opts
5511-
)
5512-
end
5411+
MRHS.new(parts: mrhs.parts << part, location: location)
55135412
end
55145413

55155414
# :call-seq:
5516-
# on_mrhs_add_star: (
5517-
# (MRHS | MRHSNewFromArgs) mrhs,
5518-
# untyped star
5519-
# ) -> MRHSAddStar
5520-
def on_mrhs_add_star(mrhs, star)
5415+
# on_mrhs_add_star: (MRHS mrhs, untyped value) -> MRHS
5416+
def on_mrhs_add_star(mrhs, value)
55215417
beginning = find_token(Op, '*')
5522-
ending = star || beginning
5418+
ending = value || beginning
55235419

5524-
MRHSAddStar.new(
5525-
mrhs: mrhs,
5526-
star: star,
5527-
location: beginning.location.to(ending.location)
5528-
)
5529-
end
5530-
5531-
# MRHSNewFromArgs represents the shorthand of a multiple assignment that
5532-
# allows you to assign values using just commas as opposed to assigning from
5533-
# an array.
5534-
#
5535-
# values = first, second, third
5536-
#
5537-
class MRHSNewFromArgs
5538-
# [Args] the arguments being used in the assignment
5539-
attr_reader :arguments
5540-
5541-
# [Location] the location of this node
5542-
attr_reader :location
5543-
5544-
def initialize(arguments:, location:)
5545-
@arguments = arguments
5546-
@location = location
5547-
end
5548-
5549-
def pretty_print(q)
5550-
q.group(2, '(', ')') do
5551-
q.text('mrhs_new_from_args')
5420+
arg_star =
5421+
ArgStar.new(
5422+
value: value,
5423+
location: beginning.location.to(ending.location)
5424+
)
55525425

5553-
q.breakable
5554-
q.pp(arguments)
5426+
location =
5427+
if mrhs.parts.empty?
5428+
arg_star.location
5429+
else
5430+
mrhs.location.to(arg_star.location)
55555431
end
5556-
end
55575432

5558-
def to_json(*opts)
5559-
{ type: :mrhs_new_from_args, args: arguments, loc: location }.to_json(
5560-
*opts
5561-
)
5562-
end
5433+
MRHS.new(parts: mrhs.parts << arg_star, location: location)
55635434
end
55645435

55655436
# :call-seq:
5566-
# on_mrhs_new_from_args: (Args arguments) -> MRHSNewFromArgs
5437+
# on_mrhs_new_from_args: (Args arguments) -> MRHS
55675438
def on_mrhs_new_from_args(arguments)
5568-
MRHSNewFromArgs.new(arguments: arguments, location: arguments.location)
5439+
MRHS.new(parts: arguments.parts, location: arguments.location)
55695440
end
55705441

55715442
# Next represents using the +next+ keyword.

test/syntax_tree_test.rb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def test_assoc
167167

168168
at = location(chars: 2..14)
169169
assert_node(Assoc, 'assoc', source, at: at) do |node|
170-
node.contents.assocs.first
170+
node.assocs.first
171171
end
172172
end
173173

@@ -176,18 +176,10 @@ def test_assoc_splat
176176

177177
at = location(chars: 2..9)
178178
assert_node(AssocSplat, 'assoc_splat', source, at: at) do |node|
179-
node.contents.assocs.first
179+
node.assocs.first
180180
end
181181
end
182182

183-
def test_assoclist_from_args
184-
type = 'assoclist_from_args'
185-
source = '{ key1: value1, key2: value2 }'
186-
187-
at = location(chars: 1..29)
188-
assert_node(AssocListFromArgs, type, source, at: at, &:contents)
189-
end
190-
191183
def test_backref
192184
assert_node(Backref, 'backref', '$1')
193185
end
@@ -408,7 +400,7 @@ def test_dyna_symbol_hash_key
408400

409401
at = location(chars: 2..11)
410402
assert_node(DynaSymbol, 'dyna_symbol', source, at: at) do |node|
411-
node.contents.assocs.first.key
403+
node.assocs.first.key
412404
end
413405
end
414406

@@ -605,7 +597,7 @@ def test_label
605597

606598
at = location(chars: 2..6)
607599
assert_node(Label, 'label', source, at: at) do |node|
608-
node.contents.assocs.first.key
600+
node.assocs.first.key
609601
end
610602
end
611603

@@ -689,8 +681,8 @@ def test_mrhs
689681
def test_mrhs_add_star
690682
source = 'values = first, *rest'
691683

692-
at = location(chars: 16..21)
693-
assert_node(MRHSAddStar, 'mrhs_add_star', source, at: at, &:value)
684+
at = location(chars: 9..21)
685+
assert_node(MRHS, 'mrhs', source, at: at, &:value)
694686
end
695687

696688
def test_next

0 commit comments

Comments
 (0)