Skip to content

Commit f23454d

Browse files
committed
Remodel the plugins so that the options are available
1 parent e68f225 commit f23454d

File tree

10 files changed

+179
-183
lines changed

10 files changed

+179
-183
lines changed

lib/syntax_tree/formatter.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ module SyntaxTree
44
# A slightly enhanced PP that knows how to format recursively including
55
# comments.
66
class Formatter < PrettierPrint
7+
# We want to minimize as much as possible the number of options that are
8+
# available in syntax tree. For the most part, if users want non-default
9+
# formatting, they should override the format methods on the specific nodes
10+
# themselves. However, because of some history with prettier and the fact
11+
# that folks have become entrenched in their ways, we decided to provide a
12+
# small amount of configurability.
13+
#
14+
# Note that we're keeping this in a global-ish hash instead of just
15+
# overriding methods on classes so that other plugins can reference this if
16+
# necessary. For example, the RBS plugin references the quote style.
17+
OPTIONS = { quote: "\"", trailing_comma: false }
18+
719
COMMENT_PRIORITY = 1
820
HEREDOC_PRIORITY = 2
921

@@ -14,13 +26,15 @@ class Formatter < PrettierPrint
1426
attr_reader :quote, :trailing_comma
1527
alias trailing_comma? trailing_comma
1628

17-
def initialize(source, ...)
18-
super(...)
29+
def initialize(source, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma])
30+
super(*args)
1931

2032
@source = source
2133
@stack = []
22-
@quote = "\""
23-
@trailing_comma = false
34+
35+
# Memoizing these values per formatter to make access faster.
36+
@quote = quote
37+
@trailing_comma = trailing_comma
2438
end
2539

2640
def self.format(source, node)

lib/syntax_tree/formatter/single_quotes.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/syntax_tree/formatter/trailing_comma.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# frozen_string_literal: true
22

3-
require "syntax_tree/formatter/single_quotes"
4-
SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::SingleQuotes)
3+
SyntaxTree::Formatter::OPTIONS[:quote] = "'"
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# frozen_string_literal: true
22

3-
require "syntax_tree/formatter/trailing_comma"
4-
SyntaxTree::Formatter.prepend(SyntaxTree::Formatter::TrailingComma)
3+
SyntaxTree::Formatter::OPTIONS[:trailing_comma] = true

test/formatter/single_quotes_test.rb

Lines changed: 0 additions & 51 deletions
This file was deleted.

test/formatter/trailing_comma_test.rb

Lines changed: 0 additions & 97 deletions
This file was deleted.

test/plugin/single_quotes_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module SyntaxTree
6+
class SingleQuotesTest < Minitest::Test
7+
OPTIONS = Plugin.options("syntax_tree/plugin/single_quotes")
8+
9+
def test_empty_string_literal
10+
assert_format("''\n", "\"\"")
11+
end
12+
13+
def test_string_literal
14+
assert_format("'string'\n", "\"string\"")
15+
end
16+
17+
def test_string_literal_with_interpolation
18+
assert_format("\"\#{foo}\"\n")
19+
end
20+
21+
def test_dyna_symbol
22+
assert_format(":'symbol'\n", ":\"symbol\"")
23+
end
24+
25+
def test_single_quote_in_string
26+
assert_format("\"str'ing\"\n")
27+
end
28+
29+
def test_label
30+
assert_format(
31+
"{ foo => foo, :'bar' => bar }\n",
32+
"{ foo => foo, \"bar\": bar }"
33+
)
34+
end
35+
36+
private
37+
38+
def assert_format(expected, source = expected)
39+
formatter = Formatter.new(source, [], **OPTIONS)
40+
SyntaxTree.parse(source).format(formatter)
41+
42+
formatter.flush
43+
assert_equal(expected, formatter.output.join)
44+
end
45+
end
46+
end

test/plugin/trailing_comma_test.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module SyntaxTree
6+
class TrailingCommaTest < Minitest::Test
7+
OPTIONS = Plugin.options("syntax_tree/plugin/trailing_comma")
8+
9+
def test_arg_paren_flat
10+
assert_format("foo(a)\n")
11+
end
12+
13+
def test_arg_paren_break
14+
assert_format(<<~EXPECTED, <<~SOURCE)
15+
foo(
16+
#{"a" * 80},
17+
)
18+
EXPECTED
19+
foo(#{"a" * 80})
20+
SOURCE
21+
end
22+
23+
def test_arg_paren_block
24+
assert_format(<<~EXPECTED, <<~SOURCE)
25+
foo(
26+
&#{"a" * 80}
27+
)
28+
EXPECTED
29+
foo(&#{"a" * 80})
30+
SOURCE
31+
end
32+
33+
def test_arg_paren_command
34+
assert_format(<<~EXPECTED, <<~SOURCE)
35+
foo(
36+
bar #{"a" * 80}
37+
)
38+
EXPECTED
39+
foo(bar #{"a" * 80})
40+
SOURCE
41+
end
42+
43+
def test_arg_paren_command_call
44+
assert_format(<<~EXPECTED, <<~SOURCE)
45+
foo(
46+
bar.baz #{"a" * 80}
47+
)
48+
EXPECTED
49+
foo(bar.baz #{"a" * 80})
50+
SOURCE
51+
end
52+
53+
def test_array_literal_flat
54+
assert_format("[a]\n")
55+
end
56+
57+
def test_array_literal_break
58+
assert_format(<<~EXPECTED, <<~SOURCE)
59+
[
60+
#{"a" * 80},
61+
]
62+
EXPECTED
63+
[#{"a" * 80}]
64+
SOURCE
65+
end
66+
67+
def test_hash_literal_flat
68+
assert_format("{ a: a }\n")
69+
end
70+
71+
def test_hash_literal_break
72+
assert_format(<<~EXPECTED, <<~SOURCE)
73+
{
74+
a:
75+
#{"a" * 80},
76+
}
77+
EXPECTED
78+
{ a: #{"a" * 80} }
79+
SOURCE
80+
end
81+
82+
private
83+
84+
def assert_format(expected, source = expected)
85+
formatter = Formatter.new(source, [], **OPTIONS)
86+
SyntaxTree.parse(source).format(formatter)
87+
88+
formatter.flush
89+
assert_equal(expected, formatter.output.join)
90+
end
91+
end
92+
end

0 commit comments

Comments
 (0)