synvert-core 0.15.2 → 0.19.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -2
  3. data/Gemfile +2 -0
  4. data/Guardfile +2 -0
  5. data/Rakefile +2 -0
  6. data/lib/synvert/core.rb +2 -3
  7. data/lib/synvert/core/configuration.rb +10 -17
  8. data/lib/synvert/core/engine.rb +1 -1
  9. data/lib/synvert/core/engine/erb.rb +31 -23
  10. data/lib/synvert/core/exceptions.rb +5 -3
  11. data/lib/synvert/core/node_ext.rb +107 -101
  12. data/lib/synvert/core/rewriter.rb +22 -16
  13. data/lib/synvert/core/rewriter/action.rb +4 -6
  14. data/lib/synvert/core/rewriter/action/append_action.rb +3 -3
  15. data/lib/synvert/core/rewriter/action/insert_action.rb +9 -5
  16. data/lib/synvert/core/rewriter/action/insert_after_action.rb +2 -2
  17. data/lib/synvert/core/rewriter/action/remove_action.rb +2 -2
  18. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +5 -4
  19. data/lib/synvert/core/rewriter/action/replace_with_action.rb +7 -5
  20. data/lib/synvert/core/rewriter/condition.rb +1 -1
  21. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +2 -2
  22. data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +2 -3
  23. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +2 -2
  24. data/lib/synvert/core/rewriter/gem_spec.rb +11 -11
  25. data/lib/synvert/core/rewriter/helper.rb +4 -6
  26. data/lib/synvert/core/rewriter/instance.rb +37 -23
  27. data/lib/synvert/core/rewriter/ruby_version.rb +1 -1
  28. data/lib/synvert/core/rewriter/scope.rb +1 -1
  29. data/lib/synvert/core/rewriter/scope/goto_scope.rb +2 -1
  30. data/lib/synvert/core/rewriter/scope/within_scope.rb +23 -7
  31. data/lib/synvert/core/rewriter/warning.rb +1 -1
  32. data/lib/synvert/core/version.rb +2 -2
  33. data/spec/spec_helper.rb +3 -6
  34. data/spec/support/parser_helper.rb +2 -0
  35. data/spec/synvert/core/engine/erb_spec.rb +32 -30
  36. data/spec/synvert/core/node_ext_spec.rb +57 -54
  37. data/spec/synvert/core/rewriter/action/append_action_spec.rb +2 -0
  38. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +10 -8
  39. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +5 -3
  40. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +3 -1
  41. data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +2 -0
  42. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +17 -11
  43. data/spec/synvert/core/rewriter/action_spec.rb +2 -0
  44. data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +19 -9
  45. data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +23 -12
  46. data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +19 -9
  47. data/spec/synvert/core/rewriter/condition_spec.rb +2 -0
  48. data/spec/synvert/core/rewriter/gem_spec_spec.rb +13 -10
  49. data/spec/synvert/core/rewriter/helper_spec.rb +36 -31
  50. data/spec/synvert/core/rewriter/instance_spec.rb +118 -66
  51. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +10 -6
  52. data/spec/synvert/core/rewriter/scope/within_scope.rb +18 -9
  53. data/spec/synvert/core/rewriter/scope_spec.rb +2 -0
  54. data/spec/synvert/core/rewriter/warning_spec.rb +2 -0
  55. data/spec/synvert/core/rewriter_spec.rb +106 -73
  56. metadata +2 -4
  57. data/spec/synvert/core/configuration_spec.rb +0 -10
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # GemSpec checks and compares gem version.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Scope finds out nodes which match rules.
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Go to and change its scope to a child node.
@@ -18,6 +18,7 @@ module Synvert::Core
18
18
  def process
19
19
  current_node = @instance.current_node
20
20
  return unless current_node
21
+
21
22
  child_node = current_node.send @child_node_name
22
23
  @instance.process_with_other_node child_node do
23
24
  @instance.instance_eval &@block
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # WithinScope finds out nodes which match rules, then change its scope to matching node.
@@ -7,10 +7,12 @@ module Synvert::Core
7
7
  #
8
8
  # @param instance [Synvert::Core::Rewriter::Instance]
9
9
  # @param rules [Hash]
10
+ # @param options [Hash]
10
11
  # @param block [Block]
11
- def initialize(instance, rules, &block)
12
+ def initialize(instance, rules, options = { recursive: true }, &block)
12
13
  @instance = instance
13
14
  @rules = rules
15
+ @options = options
14
16
  @block = block
15
17
  end
16
18
 
@@ -19,12 +21,9 @@ module Synvert::Core
19
21
  def process
20
22
  current_node = @instance.current_node
21
23
  return unless current_node
24
+
25
+ matching_nodes = find_matching_nodes(current_node)
22
26
  @instance.process_with_node current_node do
23
- matching_nodes = []
24
- matching_nodes << current_node if current_node.match? @rules
25
- current_node.recursive_children do |child_node|
26
- matching_nodes << child_node if child_node.match? @rules
27
- end
28
27
  matching_nodes.each do |matching_node|
29
28
  @instance.process_with_node matching_node do
30
29
  @instance.instance_eval &@block
@@ -32,5 +31,22 @@ module Synvert::Core
32
31
  end
33
32
  end
34
33
  end
34
+
35
+ private
36
+
37
+ def find_matching_nodes(current_node)
38
+ matching_nodes = []
39
+ if @options[:recursive]
40
+ matching_nodes << current_node if current_node.match? @rules
41
+ current_node.recursive_children do |child_node|
42
+ matching_nodes << child_node if child_node.match? @rules
43
+ end
44
+ else
45
+ current_node.each do |child_node|
46
+ matching_nodes << child_node if child_node.match? @rules
47
+ end
48
+ end
49
+ matching_nodes
50
+ end
35
51
  end
36
52
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert::Core
4
4
  # Warning is used to save warning message.
@@ -1,7 +1,7 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.15.2"
5
+ VERSION = '0.19.0'
6
6
  end
7
7
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
 
3
5
  require 'synvert/core'
4
6
 
@@ -16,9 +18,4 @@ RSpec.configure do |config|
16
18
  config.filter_run :focus
17
19
 
18
20
  config.order = 'random'
19
-
20
- config.before do
21
- Synvert::Core::Configuration.instance.set :path, '.'
22
- Synvert::Core::Configuration.instance.set :skip_files, []
23
- end
24
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParserHelper
2
4
  def parse(code)
3
5
  Parser::CurrentRuby.parse code
@@ -1,39 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core
4
6
  describe Engine::ERB do
5
- it "encodes / decodes" do
6
- source =<<-EOF
7
- <%content_for :head do%>
8
- <style>
9
- body {
10
- background-image: url(<%= asset_path('bg.png') %>);
11
- }
12
- </style>
13
- <%end%>
14
-
15
- <%
16
- foo = 'bar'
17
- post = Post.find(:first)
18
- bar = 'foo'
19
- %>
20
-
21
- <% if User.current &&
22
- User.current.admin %>
23
- <%= rounded_content("page") do %>
24
- <div class='test'>
25
- <% if post %>
26
- <div id="title"><%= foo %></div>
27
- <% form_for post do |f| %>
28
- <label><%= link_to_function 'test', "confirm('test');" %></label>
29
- <%= f.text_field 'bar' %>
30
- <% end %>
31
- <% end %></div>
32
- <% end %>
33
- <% 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 %>
34
36
  EOF
35
37
  encoded_source = Engine::ERB.encode(source)
36
- buffer = Parser::Source::Buffer.new "(test)"
38
+ buffer = Parser::Source::Buffer.new '(test)'
37
39
  buffer.source = encoded_source
38
40
  parser = Parser::CurrentRuby.new
39
41
  parser.reset
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Parser::AST::Node do
@@ -101,13 +103,13 @@ describe Parser::AST::Node do
101
103
 
102
104
  describe '#arguments' do
103
105
  it 'gets for def node' do
104
- node = parse("def test(foo, bar); foo + bar; end")
105
- expect(node.arguments.map { |argument| argument.to_source }).to eq ['foo', 'bar']
106
+ node = parse('def test(foo, bar); foo + bar; end')
107
+ expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
106
108
  end
107
109
 
108
110
  it 'gets for defs node' do
109
- node = parse("def self.test(foo, bar); foo + bar; end")
110
- expect(node.arguments.map { |argument| argument.to_source }).to eq ['foo', 'bar']
111
+ node = parse('def self.test(foo, bar); foo + bar; end')
112
+ expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
111
113
  end
112
114
 
113
115
  it 'gets for block node' do
@@ -170,129 +172,129 @@ describe Parser::AST::Node do
170
172
  end
171
173
  end
172
174
 
173
- describe "#keys" do
175
+ describe '#keys' do
174
176
  it 'gets for hash node' do
175
177
  node = parse("{:foo => :bar, 'foo' => 'bar'}")
176
178
  expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
177
179
  end
178
180
  end
179
181
 
180
- describe "#values" do
182
+ describe '#values' do
181
183
  it 'gets for hash node' do
182
184
  node = parse("{:foo => :bar, 'foo' => 'bar'}")
183
185
  expect(node.values).to eq [parse(':bar'), parse("'bar'")]
184
186
  end
185
187
  end
186
188
 
187
- describe "#has_key?" do
188
- it "gets true if key exists" do
189
- node = parse("{:foo => :bar}")
189
+ describe '#has_key?' do
190
+ it 'gets true if key exists' do
191
+ node = parse('{:foo => :bar}')
190
192
  expect(node.has_key?(:foo)).to be_truthy
191
193
  end
192
194
 
193
- it "gets false if key does not exist" do
194
- node = parse("{:foo => :bar}")
195
+ it 'gets false if key does not exist' do
196
+ node = parse('{:foo => :bar}')
195
197
  expect(node.has_key?('foo')).to be_falsey
196
198
  end
197
199
  end
198
200
 
199
- describe "#hash_value" do
200
- it "gets value of specified key" do
201
- node = parse("{:foo => :bar}")
201
+ describe '#hash_value' do
202
+ it 'gets value of specified key' do
203
+ node = parse('{:foo => :bar}')
202
204
  expect(node.hash_value(:foo)).to eq parse(':bar')
203
205
  end
204
206
 
205
- it "gets nil if key does not exist" do
206
- node = parse("{:foo => :bar}")
207
+ it 'gets nil if key does not exist' do
208
+ node = parse('{:foo => :bar}')
207
209
  expect(node.hash_value(:bar)).to be_nil
208
210
  end
209
211
  end
210
212
 
211
- describe "#key" do
213
+ describe '#key' do
212
214
  it 'gets for pair node' do
213
215
  node = parse("{:foo => 'bar'}").children[0]
214
216
  expect(node.key).to eq parse(':foo')
215
217
  end
216
218
  end
217
219
 
218
- describe "#value" do
220
+ describe '#value' do
219
221
  it 'gets for hash node' do
220
222
  node = parse("{:foo => 'bar'}").children[0]
221
223
  expect(node.value).to eq parse("'bar'")
222
224
  end
223
225
  end
224
226
 
225
- describe "#condition" do
227
+ describe '#condition' do
226
228
  it 'gets for if node' do
227
229
  node = parse('if defined?(Bundler); end')
228
230
  expect(node.condition).to eq parse('defined?(Bundler)')
229
231
  end
230
232
  end
231
233
 
232
- describe "#left_value" do
234
+ describe '#left_value' do
233
235
  it 'gets for masgn' do
234
- node = parse("a, b = 1, 2")
236
+ node = parse('a, b = 1, 2')
235
237
  expect(node.left_value.to_source).to eq 'a, b'
236
238
  end
237
239
 
238
240
  it 'gets for lvasgn' do
239
- node = parse("a = 1")
241
+ node = parse('a = 1')
240
242
  expect(node.left_value).to eq :a
241
243
  end
242
244
 
243
245
  it 'gets for ivasgn' do
244
- node = parse("@a = 1")
246
+ node = parse('@a = 1')
245
247
  expect(node.left_value).to eq :@a
246
248
  end
247
249
  end
248
250
 
249
- describe "#right_value" do
251
+ describe '#right_value' do
250
252
  it 'gets for masgn' do
251
- node = parse("a, b = 1, 2")
253
+ node = parse('a, b = 1, 2')
252
254
  expect(node.right_value).to eq parse('[1, 2]')
253
255
  end
254
256
 
255
257
  it 'gets for masgn' do
256
- node = parse("a, b = params")
257
- expect(node.right_value).to eq parse("params")
258
+ node = parse('a, b = params')
259
+ expect(node.right_value).to eq parse('params')
258
260
  end
259
261
 
260
262
  it 'gets for lvasgn' do
261
- node = parse("a = 1")
262
- expect(node.right_value).to eq parse("1")
263
+ node = parse('a = 1')
264
+ expect(node.right_value).to eq parse('1')
263
265
  end
264
266
 
265
267
  it 'gets for ivasgn' do
266
- node = parse("@a = 1")
267
- expect(node.right_value).to eq parse("1")
268
+ node = parse('@a = 1')
269
+ expect(node.right_value).to eq parse('1')
268
270
  end
269
271
  end
270
272
 
271
- describe "#to_value" do
273
+ describe '#to_value' do
272
274
  it 'gets for int' do
273
- node = parse("1")
275
+ node = parse('1')
274
276
  expect(node.to_value).to eq 1
275
277
  end
276
278
 
277
279
  it 'gets for string' do
278
280
  node = parse("'str'")
279
- expect(node.to_value).to eq "str"
281
+ expect(node.to_value).to eq 'str'
280
282
  end
281
283
 
282
284
  it 'gets for symbol' do
283
- node = parse(":str")
285
+ node = parse(':str')
284
286
  expect(node.to_value).to eq :str
285
287
  end
286
288
 
287
289
  it 'get for boolean' do
288
- node = parse("true")
290
+ node = parse('true')
289
291
  expect(node.to_value).to be_truthy
290
- node = parse("false")
292
+ node = parse('false')
291
293
  expect(node.to_value).to be_falsey
292
294
  end
293
295
 
294
296
  it 'get for range' do
295
- node = parse("(1..10)")
297
+ node = parse('(1..10)')
296
298
  expect(node.to_value).to eq (1..10)
297
299
  end
298
300
 
@@ -382,13 +384,13 @@ describe Parser::AST::Node do
382
384
  it 'matches arguments any' do
383
385
  source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
384
386
  node = parse(source)
385
- expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
387
+ expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
386
388
  end
387
389
 
388
390
  it 'matches not' do
389
391
  source = 'class Synvert; end'
390
392
  node = parse(source)
391
- expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
393
+ expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
392
394
  end
393
395
  end
394
396
 
@@ -413,24 +415,25 @@ describe Parser::AST::Node do
413
415
  it 'rewrites for ArgumentsNode' do
414
416
  source = 'test { |a, b| }'
415
417
  node = parse(source)
416
- expect(node.rewritten_source('{{arguments}}')).to eq %(a, b)
418
+ expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
417
419
  end
418
420
 
419
- it 'rewrites array with multi line given as argument for method'do
420
- source = <<-EOS.strip
421
- long_name_method([
422
- 1,
423
- 2,
424
- 3
425
- ])
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
+ ])
426
428
  EOS
429
+
427
430
  node = parse(source)
428
- expect(node.rewritten_source('{{arguments}}')).to eq <<-EOS.strip
429
- [
430
- 1,
431
- 2,
432
- 3
433
- ]
431
+ expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
432
+ [
433
+ 1,
434
+ 2,
435
+ 3
436
+ ]
434
437
  EOS
435
438
  end
436
439
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert::Core