Skip to content

Auto corrected by following Format Ruby Code #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ GEM
tzinfo (~> 2.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
bigdecimal (3.1.9)
coderay (1.1.3)
concurrent-ruby (1.3.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
diff-lcs (1.5.0)
drb (2.2.1)
Expand All @@ -40,36 +40,36 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lumberjack (1.2.8)
method_source (1.0.0)
minitest (5.24.1)
mutex_m (0.2.0)
minitest (5.25.4)
mutex_m (0.3.0)
nenv (0.3.0)
node_mutation (1.24.4)
node_query (1.15.4)
node_query (1.16.0)
node_visitor (1.1.0)
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
parallel (1.25.1)
parser (3.3.4.0)
parallel (1.26.3)
parser (3.3.6.0)
ast (~> 2.4.1)
racc
parser_node_ext (1.4.2)
parser
prettier_print (1.2.1)
prism (0.30.0)
prism (1.3.0)
prism_ext (0.4.2)
prism
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.8.0)
racc (1.8.1)
rake (13.0.3)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
Expand All @@ -92,8 +92,8 @@ GEM
prettier_print (>= 1.2.0)
syntax_tree_ext (0.9.2)
syntax_tree
synvert-core (2.1.2)
activesupport
synvert-core (2.2.1)
activesupport (= 7.1.3.4)
node_mutation (>= 1.24.4)
node_query (>= 1.15.4)
node_visitor (>= 1.1.0)
Expand Down
8 changes: 4 additions & 4 deletions lib/helpers/parse_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Set number_of_workers to 1 to skip parallel.
with_configurations(number_of_workers: 1) do
within_file 'db/schema.rb' do
within_node node_type: 'call_node', name: 'create_table' do
within_node node_type: 'call_node', name: 'create_table' do
table_name = node.arguments.arguments.first.to_value
with_node node_type: 'call_node', receiver: 't', message: { not: 'index' } do
column_name = node.arguments.arguments.first.to_value
Expand Down Expand Up @@ -92,7 +92,7 @@ def find_or_create_table_definition(table_name)
end

def find_table_definition_by_table_name(table_name)
@table_definitions.find { |table| table.name == table_name }
@table_definitions.find { |table| table.name == table_name }
end

def to_h
Expand All @@ -118,7 +118,7 @@ def add_index(name, columns)
end

def find_index_definition_by_column_names(column_names)
@indices.find { |index_definition| index_definition.columns == column_names }
@indices.find { |index_definition| index_definition.columns == column_names }
end

def get_column_names
Expand Down Expand Up @@ -175,7 +175,7 @@ def add_model_association(model_name, association_name, association_type, **asso
end

def find_or_create_model_definition(model_name)
model_definition = @model_definitions.find { |model_definition| model_definition.name == model_name }
model_definition = @model_definitions.find { |model_definition| model_definition.name == model_name }
return model_definition if model_definition

model_definition = ModelDefinition.new(name: model_name)
Expand Down
44 changes: 35 additions & 9 deletions lib/helpers/parse_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@
end

add_callback :call_node, at: 'start' do |node|
if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type)
if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[
constant_read_node constant_path_node
].include?(node.arguments.arguments.first.type)

definitions.add_include_module(node.arguments.arguments.first.to_source)
end
if node.receiver.nil? && node.name == :prepend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type)
if node.receiver.nil? && node.name == :prepend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[
constant_read_node constant_path_node
].include?(node.arguments.arguments.first.type)

definitions.add_prepend_module(node.arguments.arguments.first.to_source)
end
if node.receiver.nil? && node.name == :extend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type)
if node.receiver.nil? && node.name == :extend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[
constant_read_node constant_path_node
].include?(node.arguments.arguments.first.type)

definitions.add_extend_module(node.arguments.arguments.first.to_source)
end
end
Expand Down Expand Up @@ -182,7 +191,7 @@ def find_classes_by_ancestor(superclass)
end

def find_method_by_name(name)
methods.find { |method_definition| method_definition.name == name }
methods.find { |method_definition| method_definition.name == name }
end

def full_name
Expand Down Expand Up @@ -259,7 +268,12 @@ def initialize
end

def to_h
{ modules: @modules.map(&:to_h), classes: @classes.map(&:to_h), constants: @constants, methods: @methods.map(&:to_h) }
{
modules: @modules.map(&:to_h),
classes: @classes.map(&:to_h),
constants: @constants,
methods: @methods.map(&:to_h)
}
end
end

