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

Commit 4d576c1

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

File tree

9 files changed

+36
-144
lines changed

9 files changed

+36
-144
lines changed

lib/syntax_tree/dsl.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -952,15 +952,6 @@ def Word(parts)
952952
Word.new(parts: parts, location: Location.default)
953953
end
954954

955-
# Create a new Words node.
956-
def Words(beginning, elements)
957-
Words.new(
958-
beginning: beginning,
959-
elements: elements,
960-
location: Location.default
961-
)
962-
end
963-
964955
# Create a new WordsBeg node.
965956
def WordsBeg(value)
966957
WordsBeg.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
@@ -959,13 +959,6 @@ def visit_word(node)
959959
end
960960
end
961961

962-
def visit_words(node)
963-
node(node, "words") do
964-
list("elements", node.elements)
965-
comments(node)
966-
end
967-
end
968-
969962
def visit_words_beg(node)
970963
node(node, "words_beg") { field("value", node.value) }
971964
end

lib/syntax_tree/mutation_visitor.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -863,14 +863,6 @@ def visit_word(node)
863863
node.copy(parts: visit_all(node.parts))
864864
end
865865

866-
# Visit a Words node.
867-
def visit_words(node)
868-
node.copy(
869-
beginning: visit(node.beginning),
870-
elements: visit_all(node.elements)
871-
)
872-
end
873-
874866
# Visit a WordsBeg node.
875867
def visit_words_beg(node)
876868
node.copy

lib/syntax_tree/node.rb

Lines changed: 9 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
1217+
when QSymbolsBeg, QWordsBeg, WordsBeg
12181218
q.text(lbracket.value)
12191219
else
12201220
q.format(lbracket)
@@ -1228,6 +1228,10 @@ def format(q)
12281228
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
12291229
q.text(part.value)
12301230
end
1231+
when WordsBeg
1232+
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
1233+
q.format(part)
1234+
end
12311235
else
12321236
q.format(contents)
12331237
q.if_break { q.text(",") } if q.trailing_comma?
@@ -1238,7 +1242,7 @@ def format(q)
12381242
q.breakable_empty
12391243

12401244
case lbracket
1241-
when QSymbolsBeg, QWordsBeg
1245+
when QSymbolsBeg, QWordsBeg, WordsBeg
12421246
q.text(lbracket.value[-1] == "{" ? "}" : "]")
12431247
else
12441248
q.text("]")
@@ -1416,7 +1420,7 @@ def ===(other)
14161420
module AssignFormatting
14171421
def self.skip_indent?(value)
14181422
case value
1419-
when ArrayLiteral, HashLiteral, Heredoc, Lambda, Symbols, Words
1423+
when ArrayLiteral, HashLiteral, Heredoc, Lambda, Symbols
14201424
true
14211425
when CallNode
14221426
skip_indent?(value.receiver)
@@ -11825,7 +11829,8 @@ def modifier?
1182511829
#
1182611830
# %W[a#{b}c xyz]
1182711831
#
11828-
# In the example above, there would be two Word nodes within a parent Words
11832+
# In the example above, there would be two Word nodes within a parent
11833+
# ArrayLiteral
1182911834
# node.
1183011835
class Word < Node
1183111836
# [Array[ StringEmbExpr | StringDVar | TStringContent ]] the parts of the
@@ -11879,82 +11884,6 @@ def ===(other)
1187911884
end
1188011885
end
1188111886

11882-
# Words represents a string literal array with interpolation.
11883-
#
11884-
# %W[one two three]
11885-
#
11886-
class Words < Node
11887-
# [WordsBeg] the token that opens this array literal
11888-
attr_reader :beginning
11889-
11890-
# [Array[ Word ]] the elements of this array
11891-
attr_reader :elements
11892-
11893-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
11894-
attr_reader :comments
11895-
11896-
def initialize(beginning:, elements:, location:)
11897-
@beginning = beginning
11898-
@elements = elements
11899-
@location = location
11900-
@comments = []
11901-
end
11902-
11903-
def accept(visitor)
11904-
visitor.visit_words(self)
11905-
end
11906-
11907-
def child_nodes
11908-
[]
11909-
end
11910-
11911-
def copy(beginning: nil, elements: nil, location: nil)
11912-
Words.new(
11913-
beginning: beginning || self.beginning,
11914-
elements: elements || self.elements,
11915-
location: location || self.location
11916-
)
11917-
end
11918-
11919-
alias deconstruct child_nodes
11920-
11921-
def deconstruct_keys(_keys)
11922-
{
11923-
beginning: beginning,
11924-
elements: elements,
11925-
location: location,
11926-
comments: comments
11927-
}
11928-
end
11929-
11930-
def format(q)
11931-
opening, closing = "%W[", "]"
11932-
11933-
if elements.any? { |element| element.match?(/[\[\]]/) }
11934-
opening = beginning.value
11935-
closing = Quotes.matching(opening[2])
11936-
end
11937-
11938-
q.text(opening)
11939-
q.group do
11940-
q.indent do
11941-
q.breakable_empty
11942-
q.seplist(
11943-
elements,
11944-
ArrayLiteral::BREAKABLE_SPACE_SEPARATOR
11945-
) { |element| q.format(element) }
11946-
end
11947-
q.breakable_empty
11948-
end
11949-
q.text(closing)
11950-
end
11951-
11952-
def ===(other)
11953-
other.is_a?(Words) && beginning === other.beginning &&
11954-
ArrayMatch.call(elements, other.elements)
11955-
end
11956-
end
11957-
1195811887
# WordsBeg represents the beginning of a string literal array with
1195911888
# interpolation.
1196011889
#

