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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/reweave/execution/engine.rb +6 -0
  3. data/lib/reweave/execution/execution_context.rb +4 -0
  4. data/lib/reweave/recipe/recipe_option.rb +2 -1
  5. data/lib/reweave/recipes/rails/solid_cache_migration.rb +1 -20
  6. data/lib/reweave/recipes/rails/solid_queue_migration.rb +1 -13
  7. data/lib/reweave/recipes/rails/update_active_record.rb +5 -5
  8. data/lib/reweave/recipes/rails/upgrade_2_3_to_3_0.rb +59 -0
  9. data/lib/reweave/recipes/rails/upgrade_3_0_to_3_1.rb +42 -0
  10. data/lib/reweave/recipes/rails/upgrade_3_1_to_3_2.rb +39 -0
  11. data/lib/reweave/recipes/rails/upgrade_3_2_to_4_0.rb +81 -0
  12. data/lib/reweave/recipes/rails/upgrade_4_0_to_4_1.rb +39 -0
  13. data/lib/reweave/recipes/rails/upgrade_4_1_to_4_2.rb +46 -0
  14. data/lib/reweave/recipes/rails/upgrade_4_2_to_5_0.rb +77 -0
  15. data/lib/reweave/recipes/rails/upgrade_5_0_to_5_1.rb +51 -0
  16. data/lib/reweave/recipes/rails/upgrade_5_1_to_5_2.rb +54 -0
  17. data/lib/reweave/recipes/rails/upgrade_5_2_to_6_0.rb +57 -0
  18. data/lib/reweave/recipes/rails/upgrade_6_0_to_6_1.rb +45 -0
  19. data/lib/reweave/recipes/rails/upgrade_6_1_to_7_0.rb +81 -0
  20. data/lib/reweave/recipes/rails/upgrade_7_0_to_7_1.rb +46 -0
  21. data/lib/reweave/recipes/rails/upgrade_7_1_to_7_2.rb +51 -0
  22. data/lib/reweave/recipes/rails/upgrade_7_2_to_8_0.rb +82 -0
  23. data/lib/reweave/recipes/rails/upgrade_8_0_to_8_1.rb +37 -0
  24. data/lib/reweave/recipes/rails/upgrade_rails.rb +36 -2
  25. data/lib/reweave/recipes/ruby/modernize_syntax.rb +6 -17
  26. data/lib/reweave/recipes/ruby/upgrade_1_8_to_1_9.rb +76 -0
  27. data/lib/reweave/recipes/ruby/upgrade_1_9_to_2_0.rb +67 -0
  28. data/lib/reweave/recipes/ruby/upgrade_2_0_to_2_1.rb +50 -0
  29. data/lib/reweave/recipes/ruby/upgrade_2_1_to_2_2.rb +48 -0
  30. data/lib/reweave/recipes/ruby/upgrade_2_2_to_2_3.rb +75 -0
  31. data/lib/reweave/recipes/ruby/upgrade_2_3_to_2_4.rb +74 -0
  32. data/lib/reweave/recipes/ruby/upgrade_2_4_to_2_5.rb +53 -0
  33. data/lib/reweave/recipes/ruby/upgrade_2_5_to_2_6.rb +57 -0
  34. data/lib/reweave/recipes/ruby/upgrade_2_6_to_2_7.rb +71 -0
  35. data/lib/reweave/recipes/ruby/upgrade_2_7_to_3_0.rb +122 -0
  36. data/lib/reweave/recipes/ruby/upgrade_3_0_to_3_1.rb +50 -0
  37. data/lib/reweave/recipes/ruby/upgrade_3_1_to_3_2.rb +37 -0
  38. data/lib/reweave/recipes/ruby/upgrade_3_2_to_3_3.rb +37 -0
  39. data/lib/reweave/recipes/ruby/upgrade_3_3_to_4_0.rb +37 -0
  40. data/lib/reweave/recipes/ruby/upgrade_ruby.rb +49 -0
  41. data/lib/reweave/semantic/analyzer.rb +30 -0
  42. data/lib/reweave/semantic/symbol.rb +40 -0
  43. data/lib/reweave/semantic/symbol_collector.rb +159 -0
  44. data/lib/reweave/semantic/symbol_table.rb +100 -0
  45. data/lib/reweave/version.rb +1 -1
  46. data/lib/reweave/visitor/recipe_helpers.rb +222 -0
  47. data/lib/reweave/visitor/ruby_visitor.rb +3 -0
  48. metadata +37 -1
