synvert-core 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/synvert/core.rb +1 -1
  4. data/lib/synvert/core/engine/erb.rb +29 -22
  5. data/lib/synvert/core/node_ext.rb +102 -101
  6. data/lib/synvert/core/rewriter.rb +19 -13
  7. data/lib/synvert/core/rewriter/action.rb +3 -5
  8. data/lib/synvert/core/rewriter/action/append_action.rb +1 -1
  9. data/lib/synvert/core/rewriter/action/insert_action.rb +7 -3
  10. data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
  11. data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
  12. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +4 -3
  13. data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -4
  14. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
  15. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -2
  16. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
  17. data/lib/synvert/core/rewriter/gem_spec.rb +2 -1
  18. data/lib/synvert/core/rewriter/helper.rb +3 -5
  19. data/lib/synvert/core/rewriter/instance.rb +20 -17
  20. data/lib/synvert/core/version.rb +1 -1
  21. data/spec/spec_helper.rb +1 -1
  22. data/spec/synvert/core/engine/erb_spec.rb +30 -30
  23. data/spec/synvert/core/node_ext_spec.rb +53 -52
  24. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +8 -8
  25. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +3 -3
  26. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
  27. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +13 -9
  28. data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +17 -9
  29. data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +21 -12
  30. data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +17 -9
  31. data/spec/synvert/core/rewriter/gem_spec_spec.rb +6 -4
  32. data/spec/synvert/core/rewriter/helper_spec.rb +34 -31
  33. data/spec/synvert/core/rewriter/instance_spec.rb +100 -66
  34. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +8 -6
  35. data/spec/synvert/core/rewriter/scope/within_scope.rb +15 -8
  36. data/spec/synvert/core/rewriter_spec.rb +99 -68
  37. metadata +2 -2
@@ -8,10 +8,11 @@ module Synvert::Core
8
8
  rewriter = Rewriter.new('foo', 'bar')
9
9
  Rewriter::Instance.new(rewriter, 'file pattern')
10
10
  }
11
- let(:source) {'''
11
+ let(:source) {
12
+ '
12
13
  Factory.define :user do |user|
13
14
  end
14
- '''
15
+ '
15
16
  }
16
17
  let(:node) { Parser::CurrentRuby.parse(source) }
17
18
  before do
@@ -23,10 +24,11 @@ end
23
24
  it 'call block with child node' do
24
25
  run = false
25
26
  type_in_scope = nil
26
- scope = Rewriter::GotoScope.new instance, :caller do
27
- run = true
28
- type_in_scope = node.type
29
- end
27
+ scope =
28
+ Rewriter::GotoScope.new instance, :caller do
29
+ run = true
30
+ type_in_scope = node.type
31
+ end
30
32
  scope.process
31
33
  expect(run).to be_truthy
32
34
  expect(type_in_scope).to eq :send
@@ -8,7 +8,8 @@ module Synvert::Core
8
8
  rewriter = Rewriter.new('foo', 'bar')
9
9
  Rewriter::Instance.new(rewriter, 'file pattern')
10
10
  }