Expand All @@ -286,14 +300,24 @@ def to_h
methods: @methods.map(&:to_h),
static_methods: @static_methods.map(&:to_h),
constants: @constants,
singleton: @singleton &.to_h,
singleton: @singleton&.to_h,
ancestors: @ancestors
}
end
end

class ClassDefinition < BaseDefinition
attr_reader :parent, :name, :superclass, :modules, :classes, :methods, :static_methods, :constants, :include_modules, :prepend_modules, :extend_modules
attr_reader :parent,
:name,
:superclass,
:modules,
:classes,
:methods,
:static_methods,
:constants,
:include_modules,
:prepend_modules,
:extend_modules
attr_accessor :singleton, :ancestors

def initialize(parent:, name:, superclass:)
Expand Down Expand Up @@ -360,11 +384,13 @@ def initialize(parent:, name:)

def call_method?(method_name)
local_calls.include?(method_name) ||
local_calls.any? { |local_call_method_name| parent.find_method_by_name(local_call_method_name)&.call_method?(method_name) }
local_calls.any? { |local_call_method_name|
parent.find_method_by_name(local_call_method_name)&.call_method?(method_name)
}
end

def call_any_method?(method_names)
method_names.any? { |method_name| call_method?(method_name) }
method_names.any? { |method_name| call_method?(method_name) }
end

def to_h
Expand Down
5 changes: 4 additions & 1 deletion lib/helpers/set_rails_load_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
within_file 'config/application.rb' do
with_node node_type: 'class_node', constant_path: 'Application' do
exists = false
with_node node_type: 'call_node', receiver: 'config', name: 'load_defaults', arguments: { arguments: { length: 1 } } do
with_node node_type: 'call_node',
receiver: 'config',
name: 'load_defaults',
arguments: { arguments: { length: 1 } } do
exists = true
replace_with "config.load_defaults #{rails_version}"
end
Expand Down
16 changes: 14 additions & 2 deletions lib/rails/convert_after_commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
with_node node_type: 'call_node',
receiver: nil,
name: 'after_commit',
arguments: { node_type: 'arguments_node', arguments: { size: 2, '1': { node_type: 'keyword_hash_node', on_value: { in: %i[create update destroy] } } } } do
arguments: {
node_type: 'arguments_node',
arguments: {
size: 2,
'1': { node_type: 'keyword_hash_node', on_value: { in: %i[create update destroy] } }
}
} do
group do
replace :name, with: 'after_{{arguments.arguments.-1.on_value.to_value}}_commit'
delete 'arguments.arguments.-1.on_element', and_comma: true
Expand All @@ -60,7 +66,13 @@
with_node node_type: 'call_node',
receiver: nil,
message: 'after_commit',
arguments: { node_type: 'arguments_node', arguments: { size: 2, '1': { node_type: 'keyword_hash_node', on_value: { node_type: 'array_node' } } } } do
arguments: {
node_type: 'arguments_node',
arguments: {
size: 2,
'1': { node_type: 'keyword_hash_node', on_value: { node_type: 'array_node' } }
}
} do
group do
if node.arguments.arguments[1].on_value.elements.size == 1
replace :message, with: 'after_{{arguments.arguments.-1.on_value.elements.0.to_value}}_commit'
Expand Down
8 changes: 2 additions & 6 deletions lib/rails/convert_configs_3_0_to_3_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,12 @@ def down

within_file 'config/environments/test.rb' do
# prepend config.static_cache_control = "public, max-age=3600"
unless_exist_node node_type: 'call_node',
receiver: 'config',
name: 'static_cache_control=' do
unless_exist_node node_type: 'call_node', receiver: 'config', name: 'static_cache_control=' do
prepend 'config.static_cache_control = "public, max-age=3600"'
end

# prepend config.serve_static_assets = true
unless_exist_node node_type: 'call_node',
receiver: 'config',
name: 'serve_static_assets=' do
unless_exist_node node_type: 'call_node', receiver: 'config', name: 'serve_static_assets=' do
prepend 'config.serve_static_assets = true'
end
end
Expand Down
26 changes: 22 additions & 4 deletions lib/rails/convert_configs_3_2_to_4_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@
end

