|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | Synvert::Rewriter.new 'rails', 'convert_model_lambda_scope' do
|
4 |
| - configure(parser: Synvert::PARSER_PARSER) |
| 4 | + configure(parser: Synvert::PRISM_PARSER) |
5 | 5 |
|
6 | 6 | description <<~EOS
|
7 | 7 | It converts activerecord scope to lambda scope.
|
@@ -33,38 +33,80 @@ class Post < ActiveRecord::Base
|
33 | 33 |
|
34 | 34 | within_files Synvert::RAILS_MODEL_FILES do
|
35 | 35 | # 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: ' }' |
43 | 51 | end
|
| 52 | + end |
44 | 53 |
|
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}} }' |
51 | 72 | end
|
| 73 | + end |
52 | 74 |
|
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}} }' |
55 | 93 | end
|
56 | 94 | end
|
57 | 95 |
|
58 | 96 | # 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}} }' |
63 | 102 | end
|
64 | 103 |
|
65 | 104 | # 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}} }' |
68 | 110 | end
|
69 | 111 | end
|
70 | 112 | end
|
0 commit comments