synvert-core 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.17.0"
5
+ VERSION = '0.18.0'
6
6
  end
7
7
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require 'synvert/core'
6
6
 
@@ -4,38 +4,38 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Engine::ERB do
7
- it "encodes / decodes" do
8
- source =<<-EOF
9
- <%content_for :head do%>
10
- <style>
11
- body {
12
- background-image: url(<%= asset_path('bg.png') %>);
13
- }
14
- </style>
15
- <%end%>
16
-
17
- <%
18
- foo = 'bar'
19
- post = Post.find(:first)
20
- bar = 'foo'
21
- %>
22
-
23
- <% if User.current &&
24
- User.current.admin %>
25
- <%= rounded_content("page") do %>
26
- <div class='test'>
27
- <% if post %>
28
- <div id="title"><%= foo %></div>
29
- <% form_for post do |f| %>
30
- <label><%= link_to_function 'test', "confirm('test');" %></label>
31
- <%= f.text_field 'bar' %>
32
- <% end %>
33
- <% end %></div>
34
- <% end %>
35
- <% end %>
7
+ it 'encodes / decodes' do
8
+ source = <<~EOF
9
+ <%content_for :head do%>
10
+ <style>
11
+ body {
12
+ background-image: url(<%= asset_path('bg.png') %>);
13
+ }
14
+ </style>
15
+ <%end%>
16
+
17
+ <%
18
+ foo = 'bar'
19
+ post = Post.find(:first)
20
+ bar = 'foo'
21
+ %>
22
+
23
+ <% if User.current &&
24
+ User.current.admin %>
25
+ <%= rounded_content("page") do %>
26
+ <div class='test'>
27
+ <% if post %>
28
+ <div id="title"><%= foo %></div>
29
+ <% form_for post do |f| %>
30
+ <label><%= link_to_function 'test', "confirm('test');" %></label>
31
+ <%= f.text_field 'bar' %>
32
+ <% end %>
33
+ <% end %></div>
34
+ <% end %>
35
+ <% end %>
36
36
  EOF
37
37
  encoded_source = Engine::ERB.encode(source)
38
- buffer = Parser::Source::Buffer.new "(test)"
38
+ buffer = Parser::Source::Buffer.new '(test)'
39
39
  buffer.source = encoded_source
40
40
  parser = Parser::CurrentRuby.new
41
41
  parser.reset
@@ -103,12 +103,12 @@ describe Parser::AST::Node do
103
103
 
104
104
  describe '#arguments' do
105
105
  it 'gets for def node' do
106
- node = parse("def test(foo, bar); foo + bar; end")
106
+ node = parse('def test(foo, bar); foo + bar; end')
107
107
  expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
108
108
  end
109
109
 
110
110
  it 'gets for defs node' do
111
- node = parse("def self.test(foo, bar); foo + bar; end")
111
+ node = parse('def self.test(foo, bar); foo + bar; end')
112
112
  expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
113
113
  end
114
114
 
@@ -172,129 +172,129 @@ describe Parser::AST::Node do
172
172
  end
173
173
  end
174
174
 
175
- describe "#keys" do
175
+ describe '#keys' do
176
176
  it 'gets for hash node' do
177
177
  node = parse("{:foo => :bar, 'foo' => 'bar'}")
178
178
  expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
179
179
  end
180
180
  end
181
181
 
182
- describe "#values" do
182
+ describe '#values' do
183
183
  it 'gets for hash node' do
184
184
  node = parse("{:foo => :bar, 'foo' => 'bar'}")
185
185
  expect(node.values).to eq [parse(':bar'), parse("'bar'")]
186
186
  end
187
187
  end
188
188
 
189
- describe "#has_key?" do
190
- it "gets true if key exists" do
191
- node = parse("{:foo => :bar}")
189
+ describe '#has_key?' do
190
+ it 'gets true if key exists' do
191
+ node = parse('{:foo => :bar}')
192
192
  expect(node.has_key?(:foo)).to be_truthy
193
193
  end
194
194
 
