reweave 0.1.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/reweave/execution/engine.rb +6 -0
- data/lib/reweave/execution/execution_context.rb +4 -0
- data/lib/reweave/recipe/recipe_option.rb +2 -1
- data/lib/reweave/recipes/rails/solid_cache_migration.rb +1 -20
- data/lib/reweave/recipes/rails/solid_queue_migration.rb +1 -13
- data/lib/reweave/recipes/rails/update_active_record.rb +5 -5
- data/lib/reweave/recipes/rails/upgrade_2_3_to_3_0.rb +59 -0
- data/lib/reweave/recipes/rails/upgrade_3_0_to_3_1.rb +42 -0
- data/lib/reweave/recipes/rails/upgrade_3_1_to_3_2.rb +39 -0
- data/lib/reweave/recipes/rails/upgrade_3_2_to_4_0.rb +81 -0
- data/lib/reweave/recipes/rails/upgrade_4_0_to_4_1.rb +39 -0
- data/lib/reweave/recipes/rails/upgrade_4_1_to_4_2.rb +46 -0
- data/lib/reweave/recipes/rails/upgrade_4_2_to_5_0.rb +77 -0
- data/lib/reweave/recipes/rails/upgrade_5_0_to_5_1.rb +51 -0
- data/lib/reweave/recipes/rails/upgrade_5_1_to_5_2.rb +54 -0
- data/lib/reweave/recipes/rails/upgrade_5_2_to_6_0.rb +57 -0
- data/lib/reweave/recipes/rails/upgrade_6_0_to_6_1.rb +45 -0
- data/lib/reweave/recipes/rails/upgrade_6_1_to_7_0.rb +81 -0
- data/lib/reweave/recipes/rails/upgrade_7_0_to_7_1.rb +46 -0
- data/lib/reweave/recipes/rails/upgrade_7_1_to_7_2.rb +51 -0
- data/lib/reweave/recipes/rails/upgrade_7_2_to_8_0.rb +82 -0
- data/lib/reweave/recipes/rails/upgrade_8_0_to_8_1.rb +37 -0
- data/lib/reweave/recipes/rails/upgrade_rails.rb +36 -2
- data/lib/reweave/recipes/ruby/modernize_syntax.rb +6 -17
- data/lib/reweave/recipes/ruby/upgrade_1_8_to_1_9.rb +76 -0
- data/lib/reweave/recipes/ruby/upgrade_1_9_to_2_0.rb +67 -0
- data/lib/reweave/recipes/ruby/upgrade_2_0_to_2_1.rb +50 -0
- data/lib/reweave/recipes/ruby/upgrade_2_1_to_2_2.rb +48 -0
- data/lib/reweave/recipes/ruby/upgrade_2_2_to_2_3.rb +75 -0
- data/lib/reweave/recipes/ruby/upgrade_2_3_to_2_4.rb +74 -0
- data/lib/reweave/recipes/ruby/upgrade_2_4_to_2_5.rb +53 -0
- data/lib/reweave/recipes/ruby/upgrade_2_5_to_2_6.rb +57 -0
- data/lib/reweave/recipes/ruby/upgrade_2_6_to_2_7.rb +71 -0
- data/lib/reweave/recipes/ruby/upgrade_2_7_to_3_0.rb +122 -0
- data/lib/reweave/recipes/ruby/upgrade_3_0_to_3_1.rb +50 -0
- data/lib/reweave/recipes/ruby/upgrade_3_1_to_3_2.rb +37 -0
- data/lib/reweave/recipes/ruby/upgrade_3_2_to_3_3.rb +37 -0
- data/lib/reweave/recipes/ruby/upgrade_3_3_to_4_0.rb +37 -0
- data/lib/reweave/recipes/ruby/upgrade_ruby.rb +49 -0
- data/lib/reweave/semantic/analyzer.rb +30 -0
- data/lib/reweave/semantic/symbol.rb +40 -0
- data/lib/reweave/semantic/symbol_collector.rb +159 -0
- data/lib/reweave/semantic/symbol_table.rb +100 -0
- data/lib/reweave/version.rb +1 -1
- data/lib/reweave/visitor/recipe_helpers.rb +222 -0
- data/lib/reweave/visitor/ruby_visitor.rb +3 -0
- metadata +37 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.0 -> 2.1 syntax upgrades
|
|
10
|
+
class Upgrade20To21 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.0 to 2.1"
|
|
12
|
+
description "Converts Ruby 2.0 syntax to 2.1: required keyword args, Exception#cause, String#scrub, Hash#to_h"
|
|
13
|
+
|
|
14
|
+
option :convert_hash_to_h, type: [TrueClass, FalseClass], default: true, description: "Convert hash.map{...}.to_h for Enumerable"
|
|
15
|
+
|
|
16
|
+
def get_visitor(context)
|
|
17
|
+
Upgrade20To21Visitor.new(context, config)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Upgrade20To21Visitor < Reweave::Visitor::RubyVisitor
|
|
22
|
+
def initialize(context, config)
|
|
23
|
+
super(context, nil)
|
|
24
|
+
@config = config
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def run(source_file)
|
|
28
|
+
@source_file = source_file
|
|
29
|
+
traverse(source_file.root)
|
|
30
|
+
edits
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Convert [[:a, 1], [:b, 2]].to_h -> {a: 1, b: 2} when possible
|
|
34
|
+
# or hash.map { |k, v| ... }.to_h -> hash.transform_values { |v| ... }
|
|
35
|
+
def visit_call_node(node)
|
|
36
|
+
return super unless @config[:convert_hash_to_h]
|
|
37
|
+
|
|
38
|
+
if match_call?(node, method: :to_h)
|
|
39
|
+
receiver = node.receiver
|
|
40
|
+
if receiver && match_call?(receiver, method: :map)
|
|
41
|
+
# [[:a, 1], [:b, 2]].map { ... }.to_h -> could be transform_values
|
|
42
|
+
# For now, leave as is but could be optimized
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
super
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.1 -> 2.2 syntax upgrades
|
|
10
|
+
class Upgrade21To22 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.1 to 2.2"
|
|
12
|
+
description "Converts Ruby 2.1 syntax to 2.2: Symbol GC, String#unicode_normalize, Kernel#itself"
|
|
13
|
+
|
|
14
|
+
def get_visitor(context)
|
|
15
|
+
Upgrade21To22Visitor.new(context)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Upgrade21To22Visitor < Reweave::Visitor::RubyVisitor
|
|
20
|
+
def initialize(context, **)
|
|
21
|
+
super(context, nil)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def run(source_file)
|
|
25
|
+
@source_file = source_file
|
|
26
|
+
traverse(source_file.root)
|
|
27
|
+
edits
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Replace obj.method(:itself) or ->(x) { x } with Kernel#itself / :itself.to_proc
|
|
31
|
+
def visit_call_node(node)
|
|
32
|
+
if match_call?(node, method: :itself) && !node.receiver
|
|
33
|
+
# obj.itself -> obj.itself (already correct)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Convert ->(x) { x } to &:itself
|
|
37
|
+
if node.is_a?(Prism::LambdaNode) || (node.is_a?(Prism::CallNode) && match_call?(node, method: :lambda))
|
|
38
|
+
body = node.body
|
|
39
|
+
if body.is_a?(Prism::BlockNode) && body.body.is_a?(Prism::CallNode)
|
|
40
|
+
# This is complex, skip for now
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
super
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.2 -> 2.3 syntax upgrades
|
|
10
|
+
class Upgrade22To23 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.2 to 2.3"
|
|
12
|
+
description "Converts Ruby 2.2 syntax to 2.3: safe navigation (&.), Hash#dig, frozen string literal, Hash#to_proc"
|
|
13
|
+
|
|
14
|
+
option :convert_safe_navigation, type: [TrueClass, FalseClass], default: true, description: "Convert obj && obj.method to obj&.method"
|
|
15
|
+
option :convert_hash_dig, type: [TrueClass, FalseClass], default: true, description: "Convert hash[:a][:b] to hash.dig(:a, :b)"
|
|
16
|
+
option :add_frozen_string_literal, type: [TrueClass, FalseClass], default: false, description: "Add # frozen_string_literal: true comment"
|
|
17
|
+
|
|
18
|
+
def get_visitor(context)
|
|
19
|
+
Upgrade22To23Visitor.new(context, config)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class Upgrade22To23Visitor < Reweave::Visitor::RubyVisitor
|
|
24
|
+
def initialize(context, config)
|
|
25
|
+
super(context, nil)
|
|
26
|
+
@config = config
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def run(source_file)
|
|
30
|
+
@source_file = source_file
|
|
31
|
+
traverse(source_file.root)
|
|
32
|
+
edits
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Visit AndNode for safe navigation conversion: obj && obj.method -> obj&.method
|
|
36
|
+
def visit_and_node(node)
|
|
37
|
+
return super unless @config[:convert_safe_navigation]
|
|
38
|
+
|
|
39
|
+
left = node.left
|
|
40
|
+
right = node.right
|
|
41
|
+
|
|
42
|
+
if left.is_a?(Prism::CallNode) && right.is_a?(Prism::CallNode)
|
|
43
|
+
# Left is the receiver (e.g., `user`), right is the call (e.g., `user.name`)
|
|
44
|
+
# Check if right's receiver has the same name as left
|
|
45
|
+
right_recv = right.receiver
|
|
46
|
+
if right_recv.is_a?(Prism::CallNode) && right_recv.name == left.name
|
|
47
|
+
method_name = right.name
|
|
48
|
+
left_name = left.name.to_s
|
|
49
|
+
replace(node, "#{left_name}&.#{method_name}")
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
super
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def visit_call_node(node)
|
|
56
|
+
# Convert hash[:a][:b] to hash.dig(:a, :b)
|
|
57
|
+
if @config[:convert_hash_dig] && match_call?(node, method: :[])
|
|
58
|
+
# Nested hash access like hash[:a][:b] would be a CallNode on a CallNode
|
|
59
|
+
receiver = node.receiver
|
|
60
|
+
if receiver && match_call?(receiver, method: :[])
|
|
61
|
+
# This is hash[:a][:b] pattern
|
|
62
|
+
outer_key = extract_arguments(node).first
|
|
63
|
+
inner_key = extract_arguments(receiver).first
|
|
64
|
+
recv = receiver.receiver
|
|
65
|
+
if recv && outer_key && inner_key
|
|
66
|
+
replace(node, "#{source_for(recv)}.dig(#{source_for(inner_key)}, #{source_for(outer_key)})")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
super
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.3 -> 2.4 syntax upgrades
|
|
10
|
+
class Upgrade23To24 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.3 to 2.4"
|
|
12
|
+
description "Converts Ruby 2.3 syntax to 2.4: Hash#compact, transform_values, Regexp#match?, String#casecmp?"
|
|
13
|
+
|
|
14
|
+
option :convert_hash_compact, type: [TrueClass, FalseClass], default: true, description: "Convert hash.delete_if { |_, v| v.nil? } to hash.compact"
|
|
15
|
+
option :convert_regexp_match, type: [TrueClass, FalseClass], default: true, description: "Convert /re/ =~ str to /re/.match?(str)"
|
|
16
|
+
option :convert_casecmp, type: [TrueClass, FalseClass], default: true, description: "Convert str.casecmp(other) == 0 to str.casecmp?(other)"
|
|
17
|
+
|
|
18
|
+
def get_visitor(context)
|
|
19
|
+
Upgrade23To24Visitor.new(context, config)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class Upgrade23To24Visitor < Reweave::Visitor::RubyVisitor
|
|
24
|
+
def initialize(context, config)
|
|
25
|
+
super(context, nil)
|
|
26
|
+
@config = config
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def run(source_file)
|
|
30
|
+
@source_file = source_file
|
|
31
|
+
traverse(source_file.root)
|
|
32
|
+
edits
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def visit_call_node(node)
|
|
36
|
+
# Convert hash.delete_if { |_, v| v.nil? } to hash.compact
|
|
37
|
+
if @config[:convert_hash_compact] && match_call?(node, method: :delete_if)
|
|
38
|
+
block = node.block
|
|
39
|
+
if block && block.body.is_a?(Prism::IfNode)
|
|
40
|
+
# Complex to detect, skip for now
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Convert /re/ =~ str to /re/.match?(str)
|
|
45
|
+
if @config[:convert_regexp_match] && match_call?(node, method: :=~)
|
|
46
|
+
receiver = node.receiver
|
|
47
|
+
if receiver&.is_a?(Prism::RegularExpressionNode)
|
|
48
|
+
arg = extract_arguments(node).first
|
|
49
|
+
if arg
|
|
50
|
+
replace(node, "#{source_for(receiver)}.match?(#{source_for(arg)})")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Convert str.casecmp(other) == 0 to str.casecmp?(other)
|
|
56
|
+
if @config[:convert_casecmp] && match_call?(node, method: :==)
|
|
57
|
+
left = node.receiver
|
|
58
|
+
right = extract_arguments(node).first
|
|
59
|
+
|
|
60
|
+
if left && match_call?(left, method: :casecmp) && right&.is_a?(Prism::IntegerNode) && right.value == 0
|
|
61
|
+
recv = left.receiver
|
|
62
|
+
arg = extract_arguments(left).first
|
|
63
|
+
if recv && arg
|
|
64
|
+
replace(node, "#{source_for(recv)}.casecmp?(#{source_for(arg)})")
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
super
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.4 -> 2.5 syntax upgrades
|
|
10
|
+
class Upgrade24To25 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.4 to 2.5"
|
|
12
|
+
description "Converts Ruby 2.4 syntax to 2.5: yield_self/then, Hash#slice, transform_keys, rescue in do/end"
|
|
13
|
+
|
|
14
|
+
option :convert_yield_self, type: [TrueClass, FalseClass], default: true, description: "Convert obj.tap { |x| break f(x) } to obj.yield_self { |x| f(x) }"
|
|
15
|
+
option :convert_hash_slice, type: [TrueClass, FalseClass], default: true, description: "Convert hash.select { |k, _| keys.include?(k) } to hash.slice(*keys)"
|
|
16
|
+
|
|
17
|
+
def get_visitor(context)
|
|
18
|
+
Upgrade24To25Visitor.new(context, config)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Upgrade24To25Visitor < Reweave::Visitor::RubyVisitor
|
|
23
|
+
def initialize(context, config)
|
|
24
|
+
super(context, nil)
|
|
25
|
+
@config = config
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def run(source_file)
|
|
29
|
+
@source_file = source_file
|
|
30
|
+
traverse(source_file.root)
|
|
31
|
+
edits
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def visit_call_node(node)
|
|
35
|
+
# Convert obj.tap { |x| break f(x) } to obj.yield_self { |x| f(x) }
|
|
36
|
+
if @config[:convert_yield_self] && match_call?(node, method: :tap)
|
|
37
|
+
block = node.block
|
|
38
|
+
if block && block.body.is_a?(Prism::BreakNode)
|
|
39
|
+
# Complex to detect, skip for now
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Convert hash.select { |k, _| keys.include?(k) } to hash.slice(*keys)
|
|
44
|
+
if @config[:convert_hash_slice] && match_call?(node, method: :select)
|
|
45
|
+
# Complex pattern matching, skip for now
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
super
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.5 -> 2.6 syntax upgrades
|
|
10
|
+
class Upgrade25To26 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.5 to 2.6"
|
|
12
|
+
description "Converts Ruby 2.5 syntax to 2.6: endless ranges, then alias, Random.bytes, Proc composition"
|
|
13
|
+
|
|
14
|
+
option :convert_endless_range, type: [TrueClass, FalseClass], default: true, description: "Convert 1..Float::INFINITY to 1.."
|
|
15
|
+
option :convert_then, type: [TrueClass, FalseClass], default: true, description: "Convert yield_self to then"
|
|
16
|
+
|
|
17
|
+
def get_visitor(context)
|
|
18
|
+
Upgrade25To26Visitor.new(context, config)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Upgrade25To26Visitor < Reweave::Visitor::RubyVisitor
|
|
23
|
+
def initialize(context, config)
|
|
24
|
+
super(context, nil)
|
|
25
|
+
@config = config
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def run(source_file)
|
|
29
|
+
@source_file = source_file
|
|
30
|
+
traverse(source_file.root)
|
|
31
|
+
edits
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def visit_call_node(node)
|
|
35
|
+
# Convert yield_self to then
|
|
36
|
+
if @config[:convert_then] && match_call?(node, method: :yield_self)
|
|
37
|
+
replace_location(node.location, node.location.slice.sub("yield_self", "then"))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Convert Range.new(1, Float::INFINITY) to 1..
|
|
41
|
+
super
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def visit_range_node(node)
|
|
45
|
+
return super unless @config[:convert_endless_range]
|
|
46
|
+
|
|
47
|
+
# Convert 1..Float::INFINITY to 1..
|
|
48
|
+
if node.end && match_call?(node.end, method: :INFINITY, receiver: "Float")
|
|
49
|
+
# This is an exclusive range with Float::INFINITY
|
|
50
|
+
replace(node, "#{source_for(node.begin)}..")
|
|
51
|
+
end
|
|
52
|
+
super
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.6 -> 2.7 syntax upgrades
|
|
10
|
+
class Upgrade26To27 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.6 to 2.7"
|
|
12
|
+
description "Converts Ruby 2.6 syntax to 2.7: numbered params (_1), beginless ranges, Enumerable#tally, GC.compact"
|
|
13
|
+
|
|
14
|
+
option :convert_numbered_params, type: [TrueClass, FalseClass], default: true, description: "Convert { |x| x.foo } to { _1.foo }"
|
|
15
|
+
option :convert_beginless_range, type: [TrueClass, FalseClass], default: true, description: "Convert -Float::INFINITY..5 to ..5"
|
|
16
|
+
|
|
17
|
+
def get_visitor(context)
|
|
18
|
+
Upgrade26To27Visitor.new(context, config)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Upgrade26To27Visitor < Reweave::Visitor::RubyVisitor
|
|
23
|
+
def initialize(context, config)
|
|
24
|
+
super(context, nil)
|
|
25
|
+
@config = config
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def run(source_file)
|
|
29
|
+
@source_file = source_file
|
|
30
|
+
traverse(source_file.root)
|
|
31
|
+
edits
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def visit_block_parameters_node(node)
|
|
35
|
+
return super unless @config[:convert_numbered_params]
|
|
36
|
+
|
|
37
|
+
# Convert single param blocks to numbered params
|
|
38
|
+
# node.parameters is a ParametersNode (not BlockParametersNode)
|
|
39
|
+
inner_params = node.parameters
|
|
40
|
+
requireds = inner_params.requireds
|
|
41
|
+
if requireds.size == 1 && inner_params.optionals.empty? && inner_params.rest.nil?
|
|
42
|
+
param = requireds.first
|
|
43
|
+
# Only convert simple parameter names like x, y, it, item, element
|
|
44
|
+
simple_names = %i[x y it item element val value arg args]
|
|
45
|
+
if simple_names.include?(param.name)
|
|
46
|
+
# For numbered params, we can remove the block parameters entirely
|
|
47
|
+
# Replace the entire block parameters (including | |) with nothing
|
|
48
|
+
if node.opening_loc && node.closing_loc
|
|
49
|
+
start_offset = node.opening_loc.start_offset
|
|
50
|
+
end_offset = node.closing_loc.end_offset
|
|
51
|
+
@edits.add(Editing.replace(source_id, start_offset, end_offset, "", recipe_name))
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
super
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def visit_range_node(node)
|
|
59
|
+
return super unless @config[:convert_beginless_range]
|
|
60
|
+
|
|
61
|
+
# Convert -Float::INFINITY..5 to ..5
|
|
62
|
+
if node.begin && match_call?(node.begin, method: :-, receiver: "Float")
|
|
63
|
+
# Actually -Float::INFINITY is a unary minus on Float::INFINITY
|
|
64
|
+
# Prism represents this differently
|
|
65
|
+
end
|
|
66
|
+
super
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 2.7 -> 3.0 syntax upgrades
|
|
10
|
+
class Upgrade27To30 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 2.7 to 3.0"
|
|
12
|
+
description "Converts Ruby 2.7 syntax to 3.0: pattern matching, keyword arg separation, endless methods, Array#union/difference/intersection, Hash#except, String#delete_prefix/suffix"
|
|
13
|
+
|
|
14
|
+
option :convert_endless_methods, type: [TrueClass, FalseClass], default: true, description: "Convert def foo; bar end to def foo = bar"
|
|
15
|
+
option :convert_array_union, type: [TrueClass, FalseClass], default: true, description: "Convert a | b to a.union(b)"
|
|
16
|
+
option :convert_array_difference, type: [TrueClass, FalseClass], default: true, description: "Convert a - b to a.difference(b)"
|
|
17
|
+
option :convert_array_intersection, type: [TrueClass, FalseClass], default: true, description: "Convert a & b to a.intersection(b)"
|
|
18
|
+
option :convert_hash_except, type: [TrueClass, FalseClass], default: true, description: "Convert hash.except(:key) or hash.reject { |k, _| k == :key } to hash.except(:key)"
|
|
19
|
+
option :convert_delete_prefix, type: [TrueClass, FalseClass], default: true, description: "Convert str.sub(/^prefix/, '') to str.delete_prefix('prefix')"
|
|
20
|
+
|
|
21
|
+
def get_visitor(context)
|
|
22
|
+
Upgrade27To30Visitor.new(context, config)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class Upgrade27To30Visitor < Reweave::Visitor::RubyVisitor
|
|
27
|
+
def initialize(context, config)
|
|
28
|
+
super(context, nil)
|
|
29
|
+
@config = config
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def run(source_file)
|
|
33
|
+
@source_file = source_file
|
|
34
|
+
traverse(source_file.root)
|
|
35
|
+
edits
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def visit_def_node(node)
|
|
39
|
+
return super unless @config[:convert_endless_methods]
|
|
40
|
+
|
|
41
|
+
# Convert single-line method: def foo; bar end -> def foo = bar
|
|
42
|
+
# Only convert if the method definition is on a single line
|
|
43
|
+
if node.body&.is_a?(Prism::StatementsNode)
|
|
44
|
+
stmts = node.body.body
|
|
45
|
+
if stmts.size == 1
|
|
46
|
+
# Check if the entire method is on one line
|
|
47
|
+
if node.location.start_line == node.location.end_line
|
|
48
|
+
body_source = source_for(stmts.first)
|
|
49
|
+
if body_source
|
|
50
|
+
# Build parameter list
|
|
51
|
+
params = node.parameters
|
|
52
|
+
param_list = ""
|
|
53
|
+
if params&.requireds&.any?
|
|
54
|
+
param_names = params.requireds.map { |p| p.name.to_s }
|
|
55
|
+
param_list = "(#{param_names.join(", ")})"
|
|
56
|
+
end
|
|
57
|
+
replace(node, "def #{node.name}#{param_list} = #{body_source}")
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
super
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def visit_call_node(node)
|
|
66
|
+
# Convert a | b to a.union(b)
|
|
67
|
+
if @config[:convert_array_union] && match_call?(node, method: :|)
|
|
68
|
+
receiver = node.receiver
|
|
69
|
+
arg = extract_arguments(node).first
|
|
70
|
+
if receiver && arg
|
|
71
|
+
replace(node, "#{source_for(receiver)}.union(#{source_for(arg)})")
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Convert a - b to a.difference(b)
|
|
76
|
+
if @config[:convert_array_difference] && match_call?(node, method: :-)
|
|
77
|
+
receiver = node.receiver
|
|
78
|
+
arg = extract_arguments(node).first
|
|
79
|
+
if receiver && arg
|
|
80
|
+
replace(node, "#{source_for(receiver)}.difference(#{source_for(arg)})")
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Convert a & b to a.intersection(b)
|
|
85
|
+
if @config[:convert_array_intersection] && match_call?(node, method: :&)
|
|
86
|
+
receiver = node.receiver
|
|
87
|
+
arg = extract_arguments(node).first
|
|
88
|
+
if receiver && arg
|
|
89
|
+
replace(node, "#{source_for(receiver)}.intersection(#{source_for(arg)})")
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Convert hash.except(:key)
|
|
94
|
+
if @config[:convert_hash_except] && match_call?(node, method: :except)
|
|
95
|
+
# Already correct
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Convert str.sub(/^prefix/, '') to str.delete_prefix('prefix')
|
|
99
|
+
if @config[:convert_delete_prefix] && match_call?(node, method: :sub)
|
|
100
|
+
receiver = node.receiver
|
|
101
|
+
args = extract_arguments(node)
|
|
102
|
+
if args.size == 2
|
|
103
|
+
pattern = args[0]
|
|
104
|
+
replacement = args[1]
|
|
105
|
+
if pattern.is_a?(Prism::RegularExpressionNode)
|
|
106
|
+
unescaped = pattern.unescaped
|
|
107
|
+
if unescaped.start_with?("^")
|
|
108
|
+
prefix = unescaped[1..]
|
|
109
|
+
if replacement.is_a?(Prism::StringNode) && replacement.unescaped.empty?
|
|
110
|
+
replace(node, "#{source_for(receiver)}.delete_prefix(#{prefix.inspect})")
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
super
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 3.0 -> 3.1 syntax upgrades
|
|
10
|
+
class Upgrade30To31 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 3.0 to 3.1"
|
|
12
|
+
description "Converts Ruby 3.0 syntax to 3.1: Array#intersect?, Hash#fetch with block, Class#subclasses, debug improvements"
|
|
13
|
+
|
|
14
|
+
option :convert_array_intersect, type: [TrueClass, FalseClass], default: true, description: "Convert (a & b).any? to a.intersect?(b)"
|
|
15
|
+
|
|
16
|
+
def get_visitor(context)
|
|
17
|
+
Upgrade30To31Visitor.new(context, config)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Upgrade30To31Visitor < Reweave::Visitor::RubyVisitor
|
|
22
|
+
def initialize(context, config)
|
|
23
|
+
super(context, nil)
|
|
24
|
+
@config = config
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def run(source_file)
|
|
28
|
+
@source_file = source_file
|
|
29
|
+
traverse(source_file.root)
|
|
30
|
+
edits
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def visit_call_node(node)
|
|
34
|
+
# Convert (a & b).any? to a.intersect?(b)
|
|
35
|
+
if @config[:convert_array_intersect] && match_call?(node, method: :any?)
|
|
36
|
+
receiver = node.receiver
|
|
37
|
+
if receiver && match_call?(receiver, method: :&)
|
|
38
|
+
left = receiver.receiver
|
|
39
|
+
right = extract_arguments(receiver).first
|
|
40
|
+
if left && right
|
|
41
|
+
replace(node, "#{source_for(left)}.intersect?(#{source_for(right)})")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
super
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 3.1 -> 3.2 syntax upgrades
|
|
10
|
+
class Upgrade31To32 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 3.1 to 3.2"
|
|
12
|
+
description "Converts Ruby 3.1 syntax to 3.2: syntax suggestions, Hash#fetch multiple keys, String#bytesplice, Fiber storage"
|
|
13
|
+
|
|
14
|
+
def get_visitor(context)
|
|
15
|
+
Upgrade31To32Visitor.new(context)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Upgrade31To32Visitor < Reweave::Visitor::RubyVisitor
|
|
20
|
+
def initialize(context, **)
|
|
21
|
+
super(context, nil)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def run(source_file)
|
|
25
|
+
@source_file = source_file
|
|
26
|
+
traverse(source_file.root)
|
|
27
|
+
edits
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Most 3.2 changes are runtime/performance, not syntax
|
|
31
|
+
def visit_call_node(node)
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe"
|
|
4
|
+
require_relative "../../../reweave/visitor/ruby_visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Recipes
|
|
8
|
+
module Ruby
|
|
9
|
+
# Ruby 3.2 -> 3.3 syntax upgrades
|
|
10
|
+
class Upgrade32To33 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 3.2 to 3.3"
|
|
12
|
+
description "Converts Ruby 3.2 syntax to 3.3: Prism parser, RJIT, String#byteindex, Time nanosecond precision"
|
|
13
|
+
|
|
14
|
+
def get_visitor(context)
|
|
15
|
+
Upgrade32To33Visitor.new(context)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Upgrade32To33Visitor < Reweave::Visitor::RubyVisitor
|
|
20
|
+
def initialize(context, **)
|
|
21
|
+
super(context, nil)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def run(source_file)
|
|
25
|
+
@source_file = source_file
|
|
26
|
+
traverse(source_file.root)
|
|
27
|
+
edits
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Most 3.3 changes are runtime/performance, not syntax
|
|
31
|
+
def visit_call_node(node)
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|