synvert-core 0.33.1 → 0.38.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1f74df180f3b7583a026a0549fe82d57a075fae69990b2dd4a20d22f391fcc5
4
- data.tar.gz: 0a2159eb88c03c812b3a3a5a2d45e6cdfa79f899c70e820d538a90a6aa17cf9b
3
+ metadata.gz: 02bb61b261f5a4b8a3f528736987402add733b18c98228b0e2345559d2694d63
4
+ data.tar.gz: 6dd428e975dcbcadbdc4b03dd3ed2b7a846ce37c1d9e13303a68ba33a6d4e6fa
5
5
  SHA512:
6
- metadata.gz: bd505776d1d2f97f5acae2def15b808ee05536193f076e1d3ce683bdbf8ada56e89acf261db5247fc535bdf3444fb7ee8c58f2b142427ec20ac1325abdc31940
7
- data.tar.gz: 971fcf29e04c1aebc6e32d5c434adc0ec12e18a6732ff415970e08318b4af885598b4925567c691cd7ce1d8dbd7b7802bed4426cf71ae6dfb21f5546d18c2c73
6
+ metadata.gz: 7b374648741dc853387ff5c2f4bb5ad9518bee56a1646377e8cac22e7c208285f9c9db52882c05de823cf53cb3bdc6dbd365b9a1c505c88726eb3358651c1dc6
7
+ data.tar.gz: 8097ef7ad23287c35bf9d40a258a4d7c0b5d63ab0e99ab3667dc52d46cd7a51fa0e7f8d0cddee07054e87019bbf449704f3c01ba4cafcda425a30161894a5130
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.38.0 (2021-06-21)
4
+
5
+ * Add `xxx_source` for `hash` node
6
+
7
+ ## 0.36.0 (2021-06-21)
8
+
9
+ * Require `active_support/core_ext/array`
10
+
11
+ ## 0.35.0 (2021-05-17)
12
+
13
+ * Add `contain` rule
14
+
15
+ ## 0.34.0 (2021-05-16)
16
+
17
+ * `child_node_name` supports [:def, :parentheses] and [:defs, :parentheses]
18
+ * Rename `pipe` to `pipes`
19
+
3
20
  ## 0.33.0 (2021-05-10)
4
21
 
5
22
  * Add `body` for `class` node
data/lib/synvert/core.rb CHANGED
@@ -6,6 +6,7 @@ require 'parser'
6
6
  require 'parser/current'
7
7
  require 'ast'
8
8
  require 'active_support/core_ext/object'
9
+ require 'active_support/core_ext/array'
9
10
  require 'erubis'
10
11
  require 'set'
11
12
  require 'synvert/core/node_ext'
@@ -269,6 +269,12 @@ module Parser::AST
269
269
  return hash_value(key.to_sym)&.to_value if key?(key.to_sym)
270
270
  return hash_value(key.to_s)&.to_value if key?(key.to_s)
271
271
 
272
+ return nil
273
+ elsif :hash == type && method_name.to_s.include?('_source')
274
+ key = method_name.to_s.sub('_source', '')
275
+ return hash_value(key.to_sym)&.to_source if key?(key.to_sym)
276
+ return hash_value(key.to_s)&.to_source if key?(key.to_s)
277
+
272
278
  return nil
273
279
  end
274
280
 
@@ -281,6 +287,9 @@ module Parser::AST
281
287
  elsif :hash == type && method_name.to_s.include?('_value')
282
288
  key = method_name.to_s.sub('_value', '')
283
289
  return true if key?(key.to_sym) || key?(key.to_s)
290
+ elsif :hash == type && method_name.to_s.include?('_source')
291
+ key = method_name.to_s.sub('_source', '')
292
+ return true if key?(key.to_sym) || key?(key.to_s)
284
293
  end
285
294
 
286
295
  super
@@ -329,15 +338,15 @@ module Parser::AST
329
338
  # @return [Parser::Source::Range] source range of child node.
330
339
  def child_node_range(child_name)
331
340
  case [type, child_name]
332
- when %i[block pipe]
341
+ when %i[block pipes], %i[def parentheses], %i[defs parentheses]
333
342
  Parser::Source::Range.new('(string)', arguments.loc.expression.begin_pos, arguments.loc.expression.end_pos)
334
343
  when %i[block arguments], %i[def arguments], %i[defs arguments]
335
- Parser::Source::Range.new('(string)', arguments.first.loc.expression.begin_pos, arguments.last.loc.expression.end_pos)
336
- when %i[class name]
337
- loc.name
338
- when %i[def name]
339
- loc.name
340
- when %i[defs name]
344
+ Parser::Source::Range.new(
345
+ '(string)',
346
+ arguments.first.loc.expression.begin_pos,
347
+ arguments.last.loc.expression.end_pos
348
+ )
349
+ when %i[class name], %i[def name], %i[defs name]
341
350
  loc.name
342
351
  when %i[defs dot]
