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

Commit436c698

Browse files
committed
Add strip_hash plugin
1 parentf07c906 commit436c698

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 16 additions & 3 deletions
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
definitialize(
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
ifquote ==:default
@@ -74,6 +76,14 @@ def initialize(
7476
else
7577
target_ruby_version
7678
end
79+
80+
@strip_hash=
81+
ifstrip_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
aliastrailing_comma?trailing_comma
93104
aliasdisable_auto_ternary?disable_auto_ternary
105+
aliasstrip_hash?strip_hash
94106

95107
definitialize(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
defself.format(source,node,base_indentation=0)

‎lib/syntax_tree/node.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5754,11 +5754,11 @@ def format_contents(q)
57545754
q.breakable_empty
57555755
else
57565756
q.indentdo
5757-
q.breakable_space
5757+
q.strip_hash? ?q.breakable_empty :q.breakable_space
57585758
q.seplist(assocs){ |assoc|q.format(assoc)}
57595759
q.if_break{q.text(",")}ifq.trailing_comma?
57605760
end
5761-
q.breakable_space
5761+
q.strip_hash? ?q.breakable_empty :q.breakable_space
57625762
end
57635763

57645764
q.text("}")

‎lib/syntax_tree/plugin/strip_hash.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
moduleSyntaxTree
4+
classFormatter
5+
STRIP_HASH=true
6+
end
7+
end

‎test/plugin/strip_hash_test.rb

Lines changed: 31 additions & 0 deletions
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+
moduleSyntaxTree
6+
classStripHashTest <Minitest::Test
7+
deftest_single_hash
8+
assert_format("{foo: 1}\n")
9+
end
10+
11+
deftest_multi_line_hash
12+
assert_format(<<~EXPECTED)
13+
{
14+
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: 1,
15+
baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 2
16+
}
17+
EXPECTED
18+
end
19+
20+
private
21+
22+
defassert_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)

[8]ページ先頭

©2009-2025 Movatter.jp