File tree 13 files changed +78
-30
lines changed
13 files changed +78
-30
lines changed Original file line number Diff line number Diff line change 8
8
fail-fast : false
9
9
matrix :
10
10
ruby :
11
- - ' 2.7'
11
+ - ' 2.7.0 '
12
12
- ' 3.0'
13
13
- ' 3.1'
14
14
- head
Original file line number Diff line number Diff line change 12
12
13
13
task default : :test
14
14
15
- SOURCE_FILES =
16
- FileList [ %w[ Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb test/*.rb ] ]
15
+ configure = -> ( task ) do
16
+ task . source_files =
17
+ FileList [ %w[ Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb test/*.rb ] ]
17
18
18
- SyntaxTree ::Rake ::CheckTask . new { |t | t . source_files = SOURCE_FILES }
19
- SyntaxTree ::Rake ::WriteTask . new { |t | t . source_files = SOURCE_FILES }
19
+ # Since Syntax Tree supports back to Ruby 2.7.0, we need to make sure that we
20
+ # format our code such that it's compatible with that version. This actually
21
+ # has very little effect on the output, the only change at the moment is that
22
+ # Ruby < 2.7.3 didn't allow a newline before the closing brace of a hash
23
+ # pattern.
24
+ task . target_ruby_version = Gem ::Version . new ( "2.7.0" )
25
+ end
26
+
27
+ SyntaxTree ::Rake ::CheckTask . new ( &configure )
28
+ SyntaxTree ::Rake ::WriteTask . new ( &configure )
Original file line number Diff line number Diff line change @@ -283,6 +283,12 @@ def parser
283
283
opts . on ( "--print-width=NUMBER" , Integer ) do |print_width |
284
284
@print_width = print_width
285
285
end
286
+
287
+ # If there is a target ruby version specified on the command line,
288
+ # parse that out and use it when formatting.
289
+ opts . on ( "--target-ruby-version=VERSION" ) do |version |
290
+ Formatter ::OPTIONS [ :target_ruby_version ] = Gem ::Version . new ( version )
291
+ end
286
292
end
287
293
end
288
294
end
Original file line number Diff line number Diff line change @@ -14,7 +14,11 @@ class Formatter < PrettierPrint
14
14
# Note that we're keeping this in a global-ish hash instead of just
15
15
# overriding methods on classes so that other plugins can reference this if
16
16
# necessary. For example, the RBS plugin references the quote style.
17
- OPTIONS = { quote : "\" " , trailing_comma : false }
17
+ OPTIONS = {
18
+ quote : "\" " ,
19
+ trailing_comma : false ,
20
+ target_ruby_version : Gem ::Version . new ( RUBY_VERSION )
21
+ }
18
22
19
23
COMMENT_PRIORITY = 1
20
24
HEREDOC_PRIORITY = 2
@@ -23,14 +27,15 @@ class Formatter < PrettierPrint
23
27
24
28
# These options are overridden in plugins to we need to make sure they are
25
29
# available here.
26
- attr_reader :quote , :trailing_comma
30
+ attr_reader :quote , :trailing_comma , :target_ruby_version
27
31
alias trailing_comma? trailing_comma
28
32
29
33
def initialize (
30
34
source ,
31
35
*args ,
32
36
quote : OPTIONS [ :quote ] ,
33
- trailing_comma : OPTIONS [ :trailing_comma ]
37
+ trailing_comma : OPTIONS [ :trailing_comma ] ,
38
+ target_ruby_version : OPTIONS [ :target_ruby_version ]
34
39
)
35
40
super ( *args )
36
41
@@ -40,6 +45,7 @@ def initialize(
40
45
# Memoizing these values per formatter to make access faster.
41
46
@quote = quote
42
47
@trailing_comma = trailing_comma
48
+ @target_ruby_version = target_ruby_version
43
49
end
44
50
45
51
def self . format ( source , node )
Original file line number Diff line number Diff line change @@ -2132,8 +2132,7 @@ def format(q)
2132
2132
in [
2133
2133
Paren [
2134
2134
contents : {
2135
- body : [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ]
2136
- }
2135
+ body : [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ] }
2137
2136
]
2138
2137
]
2139
2138
# Here we have a single argument that is a set of parentheses wrapping
@@ -5116,8 +5115,13 @@ def format(q)
5116
5115
q . breakable
5117
5116
contents . call
5118
5117
end
5119
- q . breakable
5120
- q . text ( "}" )
5118
+
5119
+ if q . target_ruby_version < Gem ::Version . new ( "2.7.3" )
5120
+ q . text ( " }" )
5121
+ else
5122
+ q . breakable
5123
+ q . text ( "}" )
5124
+ end
5121
5125
end
5122
5126
end
5123
5127
end
@@ -5204,8 +5208,7 @@ def call(q, node)
5204
5208
false
5205
5209
in {
5206
5210
statements : { body : [ truthy ] } ,
5207
- consequent : Else [ statements : { body : [ falsy ] } ]
5208
- }
5211
+ consequent : Else [ statements : { body : [ falsy ] } ] }
5209
5212
ternaryable? ( truthy ) && ternaryable? ( falsy )
5210
5213
else
5211
5214
false
Original file line number Diff line number Diff line change @@ -39,16 +39,22 @@ class CheckTask < ::Rake::TaskLib
39
39
# Defaults to 80.
40
40
attr_accessor :print_width
41
41
42
+ # The target Ruby version to use for formatting.
43
+ # Defaults to Gem::Version.new(RUBY_VERSION).
44
+ attr_accessor :target_ruby_version
45
+
42
46
def initialize (
43
47
name = :"stree:check" ,
44
48
source_files = ::Rake ::FileList [ "lib/**/*.rb" ] ,
45
49
plugins = [ ] ,
46
- print_width = DEFAULT_PRINT_WIDTH
50
+ print_width = DEFAULT_PRINT_WIDTH ,
51
+ target_ruby_version = Gem ::Version . new ( RUBY_VERSION )
47
52
)
48
53
@name = name
49
54
@source_files = source_files
50
55
@plugins = plugins
51
56
@print_width = print_width
57
+ @target_ruby_version = target_ruby_version
52
58
53
59
yield self if block_given?
54
60
define_task
@@ -64,10 +70,15 @@ def define_task
64
70
def run_task
65
71
arguments = [ "check" ]
66
72
arguments << "--plugins=#{ plugins . join ( "," ) } " if plugins . any?
73
+
67
74
if print_width != DEFAULT_PRINT_WIDTH
68
75
arguments << "--print-width=#{ print_width } "
69
76
end
70
77
78
+ if target_ruby_version != Gem ::Version . new ( RUBY_VERSION )
79
+ arguments << "--target-ruby-version=#{ target_ruby_version } "
80
+ end
81
+
71
82
SyntaxTree ::CLI . run ( arguments + Array ( source_files ) )
72
83
end
73
84
end
Original file line number Diff line number Diff line change @@ -39,16 +39,22 @@ class WriteTask < ::Rake::TaskLib
39
39
# Defaults to 80.
40
40
attr_accessor :print_width
41
41
42
+ # The target Ruby version to use for formatting.
43
+ # Defaults to Gem::Version.new(RUBY_VERSION).
44
+ attr_accessor :target_ruby_version
45
+
42
46
def initialize (
43
47
name = :"stree:write" ,
44
48
source_files = ::Rake ::FileList [ "lib/**/*.rb" ] ,
45
49
plugins = [ ] ,
46
- print_width = DEFAULT_PRINT_WIDTH
50
+ print_width = DEFAULT_PRINT_WIDTH ,
51
+ target_ruby_version = Gem ::Version . new ( RUBY_VERSION )
47
52
)
48
53
@name = name
49
54
@source_files = source_files
50
55
@plugins = plugins
51
56
@print_width = print_width
57
+ @target_ruby_version = target_ruby_version
52
58
53
59
yield self if block_given?
54
60
define_task
@@ -64,10 +70,15 @@ def define_task
64
70
def run_task
65
71
arguments = [ "write" ]
66
72
arguments << "--plugins=#{ plugins . join ( "," ) } " if plugins . any?
73
+
67
74
if print_width != DEFAULT_PRINT_WIDTH
68
75
arguments << "--print-width=#{ print_width } "
69
76
end
70
77
78
+ if target_ruby_version != Gem ::Version . new ( RUBY_VERSION )
79
+ arguments << "--target-ruby-version=#{ target_ruby_version } "
80
+ end
81
+
71
82
SyntaxTree ::CLI . run ( arguments + Array ( source_files ) )
72
83
end
73
84
end
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
. reject { |f | f . match ( %r{^(test|spec|features)/} ) }
20
20
end
21
21
22
- spec . required_ruby_version = ">= 2.7.3 "
22
+ spec . required_ruby_version = ">= 2.7.0 "
23
23
24
24
spec . bindir = "exe"
25
25
spec . executables = spec . files . grep ( %r{^exe/} ) { |f | File . basename ( f ) }
Original file line number Diff line number Diff line change 1
- %
1
+ % # >= 2.7.3
2
2
def foo(...)
3
3
bar(:baz, ...)
4
4
end
Original file line number Diff line number Diff line change 30
30
case foo
31
31
in **bar
32
32
end
33
- %
33
+ % # >= 2.7.3
34
34
case foo
35
35
in {
36
36
foo:, # comment1
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ def foo(*)
16
16
%
17
17
def foo(*rest)
18
18
end
19
- %
19
+ % # >= 2.7.3
20
20
def foo(...)
21
21
end
22
22
%
Original file line number Diff line number Diff line change @@ -104,16 +104,18 @@ def test_arg_star
104
104
end
105
105
end
106
106
107
- def test_args_forward
108
- source = <<~SOURCE
109
- def get(...)
110
- request(:GET, ...)
111
- end
112
- SOURCE
107
+ guard_version ( "2.7.3" ) do
108
+ def test_args_forward
109
+ source = <<~SOURCE
110
+ def get(...)
111
+ request(:GET, ...)
112
+ end
113
+ SOURCE
113
114
114
- at = location ( lines : 2 ..2 , chars : 29 ..32 )
115
- assert_node ( ArgsForward , source , at : at ) do |node |
116
- node . bodystmt . statements . body . first . arguments . arguments . parts . last
115
+ at = location ( lines : 2 ..2 , chars : 29 ..32 )
116
+ assert_node ( ArgsForward , source , at : at ) do |node |
117
+ node . bodystmt . statements . body . first . arguments . arguments . parts . last
118
+ end
117
119
end
118
120
end
119
121
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ def initialize
26
26
@called = nil
27
27
end
28
28
29
- def method_missing ( called , ... )
29
+ def method_missing ( called , * , ** )
30
30
@called = called
31
31
end
32
32
end
You can’t perform that action at this time.
0 commit comments