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,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.3 -> 4.0 syntax upgrades (speculative/preview)
|
|
10
|
+
class Upgrade33To40 < Recipe
|
|
11
|
+
display_name "Upgrade Ruby 3.3 to 4.0"
|
|
12
|
+
description "Converts Ruby 3.3 syntax to 4.0: breaking changes (placeholder for future)"
|
|
13
|
+
|
|
14
|
+
def get_visitor(context)
|
|
15
|
+
Upgrade33To40Visitor.new(context)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Upgrade33To40Visitor < 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
|
+
# Ruby 4.0 changes TBD - placeholder for future breaking changes
|
|
31
|
+
def visit_call_node(node)
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../reweave/recipe/composite_recipe"
|
|
4
|
+
require_relative "upgrade_1_8_to_1_9"
|
|
5
|
+
require_relative "upgrade_1_9_to_2_0"
|
|
6
|
+
require_relative "upgrade_2_0_to_2_1"
|
|
7
|
+
require_relative "upgrade_2_1_to_2_2"
|
|
8
|
+
require_relative "upgrade_2_2_to_2_3"
|
|
9
|
+
require_relative "upgrade_2_3_to_2_4"
|
|
10
|
+
require_relative "upgrade_2_4_to_2_5"
|
|
11
|
+
require_relative "upgrade_2_5_to_2_6"
|
|
12
|
+
require_relative "upgrade_2_6_to_2_7"
|
|
13
|
+
require_relative "upgrade_2_7_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_3_3"
|
|
17
|
+
require_relative "upgrade_3_3_to_4_0"
|
|
18
|
+
|
|
19
|
+
module Reweave
|
|
20
|
+
module Recipes
|
|
21
|
+
module Ruby
|
|
22
|
+
# Composite recipe for upgrading Ruby versions from 1.8 to 4.0
|
|
23
|
+
class UpgradeRuby < CompositeRecipe
|
|
24
|
+
display_name "Upgrade Ruby"
|
|
25
|
+
description "Comprehensive Ruby upgrade recipe combining all version migration recipes from 1.8 to 4.0"
|
|
26
|
+
|
|
27
|
+
def initialize(**options)
|
|
28
|
+
recipes = [
|
|
29
|
+
Upgrade18To19.new(**options),
|
|
30
|
+
Upgrade19To20.new(**options),
|
|
31
|
+
Upgrade20To21.new(**options),
|
|
32
|
+
Upgrade21To22.new(**options),
|
|
33
|
+
Upgrade22To23.new(**options),
|
|
34
|
+
Upgrade23To24.new(**options),
|
|
35
|
+
Upgrade24To25.new(**options),
|
|
36
|
+
Upgrade25To26.new(**options),
|
|
37
|
+
Upgrade26To27.new(**options),
|
|
38
|
+
Upgrade27To30.new(**options),
|
|
39
|
+
Upgrade30To31.new(**options),
|
|
40
|
+
Upgrade31To32.new(**options),
|
|
41
|
+
Upgrade32To33.new(**options),
|
|
42
|
+
Upgrade33To40.new(**options)
|
|
43
|
+
]
|
|
44
|
+
super(recipes, **options)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "symbol_table"
|
|
4
|
+
require_relative "symbol_collector"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Semantic
|
|
8
|
+
# Builds a SymbolTable by scanning all source files in a Project.
|
|
9
|
+
#
|
|
10
|
+
# The analyzer walks every valid Ruby source file, collects method and
|
|
11
|
+
# constant definitions, and produces a cross-file symbol index that
|
|
12
|
+
# recipes can query for context-aware transformations.
|
|
13
|
+
class Analyzer
|
|
14
|
+
def initialize(project)
|
|
15
|
+
@project = project
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Analyze all source files in the project and return a populated
|
|
19
|
+
# SymbolTable.
|
|
20
|
+
def analyze
|
|
21
|
+
table = SymbolTable.new
|
|
22
|
+
@project.valid_source_files.each do |source_file|
|
|
23
|
+
collector = SymbolCollector.new(table, source_file.path, source: source_file.source)
|
|
24
|
+
collector.run(source_file.root)
|
|
25
|
+
end
|
|
26
|
+
table
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Reweave
|
|
4
|
+
module Semantic
|
|
5
|
+
# Represents a resolved symbol (method or constant) in the codebase.
|
|
6
|
+
class Symbol
|
|
7
|
+
attr_reader :name, :kind, :file_path, :line, :column, :namespace, :source
|
|
8
|
+
|
|
9
|
+
# @param name [Symbol] the symbol name
|
|
10
|
+
# @param kind [Symbol] :method or :constant
|
|
11
|
+
# @param file_path [String] path to the file where the symbol is defined
|
|
12
|
+
# @param line [Integer] line number (1-based)
|
|
13
|
+
# @param column [Integer] column number (0-based)
|
|
14
|
+
# @param namespace [String, nil] enclosing class/module namespace (e.g., "User", "Admin::BaseController")
|
|
15
|
+
# @param source [String, nil] source text of the definition
|
|
16
|
+
def initialize(name:, kind:, file_path:, line:, column:, namespace: nil, source: nil)
|
|
17
|
+
@name = name
|
|
18
|
+
@kind = kind
|
|
19
|
+
@file_path = file_path
|
|
20
|
+
@line = line
|
|
21
|
+
@column = column
|
|
22
|
+
@namespace = namespace
|
|
23
|
+
@source = source
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def method?
|
|
27
|
+
kind == :method
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def constant?
|
|
31
|
+
kind == :constant
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_s
|
|
35
|
+
"#<Reweave::Semantic::Symbol #{kind}=#{name} #{file_path}:#{line}>"
|
|
36
|
+
end
|
|
37
|
+
alias_method :inspect, :to_s
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "symbol_table"
|
|
4
|
+
require_relative "../../reweave/visitor/visitor"
|
|
5
|
+
|
|
6
|
+
module Reweave
|
|
7
|
+
module Semantic
|
|
8
|
+
# Walks a single source file's AST and collects method and constant
|
|
9
|
+
# definitions into a SymbolTable. Tracks the current class/module
|
|
10
|
+
# namespace so symbols are indexed with their proper scope.
|
|
11
|
+
class SymbolCollector < Reweave::Visitor::Base
|
|
12
|
+
def initialize(symbol_table, file_path, source: nil)
|
|
13
|
+
super(nil, nil)
|
|
14
|
+
@symbol_table = symbol_table
|
|
15
|
+
@file_path = file_path
|
|
16
|
+
@source = source || File.read(file_path, encoding: "bom|utf-8")
|
|
17
|
+
@namespace_stack = []
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Collect symbols from a source file's AST root node.
|
|
21
|
+
def run(root_node)
|
|
22
|
+
traverse(root_node)
|
|
23
|
+
@symbol_table
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Enter a class definition: record the constant and push namespace.
|
|
27
|
+
def visit_class_node(node)
|
|
28
|
+
const_name = constant_full_name(node.constant_path)
|
|
29
|
+
namespace = current_namespace
|
|
30
|
+
full_name = namespace ? "#{namespace}::#{const_name}" : const_name
|
|
31
|
+
|
|
32
|
+
# Record the class itself as a constant
|
|
33
|
+
@symbol_table.add_constant(
|
|
34
|
+
const_name.to_sym,
|
|
35
|
+
@file_path,
|
|
36
|
+
node.location.start_line,
|
|
37
|
+
node.location.start_column,
|
|
38
|
+
source_for(node),
|
|
39
|
+
namespace: namespace
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
@namespace_stack.push(full_name)
|
|
43
|
+
visit_child_nodes(node) # traverse body
|
|
44
|
+
@namespace_stack.pop
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Enter a module definition: record the constant and push namespace.
|
|
48
|
+
def visit_module_node(node)
|
|
49
|
+
const_name = constant_full_name(node.constant_path)
|
|
50
|
+
namespace = current_namespace
|
|
51
|
+
full_name = namespace ? "#{namespace}::#{const_name}" : const_name
|
|
52
|
+
|
|
53
|
+
@symbol_table.add_constant(
|
|
54
|
+
const_name.to_sym,
|
|
55
|
+
@file_path,
|
|
56
|
+
node.location.start_line,
|
|
57
|
+
node.location.start_column,
|
|
58
|
+
source_for(node),
|
|
59
|
+
namespace: namespace
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
@namespace_stack.push(full_name)
|
|
63
|
+
visit_child_nodes(node) # traverse body
|
|
64
|
+
@namespace_stack.pop
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Enter a method definition: record instance or class method.
|
|
68
|
+
def visit_def_node(node)
|
|
69
|
+
method_name = node.name
|
|
70
|
+
namespace = current_namespace || "Object"
|
|
71
|
+
|
|
72
|
+
if node.receiver.nil?
|
|
73
|
+
# Instance method: def foo; end
|
|
74
|
+
@symbol_table.add_method(
|
|
75
|
+
method_name, namespace, @file_path,
|
|
76
|
+
node.location.start_line, node.location.start_column,
|
|
77
|
+
source_for(node), instance_method: true
|
|
78
|
+
)
|
|
79
|
+
elsif node.receiver.is_a?(Prism::SelfNode)
|
|
80
|
+
# Class method: def self.foo; end
|
|
81
|
+
@symbol_table.add_method(
|
|
82
|
+
method_name, namespace, @file_path,
|
|
83
|
+
node.location.start_line, node.location.start_column,
|
|
84
|
+
source_for(node), instance_method: false
|
|
85
|
+
)
|
|
86
|
+
elsif node.receiver.is_a?(Prism::ConstantReadNode)
|
|
87
|
+
# Class method on a constant: def User.foo; end
|
|
88
|
+
const_ns = node.receiver.name.to_s
|
|
89
|
+
@symbol_table.add_method(
|
|
90
|
+
method_name, const_ns, @file_path,
|
|
91
|
+
node.location.start_line, node.location.start_column,
|
|
92
|
+
source_for(node), instance_method: false
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
visit_child_nodes(node) # traverse body
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Constant assignment: FOO = value
|
|
100
|
+
def visit_constant_write_node(node)
|
|
101
|
+
namespace = current_namespace
|
|
102
|
+
@symbol_table.add_constant(
|
|
103
|
+
node.name,
|
|
104
|
+
@file_path,
|
|
105
|
+
node.location.start_line,
|
|
106
|
+
node.location.start_column,
|
|
107
|
+
source_for(node),
|
|
108
|
+
namespace: namespace
|
|
109
|
+
)
|
|
110
|
+
visit_child_nodes(node)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Namespaced constant assignment: Foo::BAR = value
|
|
114
|
+
def visit_constant_path_write_node(node)
|
|
115
|
+
const_name = constant_full_name(node.target)
|
|
116
|
+
namespace = current_namespace
|
|
117
|
+
full_name = namespace ? "#{namespace}::#{const_name}" : const_name
|
|
118
|
+
|
|
119
|
+
@symbol_table.add_constant(
|
|
120
|
+
node.name,
|
|
121
|
+
@file_path,
|
|
122
|
+
node.location.start_line,
|
|
123
|
+
node.location.start_column,
|
|
124
|
+
source_for(node),
|
|
125
|
+
namespace: full_name
|
|
126
|
+
)
|
|
127
|
+
visit_child_nodes(node)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def current_namespace
|
|
133
|
+
@namespace_stack.last
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def source_for(node)
|
|
137
|
+
loc = node.location
|
|
138
|
+
@source[loc.start_offset...loc.end_offset]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Convert a Prism constant path node to its full name string.
|
|
142
|
+
# ConstantReadNode(name: :User) -> "User"
|
|
143
|
+
# ConstantPathNode(parent: :Foo, name: :Bar) -> "Foo::Bar"
|
|
144
|
+
def constant_full_name(node)
|
|
145
|
+
case node
|
|
146
|
+
when Prism::ConstantReadNode
|
|
147
|
+
node.name.to_s
|
|
148
|
+
when Prism::ConstantPathNode
|
|
149
|
+
parent_name = constant_full_name(node.parent)
|
|
150
|
+
"#{parent_name}::#{node.name}"
|
|
151
|
+
when Prism::ConstantPathWriteNode
|
|
152
|
+
constant_full_name(node.target)
|
|
153
|
+
else
|
|
154
|
+
node.name.to_s
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "symbol"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Semantic
|
|
7
|
+
# Index of all symbols (methods and constants) across a project.
|
|
8
|
+
#
|
|
9
|
+
# Methods are indexed by both instance and class method keys:
|
|
10
|
+
# - "Namespace#method_name" for instance methods
|
|
11
|
+
# - "Namespace.method_name" for class (singleton) methods
|
|
12
|
+
#
|
|
13
|
+
# Constants are indexed by their full qualified name:
|
|
14
|
+
# - "User" for top-level constants
|
|
15
|
+
# - "Admin::BaseController" for nested constants
|
|
16
|
+
class SymbolTable
|
|
17
|
+
def initialize
|
|
18
|
+
@instance_methods = {} # "Namespace#method_name" => Symbol
|
|
19
|
+
@class_methods = {} # "Namespace.method_name" => Symbol
|
|
20
|
+
@constants = {} # "ConstantName" => Symbol
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Add a method definition to the table.
|
|
24
|
+
def add_method(name, namespace, file_path, line, column, source, instance_method: true)
|
|
25
|
+
key = build_method_key(name, namespace, instance_method: instance_method)
|
|
26
|
+
symbol = Symbol.new(
|
|
27
|
+
name: name,
|
|
28
|
+
kind: :method,
|
|
29
|
+
file_path: file_path,
|
|
30
|
+
line: line,
|
|
31
|
+
column: column,
|
|
32
|
+
namespace: namespace,
|
|
33
|
+
source: source
|
|
34
|
+
)
|
|
35
|
+
if instance_method
|
|
36
|
+
@instance_methods[key] = symbol
|
|
37
|
+
else
|
|
38
|
+
@class_methods[key] = symbol
|
|
39
|
+
end
|
|
40
|
+
symbol
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Add a constant definition to the table.
|
|
44
|
+
def add_constant(name, file_path, line, column, source, namespace: nil)
|
|
45
|
+
full_name = namespace ? "#{namespace}::#{name}" : name.to_s
|
|
46
|
+
symbol = Symbol.new(
|
|
47
|
+
name: name,
|
|
48
|
+
kind: :constant,
|
|
49
|
+
file_path: file_path,
|
|
50
|
+
line: line,
|
|
51
|
+
column: column,
|
|
52
|
+
namespace: namespace,
|
|
53
|
+
source: source
|
|
54
|
+
)
|
|
55
|
+
@constants[full_name] = symbol
|
|
56
|
+
symbol
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Resolve an instance method call: "namespace.method_name" or "namespace#method_name"
|
|
60
|
+
def resolve_method(name, namespace = nil)
|
|
61
|
+
key = build_method_key(name, namespace, instance_method: true)
|
|
62
|
+
@instance_methods[key] || @class_methods[build_method_key(name, namespace, instance_method: false)]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Resolve a constant by its (possibly qualified) name.
|
|
66
|
+
def resolve_constant(name)
|
|
67
|
+
@constants[name.to_s]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# All symbols of a given kind.
|
|
71
|
+
def symbols(kind: nil)
|
|
72
|
+
if kind
|
|
73
|
+
all_symbols.select { |s| s.kind == kind }
|
|
74
|
+
else
|
|
75
|
+
all_symbols
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def size
|
|
80
|
+
@instance_methods.size + @class_methods.size + @constants.size
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def empty?
|
|
84
|
+
size.zero?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def all_symbols
|
|
90
|
+
@instance_methods.values + @class_methods.values + @constants.values
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def build_method_key(name, namespace, instance_method:)
|
|
94
|
+
ns = namespace || "Object"
|
|
95
|
+
separator = instance_method ? "#" : "."
|
|
96
|
+
"#{ns}#{separator}#{name}"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
data/lib/reweave/version.rb
CHANGED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "ruby_visitor"
|
|
4
|
+
|
|
5
|
+
module Reweave
|
|
6
|
+
module Visitor
|
|
7
|
+
# Provides common recipe operations as reusable methods
|
|
8
|
+
module RecipeHelpers
|
|
9
|
+
# Replace a method call's name (e.g., update_attributes -> update)
|
|
10
|
+
def replace_method_name(node, new_name)
|
|
11
|
+
return unless node.is_a?(Prism::CallNode) && node.message_loc
|
|
12
|
+
replace_location(node.message_loc, new_name.to_s)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Replace the entire call node with a new expression
|
|
16
|
+
def replace_call(node, new_source)
|
|
17
|
+
replace(node, new_source)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Replace a specific argument in a call
|
|
21
|
+
def replace_argument(node, arg_index, new_source)
|
|
22
|
+
args = extract_arguments(node)
|
|
23
|
+
return unless args[arg_index]
|
|
24
|
+
replace(args[arg_index], new_source)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Replace the first argument if it matches a value
|
|
28
|
+
def replace_first_arg_if(node, old_value, new_source)
|
|
29
|
+
args = extract_arguments(node)
|
|
30
|
+
return unless args.first
|
|
31
|
+
arg = args.first
|
|
32
|
+
if values_equal?(arg, old_value)
|
|
33
|
+
replace(arg, new_source)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Check if a node's value equals a given value (handles SymbolNode, StringNode, etc.)
|
|
38
|
+
def values_equal?(node, expected)
|
|
39
|
+
case node
|
|
40
|
+
when Prism::SymbolNode
|
|
41
|
+
node.value == expected.to_s
|
|
42
|
+
when Prism::StringNode
|
|
43
|
+
node.unescaped == expected.to_s
|
|
44
|
+
when Prism::IntegerNode
|
|
45
|
+
node.value == expected
|
|
46
|
+
else
|
|
47
|
+
false
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Match a config setter: config.xxx.yyy = value
|
|
52
|
+
# path_parts is the receiver chain from outermost to innermost (e.g., [:config, :active_job])
|
|
53
|
+
# method is the setter method name (e.g., :queue_adapter=)
|
|
54
|
+
def match_config_setter?(node, *path_parts, method: nil)
|
|
55
|
+
# Normalize path_parts to symbols for comparison with chain (which uses Symbol names)
|
|
56
|
+
path_parts = path_parts.map(&:to_sym)
|
|
57
|
+
|
|
58
|
+
return false unless match_call?(node, method: method || :"#{path_parts.last}=")
|
|
59
|
+
|
|
60
|
+
# Walk inward through call nodes, collecting names (innermost-first)
|
|
61
|
+
receiver = node.receiver
|
|
62
|
+
chain = []
|
|
63
|
+
while receiver.is_a?(Prism::CallNode)
|
|
64
|
+
chain << receiver.name
|
|
65
|
+
receiver = receiver.receiver
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# After the chain, receiver must be :config constant (or nil/self)
|
|
69
|
+
base_ok = receiver.nil? || receiver.is_a?(Prism::SelfNode) ||
|
|
70
|
+
(receiver.is_a?(Prism::ConstantReadNode) && receiver.name == :config)
|
|
71
|
+
return false unless base_ok
|
|
72
|
+
|
|
73
|
+
# Build full path: [:config] + chain reversed (outermost-first)
|
|
74
|
+
full_path = if receiver.is_a?(Prism::ConstantReadNode) && receiver.name == :config
|
|
75
|
+
[:config] + chain.reverse
|
|
76
|
+
else
|
|
77
|
+
chain.reverse
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
full_path == path_parts
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Match a call with specific receiver chain and method
|
|
84
|
+
def match_call_chain?(node, *chain, method: nil)
|
|
85
|
+
return false unless match_call?(node, method: method)
|
|
86
|
+
receiver = node.receiver
|
|
87
|
+
chain.reverse.each do |part|
|
|
88
|
+
return false unless receiver&.is_a?(Prism::CallNode) && receiver.name == part
|
|
89
|
+
receiver = receiver.receiver
|
|
90
|
+
end
|
|
91
|
+
receiver.nil? || receiver.is_a?(Prism::SelfNode) || (receiver.is_a?(Prism::ConstantReadNode) && receiver.name == :config)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Replace method call with method call on same receiver: receiver.old_method(args) -> receiver.new_method(args)
|
|
95
|
+
def replace_method_on_receiver(node, new_method_name)
|
|
96
|
+
receiver = node.receiver
|
|
97
|
+
args = extract_arguments(node)
|
|
98
|
+
return unless receiver
|
|
99
|
+
arg_sources = args.map { |a| source_for(a) }.join(", ")
|
|
100
|
+
replace(node, "#{source_for(receiver)}.#{new_method_name}(#{arg_sources})")
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Replace method call with new receiver.method: receiver.old_method(args) -> new_receiver.new_method(args)
|
|
104
|
+
def replace_receiver_and_method(node, new_receiver, new_method_name)
|
|
105
|
+
args = extract_arguments(node)
|
|
106
|
+
arg_sources = args.map { |a| source_for(a) }.join(", ")
|
|
107
|
+
replace(node, "#{new_receiver}.#{new_method_name}(#{arg_sources})")
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Convert hash rocket to symbol key syntax in place
|
|
111
|
+
def convert_hash_rocket_to_symbol_key(node)
|
|
112
|
+
return unless node.is_a?(Prism::AssocNode)
|
|
113
|
+
return unless node.key.is_a?(Prism::SymbolNode)
|
|
114
|
+
return unless node.operator_loc&.slice == "=>"
|
|
115
|
+
|
|
116
|
+
key_loc = node.key.location
|
|
117
|
+
operator_loc = node.operator_loc
|
|
118
|
+
new_key = key_loc.slice.sub(/^:/, "") + ":"
|
|
119
|
+
start_offset = key_loc.start_offset
|
|
120
|
+
end_offset = operator_loc.end_offset
|
|
121
|
+
@edits.add(Editing.replace(source_id, start_offset, end_offset, new_key, recipe_name))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Check if a node is a simple method call (no arguments, no block)
|
|
125
|
+
def simple_call?(node, method: nil)
|
|
126
|
+
return false unless node.is_a?(Prism::CallNode)
|
|
127
|
+
return false if method && node.name != method
|
|
128
|
+
extract_arguments(node).empty? && node.block.nil?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Check if a node is a method call with specific argument count
|
|
132
|
+
def call_with_args?(node, count, method: nil)
|
|
133
|
+
return false unless node.is_a?(Prism::CallNode)
|
|
134
|
+
return false if method && node.name != method
|
|
135
|
+
extract_arguments(node).size == count
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Get the first argument as a specific type
|
|
139
|
+
def first_arg(node, *types)
|
|
140
|
+
args = extract_arguments(node)
|
|
141
|
+
return nil if args.empty?
|
|
142
|
+
arg = args.first
|
|
143
|
+
return arg if types.empty? || types.any? { |t| arg.is_a?(t) }
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Get keyword argument value by key
|
|
148
|
+
def keyword_arg(node, key)
|
|
149
|
+
kwargs = extract_keyword_arguments(node)
|
|
150
|
+
kwargs[key]
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Replace keyword argument value
|
|
154
|
+
def replace_keyword_arg(node, key, new_source)
|
|
155
|
+
kwargs = extract_keyword_arguments(node)
|
|
156
|
+
return unless kwargs[key]
|
|
157
|
+
replace(kwargs[key], new_source)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Check if a symbol node matches a name
|
|
161
|
+
def symbol_node?(node, name)
|
|
162
|
+
node.is_a?(Prism::SymbolNode) && node.value == name.to_s
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Check if a string node matches a value
|
|
166
|
+
def string_node?(node, value)
|
|
167
|
+
node.is_a?(Prism::StringNode) && node.unescaped == value
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Replace a node's source with a new method call on the same receiver
|
|
171
|
+
def replace_with_method_call(node, method_name, *extra_args)
|
|
172
|
+
receiver = node.receiver
|
|
173
|
+
return unless receiver
|
|
174
|
+
args = extract_arguments(node)
|
|
175
|
+
all_args = args + extra_args
|
|
176
|
+
arg_sources = all_args.map { |a| source_for(a) }.join(", ")
|
|
177
|
+
replace(node, "#{source_for(receiver)}.#{method_name}(#{arg_sources})")
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Common pattern: replace config.setter = :old_value with :new_value
|
|
181
|
+
# config_path is the receiver chain (e.g., [:config, :active_job])
|
|
182
|
+
# method is the setter method name (e.g., :queue_adapter=)
|
|
183
|
+
def replace_config_value(node, config_path, old_values, new_value, method: nil)
|
|
184
|
+
return unless match_config_setter?(node, *config_path, method: method)
|
|
185
|
+
args = extract_arguments(node)
|
|
186
|
+
return if args.empty?
|
|
187
|
+
|
|
188
|
+
arg = args.first
|
|
189
|
+
old_value_str = case arg
|
|
190
|
+
when Prism::SymbolNode then arg.value
|
|
191
|
+
when Prism::StringNode then arg.unescaped
|
|
192
|
+
else return
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
return unless old_values.include?(old_value_str)
|
|
196
|
+
|
|
197
|
+
replacement = case arg
|
|
198
|
+
when Prism::SymbolNode then ":#{new_value}"
|
|
199
|
+
when Prism::StringNode then "\"#{new_value}\""
|
|
200
|
+
end
|
|
201
|
+
replace(arg, replacement)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Build a method call source string
|
|
205
|
+
def build_call_source(receiver_source, method_name, *arg_sources)
|
|
206
|
+
"#{receiver_source}.#{method_name}(#{arg_sources.join(", ")})"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Check if we're in a config block (config.xxx = value)
|
|
210
|
+
def in_config_block?(node)
|
|
211
|
+
current = node
|
|
212
|
+
while current
|
|
213
|
+
if current.is_a?(Prism::CallNode) && current.receiver&.is_a?(Prism::ConstantReadNode) && current.receiver.name == :config
|
|
214
|
+
return true
|
|
215
|
+
end
|
|
216
|
+
current = cursor&.parent_node
|
|
217
|
+
end
|
|
218
|
+
false
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "visitor"
|
|
4
|
+
require_relative "recipe_helpers"
|
|
4
5
|
|
|
5
6
|
module Reweave
|
|
6
7
|
module Visitor
|
|
7
8
|
class RubyVisitor < Base
|
|
9
|
+
include RecipeHelpers
|
|
10
|
+
|
|
8
11
|
# Class/Module definitions
|
|
9
12
|
def visit_class_node(node)
|
|
10
13
|
super
|