195
- it "gets false if key does not exist" do
196
- node = parse("{:foo => :bar}")
195
+ it 'gets false if key does not exist' do
196
+ node = parse('{:foo => :bar}')
197
197
  expect(node.has_key?('foo')).to be_falsey
198
198
  end
199
199
  end
200
200
 
201
- describe "#hash_value" do
202
- it "gets value of specified key" do
203
- node = parse("{:foo => :bar}")
201
+ describe '#hash_value' do
202
+ it 'gets value of specified key' do
203
+ node = parse('{:foo => :bar}')
204
204
  expect(node.hash_value(:foo)).to eq parse(':bar')
205
205
  end
206
206
 
207
- it "gets nil if key does not exist" do
208
- node = parse("{:foo => :bar}")
207
+ it 'gets nil if key does not exist' do
208
+ node = parse('{:foo => :bar}')
209
209
  expect(node.hash_value(:bar)).to be_nil
210
210
  end
211
211
  end
212
212
 
213
- describe "#key" do
213
+ describe '#key' do
214
214
  it 'gets for pair node' do
215
215
  node = parse("{:foo => 'bar'}").children[0]
216
216
  expect(node.key).to eq parse(':foo')
217
217
  end
218
218
  end
219
219
 
220
- describe "#value" do
220
+ describe '#value' do
221
221
  it 'gets for hash node' do
222
222
  node = parse("{:foo => 'bar'}").children[0]
223
223
  expect(node.value).to eq parse("'bar'")
224
224
  end
225
225
  end
226
226
 
227
- describe "#condition" do
227
+ describe '#condition' do
228
228
  it 'gets for if node' do
229
229
  node = parse('if defined?(Bundler); end')
230
230
  expect(node.condition).to eq parse('defined?(Bundler)')
231
231
  end
232
232
  end
233
233
 
234
- describe "#left_value" do
234
+ describe '#left_value' do
235
235
  it 'gets for masgn' do
236
- node = parse("a, b = 1, 2")
236
+ node = parse('a, b = 1, 2')
237
237
  expect(node.left_value.to_source).to eq 'a, b'
238
238
  end
239
239
 
240
240
  it 'gets for lvasgn' do
241
- node = parse("a = 1")
241
+ node = parse('a = 1')
242
242
  expect(node.left_value).to eq :a
243
243
  end
244
244
 
245
245
  it 'gets for ivasgn' do
246
- node = parse("@a = 1")
246
+ node = parse('@a = 1')
247
247
  expect(node.left_value).to eq :@a
248
248
  end
249
249
  end
250
250
 
251
- describe "#right_value" do
251
+ describe '#right_value' do
252
252
  it 'gets for masgn' do
253
- node = parse("a, b = 1, 2")
253
+ node = parse('a, b = 1, 2')
254
254
  expect(node.right_value).to eq parse('[1, 2]')
255
255
  end
256
256
 
257
257
  it 'gets for masgn' do
258
- node = parse("a, b = params")
259
- expect(node.right_value).to eq parse("params")
258
+ node = parse('a, b = params')
259
+ expect(node.right_value).to eq parse('params')
260
260
  end
261
261
 
262
262
  it 'gets for lvasgn' do
263
- node = parse("a = 1")
264
- expect(node.right_value).to eq parse("1")
263
+ node = parse('a = 1')
264
+ expect(node.right_value).to eq parse('1')
265
265
  end
266
266
 
267
267
  it 'gets for ivasgn' do
268
- node = parse("@a = 1")
269
- expect(node.right_value).to eq parse("1")
268
+ node = parse('@a = 1')
269
+ expect(node.right_value).to eq parse('1')
270
270
  end
271
271
  end
272
272
 
273
- describe "#to_value" do
273
+ describe '#to_value' do
274
274
  it 'gets for int' do
275
- node = parse("1")
275
+ node = parse('1')
276
276
  expect(node.to_value).to eq 1
277
277
  end
278
278
 
279
279
  it 'gets for string' do
280
280
  node = parse("'str'")
281
- expect(node.to_value).to eq "str"
281
+ expect(node.to_value).to eq 'str'
282
282
  end
283
283
 
284
284
  it 'gets for symbol' do
