Skip to content

Commit 11cd5cb

Browse files
committed
use PRISM_PARSER in rails/convert_test_request_methods_4_2_to_5_0
1 parent 83728fd commit 11cd5cb

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

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

66
description <<~EOS
77
It converts rails test request methods from 4.2 to 5.0
@@ -35,14 +35,12 @@
3535

3636
request_methods = %i[get post put patch delete]
3737

38-
helper_method :make_up_hash_pair do |key, argument_node|
38+
helper_method :make_up_hash_element do |key, argument_node|
3939
next if argument_node.to_source == 'nil'
4040

41-
if argument_node.type == :hash
41+
if argument_node.type == :keyword_hash_node || argument_node.type == :hash_node
4242
new_value =
43-
argument_node.pairs.reject { |pair_node|
44-
%i[format xhr as].include?(pair_node.key.to_value)
45-
}.map(&:to_source).join(', ')
43+
argument_node.elements.reject { |element_node| element_node.type == :assoc_node && %i[format xhr as].include?(element_node.key.to_value) }.map(&:to_source).join(', ')
4644
"#{key}: #{add_curly_brackets_if_necessary(new_value)}" if new_value.length > 0
4745
else
4846
"#{key}: #{argument_node.to_source}"
@@ -53,52 +51,77 @@
5351
# =>
5452
# get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }.
5553
within_files Synvert::RAILS_CONTROLLER_TEST_FILES do
56-
with_node node_type: 'send', message: { in: request_methods } do
57-
next unless node.arguments.size > 1
58-
next unless node.arguments[1].type == :hash
59-
next if node.arguments[1].key?(:params)
60-
next if node.arguments[1].kwsplats.any? # we are not able to handle kwsplat here
61-
62-
format_value = node.arguments[1].format_value || node.arguments[1].as_value
63-
xhr_value = node.arguments[1].xhr_value
54+
with_node node_type: 'call_node',
55+
name: { in: request_methods },
56+
arguments: {
57+
node_type: 'arguments_node',
58+
arguments: {
59+
size: { gt: 1 },
60+
'1': { node_type: { in: ['keyword_hash_node', 'hash_node'] }, params_value: nil }
61+
}
62+
} do
63+
# skip if element of hash node is assoc_splat_node
64+
next if node.arguments.arguments[1].elements.any? { |element| element.type != :assoc_node }
65+
66+
format_value = node.arguments.arguments[1].format_value || node.arguments.arguments[1].as_value
67+
xhr_value = node.arguments.arguments[1].xhr_value
6468
options = []
65-
options << make_up_hash_pair('params', node.arguments[1])
66-
options << make_up_hash_pair('session', node.arguments[2]) if node.arguments.size > 2
67-
options << make_up_hash_pair('flash', node.arguments[3]) if node.arguments.size > 3
69+
options << make_up_hash_element('params', node.arguments.arguments[1])
70+
options << make_up_hash_element('session', node.arguments.arguments[2]) if node.arguments.arguments.size > 2
71+
options << make_up_hash_element('flash', node.arguments.arguments[3]) if node.arguments.arguments.size > 3
6872
options << "as: #{format_value.to_source}" if format_value
6973
options << "xhr: #{xhr_value.to_source}" if xhr_value
70-
replace :arguments, with: "{{arguments.first}}, #{options.compact.join(', ')}"
74+
replace :arguments, with: "{{arguments.arguments.0}}, #{options.compact.join(', ')}"
7175
end
7276

73-
with_node node_type: 'send', message: 'xhr' do
74-
if node.arguments.size == 2
75-
replace :message, with: '{{arguments.first.to_string}}'
76-
replace :arguments, with: '{{arguments.1}}, xhr: true'
77-
next
78-
end
79-
format_value = node.arguments[2].type == :hash && node.arguments[2].format_value
77+
with_node node_type: 'call_node', name: 'xhr', arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do
78+
replace :message, with: '{{arguments.arguments.0.to_string}}'
79+
replace :arguments, with: '{{arguments.arguments.1}}, xhr: true'
80+
end
81+
82+
with_node node_type: 'call_node', name: 'xhr', arguments: { node_type: 'arguments_node', arguments: { size: { gt: 2 } } } do
83+
format_value = node.arguments.arguments[2].type == :hash && node.arguments.arguments[2].format_value
8084
options = []
81-
options << make_up_hash_pair('params', node.arguments[2])
82-
options << make_up_hash_pair('session', node.arguments[3]) if node.arguments.size > 3
83-
options << make_up_hash_pair('flash', node.arguments[4]) if node.arguments.size > 4
85+
options << make_up_hash_element('params', node.arguments.arguments[2])
86+
options << make_up_hash_element('session', node.arguments.arguments[3]) if node.arguments.arguments.size > 3
87+
options << make_up_hash_element('flash', node.arguments.arguments[4]) if node.arguments.arguments.size > 4
8488
options << "as: #{format_value.to_source}" if format_value
85-
replace :message, with: '{{arguments.first.to_string}}'
86-
replace :arguments, with: "{{arguments.1}}, #{options.compact.join(', ')}, xhr: true"
89+
replace :message, with: '{{arguments.arguments.0.to_string}}'
90+
replace :arguments, with: "{{arguments.arguments.1}}, #{options.compact.join(', ')}, xhr: true"
8791
end
8892
end
8993

9094
# get '/posts/1', user_id: user.id, { 'HTTP_AUTHORIZATION' => 'fake' }
9195
# =>
9296
# get '/posts/1', params: { user_id: user.id }, headers: { 'HTTP_AUTHORIZATION' => 'fake' }
9397
within_files Synvert::RAILS_INTEGRATION_TEST_FILES do
94-
with_node node_type: 'send', message: { in: request_methods } do
95-
next unless node.arguments.size > 1
96-
next if node.arguments[1].type == :hash && (node.arguments[1].key?(:params) || node.arguments[1].key?(:headers))
97-
98+
with_node node_type: 'call_node',
99+
name: { in: request_methods },
100+
arguments: {
101+
node_type: 'arguments_node',
102+
arguments: {
103+
size: { gt: 1 },
104+
'1': { node_type: { in: ['keyword_hash_node', 'hash_node'] }, params_value: nil, headers_value: nil }
105+
}
106+
} do
98107
options = []
99-
options << make_up_hash_pair('params', node.arguments[1])
100-
options << make_up_hash_pair('headers', node.arguments[2]) if node.arguments.size > 2
101-
replace :arguments, with: "{{arguments.first}}, #{options.compact.join(', ')}"
108+
options << make_up_hash_element('params', node.arguments.arguments[1])
109+
options << make_up_hash_element('headers', node.arguments.arguments[2]) if node.arguments.arguments.size > 2
110+
replace :arguments, with: "{{arguments.arguments.0}}, #{options.compact.join(', ')}"
111+
end
112+
113+
with_node node_type: 'call_node',
114+
name: { in: request_methods },
115+
arguments: {
116+
node_type: 'arguments_node',
117+
arguments: {
118+
size: { gt: 1 },
119+
'1': nil,
120+
'2': { node_type: { in: ['keyword_hash_node', 'hash_node'] } }
121+
}
122+
} do
123+
delete 'arguments.arguments.1', and_comma: true
124+
insert 'headers: ', to: 'arguments.arguments.2', at: 'beginning'
102125
end
103126
end
104127
end

0 commit comments

Comments
 (0)