Skip to content

Commit b79d921

Browse files
committed
Add compact_hash plugin
1 parent 113b68c commit b79d921

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ To register plugins, define a file somewhere in your load path named `syntax_tre
711711
* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
712712
* `plugin/trailing_comma` - This will put trailing commas into multiline array literals, hash literals, and method calls that can support trailing commas.
713713
* `plugin/disable_auto_ternary` - This will prevent the automatic conversion of `if ... else` to ternary expressions.
714+
* `plugin/strip_hash` - This will remove the spaces at the beginning and end of hashes.
714715

715716
If you're using Syntax Tree as a library, you can require those files directly or manually pass those options to the formatter initializer through the `SyntaxTree::Formatter::Options` class.
716717

lib/syntax_tree/formatter.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class Options
2424
attr_reader :quote,
2525
:trailing_comma,
2626
:disable_auto_ternary,
27-
:target_ruby_version
27+
:target_ruby_version,
28+
:strip_hash
2829

2930
def initialize(
3031
quote: :default,
3132
trailing_comma: :default,
3233
disable_auto_ternary: :default,
33-
target_ruby_version: :default
34+
target_ruby_version: :default,
35+
strip_hash: :default
3436
)
3537
@quote =
3638
if quote == :default
@@ -74,6 +76,14 @@ def initialize(
7476
else
7577
target_ruby_version
7678
end
79+
80+
@strip_hash =
81+
if strip_hash == :default
82+
# This plugin removes the spaces at the beginning and end of hashes.
83+
defined?(STRIP_HASH)
84+
else
85+
strip_hash
86+
end
7787
end
7888
end
7989

@@ -87,10 +97,12 @@ def initialize(
8797
attr_reader :quote,
8898
:trailing_comma,
8999
:disable_auto_ternary,
90-
:target_ruby_version
100+
:target_ruby_version,
101+
:strip_hash
91102

92103
alias trailing_comma? trailing_comma
93104
alias disable_auto_ternary? disable_auto_ternary
105+
alias strip_hash? strip_hash
94106

95107
def initialize(source, *args, options: Options.new)
96108
super(*args)
@@ -103,6 +115,7 @@ def initialize(source, *args, options: Options.new)
103115
@trailing_comma = options.trailing_comma
104116
@disable_auto_ternary = options.disable_auto_ternary
105117
@target_ruby_version = options.target_ruby_version
118+
@strip_hash = options.strip_hash
106119
end
107120

108121
def self.format(source, node, base_indentation = 0)

lib/syntax_tree/node.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5751,11 +5751,11 @@ def format_contents(q)
57515751
q.breakable_empty
57525752
else
57535753
q.indent do
5754-
q.breakable_space
5754+
q.strip_hash? ? q.breakable_empty : q.breakable_space
57555755
q.seplist(assocs) { |assoc| q.format(assoc) }
57565756
q.if_break { q.text(",") } if q.trailing_comma?
57575757
end
5758-
q.breakable_space
5758+
q.strip_hash? ? q.breakable_empty : q.breakable_space
57595759
end
57605760

57615761
q.text("}")

lib/syntax_tree/plugin/strip_hash.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module SyntaxTree
4+
class Formatter
5+
STRIP_HASH = true
6+
end
7+
end

test/plugin/strip_hash_test.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module SyntaxTree
6+
class StripHashTest < Minitest::Test
7+
def test_single_hash
8+
assert_format("{foo: 1}\n")
9+
end
10+
11+
def test_multi_line_hash
12+
assert_format(<<~EXPECTED)
13+
{
14+
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: 1,
15+
baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 2
16+
}
17+
EXPECTED
18+
end
19+
20+
private
21+
22+
def assert_format(expected, source = expected)
23+
options = Formatter::Options.new(strip_hash: true)
24+
formatter = Formatter.new(source, [], options: options)
25+
SyntaxTree.parse(source).format(formatter)
26+
27+
formatter.flush
28+
assert_equal(expected, formatter.output.join)
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)