285
- node = parse(":str")
285
+ node = parse(':str')
286
286
  expect(node.to_value).to eq :str
287
287
  end
288
288
 
289
289
  it 'get for boolean' do
290
- node = parse("true")
290
+ node = parse('true')
291
291
  expect(node.to_value).to be_truthy
292
- node = parse("false")
292
+ node = parse('false')
293
293
  expect(node.to_value).to be_falsey
294
294
  end
295
295
 
296
296
  it 'get for range' do
297
- node = parse("(1..10)")
297
+ node = parse('(1..10)')
298
298
  expect(node.to_value).to eq (1..10)
299
299
  end
300
300
 
@@ -384,13 +384,13 @@ describe Parser::AST::Node do
384
384
  it 'matches arguments any' do
385
385
  source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
386
386
  node = parse(source)
387
- expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
387
+ expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
388
388
  end
389
389
 
390
390
  it 'matches not' do
391
391
  source = 'class Synvert; end'
392
392
  node = parse(source)
393
- expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
393
+ expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
394
394
  end
395
395
  end
396
396
 
@@ -415,24 +415,25 @@ describe Parser::AST::Node do
415
415
  it 'rewrites for ArgumentsNode' do
416
416
  source = 'test { |a, b| }'
417
417
  node = parse(source)
418
- expect(node.rewritten_source('{{arguments}}')).to eq %(a, b)
418
+ expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
419
419
  end
420
420
 
421
- it 'rewrites array with multi line given as argument for method'do
422
- source = <<-EOS.strip
423
- long_name_method([
424
- 1,
425
- 2,
426
- 3
427
- ])
421
+ it 'rewrites array with multi line given as argument for method' do
422
+ source = <<~EOS.strip
423
+ long_name_method([
424
+ 1,
425
+ 2,
426
+ 3
427
+ ])
428
428
  EOS
429
+
429
430
  node = parse(source)
430
- expect(node.rewritten_source('{{arguments}}')).to eq <<-EOS.strip
431
- [
432
- 1,
433
- 2,
434
- 3
435
- ]
431
+ expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
432
+ [
433
+ 1,
434
+ 2,
435
+ 3
436
+ ]
436
437
  EOS
437
438
  end
438
439
  end
@@ -13,11 +13,11 @@ module Synvert::Core
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
16
- expect(subject.begin_pos).to eq "Synvert::Application.configure do".length
16
+ expect(subject.begin_pos).to eq 'Synvert::Application.configure do'.length
17
17
  end
18
18
 
19
19
  it 'gets end_pos' do
20
- expect(subject.end_pos).to eq "Synvert::Application.configure do".length
20
+ expect(subject.end_pos).to eq 'Synvert::Application.configure do'.length
21
21
  end
22
22
 
23
23
  it 'gets rewritten_code' do
@@ -34,11 +34,11 @@ module Synvert::Core
34
34
  }
35
35
 
36
36
  it 'gets begin_pos' do
37
- expect(subject.begin_pos).to eq "RSpec.configure do |config|".length
37
+ expect(subject.begin_pos).to eq 'RSpec.configure do |config|'.length
38
38
  end
39
39
 
40
40
  it 'gets end_pos' do
41
- expect(subject.end_pos).to eq "RSpec.configure do |config|".length
41
+ expect(subject.end_pos).to eq 'RSpec.configure do |config|'.length
42
42
  end
43
43
 
44
44
  it 'gets rewritten_code' do
@@ -55,11 +55,11 @@ module Synvert::Core
55
55
  }
56
56
 
57
57
  it 'gets begin_pos' do
58
- expect(subject.begin_pos).to eq "class User".length
58
+ expect(subject.begin_pos).to eq 'class User'.length
59
59
  end
60
60
 
61
61
  it 'gets end_pos' do
62
- expect(subject.end_pos).to eq "class User".length
62
+ expect(subject.end_pos).to eq 'class User'.length
63
63
  end
64
64
 
65
65
  it 'gets rewritten_code' do
@@ -76,11 +76,11 @@ module Synvert::Core
76
76
  }
