Skip to content

Commit a8529e1

Browse files
committed
use PRISM_PARSER in rails/convert_model_lambda_scope
1 parent ddf776e commit a8529e1

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

lib/rails/convert_model_lambda_scope.rb

+64-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
Synvert::Rewriter.new 'rails', 'convert_model_lambda_scope' do
4-
configure(parser: Synvert::PARSER_PARSER)
4+
configure(parser: Synvert::PRISM_PARSER)
55

66
description <<~EOS
77
It converts activerecord scope to lambda scope.
@@ -33,38 +33,80 @@ class Post < ActiveRecord::Base
3333

3434
within_files Synvert::RAILS_MODEL_FILES do
3535
# scope :active, where(active: true) => scope :active, -> { where(active: true) }
36-
with_node node_type: 'send', receiver: nil, message: 'scope' do
37-
with_node node_type: 'block', caller: { node_type: 'send', receiver: nil, message: 'proc' } do
38-
if node.arguments.length > 0
39-
replace_with '->({{arguments}}) { {{body}} }'
40-
else
41-
replace_with '-> { {{body}} }'
42-
end
36+
with_node node_type: 'call_node',
37+
receiver: nil,
38+
name: 'scope',
39+
arguments: {
40+
node_type: 'arguments_node',
41+
arguments: {
42+
size: 2,
43+
last: {
44+
node_type: 'call_node',
45+
name: { not_in: ['new', 'proc', 'lambda'] }
46+
}
47+
}
48+
} do
49+
goto_node 'arguments.arguments.last' do
50+
wrap prefix: '-> { ', suffix: ' }'
4351
end
52+
end
4453

45-
with_node node_type: 'block', caller: { node_type: 'send', receiver: 'Proc', message: 'new' } do
46-
if node.arguments.length > 0
47-
replace_with '->({{arguments}}) { {{body}} }'
48-
else
49-
replace_with '-> { {{body}} }'
50-
end
54+
with_node node_type: 'call_node',
55+
receiver: nil,
56+
name: 'scope',
57+
arguments: {
58+
node_type: 'arguments_node',
59+
arguments: {
60+
size: 2,
61+
last: {
62+
node_type: 'call_node',
63+
receiver: 'Proc',
64+
name: 'new'
65+
}
66+
}
67+
} do
68+
if node.arguments.arguments.last.block.parameters
69+
replace 'arguments.arguments.last', with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }'
70+
else
71+
replace 'arguments.arguments.last', with: '-> { {{arguments.arguments.last.block.body}} }'
5172
end
73+
end
5274

53-
unless_exist_node node_type: 'block', caller: { node_type: 'send', message: 'lambda' } do
54-
replace_with 'scope {{arguments.first}}, -> { {{arguments.last}} }'
75+
with_node node_type: 'call_node',
76+
receiver: nil,
77+
name: 'scope',
78+
arguments: {
79+
node_type: 'arguments_node',
80+
arguments: {
81+
size: 2,
82+
last: {
83+
node_type: 'call_node',
84+
receiver: nil,
85+
name: { in: ['proc', 'lambda'] }
86+
}
87+
}
88+
} do
89+
if node.arguments.arguments.last.block.parameters
90+
replace 'arguments.arguments.last', with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }'
91+
else
92+
replace 'arguments.arguments.last', with: '-> { {{arguments.arguments.last.block.body}} }'
5593
end
5694
end
5795

5896
# default_scope order("updated_at DESC") => default_scope -> { order("updated_at DESC") }
59-
with_node node_type: 'send', receiver: nil, message: 'default_scope' do
60-
unless_exist_node node_type: 'block', caller: { node_type: 'send', message: 'lambda' } do
61-
replace_with 'default_scope -> { {{arguments.last}} }'
62-
end
97+
with_node node_type: 'call_node',
98+
receiver: nil,
99+
name: 'default_scope',
100+
arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'call_node' } } } do
101+
replace_with 'default_scope -> { {{arguments.arguments.first}} }'
63102
end
64103

65104
# default_scope { order("updated_at DESC") } => default_scope -> { order("updated_at DESC") }
66-
with_node node_type: 'block', caller: { node_type: 'send', receiver: nil, message: 'default_scope' } do
67-
replace_with 'default_scope -> { {{body}} }'
105+
with_node node_type: 'call_node',
106+
receiver: nil,
107+
name: 'default_scope',
108+
block: { node_type: 'block_node' } do
109+
replace_with 'default_scope -> { {{block.body.body}} }'
68110
end
69111
end
70112
end

0 commit comments

Comments
 (0)