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

Commit 1d54928

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

File tree

9 files changed

+30
-141
lines changed

9 files changed

+30
-141
lines changed

lib/syntax_tree/dsl.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -654,15 +654,6 @@ def QSymbolsBeg(value)
654654
QSymbolsBeg.new(value: value, location: Location.default)
655655
end
656656

657-
# Create a new QWords node.
658-
def QWords(beginning, elements)
659-
QWords.new(
660-
beginning: beginning,
661-
elements: elements,
662-
location: Location.default
663-
)
664-
end
665-
666657
# Create a new QWordsBeg node.
667658
def QWordsBeg(value)
668659
QWordsBeg.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
@@ -658,13 +658,6 @@ def visit_qsymbols_beg(node)
658658
node(node, "qsymbols_beg") { field("value", node.value) }
659659
end
660660

661-
def visit_qwords(node)
662-
node(node, "qwords") do
663-
list("elements", node.elements)
664-
comments(node)
665-
end
666-
end
667-
668661
def visit_qwords_beg(node)
669662
node(node, "qwords_beg") { field("value", node.value) }
670663
end

lib/syntax_tree/mutation_visitor.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,14 +596,6 @@ def visit_qsymbols_beg(node)
596596
node.copy
597597
end
598598

599-
# Visit a QWords node.
600-
def visit_qwords(node)
601-
node.copy(
602-
beginning: visit(node.beginning),
603-
elements: visit_all(node.elements)
604-
)
605-
end
606-
607599
# Visit a QWordsBeg node.
608600
def visit_qwords_beg(node)
609601
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
1217+
when QSymbolsBeg, QWordsBeg
12181218
q.text(lbracket.value)
12191219
else
12201220
q.format(lbracket)
@@ -1224,7 +1224,7 @@ def format(q)
12241224
q.indent do
12251225
q.breakable_empty
12261226
case lbracket
1227-
when QSymbolsBeg
1227+
when QSymbolsBeg, QWordsBeg
12281228
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
12291229
q.text(part.value)
12301230
end
@@ -1238,7 +1238,7 @@ def format(q)
12381238
q.breakable_empty
12391239

12401240
case lbracket
1241-
when QSymbolsBeg
1241+
when QSymbolsBeg, QWordsBeg
12421242
q.text(lbracket.value[-1] == "{" ? "}" : "]")
12431243
else
12441244
q.text("]")
@@ -1416,7 +1416,7 @@ def ===(other)
14161416
module AssignFormatting
14171417
def self.skip_indent?(value)
14181418
case value
1419-
when ArrayLiteral, HashLiteral, Heredoc, Lambda, QWords, Symbols, Words
1419+
when ArrayLiteral, HashLiteral, Heredoc, Lambda, Symbols, Words
14201420
true
14211421
when CallNode
14221422
skip_indent?(value.receiver)
@@ -8674,82 +8674,6 @@ def ===(other)
86748674
end
86758675
end
86768676

8677-
# QWords represents a string literal array without interpolation.
8678-
#
8679-
# %w[one two three]
8680-
#
8681-
class QWords < Node
8682-
# [QWordsBeg] the token that opens this array literal
8683-
attr_reader :beginning
8684-
8685-
# [Array[ TStringContent ]] the elements of the array
8686-
attr_reader :elements
8687-
8688-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
8689-
attr_reader :comments
8690-
8691-
def initialize(beginning:, elements:, location:)
8692-
@beginning = beginning
8693-
@elements = elements
8694-
@location = location
8695-
@comments = []
8696-
end
8697-
8698-
def accept(visitor)
8699-
visitor.visit_qwords(self)
8700-
end
8701-
8702-
def child_nodes
8703-
[]
8704-
end
8705-
8706-
def copy(beginning: nil, elements: nil, location: nil)
8707-
QWords.new(
8708-
beginning: beginning || self.beginning,
8709-
elements: elements || self.elements,
8710-
location: location || self.location
8711-
)
8712-
end
8713-
8714-
alias deconstruct child_nodes
8715-
8716-
def deconstruct_keys(_keys)
8717-
{
8718-
beginning: beginning,
8719-
elements: elements,
8720-
location: location,
8721-
comments: comments
8722-
}
8723-
end
8724-
8725-
def format(q)
8726-
opening, closing = "%w[", "]"
8727-
8728-
if elements.any? { |element| element.match?(/[\[\]]/) }
8729-
opening = beginning.value
8730-
closing = Quotes.matching(opening[2])
8731-
end
8732-
8733-
q.text(opening)
8734-
q.group do
8735-
q.indent do
8736-
q.breakable_empty
8737-
q.seplist(
8738-
elements,
8739-
ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
8740-
) { |element| q.format(element) }
8741-
end
8742-
q.breakable_empty
8743-
end
8744-
q.text(closing)
8745-
end
8746-
8747-
def ===(other)
8748-
other.is_a?(QWords) && beginning === other.beginning &&
8749-
ArrayMatch.call(elements, other.elements)
8750-
end
8751-
end
8752-
87538677
# QWordsBeg represents the beginning of a string literal array.
87548678
#
87558679
# %w[one two three]

lib/syntax_tree/parser.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def on_args_new
618618

619619
# :call-seq:
620620
# on_array: ((nil | Args | Array [ TStringContent ]) contents) ->
621-
# ArrayLiteral | QWords | Symbols | Words
621+
# ArrayLiteral | Symbols | Words
622622
def on_array(contents)
623623
if !contents || contents.is_a?(Args)
624624
lbracket = consume_token(LBracket)
@@ -3054,11 +3054,14 @@ def on_qsymbols_new
30543054
end
30553055

30563056
# :call-seq:
3057-
# on_qwords_add: (QWords qwords, TStringContent element) -> QWords
3057+
# on_qwords_add: (ArrayLiteral qwords, TStringContent element) ->
3058+
# ArrayLiteral
30583059
def on_qwords_add(qwords, element)
3059-
QWords.new(
3060-
beginning: qwords.beginning,
3061-
elements: qwords.elements << element,
3060+
qwords.contents.parts << element
3061+
3062+
ArrayLiteral.new(
3063+
lbracket: qwords.lbracket,
3064+
contents: qwords.contents,
30623065
location: qwords.location.to(element.location)
30633066
)
30643067
end
@@ -3083,13 +3086,16 @@ def on_qwords_beg(value)
30833086
end
30843087

30853088
# :call-seq:
3086-
# on_qwords_new: () -> QWords
3089+
# on_qwords_new: () -> ArrayLiteral
30873090
def on_qwords_new
30883091
beginning = consume_token(QWordsBeg)
30893092

3090-
QWords.new(
3091-
beginning: beginning,
3092-
elements: [],
3093+
ArrayLiteral.new(
3094+
lbracket: beginning,
3095+
contents: Args.new(
3096+
parts: [],
3097+
location: beginning.location
3098+
),
30933099
location: beginning.location
30943100
)
30953101
end

lib/syntax_tree/translation/parser.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,17 +1906,6 @@ def visit_program(node)
19061906
visit(node.statements)
19071907
end
19081908

1909-
# Visit a QWords node.
1910-
def visit_qwords(node)
1911-
visit_array(
1912-
ArrayLiteral.new(
1913-
lbracket: node.beginning,
1914-
contents: Args.new(parts: node.elements, location: node.location),
1915-
location: node.location
1916-
)
1917-
)
1918-
end
1919-
19201909
# Visit a RangeNode node.
19211910
def visit_range(node)
19221911
s(

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ class Visitor < BasicVisitor
278278
# Visit a QSymbolsBeg node.
279279
alias visit_qsymbols_beg visit_child_nodes
280280

281-
# Visit a QWords node.
282-
alias visit_qwords visit_child_nodes
283-
284281
# Visit a QWordsBeg node.
285282
alias visit_qwords_beg visit_child_nodes
286283

lib/syntax_tree/yarv/compiler.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ def visit_mrhs(node)
178178
visit_all(node.parts)
179179
end
180180

181-
def visit_qwords(node)
182-
visit_all(node.elements)
183-
end
184-
185181
def visit_range(node)
186182
left, right = [visit(node.left), visit(node.right)]
187183
node.operator.value === ".." ? left..right : left...right
@@ -387,6 +383,16 @@ def visit_args(node)
387383
end
388384

389385
def visit_array(node)
386+
if node.lbracket.is_a?(QWordsBeg)
387+
if options.frozen_string_literal?
388+
iseq.duparray(node.accept(RubyVisitor.new))
389+
else
390+
visit(node.contents)
391+
iseq.newarray(node.contents.parts.length)
392+
end
393+
return
394+
end
395+
390396
if (compiled = RubyVisitor.compile(node))
391397
iseq.duparray(compiled)
392398
elsif node.contents && node.contents.parts.length == 1 &&
@@ -1412,15 +1418,6 @@ def visit_program(node)
14121418
top_iseq
14131419
end
14141420

1415-
def visit_qwords(node)
1416-
if options.frozen_string_literal?
1417-
iseq.duparray(node.accept(RubyVisitor.new))
1418-
else
1419-
visit_all(node.elements)
1420-
iseq.newarray(node.elements.length)
1421-
end
1422-
end
1423-
14241421
def visit_range(node)
14251422
if (compiled = RubyVisitor.compile(node))
14261423
iseq.putobject(compiled)

test/node_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def test_qsymbols
778778
end
779779

780780
def test_qwords
781-
assert_node(QWords, "%w[one two three]")
781+
assert_node(ArrayLiteral, "%w[one two three]")
782782
end
783783

784784
def test_rational

0 commit comments

Comments
 (0)