From 436c698826a0d5acf8b401a941a8dd5b71aa8ef0 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 4 Apr 2023 20:18:17 -0700 Subject: [PATCH] Add strip_hash plugin --- README.md | 1 + lib/syntax_tree/formatter.rb | 19 ++++++++++++++--- lib/syntax_tree/node.rb | 4 ++-- lib/syntax_tree/plugin/strip_hash.rb | 7 +++++++ test/plugin/strip_hash_test.rb | 31 ++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 lib/syntax_tree/plugin/strip_hash.rb create mode 100644 test/plugin/strip_hash_test.rb diff --git a/README.md b/README.md index 6e1119df..c12aa906 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/syntax_tree/formatter.rb b/lib/syntax_tree/formatter.rb index 2b229885..8471d4bd 100644 --- a/lib/syntax_tree/formatter.rb +++ b/lib/syntax_tree/formatter.rb @@ -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) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 3b676552..75a8909f 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -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("}") diff --git a/lib/syntax_tree/plugin/strip_hash.rb b/lib/syntax_tree/plugin/strip_hash.rb new file mode 100644 index 00000000..bf67d6d0 --- /dev/null +++ b/lib/syntax_tree/plugin/strip_hash.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module SyntaxTree + class Formatter + STRIP_HASH = true + end +end diff --git a/test/plugin/strip_hash_test.rb b/test/plugin/strip_hash_test.rb new file mode 100644 index 00000000..3adb489f --- /dev/null +++ b/test/plugin/strip_hash_test.rb @@ -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