lib/syntax_tree/parser.rb

Lines changed: 14 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 | Symbols | Words
621+
# ArrayLiteral | Symbols
622622
def on_array(contents)
623623
if !contents || contents.is_a?(Args)
624624
lbracket = consume_token(LBracket)
@@ -4077,11 +4077,13 @@ def on_word_new
40774077
end
40784078

40794079
# :call-seq:
4080-
# on_words_add: (Words words, Word word) -> Words
4080+
# on_words_add: (ArrayLiteral words, Word word) -> ArrayLiteral
40814081
def on_words_add(words, word)
4082-
Words.new(
4083-
beginning: words.beginning,
4084-
elements: words.elements << word,
4082+
words.contents.parts << word
4083+
4084+
ArrayLiteral.new(
4085+
lbracket: words.lbracket,
4086+
contents: words.contents,
40854087
location: words.location.to(word.location)
40864088
)
40874089
end
@@ -4106,13 +4108,16 @@ def on_words_beg(value)
41064108
end
41074109

41084110
# :call-seq:
4109-
# on_words_new: () -> Words
4111+
# on_words_new: () -> ArrayLiteral
41104112
def on_words_new
41114113
beginning = consume_token(WordsBeg)
41124114

4113-
Words.new(
4114-
beginning: beginning,
4115-
elements: [],
4115+
ArrayLiteral.new(
4116+
lbracket: beginning,
4117+
contents: Args.new(
4118+
parts: [],
4119+
location: beginning.location
4120+
),
41164121
location: beginning.location
41174122
)
41184123
end

lib/syntax_tree/translation/parser.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,17 +2598,6 @@ def visit_word(node)
25982598
)
25992599
end
26002600

2601-
# Visit a Words node.
2602-
def visit_words(node)
2603-
visit_array(
2604-
ArrayLiteral.new(
2605-
lbracket: node.beginning,
2606-
contents: Args.new(parts: node.elements, location: node.location),
2607-
location: node.location
2608-
)
2609-
)
2610-
end
2611-
26122601
# Visit an XStringLiteral node.
26132602
def visit_xstring_literal(node)
26142603
s(

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,6 @@ class Visitor < BasicVisitor
425425
# Visit a Word node.
426426
alias visit_word visit_child_nodes
427427

428-
# Visit a Words node.
429-
alias visit_words visit_child_nodes
430-
431428
# Visit a WordsBeg node.
432429
alias visit_words_beg visit_child_nodes
433430

lib/syntax_tree/yarv/compiler.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,6 @@ def visit_word(node)
236236
raise CompilationError
237237
end
238238
end
239-
240-
def visit_words(node)
241-
visit_all(node.elements)
242-
end
243239
end
244240

245241
# This isn't actually a visit method, though maybe it should be. It is
@@ -383,14 +379,24 @@ def visit_args(node)
383379
end
384380

385381
def visit_array(node)
386-
if node.lbracket.is_a?(QWordsBeg)
382+
case node.lbracket
383+
when QWordsBeg
387384
if options.frozen_string_literal?
388385
iseq.duparray(node.accept(RubyVisitor.new))
389386
else
390387
visit(node.contents)
391388
iseq.newarray(node.contents.parts.length)
392389
end
393390
return
391+
when WordsBeg
392+
if options.frozen_string_literal? &&
393+
(compiled = RubyVisitor.compile(node))
394+
iseq.duparray(compiled)
395+
else
396+
visit_all(node.contents.parts)
397+
iseq.newarray(node.contents.parts.length)
398+
end
399+
return
394400
end
395401

396402
if (compiled = RubyVisitor.compile(node))
@@ -1824,16 +1830,6 @@ def visit_word(node)
18241830
end
18251831
end
18261832

1827-
def visit_words(node)
1828-
if options.frozen_string_literal? &&
1829-
(compiled = RubyVisitor.compile(node))
1830-
iseq.duparray(compiled)
1831-
else
1832-
visit_all(node.elements)
1833-
iseq.newarray(node.elements.length)
1834-
end
1835-
end
1836-
18371833
def visit_xstring_literal(node)
18381834
iseq.putself
18391835
length = visit_string_parts(node)

test/node_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,11 @@ def test_while_mod
990990

991991
def test_word
992992
at = location(chars: 3..7)
993-
assert_node(Word, "%W[word]", at: at) { |node| node.elements.first }
993+
assert_node(Word, "%W[word]", at: at) { |node| node.contents.parts.first }
994994
end
995995

996996
def test_words
997-
assert_node(Words, "%W[one two three]")
997+
assert_node(ArrayLiteral, "%W[one two three]")
998998
end
999999

10001000
def test_xstring_literal

0 commit comments

Comments
 (0)