11
- let(:source) {"
11
+ let(:source) {
12
+ "
12
13
  describe Post do
13
14
  it 'gets post' do
14
15
  FactoryGirl.create :post
@@ -25,9 +26,10 @@ end
25
26
  describe '#process' do
26
27
  it 'not call block if no matching node' do
27
28
  run = false
28
- scope = Rewriter::WithinScope.new instance, type: 'send', message: 'missing' do
29
- run = true
30
- end
29
+ scope =
30
+ Rewriter::WithinScope.new instance, type: 'send', message: 'missing' do
31
+ run = true
32
+ end
31
33
  scope.process
32
34
  expect(run).to be_falsey
33
35
  end
@@ -35,10 +37,15 @@ end
35
37
  it 'call block if there is matching node' do
36
38
  run = false
37
39
  type_in_scope = nil
38
- scope = Rewriter::WithinScope.new instance, type: 'send', receiver: 'FactoryGirl', message: 'create', arguments: [':post'] do
39
- run = true
40
- type_in_scope = node.type
41
- end
40
+ scope =
41
+ Rewriter::WithinScope.new instance,
42
+ type: 'send',
43
+ receiver: 'FactoryGirl',
44
+ message: 'create',
45
+ arguments: [':post'] do
46
+ run = true
47
+ type_in_scope = node.type
48
+ end
42
49
  scope.process
43
50
  expect(run).to be_truthy
44
51
  expect(type_in_scope).to eq :send
@@ -4,27 +4,36 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter do
7
+ it '.execute' do
8
+ run = false
9
+ Rewriter.execute { run = true }
10
+ expect(run).to be_truthy
11
+ end
12
+
7
13
  it 'parses description' do
8
- rewriter = Rewriter.new 'group', 'name' do
9
- description 'rewriter description'
10
- end
14
+ rewriter =
15
+ Rewriter.new 'group', 'name' do
16
+ description 'rewriter description'
17
+ end
11
18
  rewriter.process
12
19
  expect(rewriter.description).to eq 'rewriter description'
13
20
  end
14
21
 
15
22
  it 'parses if_ruby' do
16
- #stub_const("RUBY_VERSION", '2.0.0')
17
- rewriter = Rewriter.new 'group', 'name' do
18
- if_ruby '2.0.0'
19
- end
23
+ expect(Rewriter::RubyVersion).to receive(:new).with('2.0.0')
24
+ rewriter =
25
+ Rewriter.new 'group', 'name' do
26
+ if_ruby '2.0.0'
27
+ end
20
28
  rewriter.process
21
29
  end
22
30
 
23
31
  it 'parses if_gem' do
24
- expect(Rewriter::GemSpec).to receive(:new).with('synvert', {gte: '1.0.0'})
25
- rewriter = Rewriter.new 'group', 'name' do
26
- if_gem 'synvert', {gte: '1.0.0'}
27
- end
32
+ expect(Rewriter::GemSpec).to receive(:new).with('synvert', { gte: '1.0.0' })
33
+ rewriter =
34
+ Rewriter.new 'group', 'name' do
35
+ if_gem 'synvert', { gte: '1.0.0' }
36
+ end
28
37
  rewriter.process
29
38
  end
30
39
 
@@ -32,76 +41,90 @@ module Synvert::Core
32
41
  it 'does nothing if if_ruby does not match' do
33
42
  stub_const('RUBY_VERSION', '2.0.0')
34
43
  expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
35
- rewriter = Rewriter.new 'group', 'name' do
36
- if_ruby '2.2.3'
37
- within_file 'config/routes.rb' do; end
38
- end
44
+ rewriter =
45
+ Rewriter.new 'group', 'name' do
46
+ if_ruby '2.2.3'
47
+ within_file 'config/routes.rb' do
48
+ end
49
+ end
39
50
  rewriter.process
40
51
  end
41
52
 
42
53
  it 'delegates process to instances if if_ruby matches' do
43
54
  stub_const('RUBY_VERSION', '2.0.0')
44
55
  expect_any_instance_of(Rewriter::Instance).to receive(:process)
45
- rewriter = Rewriter.new 'group', 'name' do
46
- if_ruby '1.9.3'
47
- within_file 'config/routes.rb' do; end
48
- end
56
+ rewriter =
57
+ Rewriter.new 'group', 'name' do
58
+ if_ruby '1.9.3'
59
+ within_file 'config/routes.rb' do
60
+ end
61
+ end
49
62
  rewriter.process
50
63
  end
51
64
 
52
65
  it 'does nothing if if_gem does not match' do
53
66
  expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(false)
54
67
  expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
55
- rewriter = Rewriter.new 'group', 'name' do
56
- if_gem 'synvert', '1.0.0'
57
- within_file 'config/routes.rb' do; end
58
- end
68
+ rewriter =
69
+ Rewriter.new 'group', 'name' do
70
+ if_gem 'synvert', '1.0.0'
71
+ within_file 'config/routes.rb' do
72
+ end
73
+ end
59
74
  rewriter.process
60
75
  end
61
76
 
62
77
  it 'delegates process to instances if if_gem matches' do
63
78
  expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(true)
64
79
  expect_any_instance_of(Rewriter::Instance).to receive(:process)
65
- rewriter = Rewriter.new 'group', 'name' do
66
- if_gem 'synvert', '1.0.0'
67
- within_file 'config/routes.rb' do; end
68
- end
80
+ rewriter =
81
+ Rewriter.new 'group', 'name' do
82
+ if_gem 'synvert', '1.0.0'
83
+ within_file 'config/routes.rb' do
84
+ end
85
+ end
69
86
  rewriter.process
70
87
  end
71
88
 
72
89
  it 'delegates process to instances if if_ruby and if_gem do not exist' do
73
90
  expect_any_instance_of(Rewriter::Instance).to receive(:process)
74
- rewriter = Rewriter.new 'group', 'name' do
75
- within_file 'config/routes.rb' do; end
76
- end
91
+ rewriter =
92
+ Rewriter.new 'group', 'name' do
93
+ within_file 'config/routes.rb' do
94
+ end
95
+ end
77
96
  rewriter.process
78
97
  end
79
98
 
80
99
  it 'does nothing in sandbox mode' do
81
100
  expect_any_instance_of(Rewriter::GemSpec).not_to receive(:match?)
82
101
  expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
83
- rewriter = Rewriter.new 'group', 'name' do
84
- if_gem 'synvert', '1.0.0'
85
- within_file 'config/routes.rb' do; end
86
- end
102
+ rewriter =
103
+ Rewriter.new 'group', 'name' do
104
+ if_gem 'synvert', '1.0.0'
105
+ within_file 'config/routes.rb' do
106
+ end
107
+ end
87
108
  rewriter.process_with_sandbox
88
109
  end
89
110
  end
90
111
 
91
112
  describe 'parses add_file' do
92
113
  it 'creates a new file' do
93
- rewriter = Rewriter.new 'group', 'rewriter2' do
94
- add_file 'foo.bar', 'FooBar'
95
- end
114
+ rewriter =
115
+ Rewriter.new 'group', 'rewriter2' do
116
+ add_file 'foo.bar', 'FooBar'
117
+ end
96
118
  rewriter.process
97
119
  expect(File.read('./foo.bar')).to eq 'FooBar'
98
120
  FileUtils.rm './foo.bar'
99
121
  end
100
122
 
101
123
  it 'does nothing in sandbox mode' do
102
- rewriter = Rewriter.new 'group', 'rewriter2' do
103
- add_file 'foo.bar', 'FooBar'
104
- end
124
+ rewriter =
125
+ Rewriter.new 'group', 'rewriter2' do
126
+ add_file 'foo.bar', 'FooBar'
127
+ end
105
128
  rewriter.process_with_sandbox
106
129
  expect(File.exist?('./foo.bar')).to be_falsey
107
130
  end
@@ -110,26 +133,29 @@ module Synvert::Core
110
133
  describe 'parses remove_file' do
111
134
  it 'removes a file' do
112
135
  FileUtils.touch './foo.bar'
113
- rewriter = Rewriter.new 'group', 'rewriter2' do
114
- remove_file 'foo.bar'
115
- end
136
+ rewriter =
137
+ Rewriter.new 'group', 'rewriter2' do
138
+ remove_file 'foo.bar'
139
+ end
116
140
  rewriter.process
117
141
  expect(File.exist?('./foo.bar')).to be_falsey
118
142
  end
119
143
 
120
144
  it 'does nothing if file not exist' do
121
- rewriter = Rewriter.new 'group', 'rewriter2' do
122
- remove_file 'foo.bar'
123
- end
145
+ rewriter =
146
+ Rewriter.new 'group', 'rewriter2' do
147
+ remove_file 'foo.bar'
148
+ end
124
149
  rewriter.process
125
150
  expect(File.exist?('./foo.bar')).to be_falsey
126
151
  end
127
152
 
128
153
  it 'does nothing in sandbox mode' do
129
154
  FileUtils.touch './foo.bar'
130
- rewriter = Rewriter.new 'group', 'rewriter2' do
131
- add_file 'foo.bar', 'FooBar'
132
- end
155
+ rewriter =
156
+ Rewriter.new 'group', 'rewriter2' do
157
+ add_file 'foo.bar', 'FooBar'
158
+ end
133
159
  rewriter.process_with_sandbox
134
160
  expect(File.exist?('./foo.bar')).to be_truthy
135
161
  FileUtils.rm './foo.bar'
@@ -139,46 +165,51 @@ module Synvert::Core
139
165
  describe 'parses add_snippet' do
140
166
  it 'processes the rewritter' do
141
167
  rewriter1 = Rewriter.new 'group', 'rewriter1'
142
- rewriter2 = Rewriter.new 'group', 'rewriter2' do
143
- add_snippet :group, :rewriter1
144
- end
168
+ rewriter2 =
169
+ Rewriter.new 'group', 'rewriter2' do
170
+ add_snippet :group, :rewriter1
171
+ end
145
172
  expect(rewriter1).to receive(:process)
146
173
  rewriter2.process
147
174
  end
148
175
 
149
176
  it 'adds sub_snippets' do
150
177
  rewriter1 = Rewriter.new 'group', 'rewriter1'
151
- rewriter2 = Rewriter.new 'group', 'rewriter2' do
152
- add_snippet :group, :rewriter1
153
- end
178
+ rewriter2 =
179
+ Rewriter.new 'group', 'rewriter2' do
180
+ add_snippet :group, :rewriter1
181
+ end
154
182
  expect(rewriter1).to receive(:process)
155
183
  rewriter2.process
156
184
  expect(rewriter2.sub_snippets).to eq [rewriter1]
157
185
  end
158
186
 
159
187
  it 'raises RewriterNotFound' do
160
- rewriter = Rewriter.new 'group', 'name' do
161
- add_snippet :group, :not_exist
162
- end
188
+ rewriter =
189
+ Rewriter.new 'group', 'name' do
190
+ add_snippet :group, :not_exist
191
+ end
163
192
  expect { rewriter.process }.to raise_error(RewriterNotFound)
164
193
  end
165
194
  end
166
195
 
167
196
  it 'parses helper_method' do
168
- rewriter = Rewriter.new 'group', 'name' do
169
- helper_method 'dynamic_helper' do |arg1, arg2|
170
- 'dynamic result'
197
+ rewriter =
198
+ Rewriter.new 'group', 'name' do
199
+ helper_method 'dynamic_helper' do |_arg1, _arg2|
200
+ 'dynamic result'
201
+ end
171
202
  end
172
- end
173
203
  rewriter.process
174
204
  instance = Rewriter::Instance.new(rewriter, '*.rb')
175
205
  expect(instance.dynamic_helper('arg1', 'arg2')).to eq 'dynamic result'
176
206
  end
177
207
 
178
208
  it 'parses todo' do
179
- rewriter = Rewriter.new 'group', 'name' do
180
- todo "this rewriter doesn't do blah blah blah"
181
- end
209
+ rewriter =
210
+ Rewriter.new 'group', 'name' do
211
+ todo "this rewriter doesn't do blah blah blah"
212
+ end
182
213
  rewriter.process
183
214
  expect(rewriter.todo).to eq "this rewriter doesn't do blah blah blah"
184
215
  end
@@ -203,7 +234,7 @@ module Synvert::Core
203
234
  expect { Rewriter.call 'group', 'rewriter' }.to raise_error(RewriterNotFound)
204
235
  end
205
236
 
206
- context "exist?" do
237
+ context 'exist?' do
207
238
  it 'returns true if rewriter exists' do
208
239
  Rewriter.new 'group', 'rewriter'
209
240
  expect(Rewriter.exist?('group', 'rewriter')).to be_truthy
@@ -214,7 +245,7 @@ module Synvert::Core
214
245
  end
215
246
  end
216
247
 
217
- context "available" do
248
+ context 'available' do
218
249
  it 'lists empty rewriters' do
219
250
  expect(Rewriter.availables).to eq({})
220
251
  end
@@ -222,7 +253,7 @@ module Synvert::Core
222
253
  it 'registers and lists all available rewriters' do
223
254
  rewriter1 = Rewriter.new 'group', 'rewriter1'
224
255
  rewriter2 = Rewriter.new 'group', 'rewriter2'
225
- expect(Rewriter.availables).to eq({'group' => {'rewriter1' => rewriter1, 'rewriter2' => rewriter2}})
256
+ expect(Rewriter.availables).to eq({ 'group' => { 'rewriter1' => rewriter1, 'rewriter2' => rewriter2 } })
226
257
  end
227
258
  end
228
259
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-29 00:00:00.000000000 Z
11
+ date: 2021-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser