|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | 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) |
5 | 5 |
|
6 | 6 | description <<~EOS
|
7 | 7 | It converts rails test request methods from 4.2 to 5.0
|
|
35 | 35 |
|
36 | 36 | request_methods = %i[get post put patch delete]
|
37 | 37 |
|
38 |
| - helper_method :make_up_hash_pair do |key, argument_node| |
| 38 | + helper_method :make_up_hash_element do |key, argument_node| |
39 | 39 | next if argument_node.to_source == 'nil'
|
40 | 40 |
|
41 |
| - if argument_node.type == :hash |
| 41 | + if argument_node.type == :keyword_hash_node || argument_node.type == :hash_node |
42 | 42 | 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(', ') |
46 | 44 | "#{key}: #{add_curly_brackets_if_necessary(new_value)}" if new_value.length > 0
|
47 | 45 | else
|
48 | 46 | "#{key}: #{argument_node.to_source}"
|
|
53 | 51 | # =>
|
54 | 52 | # get :show, params: { id: user.id }, flash: { notice: 'Welcome' }, session: { admin: user.admin? }.
|
55 | 53 | 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 |
64 | 68 | 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 |
68 | 72 | options << "as: #{format_value.to_source}" if format_value
|
69 | 73 | 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(', ')}" |
71 | 75 | end
|
72 | 76 |
|
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 |
80 | 84 | 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 |
84 | 88 | 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" |
87 | 91 | end
|
88 | 92 | end
|
89 | 93 |
|
90 | 94 | # get '/posts/1', user_id: user.id, { 'HTTP_AUTHORIZATION' => 'fake' }
|
91 | 95 | # =>
|
92 | 96 | # get '/posts/1', params: { user_id: user.id }, headers: { 'HTTP_AUTHORIZATION' => 'fake' }
|
93 | 97 | 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 |
98 | 107 | 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' |
102 | 125 | end
|
103 | 126 | end
|
104 | 127 | end
|
0 commit comments