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

Commit 0c0e329

Browse files
committed
Ensure Words, Symbols, QWords, and QSymbols properly format when their contents contain brackets.
1 parent 85911c9 commit 0c0e329

File tree

6 files changed

+82
-16
lines changed

6 files changed

+82
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6262
- Make sure `StringLiteral` and `StringEmbExpr` know that they can be extended by heredocs.
6363
- Ensure `Int` nodes with preceding unary `+` get formatted properly.
6464
- Properly handle byte-order mark column offsets at the beginnings of files.
65+
- Ensure `Words`, `Symbols`, `QWords`, and `QSymbols` properly format when their contents contain brackets.
6566

6667
### Removed
6768

lib/syntax_tree.rb

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,7 @@ def on_array(contents)
15061506
tstring_end = find_token(TStringEnd)
15071507

15081508
contents.class.new(
1509+
beginning: contents.beginning,
15091510
elements: contents.elements,
15101511
location: contents.location.to(tstring_end.location)
15111512
)
@@ -9257,6 +9258,9 @@ def nearest_nodes(node, comment)
92579258
# %i[one two three]
92589259
#
92599260
class QSymbols
9261+
# [QSymbolsBeg] the token that opens this array literal
9262+
attr_reader :beginning
9263+
92609264
# [Array[ TStringContent ]] the elements of the array
92619265
attr_reader :elements
92629266

@@ -9266,7 +9270,8 @@ class QSymbols
92669270
# [Array[ Comment | EmbDoc ]] the comments attached to this node
92679271
attr_reader :comments
92689272

9269-
def initialize(elements:, location:, comments: [])
9273+
def initialize(beginning:, elements:, location:, comments: [])
9274+
@beginning = beginning
92709275
@elements = elements
92719276
@location = location
92729277
@comments = comments
@@ -9277,7 +9282,14 @@ def child_nodes
92779282
end
92789283

92799284
def format(q)
9280-
q.group(0, "%i[", "]") do
9285+
opening, closing = "%i[", "]"
9286+
9287+
if elements.any? { |element| element.match?(/[\[\]]/) }
9288+
opening = beginning.value
9289+
closing = Quotes.matching(opening[2])
9290+
end
9291+
9292+
q.group(0, opening, closing) do
92819293
q.indent do
92829294
q.breakable("")
92839295
q.seplist(elements, -> { q.breakable }) do |element|
@@ -9313,6 +9325,7 @@ def to_json(*opts)
93139325
# on_qsymbols_add: (QSymbols qsymbols, TStringContent element) -> QSymbols
93149326
def on_qsymbols_add(qsymbols, element)
93159327
QSymbols.new(
9328+
beginning: qsymbols.beginning,
93169329
elements: qsymbols.elements << element,
93179330
location: qsymbols.location.to(element.location)
93189331
)
@@ -9354,16 +9367,19 @@ def on_qsymbols_beg(value)
93549367
# :call-seq:
93559368
# on_qsymbols_new: () -> QSymbols
93569369
def on_qsymbols_new
9357-
qsymbols_beg = find_token(QSymbolsBeg)
9370+
beginning = find_token(QSymbolsBeg)
93589371

9359-
QSymbols.new(elements: [], location: qsymbols_beg.location)
9372+
QSymbols.new(beginning: beginning, elements: [], location: beginning.location)
93609373
end
93619374

93629375
# QWords represents a string literal array without interpolation.
93639376
#
93649377
# %w[one two three]
93659378
#
93669379
class QWords
9380+
# [QWordsBeg] the token that opens this array literal
9381+
attr_reader :beginning
9382+
93679383
# [Array[ TStringContent ]] the elements of the array
93689384
attr_reader :elements
93699385

@@ -9373,7 +9389,8 @@ class QWords
93739389
# [Array[ Comment | EmbDoc ]] the comments attached to this node
93749390
attr_reader :comments
93759391

9376-
def initialize(elements:, location:, comments: [])
9392+
def initialize(beginning:, elements:, location:, comments: [])
9393+
@beginning = beginning
93779394
@elements = elements
93789395
@location = location
93799396
@comments = comments
@@ -9384,7 +9401,14 @@ def child_nodes
93849401
end
93859402

93869403
def format(q)
9387-
q.group(0, "%w[", "]") do
9404+
opening, closing = "%w[", "]"
9405+
9406+
if elements.any? { |element| element.match?(/[\[\]]/) }
9407+
opening = beginning.value
9408+
closing = Quotes.matching(opening[2])
9409+
end
9410+
9411+
q.group(0, opening, closing) do
93889412
q.indent do
93899413
q.breakable("")
93909414
q.seplist(elements, -> { q.breakable }) do |element|
@@ -9417,6 +9441,7 @@ def to_json(*opts)
94179441
# on_qwords_add: (QWords qwords, TStringContent element) -> QWords
94189442
def on_qwords_add(qwords, element)
94199443
QWords.new(
9444+
beginning: qwords.beginning,
94209445
elements: qwords.elements << element,
94219446
location: qwords.location.to(element.location)
94229447
)
@@ -9458,9 +9483,9 @@ def on_qwords_beg(value)
94589483
# :call-seq:
94599484
# on_qwords_new: () -> QWords
94609485
def on_qwords_new
9461-
qwords_beg = find_token(QWordsBeg)
9486+
beginning = find_token(QWordsBeg)
94629487

9463-
QWords.new(elements: [], location: qwords_beg.location)
9488+
QWords.new(beginning: beginning, elements: [], location: beginning.location)
94649489
end
94659490