@@ -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 Rails
9
+ # Rails 5.2 -> 6.0 syntax upgrades
10
+ class Upgrade52To60 < Recipe
11
+ display_name "Upgrade Rails 5.2 to 6.0"
12
+ description "Converts Rails 5.2 syntax to 6.0: Webpacker, Action Mailbox, Action Text, multiple databases, etc."
13
+
14
+ option :convert_webpacker, type: [TrueClass, FalseClass], default: true, description: "Convert asset pipeline to Webpacker"
15
+ option :convert_multiple_dbs, type: [TrueClass, FalseClass], default: true, description: "Convert to multiple databases config"
16
+ option :convert_action_mailbox, type: [TrueClass, FalseClass], default: true, description: "Convert mailbox routing"
17
+
18
+ def get_visitor(context)
19
+ Upgrade52To60Visitor.new(context, config)
20
+ end
21
+ end
22
+
23
+ class Upgrade52To60Visitor < 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 javascript_include_tag to javascript_pack_tag
37
+ if @config[:convert_webpacker] && match_call?(node, method: :javascript_include_tag)
38
+ args = extract_arguments(node)
39
+ if args.any? { |a| a.is_a?(Prism::StringNode) && a.unescaped.include?("application") }
40
+ replace_location(node.message_loc, "javascript_pack_tag")
41
+ end
42
+ end
43
+
44
+ # Convert stylesheet_link_tag to stylesheet_pack_tag
45
+ if @config[:convert_webpacker] && match_call?(node, method: :stylesheet_link_tag)
46
+ args = extract_arguments(node)
47
+ if args.any? { |a| a.is_a?(Prism::StringNode) && a.unescaped.include?("application") }
48
+ replace_location(node.message_loc, "stylesheet_pack_tag")
49
+ end
50
+ end
51
+
52
+ super
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,45 @@
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 Rails
9
+ # Rails 6.0 -> 6.1 syntax upgrades
10
+ class Upgrade60To61 < Recipe
11
+ display_name "Upgrade Rails 6.0 to 6.1"
12
+ description "Converts Rails 6.0 syntax to 6.1: horizontal sharding, strict loading, etc."
13
+
14
+ option :convert_strict_loading, type: [TrueClass, FalseClass], default: true, description: "Add strict_loading to associations"
15
+ option :convert_delegated_type, type: [TrueClass, FalseClass], default: true, description: "Use delegated_type for STI"
16
+
17
+ def get_visitor(context)
18
+ Upgrade60To61Visitor.new(context, config)
19
+ end
20
+ end
21
+
22
+ class Upgrade60To61Visitor < 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 belongs_to for delegated_type
36
+ if @config[:convert_delegated_type] && match_call?(node, method: :belongs_to)
37
+ # This is complex pattern matching
38
+ end
39
+
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,81 @@
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 Rails
9
+ # Rails 6.1 -> 7.0 syntax upgrades
10
+ class Upgrade61To70 < Recipe
11
+ display_name "Upgrade Rails 6.1 to 7.0"
12
+ description "Converts Rails 6.1 syntax to 7.0: Hotwire (Turbo/Stimulus), import maps, Propshaft, etc."
13
+
14
+ option :convert_hotwire, type: [TrueClass, FalseClass], default: true, description: "Convert UJS to Turbo/Stimulus"
15
+ option :convert_import_maps, type: [TrueClass, FalseClass], default: true, description: "Convert Webpacker to import maps"
16
+ option :convert_propshaft, type: [TrueClass, FalseClass], default: true, description: "Convert Sprockets to Propshaft"
17
+ option :convert_encrypted_config, type: [TrueClass, FalseClass], default: true, description: "Convert encrypted credentials"
18
+ option :convert_variants, type: [TrueClass, FalseClass], default: true, description: "Convert ActiveStorage variants"
19
+
20
+ def get_visitor(context)
21
+ Upgrade61To70Visitor.new(context, config)
22
+ end
23
+ end
24
+
25
+ class Upgrade61To70Visitor < Reweave::Visitor::RubyVisitor
26
+ def initialize(context, config)
27
+ super(context, nil)
28
+ @config = config
29
+ end
30
+
31
+ def run(source_file)
32
+ @source_file = source_file
33
+ traverse(source_file.root)
34
+ edits
35
+ end
36
+
37
+ def visit_call_node(node)
38
+ # Convert form_with to use Turbo
39
+ if @config[:convert_hotwire] && match_call?(node, method: :form_with)
40
+ # form_with already uses remote: true by default in 7.0
41
+ end
42
+
43
+ # Convert link_to with data: { remote: true } to data: { turbo: true }
44
+ if @config[:convert_hotwire] && match_call?(node, method: :link_to)
45
+ args = extract_arguments(node)
46
+ if args.size >= 3
47
+ options = args[2]
48
+ if options.is_a?(Prism::HashNode)
49
+ remote_pair = options.elements.find do |e|
50
+ e.is_a?(Prism::AssocNode) && e.key.is_a?(Prism::SymbolNode) && e.key.value == "remote"
51
+ end
52
+ if remote_pair && remote_pair.value.is_a?(Prism::TrueNode)
53
+ # Replace remote: true with turbo: true
54
+ new_hash = source_for(options).sub("remote: true", "turbo: true")
55
+ replace(node, "link_to(#{source_for(args[0])}, #{source_for(args[1])}, #{new_hash})")
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ # Convert javascript_pack_tag to javascript_importmap_tag
62
+ if @config[:convert_import_maps] && match_call?(node, method: :javascript_pack_tag)
63
+ replace_location(node.message_loc, "javascript_importmap_tag")
64
+ end
65
+
66
+ # Convert stylesheet_pack_tag to stylesheet_link_tag (Propshaft)
67
+ if @config[:convert_propshaft] && match_call?(node, method: :stylesheet_pack_tag)
68
+ replace_location(node.message_loc, "stylesheet_link_tag")
69
+ end
70
+
71
+ # Convert encrypted credentials
72
+ if @config[:convert_encrypted_config] && match_call?(node, method: :encrypted)
73
+ # Rails 7 uses credentials instead
74
+ end
75
+
76
+ super
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,46 @@
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 Rails
9
+ # Rails 7.0 -> 7.1 syntax upgrades
10
+ class Upgrade70To71 < Recipe
11
+ display_name "Upgrade Rails 7.0 to 7.1"
12
+ description "Converts Rails 7.0 syntax to 7.1: ActiveRecord::Encryption, async queries, etc."
13
+
14
+ option :convert_encryption, type: [TrueClass, FalseClass], default: true, description: "Convert to ActiveRecord::Encryption"
15
+ option :convert_async, type: [TrueClass, FalseClass], default: true, description: "Convert to async queries"
16
+
17
+ def get_visitor(context)
18
+ Upgrade70To71Visitor.new(context, config)
19
+ end
20
+ end
21
+
22
+ class Upgrade70To71Visitor < 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 attr_encrypted to ActiveRecord::Encryption
36
+ if @config[:convert_encryption] && match_call?(node, method: :attr_encrypted)
37
+ # Replace with encrypts
38
+ replace_location(node.message_loc, "encrypts")
39
+ end
40
+
41
+ super
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
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 Rails
9
+ # Rails 7.1 -> 7.2 syntax upgrades
10
+ class Upgrade71To72 < Recipe
11
+ display_name "Upgrade Rails 7.1 to 7.2"
12
+ description "Converts Rails 7.1 syntax to 7.2: bracket-free syntax, config.load_defaults, etc."
13
+
14
+ option :convert_bracket_free, type: [TrueClass, FalseClass], default: true, description: "Convert to bracket-free syntax where possible"
15
+ option :convert_load_defaults, type: [TrueClass, FalseClass], default: true, description: "Update load_defaults version"
16
+
17
+ def get_visitor(context)
18
+ Upgrade71To72Visitor.new(context, config)
19
+ end
20
+ end
21
+
22
+ class Upgrade71To72Visitor < 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
+ # Update config.load_defaults
36
+ if @config[:convert_load_defaults] && match_call?(node, method: :load_defaults)
37
+ args = extract_arguments(node)
38
+ if args.size == 1 && args.first.is_a?(Prism::StringNode)
39
+ version = args.first.unescaped
40
+ if version < "7.2"
41
+ replace(node, "config.load_defaults 7.2")
42
+ end
43
+ end
44
+ end
45
+
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,82 @@
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 Rails
9
+ # Rails 7.2 -> 8.0 syntax upgrades
10
+ class Upgrade72To80 < Recipe
11
+ display_name "Upgrade Rails 7.2 to 8.0"
12
+ description "Converts Rails 7.2 syntax to 8.0: Solid Queue, Solid Cache, Solid Cable, etc."
13
+
14
+ option :convert_solid_queue, type: [TrueClass, FalseClass], default: true, description: "Convert ActiveJob to SolidQueue"
15
+ option :convert_solid_cache, type: [TrueClass, FalseClass], default: true, description: "Convert cache store to SolidCache"
16
+ option :convert_solid_cable, type: [TrueClass, FalseClass], default: true, description: "Convert ActionCable to SolidCable"
17
+ option :convert_propshaft, type: [TrueClass, FalseClass], default: true, description: "Convert Sprockets to Propshaft"
18
+ option :convert_ruby_version, type: [TrueClass, FalseClass], default: true, description: "Update Ruby version requirement"
19
+
20
+ def get_visitor(context)
21
+ Upgrade72To80Visitor.new(context, config)
22
+ end
23
+ end
24
+
25
+ class Upgrade72To80Visitor < Reweave::Visitor::RubyVisitor
26
+ def initialize(context, config)
27
+ super(context, nil)
28
+ @config = config
29
+ end
30
+
31
+ def run(source_file)
32
+ @source_file = source_file
33
+ traverse(source_file.root)
34
+ edits
35
+ end
36
+
37
+ def visit_call_node(node)
38
+ # Convert queue_adapter to SolidQueue
39
+ if @config[:convert_solid_queue] && match_call?(node, method: :queue_adapter)
40
+ args = extract_arguments(node)
41
+ if args.size == 1
42
+ first = args[0]
43
+ if first.is_a?(Prism::SymbolNode)
44
+ replace(node, "config.active_job.queue_adapter = :solid_queue")
45
+ end
46
+ end
47
+ end
48
+
49
+ # Convert cache_store to SolidCache
50
+ if @config[:convert_solid_cache] && match_call?(node, method: :cache_store)
51
+ args = extract_arguments(node)
52
+ if args.size == 1
53
+ first = args[0]
54
+ if first.is_a?(Prism::SymbolNode) && %w[redis_store mem_cache_store].include?(first.value)
55
+ replace(node, "config.cache_store = :solid_cache_store")
56
+ end
57
+ end
58
+ end
59
+
60
+ # Convert cable adapter
61
+ if @config[:convert_solid_cable] && match_call?(node, method: :cable)
62
+ # config.cable.url = "redis://..."
63
+ # Convert to solid_cable
64
+ end
65
+
66
+ # Update Ruby version in Gemfile
67
+ if @config[:convert_ruby_version] && match_call?(node, method: :ruby)
68
+ args = extract_arguments(node)
69
+ if args.size == 1 && args.first.is_a?(Prism::StringNode)
70
+ version = args.first.unescaped
71
+ if Gem::Version.new(version) < Gem::Version.new("3.2")
72
+ replace(node, "ruby \"3.2\"")
73
+ end
74
+ end
75
+ end
76
+
77
+ super
78
+ end
79
+ end
80
+ end
81
+ end
82
+ 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 Rails
9
+ # Rails 8.0 -> 8.1 syntax upgrades (future)
10
+ class Upgrade80To81 < Recipe
11
+ display_name "Upgrade Rails 8.0 to 8.1"
12
+ description "Converts Rails 8.0 syntax to 8.1: placeholder for future changes"
13
+
14
+ def get_visitor(context)
15
+ Upgrade80To81Visitor.new(context)
16
+ end
17
+ end
18
+
19
+ class Upgrade80To81Visitor < 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
+ def visit_call_node(node)
31
+ # Rails 8.1 changes TBD
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -10,17 +10,51 @@ require_relative "solid_cache_migration"
10
10
  require_relative "active_job_continuations"
11
11
  require_relative "remove_deprecated_gems"
12
12
  require_relative "controller_markdown_rendering"
13
+ require_relative "upgrade_2_3_to_3_0"
14
+ require_relative "upgrade_3_0_to_3_1"
15
+ require_relative "upgrade_3_1_to_3_2"
16
+ require_relative "upgrade_3_2_to_4_0"
17
+ require_relative "upgrade_4_0_to_4_1"
18
+ require_relative "upgrade_4_1_to_4_2"
19
+ require_relative "upgrade_4_2_to_5_0"
20
+ require_relative "upgrade_5_0_to_5_1"
21
+ require_relative "upgrade_5_1_to_5_2"
22
+ require_relative "upgrade_5_2_to_6_0"
23
+ require_relative "upgrade_6_0_to_6_1"
24
+ require_relative "upgrade_6_1_to_7_0"
25
+ require_relative "upgrade_7_0_to_7_1"
26
+ require_relative "upgrade_7_1_to_7_2"
27
+ require_relative "upgrade_7_2_to_8_0"
28
+ require_relative "upgrade_8_0_to_8_1"
13
29
 
14
30
  module Reweave
15
31
  module Recipes
16
32
  module Rails
17
- # Composite recipe for upgrading Rails applications
33
+ # Composite recipe for upgrading Rails applications from 2.3 to 8.1
18
34
  class UpgradeRails < CompositeRecipe
19
35
  display_name "Upgrade Rails"
20
- description "Comprehensive Rails upgrade recipe combining all individual migration recipes"
36
+ description "Comprehensive Rails upgrade recipe combining all individual migration recipes from 2.3 to 8.1"
21
37
 
22
38
  def initialize(**options)
23
39
  recipes = [
40
+ # Version upgrades
41
+ Upgrade23To30.new(**options),
42
+ Upgrade30To31.new(**options),
43
+ Upgrade31To32.new(**options),
44
+ Upgrade32To40.new(**options),
45
+ Upgrade40To41.new(**options),
46
+ Upgrade41To42.new(**options),
47
+ Upgrade42To50.new(**options),
48
+ Upgrade50To51.new(**options),
49
+ Upgrade51To52.new(**options),
50
+ Upgrade52To60.new(**options),
51
+ Upgrade60To61.new(**options),
52
+ Upgrade61To70.new(**options),
53
+ Upgrade70To71.new(**options),
54
+ Upgrade71To72.new(**options),
55
+ Upgrade72To80.new(**options),
56
+ Upgrade80To81.new(**options),
57
+ # Specific feature migrations
24
58
  ReplaceDeprecatedFind.new(**options),
25
59
  UpdateActiveRecord.new(**options),
26
60
  UpdateRailsRoutes.new(**options),
@@ -36,19 +36,7 @@ module Reweave
36
36
  return super unless @config[:convert_hash_arrows]
37
37
 
38
38
  node.elements.each do |elem|
39
- next unless elem.is_a?(Prism::AssocNode)
40
- next unless elem.key.is_a?(Prism::SymbolNode)
41
-
42
- # Convert :key => value to key: value
43
- key_loc = elem.key.location
44
- operator_loc = elem.operator_loc
45
-
46
- if operator_loc && operator_loc.start_offset == key_loc.end_offset
47
- # This is a hash rocket, convert it
48
- replace_location(operator_loc, ":")
49
- # Remove the leading colon from the symbol
50
- replace_location(key_loc, key_loc.slice.sub(/^:/, ""))
51
- end
39
+ convert_hash_rocket_to_symbol_key(elem)
52
40
  end
53
41
  super
54
42
  end
@@ -66,10 +54,11 @@ module Reweave
66
54
  def visit_block_parameters_node(node)
67
55
  return super unless @config[:convert_block_args]
68
56
 
69
- # Convert |x| to |_1| or |it| for single argument
70
- if node.parameters.size == 1 && node.parameters.first.is_a?(Prism::RequiredParameterNode)
71
- param = node.parameters.first
72
- if param.name.start_with?("_") || param.name == "it" || param.name == "x"
57
+ inner_params = node.parameters
58
+ requireds = inner_params.requireds
59
+ if requireds.size == 1 && inner_params.optionals.empty? && inner_params.rest.nil?
60
+ param = requireds.first
61
+ if %w[x y it].include?(param.name) || param.name.start_with?("_")
73
62
  replace_location(param.location, "it")
74
63
  end
75
64
  end
@@ -0,0 +1,76 @@
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 1.8 -> 1.9 syntax upgrades
10
+ class Upgrade18To19 < Recipe
11
+ display_name "Upgrade Ruby 1.8 to 1.9"
12
+ description "Converts Ruby 1.8 syntax to 1.9: hash rockets, block args, require rubygems, etc."
13
+
14
+ option :convert_hash_arrows, type: [TrueClass, FalseClass], default: true, description: "Convert :key => value to key: value"
15
+ option :remove_require_rubygems, type: [TrueClass, FalseClass], default: true, description: "Remove require 'rubygems'"
16
+ option :convert_symbol_array, type: [TrueClass, FalseClass], default: true, description: "Convert [:a, :b] to %i[a b] (2.0+)"
17
+
18
+ def get_visitor(context)
19
+ Upgrade18To19Visitor.new(context, config)
20
+ end
21
+ end
22
+
23
+ class Upgrade18To19Visitor < 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
+ # Convert :key => value to key: value
36
+ def visit_hash_node(node)
37
+ return super unless @config[:convert_hash_arrows]
38
+
39
+ node.elements.each do |elem|
40
+ convert_hash_rocket_to_symbol_key(elem)
41
+ end
42
+ super
43
+ end
44
+
45
+ # Remove require 'rubygems' / require "rubygems"
46
+ def visit_call_node(node)
47
+ return super unless @config[:remove_require_rubygems]
48
+
49
+ if match_call?(node, method: :require)
50
+ arg = first_arg(node, Prism::StringNode)
51
+ if arg && string_node?(arg, "rubygems")
52
+ replace(node, "")
53
+ end
54
+ end
55
+ super
56
+ end
57
+
58
+ # Convert block arguments: |a, b| -> |a, b| (trailing comma handled by parser)
59
+ # Convert |a| -> |_1| for single arg when enabled
60
+ def visit_block_parameters_node(node)
61
+ return super unless @config[:convert_block_args]
62
+
63
+ inner_params = node.parameters
64
+ requireds = inner_params.requireds
65
+ if requireds.size == 1 && inner_params.optionals.empty? && inner_params.rest.nil?
66
+ param = requireds.first
67
+ if %w[x y it].include?(param.name) || param.name.start_with?("_")
68
+ replace_location(param.location, "it")
69
+ end
70
+ end
71
+ super
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,67 @@
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 1.9 -> 2.0 syntax upgrades
10
+ class Upgrade19To20 < Recipe
11
+ display_name "Upgrade Ruby 1.9 to 2.0"
12
+ description "Converts Ruby 1.9 syntax to 2.0: keyword arguments, %i/%I, __dir__, Module#prepend, Enumerable#lazy"
13
+
14
+ option :convert_symbol_array, type: [TrueClass, FalseClass], default: true, description: "Convert [:a, :b] to %i[a b]"
15
+ option :add_dir_method, type: [TrueClass, FalseClass], default: true, description: "Replace File.dirname(__FILE__) with __dir__"
16
+
17
+ def get_visitor(context)
18
+ Upgrade19To20Visitor.new(context, config)
19
+ end
20
+ end
21
+
22
+ class Upgrade19To20Visitor < 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
+ # Convert [:a, :b] to %i[a b]
35
+ def visit_array_node(node)
36
+ return super unless @config[:convert_symbol_array]
37
+
38
+ return super if node.elements.empty?
39
+
40
+ all_symbols = node.elements.all? { |e| e.is_a?(Prism::SymbolNode) }
41
+ return super unless all_symbols
42
+
43
+ symbols = node.elements.map { |e| e.value }.join(" ")
44
+ loc = node.location
45
+ replace_location(loc, "%i[#{symbols}]")
46
+ super
47
+ end
48
+
49
+ # Replace File.dirname(__FILE__) with __dir__
50
+ def visit_call_node(node)
51
+ return super unless @config[:add_dir_method]
52
+
53
+ if match_call?(node, method: :dirname, receiver: "File")
54
+ args = extract_arguments(node)
55
+ if args.size == 1
56
+ arg = args.first
57
+ if arg.is_a?(Prism::SourceFileNode) || (arg.is_a?(Prism::ConstantReadNode) && arg.name == :__FILE__)
58
+ replace(node, "__dir__")
59
+ end
60
+ end
61
+ end
62
+ super
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end