# remove config.middleware.xxx(..., ActionDispatch::BestStandardsSupport)
with_node node_type: 'call_node', arguments: { node_type: 'arguments_node', arguments: { includes: 'ActionDispatch::BestStandardsSupport' } } do
with_node node_type: 'call_node',
arguments: {
node_type: 'arguments_node',
arguments: { includes: 'ActionDispatch::BestStandardsSupport' }
} do
remove
end

Expand Down Expand Up @@ -153,9 +157,23 @@
receiver: 'ActiveSupport',
name: 'on_load',
arguments: { node_type: 'arguments_node', arguments: { size: 1, first: :active_record } },
block: { node_type: 'block_node', body: { node_type: 'statements_node', body: { size: 1, first: {
node_type: 'call_node', receiver: 'self', name: 'include_root_in_json=', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: false } }
} } } } do
block: {
node_type: 'block_node',
body: {
node_type: 'statements_node',
body: {
size: 1,
first: {
node_type: 'call_node',
receiver: 'self',
name: 'include_root_in_json=',
arguments: {
node_type: 'arguments_node', arguments: { size: 1, first: false }
}
}
}
}
} do
remove
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/rails/convert_configs_4_2_to_5_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
name: 'middleware'
},
name: 'use',
arguments: { node_type: 'arguments_node', arguments: { size: { gt: 0 }, first: { node_type: 'string_node' } } } do
arguments: {
node_type: 'arguments_node',
arguments: { size: { gt: 0 }, first: { node_type: 'string_node' } }
} do
replace 'arguments.arguments.first', with: "{{arguments.arguments.first.to_value}}"
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/rails/convert_dynamic_finders_for_rails_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
EOS

definitions = call_helper 'rails/parse'
table_columns = definitions.table_definitions.flat_map { |table_definition| table_definition.get_column_names }.uniq + ['id']
table_columns = definitions.table_definitions.flat_map { |table_definition|
table_definition.get_column_names
}
.uniq + ['id']

helper_method :dynamic_finder_to_hash do |prefix|
fields = node.name.to_s[prefix.length..-1].split('_and_')
return nil if (fields - table_columns).present?

if fields.length == node.arguments.arguments.length && :hash_node != node.arguments.arguments.first.type
fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }.join(', ')
fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }
.join(', ')
else
'{{arguments}}'
end
Expand Down
8 changes: 6 additions & 2 deletions lib/rails/convert_dynamic_finders_for_rails_4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
EOS

definitions = call_helper 'rails/parse'
table_columns = definitions.table_definitions.flat_map { |table_definition| table_definition.get_column_names }.uniq + ['id']
table_columns = definitions.table_definitions.flat_map { |table_definition|
table_definition.get_column_names
}
.uniq + ['id']

helper_method :dynamic_finder_to_hash do |prefix|
fields = node.name.to_s[prefix.length..-1].split('_and_')
return nil if (fields - table_columns).present?

if fields.length == node.arguments.arguments.length && :hash_node != node.arguments.arguments.first.type
fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }.join(', ')
fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }
.join(', ')
else
'{{arguments}}'
end
Expand Down
21 changes: 18 additions & 3 deletions lib/rails/convert_head_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
with_node node_type: 'call_node',
receiver: nil,
name: 'render',
arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', nothing_value: true } } } do
arguments: {
node_type: 'arguments_node',
arguments: { size: 1, first: { node_type: 'keyword_hash_node', nothing_value: true } }
} do
group do
replace :message, with: 'head'
if node.arguments.arguments.first.status_value.nil?
Expand All @@ -53,7 +56,13 @@
with_node node_type: 'call_node',
receiver: nil,
name: 'head',
arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', location_value: { not: nil } } } } do
arguments: {
node_type: 'arguments_node',
arguments: {
size: 1,
first: { node_type: 'keyword_hash_node', location_value: { not: nil } }
}
} do
replace 'arguments.arguments.0', with: ':ok, {{arguments.arguments.0.to_source}}'
end

Expand All @@ -63,7 +72,13 @@
with_node node_type: 'call_node',
receiver: nil,
name: 'head',
arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', status_value: { not: nil } } } } do
arguments: {
node_type: 'arguments_node',
arguments: {
size: 1,
first: { node_type: 'keyword_hash_node', status_value: { not: nil } }
}
} do
replace 'arguments.arguments.0', with: '{{arguments.arguments.0.status_source}}'
end
end
Expand Down
Loading
Loading