343
352
  loc.operator
@@ -403,7 +412,7 @@ module Parser::AST
403
412
  def match?(rules)
404
413
  flat_hash(rules).keys.all? do |multi_keys|
405
414
  case multi_keys.last
406
- when :any
415
+ when :any, :contain
407
416
  actual_values = actual_value(self, multi_keys[0...-1])
408
417
  expected = expected_value(rules, multi_keys)
409
418
  actual_values.any? { |actual| match_value?(actual, expected) }
@@ -37,9 +37,18 @@ module Synvert::Core
37
37
  def find_matching_nodes(current_node)
38
38
  matching_nodes = []
39
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
40
+ if current_node.is_a?(Parser::AST::Node)
41
+ matching_nodes << current_node if current_node.match? @rules
42
+ current_node.recursive_children do |child_node|
43
+ matching_nodes << child_node if child_node.match? @rules
44
+ end
45
+ else
46
+ current_node.each do |node|
47
+ matching_nodes << node if node.match? @rules
48
+ node.recursive_children do |child_node|
49
+ matching_nodes << child_node if child_node.match? @rules
50
+ end
51
+ end
43
52
  end
44
53
  elsif current_node.is_a?(Parser::AST::Node)
45
54
  if current_node.type == :begin
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.33.1'
5
+ VERSION = '0.38.0'
6
6
  end
7
7
  end
@@ -360,6 +360,18 @@ describe Parser::AST::Node do
360
360
  end
361
361
  end
362
362
 
363
+ describe 'key value source by method_missing' do
364
+ it 'gets for key value source' do
365
+ node = parse('{:foo => :bar}')
366
+ expect(node.foo_source).to eq ':bar'
367
+
368
+ node = parse("{'foo' => 'bar'}")
369
+ expect(node.foo_source).to eq "'bar'"
370
+
371
+ expect(node.bar_source).to be_nil
372
+ end
373
+ end
374
+
363
375
  describe '#recursive_children' do
364
376
  it 'iterates all children recursively' do
365
377
  node = parse('class Synvert; def current_node; @node; end; end')
@@ -414,6 +426,12 @@ describe Parser::AST::Node do
414
426
  expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
415
427
  end
416
428
 
429
+ it 'matches arguments contain' do
430
+ source = 'def slow(foo, bar, &block); end'
431
+ node = parse(source)
432
+ expect(node).to be_match(type: 'def', arguments: { contain: '&block' })
433
+ end
434
+
417
435
  it 'matches not' do
418
436
  source = 'class Synvert; end'
419
437
  node = parse(source)
@@ -447,9 +465,9 @@ describe Parser::AST::Node do
447
465
  expect(range.to_range).to eq(25...29)
448
466
  end
449
467
 
450
- it 'checks pipe' do
468
+ it 'checks pipes' do
451
469
  node = parse('Factory.define :user do |user|; end')
452
- range = node.child_node_range(:pipe)
470
+ range = node.child_node_range(:pipes)
453
471
  expect(range.to_range).to eq(24...30)
454
472
  end
455
473
  end
@@ -484,6 +502,12 @@ describe Parser::AST::Node do
484
502
  range = node.child_node_range(:arguments)
485
503
  expect(range.to_range).to eq(8...11)
486
504
  end
505
+
506
+ it 'checks parentheses' do
507
+ node = parse('def foo(bar); end')
508
+ range = node.child_node_range(:parentheses)
509
+ expect(range.to_range).to eq(7...12)
510
+ end
487
511
  end
488
512
 
489
513
  context 'defs node' do
@@ -510,6 +534,12 @@ describe Parser::AST::Node do
510
534
  range = node.child_node_range(:arguments)
511
535
  expect(range.to_range).to eq(13...16)
512
536
  end
537
+
538
+ it 'checks parentheses' do
539
+ node = parse('def self.foo(bar); end')
540
+ range = node.child_node_range(:parentheses)
541
+ expect(range.to_range).to eq(12...17)
542
+ end
513
543
  end
514
544
 
515
545
  context 'send node' do
@@ -8,16 +8,16 @@ module Synvert::Core
8
8
  rewriter = Rewriter.new('foo', 'bar')
9
9
  Rewriter::Instance.new(rewriter, 'file pattern')
10
10
  }
11
- let(:source) {
12
- "
13
- describe Post do
14
- it 'gets post' do
15
- FactoryGirl.create :post
16
- end
17
- end
18
- "
19
- }
11
+ let(:source) { <<~EOS }
12
+ describe Post do
13
+ it 'gets post' do
14
+ FactoryGirl.create :post
15
+ end
16
+ end
17
+ EOS
18
+
20
19
  let(:node) { Parser::CurrentRuby.parse(source) }
20
+
21
21
  before do
22
22
  Rewriter::Instance.reset
23
23
  instance.current_node = node
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.33.1
4
+ version: 0.38.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-05-15 00:00:00.000000000 Z
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport