File tree Expand file tree Collapse file tree 13 files changed +78
-30
lines changed Expand file tree Collapse file tree 13 files changed +78
-30
lines changed Original file line number Diff line number Diff line change 88 fail-fast : false
99 matrix :
1010 ruby :
11- - ' 2.7'
11+ - ' 2.7.0 '
1212 - ' 3.0'
1313 - ' 3.1'
1414 - head
Original file line number Diff line number Diff line change 1212
1313task default : :test
1414
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 ] ]
1718
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
283283 opts . on ( "--print-width=NUMBER" , Integer ) do |print_width |
284284 @print_width = print_width
285285 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
286292 end
287293 end
288294 end
Original file line number Diff line number Diff line change @@ -14,7 +14,11 @@ class Formatter < PrettierPrint
1414 # Note that we're keeping this in a global-ish hash instead of just
1515 # overriding methods on classes so that other plugins can reference this if
1616 # 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+ }
1822
1923 COMMENT_PRIORITY = 1
2024 HEREDOC_PRIORITY = 2
@@ -23,14 +27,15 @@ class Formatter < PrettierPrint
2327
2428 # These options are overridden in plugins to we need to make sure they are
2529 # available here.
26- attr_reader :quote , :trailing_comma
30+ attr_reader :quote , :trailing_comma , :target_ruby_version
2731 alias trailing_comma? trailing_comma
2832
2933 def initialize (
3034 source ,
3135 *args ,
3236 quote : OPTIONS [ :quote ] ,
33- trailing_comma : OPTIONS [ :trailing_comma ]
37+ trailing_comma : OPTIONS [ :trailing_comma ] ,
38+ target_ruby_version : OPTIONS [ :target_ruby_version ]
3439 )
3540 super ( *args )
3641
@@ -40,6 +45,7 @@ def initialize(
4045 # Memoizing these values per formatter to make access faster.
4146 @quote = quote
4247 @trailing_comma = trailing_comma
48+ @target_ruby_version = target_ruby_version
4349 end
4450
4551 def self . format ( source , node )
Original file line number Diff line number Diff line change @@ -2132,8 +2132,7 @@ def format(q)
21322132 in [
21332133 Paren [
21342134 contents : {
2135- body : [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ]
2136- }
2135+ body : [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ] }
21372136 ]
21382137 ]
21392138 # Here we have a single argument that is a set of parentheses wrapping
@@ -5116,8 +5115,13 @@ def format(q)
51165115 q . breakable
51175116 contents . call
51185117 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
51215125 end
51225126 end
51235127 end
@@ -5204,8 +5208,7 @@ def call(q, node)
52045208 false
52055209 in {
52065210 statements : { body : [ truthy ] } ,
5207- consequent : Else [ statements : { body : [ falsy ] } ]
5208- }
5211+ consequent : Else [ statements : { body : [ falsy ] } ] }
52095212 ternaryable? ( truthy ) && ternaryable? ( falsy )
52105213 else
52115214 false
Original file line number Diff line number Diff line change @@ -39,16 +39,22 @@ class CheckTask < ::Rake::TaskLib
3939 # Defaults to 80.
4040 attr_accessor :print_width
4141
42+ # The target Ruby version to use for formatting.
43+ # Defaults to Gem::Version.new(RUBY_VERSION).
44+ attr_accessor :target_ruby_version
45+
4246 def initialize (
4347 name = :"stree:check" ,
4448 source_files = ::Rake ::FileList [ "lib/**/*.rb" ] ,
4549 plugins = [ ] ,
46- print_width = DEFAULT_PRINT_WIDTH
50+ print_width = DEFAULT_PRINT_WIDTH ,
51+ target_ruby_version = Gem ::Version . new ( RUBY_VERSION )
4752 )
4853 @name = name
4954 @source_files = source_files
5055 @plugins = plugins
5156 @print_width = print_width
57+ @target_ruby_version = target_ruby_version
5258
5359 yield self if block_given?
5460 define_task
@@ -64,10 +70,15 @@ def define_task
6470 def run_task
6571 arguments = [ "check" ]
6672 arguments << "--plugins=#{ plugins . join ( "," ) } " if plugins . any?
73+
6774 if print_width != DEFAULT_PRINT_WIDTH
6875 arguments << "--print-width=#{ print_width } "
6976 end
7077
78+ if target_ruby_version != Gem ::Version . new ( RUBY_VERSION )
79+ arguments << "--target-ruby-version=#{ target_ruby_version } "
80+ end
81+
7182 SyntaxTree ::CLI . run ( arguments + Array ( source_files ) )
7283 end
7384 end
Original file line number Diff line number Diff line change @@ -39,16 +39,22 @@ class WriteTask < ::Rake::TaskLib
3939 # Defaults to 80.
4040 attr_accessor :print_width
4141
42+ # The target Ruby version to use for formatting.
43+ # Defaults to Gem::Version.new(RUBY_VERSION).
44+ attr_accessor :target_ruby_version
45+
4246 def initialize (
4347 name = :"stree:write" ,
4448 source_files = ::Rake ::FileList [ "lib/**/*.rb" ] ,
4549 plugins = [ ] ,
46- print_width = DEFAULT_PRINT_WIDTH
50+ print_width = DEFAULT_PRINT_WIDTH ,
51+ target_ruby_version = Gem ::Version . new ( RUBY_VERSION )
4752 )
4853 @name = name
4954 @source_files = source_files
5055 @plugins = plugins
5156 @print_width = print_width
57+ @target_ruby_version = target_ruby_version
5258
5359 yield self if block_given?
5460 define_task
@@ -64,10 +70,15 @@ def define_task
6470 def run_task
6571 arguments = [ "write" ]
6672 arguments << "--plugins=#{ plugins . join ( "," ) } " if plugins . any?
73+
6774 if print_width != DEFAULT_PRINT_WIDTH
6875 arguments << "--print-width=#{ print_width } "
6976 end
7077
78+ if target_ruby_version != Gem ::Version . new ( RUBY_VERSION )
79+ arguments << "--target-ruby-version=#{ target_ruby_version } "
80+ end
81+
7182 SyntaxTree ::CLI . run ( arguments + Array ( source_files ) )
7283 end
7384 end
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
1919 . reject { |f | f . match ( %r{^(test|spec|features)/} ) }
2020 end
2121
22- spec . required_ruby_version = ">= 2.7.3 "
22+ spec . required_ruby_version = ">= 2.7.0 "
2323
2424 spec . bindir = "exe"
2525 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
22def foo(...)
33 bar(:baz, ...)
44end
Original file line number Diff line number Diff line change 3030case foo
3131in **bar
3232end
33- %
33+ % # >= 2.7.3
3434case foo
3535in {
3636 foo:, # comment1
You can’t perform that action at this time.
0 commit comments