94669491
# RationalLiteral represents the use of a rational number literal.
@@ -11331,6 +11356,9 @@ def on_symbol_literal(value)
1133111356
# %I[one two three]
1133211357
#
1133311358
class Symbols
11359+
# [SymbolsBeg] the token that opens this array literal
11360+
attr_reader :beginning
11361+
1133411362
# [Array[ Word ]] the words in the symbol array literal
1133511363
attr_reader :elements
1133611364

@@ -11340,7 +11368,8 @@ class Symbols
1134011368
# [Array[ Comment | EmbDoc ]] the comments attached to this node
1134111369
attr_reader :comments
1134211370

11343-
def initialize(elements:, location:, comments: [])
11371+
def initialize(beginning:, elements:, location:, comments: [])
11372+
@beginning = beginning
1134411373
@elements = elements
1134511374
@location = location
1134611375
@comments = comments
@@ -11351,7 +11380,14 @@ def child_nodes
1135111380
end
1135211381

1135311382
def format(q)
11354-
q.group(0, "%I[", "]") do
11383+
opening, closing = "%I[", "]"
11384+
11385+
if elements.any? { |element| element.match?(/[\[\]]/) }
11386+
opening = beginning.value
11387+
closing = Quotes.matching(opening[2])
11388+
end
11389+
11390+
q.group(0, opening, closing) do
1135511391
q.indent do
1135611392
q.breakable("")
1135711393
q.seplist(elements, -> { q.breakable }) do |element|
@@ -11387,6 +11423,7 @@ def to_json(*opts)
1138711423
# on_symbols_add: (Symbols symbols, Word word) -> Symbols
1138811424
def on_symbols_add(symbols, word)
1138911425
Symbols.new(
11426+
beginning: symbols.beginning,
1139011427
elements: symbols.elements << word,
1139111428
location: symbols.location.to(word.location)
1139211429
)
@@ -11429,9 +11466,9 @@ def on_symbols_beg(value)
1142911466
# :call-seq:
1143011467
# on_symbols_new: () -> Symbols
1143111468
def on_symbols_new
11432-
symbols_beg = find_token(SymbolsBeg)
11469+
beginning = find_token(SymbolsBeg)
1143311470

11434-
Symbols.new(elements: [], location: symbols_beg.location)
11471+
Symbols.new(beginning: beginning, elements: [], location: beginning.location)
1143511472
end
1143611473

1143711474
# TLambda represents the beginning of a lambda literal.
@@ -11682,6 +11719,10 @@ def initialize(value:, location:, comments: [])
1168211719
@comments = comments
1168311720
end
1168411721

11722+
def match?(pattern)
11723+
value.match?(pattern)
11724+
end
11725+
1168511726
def child_nodes
1168611727
[]
1168711728
end
@@ -13008,6 +13049,10 @@ def initialize(parts:, location:, comments: [])
1300813049
@comments = comments
1300913050
end
1301013051

13052+
def match?(pattern)
13053+
parts.any? { |part| part.is_a?(TStringContent) && part.match?(pattern) }
13054+
end
13055+
1301113056
def child_nodes
1301213057
parts
1301313058
end
@@ -13057,6 +13102,9 @@ def on_word_new
1305713102
# %W[one two three]
1305813103
#
1305913104
class Words
13105+
# [WordsBeg] the token that opens this array literal
13106+
attr_reader :beginning
13107+
1306013108
# [Array[ Word ]] the elements of this array
1306113109
attr_reader :elements
1306213110

@@ -13066,7 +13114,8 @@ class Words
1306613114
# [Array[ Comment | EmbDoc ]] the comments attached to this node
1306713115
attr_reader :comments
1306813116

13069-
def initialize(elements:, location:, comments: [])
13117+
def initialize(beginning:, elements:, location:, comments: [])
13118+
@beginning = beginning
1307013119
@elements = elements
1307113120
@location = location
1307213121
@comments = comments
@@ -13077,7 +13126,14 @@ def child_nodes
1307713126
end
1307813127

1307913128
def format(q)
13080-
q.group(0, "%W[", "]") do
13129+
opening, closing = "%W[", "]"
13130+
13131+
if elements.any? { |element| element.match?(/[\[\]]/) }
13132+
opening = beginning.value
13133+
closing = Quotes.matching(opening[2])
13134+
end
13135+
13136+
q.group(0, opening, closing) do
1308113137
q.indent do
1308213138
q.breakable("")
1308313139
q.seplist(elements, -> { q.breakable }) do |element|
@@ -13110,6 +13166,7 @@ def to_json(*opts)
1311013166
# on_words_add: (Words words, Word word) -> Words
1311113167
def on_words_add(words, word)
1311213168
Words.new(
13169+
beginning: words.beginning,
1311313170
elements: words.elements << word,
1311413171
location: words.location.to(word.location)
1311513172
)
@@ -13152,9 +13209,9 @@ def on_words_beg(value)
1315213209
# :call-seq:
1315313210
# on_words_new: () -> Words
1315413211
def on_words_new
13155-
words_beg = find_token(WordsBeg)
13212+
beginning = find_token(WordsBeg)
1315613213

13157-
Words.new(elements: [], location: words_beg.location)
13214+
Words.new(beginning: beginning, elements: [], location: beginning.location)
1315813215
end
1315913216

1316013217
# def on_words_sep(value)

test/fixtures/qsymbols.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
%i[foo]
1616
%
1717
%i[foo] # comment
18+
%
19+
%i{foo[]}

test/fixtures/qwords.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
%w[foo]
1616
%
1717
%w[foo] # comment
18+
%
19+
%w{foo[]}

test/fixtures/symbols.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
%I[foo]
1818
%
1919
%I[foo] # comment
20+
%
21+
%I{foo[]}

test/fixtures/words.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
%W[foo]
1818
%
1919
%W[foo] # comment
20+
%
21+
%W{foo[]}

0 commit comments

Comments
 (0)