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

Commit dcc66cb

Browse files
committed
Merge Symbols into ArrayLiteral
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent 4d576c1 commit dcc66cb

File tree

9 files changed

+51
-166
lines changed

9 files changed

+51
-166
lines changed

lib/syntax_tree/dsl.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,6 @@ def SymbolLiteral(value)
812812
SymbolLiteral.new(value: value, location: Location.default)
813813
end
814814

815-
# Create a new Symbols node.
816-
def Symbols(beginning, elements)
817-
Symbols.new(
818-
beginning: beginning,
819-
elements: elements,
820-
location: Location.default
821-
)
822-
end
823-
824815
# Create a new SymbolsBeg node.
825816
def SymbolsBeg(value)
826817
SymbolsBeg.new(value: value, location: Location.default)

lib/syntax_tree/field_visitor.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,13 +833,6 @@ def visit_symbol_literal(node)
833833
end
834834
end
835835

836-
def visit_symbols(node)
837-
node(node, "symbols") do
838-
list("elements", node.elements)
839-
comments(node)
840-
end
841-
end
842-
843836
def visit_symbols_beg(node)
844837
node(node, "symbols_beg") { field("value", node.value) }
845838
end

lib/syntax_tree/mutation_visitor.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,14 +736,6 @@ def visit_symbol_literal(node)
736736
node.copy(value: visit(node.value))
737737
end
738738

739-
# Visit a Symbols node.
740-
def visit_symbols(node)
741-
node.copy(
742-
beginning: visit(node.beginning),
743-
elements: visit_all(node.elements)
744-
)
745-
end
746-
747739
# Visit a SymbolsBeg node.
748740
def visit_symbols_beg(node)
749741
node.copy

lib/syntax_tree/node.rb

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ def format(q)
12141214

12151215
q.group do
12161216
case lbracket
1217-
when QSymbolsBeg, QWordsBeg, WordsBeg
1217+
when QSymbolsBeg, QWordsBeg, WordsBeg, SymbolsBeg
12181218
q.text(lbracket.value)
12191219
else
12201220
q.format(lbracket)
@@ -1228,7 +1228,7 @@ def format(q)
12281228
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
12291229
q.text(part.value)
12301230
end
1231-
when WordsBeg
1231+
when WordsBeg, SymbolsBeg
12321232
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
12331233
q.format(part)
12341234
end
@@ -1242,7 +1242,7 @@ def format(q)
12421242
q.breakable_empty
12431243

12441244
case lbracket
1245-
when QSymbolsBeg, QWordsBeg, WordsBeg
1245+
when QSymbolsBeg, QWordsBeg, WordsBeg, SymbolsBeg
12461246
q.text(lbracket.value[-1] == "{" ? "}" : "]")
12471247
else
12481248
q.text("]")
@@ -1420,7 +1420,7 @@ def ===(other)
14201420
module AssignFormatting
14211421
def self.skip_indent?(value)
14221422
case value
1423-
when ArrayLiteral, HashLiteral, Heredoc, Lambda, Symbols
1423+
when ArrayLiteral, HashLiteral, Heredoc, Lambda
14241424
true
14251425
when CallNode
14261426
skip_indent?(value.receiver)
@@ -10444,82 +10444,6 @@ def ===(other)
1044410444
end
1044510445
end
1044610446

10447-
# Symbols represents a symbol array literal with interpolation.
10448-
#
10449-
# %I[one two three]
10450-
#
10451-
class Symbols < Node
10452-
# [SymbolsBeg] the token that opens this array literal
10453-
attr_reader :beginning
10454-
10455-
# [Array[ Word ]] the words in the symbol array literal
10456-
attr_reader :elements
10457-
10458-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
10459-
attr_reader :comments
10460-
10461-
def initialize(beginning:, elements:, location:)
10462-
@beginning = beginning
10463-
@elements = elements
10464-
@location = location
10465-
@comments = []
10466-
end
10467-
10468-
def accept(visitor)
10469-
visitor.visit_symbols(self)
10470-
end
10471-
10472-
def child_nodes
10473-
[]
10474-
end
10475-
10476-
def copy(beginning: nil, elements: nil, location: nil)
10477-
Symbols.new(
10478-
beginning: beginning || self.beginning,
10479-
elements: elements || self.elements,
10480-
location: location || self.location
10481-
)
10482-
end
10483-
10484-
alias deconstruct child_nodes
10485-
10486-
def deconstruct_keys(_keys)
10487-
{
10488-
beginning: beginning,
10489-
elements: elements,
10490-
location: location,
10491-
comments: comments
10492-
}
10493-
end
10494-
10495-
def format(q)
10496-
opening, closing = "%I[", "]"
10497-
10498-
if elements.any? { |element| element.match?(/[\[\]]/) }
10499-
opening = beginning.value
10500-
closing = Quotes.matching(opening[2])
10501-
end
10502-
10503-
q.text(opening)
10504-
q.group do
10505-
q.indent do
10506-
q.breakable_empty
10507-
q.seplist(
10508-
elements,
10509-
ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
10510-
) { |element| q.format(element) }
10511-
end
10512-
q.breakable_empty
10513-
end
10514-
q.text(closing)
10515-
end
10516-
10517-
def ===(other)
10518-
other.is_a?(Symbols) && beginning === other.beginning &&
10519-
ArrayMatch.call(elements, other.elements)
10520-
end
10521-
end
10522-
1052310447
# SymbolsBeg represents the start of a symbol array literal with
1052410448
# interpolation.
1052510449
#

lib/syntax_tree/parser.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,11 +3625,13 @@ def on_symbol_literal(value)
36253625
end
36263626

36273627
# :call-seq:
3628-
# on_symbols_add: (Symbols symbols, Word word) -> Symbols
3628+
# on_symbols_add: (ArrayLiteral symbols, Word word) -> ArrayLiteral
36293629
def on_symbols_add(symbols, word)
3630-
Symbols.new(
3631-
beginning: symbols.beginning,
3632-
elements: symbols.elements << word,
3630+
symbols.contents.parts << word
3631+
3632+
ArrayLiteral.new(
3633+
lbracket: symbols.lbracket,
3634+
contents: symbols.contents,
36333635
location: symbols.location.to(word.location)
36343636
)
36353637
end
@@ -3654,13 +3656,16 @@ def on_symbols_beg(value)
36543656
end
36553657

36563658
# :call-seq:
3657-
# on_symbols_new: () -> Symbols
3659+
# on_symbols_new: () -> ArrayLiteral
36583660
def on_symbols_new
36593661
beginning = consume_token(SymbolsBeg)
36603662

3661-
Symbols.new(
3662-
beginning: beginning,
3663-
elements: [],
3663+
ArrayLiteral.new(
3664+
lbracket: beginning,
3665+
contents: Args.new(
3666+
parts: [],
3667+
location: beginning.location
3668+
),
36643669
location: beginning.location
36653670
)
36663671
end

lib/syntax_tree/translation/parser.rb

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,20 @@ def visit_array(node)
260260
visit_all(node.contents.parts.map do |part|
261261
SymbolLiteral.new(value: part, location: part.location)
262262
end)
263+
elsif node.lbracket.is_a?(SymbolsBeg)
264+
visit_all(node.contents.parts.map do |element|
265+
part = element.parts.first
266+
267+
if element.parts.length == 1 && part.is_a?(TStringContent)
268+
SymbolLiteral.new(value: part, location: part.location)
269+
else
270+
DynaSymbol.new(
271+
parts: element.parts,
272+
quote: nil,
273+
location: element.location
274+
)
275+
end
276+
end)
263277
else
264278
visit_all(node.contents.parts)
265279
end,
@@ -2266,32 +2280,6 @@ def visit_symbol_literal(node)
22662280
)
22672281
end
22682282

2269-
# Visit a Symbols node.
2270-
def visit_symbols(node)
2271-
parts =
2272-
node.elements.map do |element|
2273-
part = element.parts.first
2274-
2275-
if element.parts.length == 1 && part.is_a?(TStringContent)
2276-
SymbolLiteral.new(value: part, location: part.location)
2277-
else
2278-
DynaSymbol.new(
2279-
parts: element.parts,
2280-
quote: nil,
2281-
location: element.location
2282-
)
2283-
end
2284-
end
2285-
2286-
visit_array(
2287-
ArrayLiteral.new(
2288-
lbracket: node.beginning,
2289-
contents: Args.new(parts: parts, location: node.location),
2290-
location: node.location
2291-
)
2292-
)
2293-
end
2294-
22952283
# Visit a TopConstField node.
22962284
def visit_top_const_field(node)
22972285
s(

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,6 @@ class Visitor < BasicVisitor
365365
# Visit a SymbolLiteral node.
366366
alias visit_symbol_literal visit_child_nodes
367367

368-
# Visit a Symbols node.
369-
alias visit_symbols visit_child_nodes
370-
371368
# Visit a SymbolsBeg node.
372369
alias visit_symbols_beg visit_child_nodes
373370

lib/syntax_tree/yarv/compiler.rb

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def visit_array(node)
129129
return [] unless node.contents
130130

131131
case node.lbracket
132-
when QSymbolsBeg
132+
when QSymbolsBeg, SymbolsBeg
133133
visit_all(node.contents.parts).map(&:to_sym)
134134
else
135135
visit_all(node.contents.parts)
@@ -204,10 +204,6 @@ def visit_symbol_literal(node)
204204
node.value.value.to_sym
205205
end
206206

207-
def visit_symbols(node)
208-
node.elements.map { |element| visit(element).to_sym }
209-
end
210-
211207
def visit_tstring_content(node)
212208
node.value
213209
end
@@ -397,6 +393,24 @@ def visit_array(node)
397393
iseq.newarray(node.contents.parts.length)
398394
end
399395
return
396+
when SymbolsBeg
397+
if (compiled = RubyVisitor.compile(node))
398+
iseq.duparray(compiled)
399+
else
400+
node.contents.parts.each do |element|
401+
if element.parts.length == 1 &&
402+
element.parts.first.is_a?(TStringContent)
403+
iseq.putobject(element.parts.first.value.to_sym)
404+
else
405+
length = visit_string_parts(element)
406+
iseq.concatstrings(length)
407+
iseq.intern
408+
end
409+
end
410+
411+
iseq.newarray(node.contents.parts.length)
412+
end
413+
return
400414
end
401415

402416
if (compiled = RubyVisitor.compile(node))
@@ -1627,25 +1641,6 @@ def visit_symbol_literal(node)
16271641
iseq.putobject(node.accept(RubyVisitor.new))
16281642
end
16291643

1630-
def visit_symbols(node)
1631-
if (compiled = RubyVisitor.compile(node))
1632-
iseq.duparray(compiled)
1633-
else
1634-
node.elements.each do |element|
1635-
if element.parts.length == 1 &&
1636-
element.parts.first.is_a?(TStringContent)
1637-
iseq.putobject(element.parts.first.value.to_sym)
1638-
else
1639-
length = visit_string_parts(element)
1640-
iseq.concatstrings(length)
1641-
iseq.intern
1642-
end
1643-
end
1644-
1645-
iseq.newarray(node.elements.length)
1646-
end
1647-
end
1648-
16491644
def visit_top_const_ref(node)
16501645
iseq.opt_getconstant_path(constant_names(node))
16511646
end

test/node_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ def test_symbol_literal
889889
end
890890

891891
def test_symbols
892-
assert_node(Symbols, "%I[one two three]")
892+
assert_node(ArrayLiteral, "%I[one two three]")
893893
end
894894

895895
def test_top_const_field

0 commit comments

Comments
 (0)