Skip to content

Commit ac41ba7

Browse files
committed
Reformat with syntax tree
1 parent f23454d commit ac41ba7

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

lib/syntax_tree/formatter.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ class Formatter < PrettierPrint
2626
attr_reader :quote, :trailing_comma
2727
alias trailing_comma? trailing_comma
2828

29-
def initialize(source, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma])
29+
def initialize(
30+
source,
31+
*args,
32+
quote: OPTIONS[:quote],
33+
trailing_comma: OPTIONS[:trailing_comma]
34+
)
3035
super(*args)
3136

3237
@source = source

lib/syntax_tree/node.rb

+5-15
Original file line numberDiff line numberDiff line change
@@ -2148,9 +2148,7 @@ def format(q)
21482148
#
21492149
q.text(" ")
21502150
format_array_contents(q, array)
2151-
in [
2152-
Paren[contents: { body: [ArrayLiteral => statement] }]
2153-
]
2151+
in [Paren[contents: { body: [ArrayLiteral => statement] }]]
21542152
# Here we have a single argument that is a set of parentheses wrapping
21552153
# an array literal that has 0 or 1 elements. We're going to skip the
21562154
# parentheses but print the array itself. This would be like if we
@@ -2178,19 +2176,15 @@ def format(q)
21782176
#
21792177
q.text(" ")
21802178
q.format(statement)
2181-
in [
2182-
Paren => part
2183-
]
2179+
in [Paren => part]
21842180
# Here we have a single argument that is a set of parentheses. We're
21852181
# going to print the parentheses themselves as if they were the set of
21862182
# arguments. This would be like if we had:
21872183
#
21882184
# break(foo.bar)
21892185
#
21902186
q.format(part)
2191-
in [
2192-
ArrayLiteral[contents: { parts: [_, _, *] }] => array
2193-
]
2187+
in [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
21942188
# Here there is a single argument that is an array literal with at
21952189
# least two elements. We skip directly into the array literal's
21962190
# elements in order to print the contents. This would be like if we
@@ -2204,9 +2198,7 @@ def format(q)
22042198
#
22052199
q.text(" ")
22062200
format_array_contents(q, array)
2207-
in [
2208-
ArrayLiteral => part
2209-
]
2201+
in [ArrayLiteral => part]
22102202
# Here there is a single argument that is an array literal with 0 or 1
22112203
# elements. In this case we're going to print the array as it is
22122204
# because skipping the brackets would change the remaining. This would
@@ -2217,9 +2209,7 @@ def format(q)
22172209
#
22182210
q.text(" ")
22192211
q.format(part)
2220-
in [
2221-
_
2222-
]
2212+
in [_]
22232213
# Here there is a single argument that hasn't matched one of our
22242214
# previous cases. We're going to print the argument as it is. This
22252215
# would be like if we had:

test/language_server_test.rb

+13-7
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ def test_formatting
111111
]
112112

113113
case run_server(messages)
114-
in { id: 1, result: { capabilities: Hash } },
115-
{ id: 2, result: [{ newText: new_text }] }
114+
in [
115+
{ id: 1, result: { capabilities: Hash } },
116+
{ id: 2, result: [{ newText: new_text }] }
117+
]
116118
assert_equal("class Bar\nend\n", new_text)
117119
end
118120
end
@@ -131,8 +133,10 @@ def test_inlay_hints
131133
]
132134

133135
case run_server(messages)
134-
in { id: 1, result: { capabilities: Hash } },
135-
{ id: 2, result: { before:, after: } }
136+
in [
137+
{ id: 1, result: { capabilities: Hash } },
138+
{ id: 2, result: { before:, after: } }
139+
]
136140
assert_equal(1, before.length)
137141
assert_equal(2, after.length)
138142
end
@@ -147,7 +151,7 @@ def test_visualizing
147151
]
148152

149153
case run_server(messages)
150-
in { id: 1, result: { capabilities: Hash } }, { id: 2, result: }
154+
in [{ id: 1, result: { capabilities: Hash } }, { id: 2, result: }]
151155
assert_equal(
152156
"(program (statements ((binary (int \"1\") + (int \"2\")))))\n",
153157
result
@@ -167,8 +171,10 @@ def test_reading_file
167171
]
168172

169173
case run_server(messages)
170-
in { id: 1, result: { capabilities: Hash } },
171-
{ id: 2, result: [{ newText: new_text }] }
174+
in [
175+
{ id: 1, result: { capabilities: Hash } },
176+
{ id: 2, result: [{ newText: new_text }] }
177+
]
172178
assert_equal("class Foo\nend\n", new_text)
173179
end
174180
end

test/location_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_deconstruct_keys
2323
location = Location.fixed(line: 1, char: 0, column: 0)
2424

2525
case location
26-
in { start_line: 1 }
26+
in start_line: 1
2727
end
2828
end
2929
end

test/parser_test.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ def test_parses_ripper_methods
3232
end
3333

3434
def test_errors_on_missing_token_with_location
35-
assert_raises(Parser::ParseError) do
36-
SyntaxTree.parse("\"foo")
37-
end
35+
assert_raises(Parser::ParseError) { SyntaxTree.parse("\"foo") }
3836
end
3937

4038
def test_errors_on_missing_token_without_location
41-
assert_raises(Parser::ParseError) do
42-
SyntaxTree.parse(":\"foo")
43-
end
39+
assert_raises(Parser::ParseError) { SyntaxTree.parse(":\"foo") }
4440
end
4541

4642
def test_handles_strings_with_non_terminated_embedded_expressions
47-
assert_raises(Parser::ParseError) do
48-
SyntaxTree.parse('"#{"')
49-
end
43+
assert_raises(Parser::ParseError) { SyntaxTree.parse('"#{"') }
5044
end
5145
end
5246
end

0 commit comments

Comments
 (0)