77
77
 
78
78
  it 'gets begin_pos' do
79
- expect(subject.begin_pos).to eq "class User < ActionRecord::Base".length
79
+ expect(subject.begin_pos).to eq 'class User < ActionRecord::Base'.length
80
80
  end
81
81
 
82
82
  it 'gets end_pos' do
83
- expect(subject.end_pos).to eq "class User < ActionRecord::Base".length
83
+ expect(subject.end_pos).to eq 'class User < ActionRecord::Base'.length
84
84
  end
85
85
 
86
86
  it 'gets rewritten_code' do
@@ -5,18 +5,18 @@ require 'spec_helper'
5
5
  module Synvert::Core
6
6
  describe Rewriter::InsertAfterAction do
7
7
  subject {
8
- source = " include Foo"
8
+ source = ' include Foo'
9
9
  node = Parser::CurrentRuby.parse(source)
10
10
  instance = double(current_node: node)
11
11
  Rewriter::InsertAfterAction.new(instance, 'include Bar')
12
12
  }
13
13
 
14
14
  it 'gets begin_pos' do
15
- expect(subject.begin_pos).to eq " include Foo".length
15
+ expect(subject.begin_pos).to eq ' include Foo'.length
16
16
  end
17
17
 
18
18
  it 'gets end_pos' do
19
- expect(subject.end_pos).to eq " include Foo".length
19
+ expect(subject.end_pos).to eq ' include Foo'.length
20
20
  end
21
21
 
22
22
  it 'gets rewritten_code' do
@@ -20,7 +20,7 @@ module Synvert::Core
20
20
  end
21
21
 
22
22
  it 'gets rewritten_code' do
23
- expect(subject.rewritten_code).to eq ""
23
+ expect(subject.rewritten_code).to eq ''
24
24
  end
25
25
  end
26
26
  end
@@ -4,20 +4,20 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::ReplaceWithAction do
7
- context "replace with single line" do
7
+ context 'replace with single line' do
8
8
  subject {
9
- source = "post = FactoryGirl.create_list :post, 2"
9
+ source = 'post = FactoryGirl.create_list :post, 2'
10
10
  send_node = Parser::CurrentRuby.parse(source).children[1]
11
11
  instance = double(current_node: send_node)
12
12
  Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
13
13
  }
14
14
 
15
15
  it 'gets begin_pos' do
16
- expect(subject.begin_pos).to eq "post = ".length
16
+ expect(subject.begin_pos).to eq 'post = '.length
17
17
  end
18
18
 
19
19
  it 'gets end_pos' do
20
- expect(subject.end_pos).to eq "post = FactoryGirl.create_list :post, 2".length
20
+ expect(subject.end_pos).to eq 'post = FactoryGirl.create_list :post, 2'.length
21
21
  end
22
22
 
23
23
  it 'gets rewritten_code' do
@@ -25,15 +25,19 @@ module Synvert::Core
25
25
  end
26
26
  end
27
27
 
28
- context "#replace with multiple line" do
28
+ context '#replace with multiple line' do
29
29
  subject {
30
- source = " its(:size) { should == 1 }"
30
+ source = ' its(:size) { should == 1 }'
31
31
  send_node = Parser::CurrentRuby.parse(source)
32
32
  instance = double(current_node: send_node)
33
- Rewriter::ReplaceWithAction.new(instance, "describe '#size' do
33
+ Rewriter::ReplaceWithAction.new(
34
+ instance,
35
+ "describe '#size' do
34
36
  subject { super().size }
35
37
  it { {{body}} }
36
- end", autoindent: false)
38
+ end",
39
+ autoindent: false
40
+ )
37
41
  }
38
42
 
39
43
  it 'gets begin_pos' do
@@ -41,7 +45,7 @@ end", autoindent: false)
41
45
  end
42
46
 
43
47
  it 'gets end_pos' do
44
- expect(subject.end_pos).to eq " its(:size) { should == 1 }".length
48
+ expect(subject.end_pos).to eq ' its(:size) { should == 1 }'.length
45
49
  end
46
50
 
47
51
  it 'gets rewritten_code' do