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

Commit 3fc1e78

Browse files
committed
Merge QSymbols into ArrayLiteral
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent 61eefa8 commit 3fc1e78

File tree

9 files changed

+71
-154
lines changed

9 files changed

+71
-154
lines changed

lib/syntax_tree/dsl.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,6 @@ def Program(statements)
649649
Program.new(statements: statements, location: Location.default)
650650
end
651651

652-
# Create a new QSymbols node.
653-
def QSymbols(beginning, elements)
654-
QSymbols.new(
655-
beginning: beginning,
656-
elements: elements,
657-
location: Location.default
658-
)
659-
end
660-
661652
# Create a new QSymbolsBeg node.
662653
def QSymbolsBeg(value)
663654
QSymbolsBeg.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
@@ -654,13 +654,6 @@ def visit_program(node)
654654
end
655655
end
656656

657-
def visit_qsymbols(node)
658-
node(node, "qsymbols") do
659-
list("elements", node.elements)
660-
comments(node)
661-
end
662-
end
663-
664657
def visit_qsymbols_beg(node)
665658
node(node, "qsymbols_beg") { field("value", node.value) }
666659
end

lib/syntax_tree/mutation_visitor.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,6 @@ def visit_program(node)
591591
node.copy(statements: visit(node.statements))
592592
end
593593

594-
# Visit a QSymbols node.
595-
def visit_qsymbols(node)
596-
node.copy(
597-
beginning: visit(node.beginning),
598-
elements: visit_all(node.elements)
599-
)
600-
end
601-
602594
# Visit a QSymbolsBeg node.
603595
def visit_qsymbols_beg(node)
604596
node.copy

lib/syntax_tree/node.rb

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,12 @@ def initialize(lbracket)
11281128
def format(q)
11291129
q.group do
11301130
q.text("[")
1131-
q.indent do
1132-
lbracket.comments.each do |comment|
1133-
q.breakable_force
1134-
comment.format(q)
1131+
if lbracket.is_a?(LBracket)
1132+
q.indent do
1133+
lbracket.comments.each do |comment|
1134+
q.breakable_force
1135+
comment.format(q)
1136+
end
11351137
end
11361138
end
11371139
q.breakable_force
@@ -1211,18 +1213,36 @@ def format(q)
12111213
end
12121214

12131215
q.group do
1214-
q.format(lbracket)
1216+
case lbracket
1217+
when QSymbolsBeg
1218+
q.text(lbracket.value)
1219+
else
1220+
q.format(lbracket)
1221+
end
12151222

12161223
if contents
12171224
q.indent do
12181225
q.breakable_empty
1219-
q.format(contents)
1220-
q.if_break { q.text(",") } if q.trailing_comma?
1226+
case lbracket
1227+
when QSymbolsBeg
1228+
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
1229+
q.text(part.value)
1230+
end
1231+
else
1232+
q.format(contents)
1233+
q.if_break { q.text(",") } if q.trailing_comma?
1234+
end
12211235
end
12221236
end
12231237

12241238
q.breakable_empty
1225-
q.text("]")
1239+
1240+
case lbracket
1241+
when QSymbolsBeg
1242+
q.text(lbracket.value[-1] == "{" ? "}" : "]")
1243+
else
1244+
q.text("]")
1245+
end
12261246
end
12271247
end
12281248

@@ -1396,8 +1416,7 @@ def ===(other)
13961416
module AssignFormatting
13971417
def self.skip_indent?(value)
13981418
case value
1399-
when ArrayLiteral, HashLiteral, Heredoc, Lambda, QSymbols, QWords,
1400-
Symbols, Words
1419+
when ArrayLiteral, HashLiteral, Heredoc, Lambda, QWords, Symbols, Words
14011420
true
14021421
when CallNode
14031422
skip_indent?(value.receiver)
@@ -8613,86 +8632,6 @@ def ===(other)
86138632
end
86148633
end
86158634

8616-
# QSymbols represents a symbol literal array without interpolation.
8617-
#
8618-
# %i[one two three]
8619-
#
8620-
class QSymbols < Node
8621-
# [QSymbolsBeg] the token that opens this array literal
8622-
attr_reader :beginning
8623-
8624-
# [Array[ TStringContent ]] the elements of the array
8625-
attr_reader :elements
8626-
8627-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
8628-
attr_reader :comments
8629-
8630-
def initialize(beginning:, elements:, location:)
8631-
@beginning = beginning
8632-
@elements = elements
8633-
@location = location
8634-
@comments = []
8635-
end
8636-
8637-
def accept(visitor)
8638-
visitor.visit_qsymbols(self)
8639-
end
8640-
8641-
def child_nodes
8642-
[]
8643-
end
8644-
8645-
def copy(beginning: nil, elements: nil, location: nil)
8646-
node =
8647-
QSymbols.new(
8648-
beginning: beginning || self.beginning,
8649-
elements: elements || self.elements,
8650-
location: location || self.location
8651-
)
8652-
8653-
node.comments.concat(comments.map(&:copy))
8654-
node
8655-
end
8656-
8657-
alias deconstruct child_nodes
8658-
8659-
def deconstruct_keys(_keys)
8660-
{
8661-
beginning: beginning,
8662-
elements: elements,
8663-
location: location,
8664-
comments: comments
8665-
}
8666-
end
8667-
8668-
def format(q)
8669-
opening, closing = "%i[", "]"
8670-
8671-
if elements.any? { |element| element.match?(/[\[\]]/) }
8672-
opening = beginning.value
8673-
closing = Quotes.matching(opening[2])
8674-
end
8675-
8676-
q.text(opening)
8677-
q.group do
8678-
q.indent do
8679-
q.breakable_empty
8680-
q.seplist(
8681-
elements,
8682-
ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
8683-
) { |element| q.format(element) }
8684-
end
8685-
q.breakable_empty
8686-
end
8687-
q.text(closing)
8688-
end
8689-
8690-
def ===(other)
8691-
other.is_a?(QSymbols) && beginning === other.beginning &&
8692-
ArrayMatch.call(elements, other.elements)
8693-
end
8694-
end
8695-
86968635
# QSymbolsBeg represents the beginning of a symbol literal array.
86978636
#
86988637
# %i[one two three]

lib/syntax_tree/parser.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ def on_args_new
617617
end
618618

619619
# :call-seq:
620-
# on_array: ((nil | Args) contents) ->
621-
# ArrayLiteral | QSymbols | QWords | Symbols | Words
620+
# on_array: ((nil | Args | Array [ TStringContent ]) contents) ->
621+
# ArrayLiteral | QWords | Symbols | Words
622622
def on_array(contents)
623623
if !contents || contents.is_a?(Args)
624624
lbracket = consume_token(LBracket)
@@ -629,6 +629,14 @@ def on_array(contents)
629629
contents: contents,
630630
location: lbracket.location.to(rbracket.location)
631631
)
632+
elsif contents.is_a?(ArrayLiteral)
633+
tstring_end = consume_tstring_end(contents.lbracket.location)
634+
635+
ArrayLiteral.new(
636+
lbracket: contents.lbracket,
637+
contents: contents.contents,
638+
location: contents.location.to(tstring_end.location)
639+
)
632640
else
633641
tstring_end = consume_tstring_end(contents.beginning.location)
634642

@@ -2999,11 +3007,14 @@ def nearest_nodes(node, comment)
29993007
end
30003008

30013009
# :call-seq:
3002-
# on_qsymbols_add: (QSymbols qsymbols, TStringContent element) -> QSymbols
3010+
# on_qsymbols_add: (ArrayLiteral qsymbols, TStringContent element) ->
3011+
# ArrayLiteral
30033012
def on_qsymbols_add(qsymbols, element)
3004-
QSymbols.new(
3005-
beginning: qsymbols.beginning,
3006-
elements: qsymbols.elements << element,
3013+
qsymbols.contents.parts << element
3014+
3015+
ArrayLiteral.new(
3016+
lbracket: qsymbols.lbracket,
3017+
contents: qsymbols.contents,
30073018
location: qsymbols.location.to(element.location)
30083019
)
30093020
end
@@ -3028,13 +3039,16 @@ def on_qsymbols_beg(value)
30283039
end
30293040

30303041
# :call-seq:
3031-
# on_qsymbols_new: () -> QSymbols
3042+
# on_qsymbols_new: () -> ArrayLiteral
30323043
def on_qsymbols_new
30333044
beginning = consume_token(QSymbolsBeg)
30343045

3035-
QSymbols.new(
3036-
beginning: beginning,
3037-
elements: [],
3046+
ArrayLiteral.new(
3047+
lbracket: beginning,
3048+
contents: Args.new(
3049+
parts: [],
3050+
location: beginning.location
3051+
),
30383052
location: beginning.location
30393053
)
30403054
end

lib/syntax_tree/translation/parser.rb

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ def visit_args_forward(node)
254254
def visit_array(node)
255255
s(
256256
:array,
257-
node.contents ? visit_all(node.contents.parts) : [],
257+
if node.contents.nil?
258+
[]
259+
elsif node.lbracket.is_a?(QSymbolsBeg)
260+
visit_all(node.contents.parts.map do |part|
261+
SymbolLiteral.new(value: part, location: part.location)
262+
end)
263+
else
264+
visit_all(node.contents.parts)
265+
end,
258266
if node.lbracket.nil?
259267
smap_collection_bare(srange_node(node))
260268
else
@@ -1898,22 +1906,6 @@ def visit_program(node)
18981906
visit(node.statements)
18991907
end
19001908

1901-
# Visit a QSymbols node.
1902-
def visit_qsymbols(node)
1903-
parts =
1904-
node.elements.map do |element|
1905-
SymbolLiteral.new(value: element, location: element.location)
1906-
end
1907-
1908-
visit_array(
1909-
ArrayLiteral.new(
1910-
lbracket: node.beginning,
1911-
contents: Args.new(parts: parts, location: node.location),
1912-
location: node.location
1913-
)
1914-
)
1915-
end
1916-
19171909
# Visit a QWords node.
19181910
def visit_qwords(node)
19191911
visit_array(

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,6 @@ class Visitor < BasicVisitor
275275
# Visit a Program node.
276276
alias visit_program visit_child_nodes
277277

278-
# Visit a QSymbols node.
279-
alias visit_qsymbols visit_child_nodes
280-
281278
# Visit a QSymbolsBeg node.
282279
alias visit_qsymbols_beg visit_child_nodes
283280

lib/syntax_tree/yarv/compiler.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ def self.compile(node)
126126

127127
visit_methods do
128128
def visit_array(node)
129-
node.contents ? visit_all(node.contents.parts) : []
129+
return [] unless node.contents
130+
131+
case node.lbracket
132+
when QSymbolsBeg
133+
visit_all(node.contents.parts).map(&:to_sym)
134+
else
135+
visit_all(node.contents.parts)
136+
end
130137
end
131138

132139
def visit_bare_assoc_hash(node)
@@ -171,10 +178,6 @@ def visit_mrhs(node)
171178
visit_all(node.parts)
172179
end
173180

174-
def visit_qsymbols(node)
175-
node.elements.map { |element| visit(element).to_sym }
176-
end
177-
178181
def visit_qwords(node)
179182
visit_all(node.elements)
180183
end
@@ -1409,10 +1412,6 @@ def visit_program(node)
14091412
top_iseq
14101413
end
14111414

1412-
def visit_qsymbols(node)
1413-
iseq.duparray(node.accept(RubyVisitor.new))
1414-
end
1415-
14161415
def visit_qwords(node)
14171416
if options.frozen_string_literal?
14181417
iseq.duparray(node.accept(RubyVisitor.new))

test/node_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def test_program
774774
end
775775

776776
def test_qsymbols
777-
assert_node(QSymbols, "%i[one two three]")
777+
assert_node(ArrayLiteral, "%i[one two three]")
778778
end
779779

780780
def test_qwords

0 commit comments

Comments
 (0)