Skip to content

Plugin to remove spaces in hash #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -711,6 +711,7 @@ To register plugins, define a file somewhere in your load path named `syntax_tre
* `plugin/single_quotes` - This will change all of your string literals to use single quotes instead of the default double quotes.
* `plugin/trailing_comma` - This will put trailing commas into multiline array literals, hash literals, and method calls that can support trailing commas.
* `plugin/disable_auto_ternary` - This will prevent the automatic conversion of `if ... else` to ternary expressions.
* `plugin/strip_hash` - This will remove the spaces at the beginning and end of hashes.

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.

19 changes: 16 additions & 3 deletions lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
@@ -24,13 +24,15 @@ class Options
attr_reader :quote,
:trailing_comma,
:disable_auto_ternary,
:target_ruby_version
:target_ruby_version,
:strip_hash

def initialize(
quote: :default,
trailing_comma: :default,
disable_auto_ternary: :default,
target_ruby_version: :default
target_ruby_version: :default,
strip_hash: :default
)
@quote =
if quote == :default
@@ -74,6 +76,14 @@ def initialize(
else
target_ruby_version
end

@strip_hash =
if strip_hash == :default
# This plugin removes the spaces at the beginning and end of hashes.
defined?(STRIP_HASH)
else
strip_hash
end
end
end

@@ -87,10 +97,12 @@ def initialize(
attr_reader :quote,
:trailing_comma,
:disable_auto_ternary,
:target_ruby_version
:target_ruby_version,
:strip_hash

alias trailing_comma? trailing_comma
alias disable_auto_ternary? disable_auto_ternary
alias strip_hash? strip_hash

def initialize(source, *args, options: Options.new)
super(*args)
@@ -103,6 +115,7 @@ def initialize(source, *args, options: Options.new)
@trailing_comma = options.trailing_comma
@disable_auto_ternary = options.disable_auto_ternary
@target_ruby_version = options.target_ruby_version
@strip_hash = options.strip_hash
end

def self.format(source, node, base_indentation = 0)
4 changes: 2 additions & 2 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
@@ -5754,11 +5754,11 @@ def format_contents(q)
q.breakable_empty
else
q.indent do
q.breakable_space
q.strip_hash? ? q.breakable_empty : q.breakable_space
q.seplist(assocs) { |assoc| q.format(assoc) }
q.if_break { q.text(",") } if q.trailing_comma?
end
q.breakable_space
q.strip_hash? ? q.breakable_empty : q.breakable_space
end

q.text("}")
7 changes: 7 additions & 0 deletions lib/syntax_tree/plugin/strip_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module SyntaxTree
class Formatter
STRIP_HASH = true
end
end
31 changes: 31 additions & 0 deletions test/plugin/strip_hash_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative "../test_helper"

module SyntaxTree
class StripHashTest < Minitest::Test
def test_single_hash
assert_format("{foo: 1}\n")
end

def test_multi_line_hash
assert_format(<<~EXPECTED)
{
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: 1,
baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 2
}
EXPECTED
end

private

def assert_format(expected, source = expected)
options = Formatter::Options.new(strip_hash: true)
formatter = Formatter.new(source, [], options: options)
SyntaxTree.parse(source).format(formatter)

formatter.flush
assert_equal(expected, formatter.output.join)
end
end
end