Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ryanb wants to merge1 commit intoruby-syntax-tree:main
base:main
Choose a base branch
Loading
fromryanb:compact-hash-plugin
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.

Expand Down
19 changes: 16 additions & 3 deletionslib/syntax_tree/formatter.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -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

Expand All@@ -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)
Expand All@@ -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)
Expand Down
4 changes: 2 additions & 2 deletionslib/syntax_tree/node.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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("}")
Expand Down
7 changes: 7 additions & 0 deletionslib/syntax_tree/plugin/strip_hash.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module SyntaxTree
class Formatter
STRIP_HASH = true
end
end
31 changes: 31 additions & 0 deletionstest/plugin/strip_hash_test.rb
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp