synvert-core 0.60.0 → 0.61.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: 91e00ecd8d7f5d6ff82a17580d088cc323d0e94b53a1488e6be50cbeb4a44b13
4
- data.tar.gz: 9e3f9dbb81cb07a27fdb8d0f2888d1c7827270d9b5bf9434ff0f649245de65a5
3
+ metadata.gz: ff83abb1009ca5d9fe97a1c2e64701c31942a1c235e2b5a9bfe6cec1875f0956
4
+ data.tar.gz: b983862aa7a7861801104d43dc19cd794a5b83e4c2be5b137888b2e92a6f8068
5
5
  SHA512:
6
- metadata.gz: 6c777f93827fa6485a6636571500f8def9cd0c8b7d910f61da7c309a84124e70415d8d86e97037ec4ee574ae783936267e446e6e1b870523fdb0ea4abea09f03
7
- data.tar.gz: 4fa8ef96adce5932eed3d927d0f878684c1bff3896829c9297df8cb20eda98f4324aed12a066e8c5b2b60996f87929cd31d911b2961414dc2e8c0356edfb3f35
6
+ metadata.gz: 6978c684ad91c8a59c9774129c5ab21162b66f4f3b4eb043337a008c2dd5a5df3634b7781442d03147ae8829b250bd73f279b31379f6b3861e852da75209ba26
7
+ data.tar.gz: 96299eb5b9b6254a3781abaff6e3d9f2ac6e1da8e1216b17990471558408bf6ceecb439175183819f3aadd860de94754639c4434705910469318e91abd57f124
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.61.0 (2021-12-10)
4
+
5
+ * Add `Node#child_node_by_name`
6
+ * Fix `Node#child_node_range` for array
7
+
3
8
  ## 0.60.0 (2021-12-02)
4
9
 
5
10
  * Add `to_string` to `sym` node
@@ -343,6 +343,43 @@ module Parser::AST
343
343
  loc.expression.line
344
344
  end
345
345
 
346
+ # Get child node by child name.
347
+ #
348
+ # @param [String] name of child node.
349
+ # @return [Parser::AST::Node] the child node.
350
+ def child_node_by_name(child_name)
351
+ direct_child_name, nested_child_name = child_name.to_s.split('.', 2)
352
+ if respond_to?(direct_child_name)
353
+ child_node = send(direct_child_name)
354
+
355
+ if nested_child_name
356
+ if child_node.is_a?(Array)
357
+ child_direct_child_name, child_nested_child_name = nested_child_name.split('.', 2)
358
+ child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name.to_i - 1] : child_node.send(child_direct_child_name)
359
+ return child_direct_child_node.child_node_by_name(child_nested_child_name) if child_nested_child_name
360
+ return child_direct_child_node if child_direct_child_node
361
+
362
+ raise Synvert::Core::MethodNotSupported,
363
+ "child_node_by_name is not handled for #{debug_info}, child_name: #{child_name}"
364
+ end
365
+
366
+ return child_node.child_node_by_name(nested_child_name)
367
+ end
368
+
369
+ return nil if child_node.nil?
370
+
371
+ return child_node if child_node.is_a?(Parser::AST::Node)
372
+
373
+ # arguments
374
+ return nil if child_node.empty?
375
+
376
+ return child_node
377
+ end
378
+
379
+ raise Synvert::Core::MethodNotSupported,
380
+ "child_node_by_name is not handled for #{debug_info}, child_name: #{child_name}"
381
+ end
382
+
346
383
  # Get the source range of child node.
347
384
  #
348
385
  # @param [String] name of child node.
@@ -382,10 +419,10 @@ module Parser::AST
382
419
 
383
420
  if nested_child_name
384
421
  if child_node.is_a?(Array)
385
- child_direct_child_name, *child_nested_child_name = nested_child_name
386
- child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name] : child_node.send(child_direct_child_name)
387
- if child_nested_child_name.length > 0
388
- return child_direct_child_node.child_node_range(child_nested_child_name.join('.'))
422
+ child_direct_child_name, child_nested_child_name = nested_child_name.split('.', 2)
423
+ child_direct_child_node = child_direct_child_name =~ /\A\d+\z/ ? child_node[child_direct_child_name.to_i - 1] : child_node.send(child_direct_child_name)
424
+ if child_nested_child_name
425
+ return child_direct_child_node.child_node_range(child_nested_child_name)
389
426
  elsif child_direct_child_node
390
427
  return (
391
428
  Parser::Source::Range.new(
@@ -486,8 +523,8 @@ module Parser::AST
486
523
  def rewritten_source(code)
487
524
  code.gsub(/{{(.*?)}}/m) do
488
525
  old_code = Regexp.last_match(1)
489
- if respond_to? old_code.split(/\.|\[/).first
490
- evaluated = instance_eval old_code
526
+ if respond_to?(old_code.split('.').first)
527
+ evaluated = child_node_by_name(old_code)
491
528
  case evaluated
492
529
  when Parser::AST::Node
493
530
  if evaluated.type == :args
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.60.0'
5
+ VERSION = '0.61.0'
6
6
  end
7
7
  end
@@ -554,6 +554,54 @@ describe Parser::AST::Node do
554
554
  end
555
555
  end
556
556
 
557
+ describe '#child_node_by_name' do
558
+ context 'block node' do
559
+ it 'checks caller' do
560
+ node = parse('Factory.define :user do |user|; end')
561
+ child_node = node.child_node_by_name(:caller)
562
+ expect(child_node).to eq node.caller
563
+ end
564
+
565
+ it 'checks arguments' do
566
+ node = parse('Factory.define :user do |user|; end')
567
+ child_node = node.child_node_by_name(:arguments)
568
+ expect(child_node).to eq node.arguments
569
+ end
570
+
571
+ it 'checks caller.receiver' do
572
+ node = parse('Factory.define :user do |user|; end')
573
+ child_node = node.child_node_by_name('caller.receiver')
574
+ expect(child_node).to eq node.caller.receiver
575
+ end
576
+
577
+ it 'checks caller.message' do
578
+ node = parse('Factory.define :user do |user|; end')
579
+ child_node = node.child_node_by_name('caller.message')
580
+ expect(child_node).to eq node.caller.message
581
+ end
582
+ end
583
+
584
+ context 'array' do
585
+ it 'checks array by index' do
586
+ node = parse('factory :admin, class: User do; end')
587
+ child_node = node.child_node_by_name('caller.arguments.2')
588
+ expect(child_node).to eq node.caller.arguments[1]
589
+ end
590
+
591
+ it 'checks array by method' do
592
+ node = parse('factory :admin, class: User do; end')
593
+ child_node = node.child_node_by_name('caller.arguments.second')
594
+ expect(child_node).to eq node.caller.arguments[1]
595
+ end
596
+
597
+ it "checks array' value" do
598
+ node = parse('factory :admin, class: User do; end')
599
+ child_node = node.child_node_by_name('caller.arguments.second.class_value')
600
+ expect(child_node).to eq node.caller.arguments[1].class_value
601
+ end
602
+ end
603
+ end
604
+
557
605
  describe '#child_node_range' do
558
606
  context 'block node' do
559
607
  it 'checks caller' do
@@ -720,6 +768,26 @@ describe Parser::AST::Node do
720
768
  expect(range).to be_nil
721
769
  end
722
770
  end
771
+
772
+ context 'array' do
773
+ it 'checks array by index' do
774
+ node = parse('factory :admin, class: User do; end')
775
+ range = node.child_node_range('caller.arguments.2')
776
+ expect(range.to_range).to eq(16...27)
777
+ end
778
+
779
+ it 'checks array by method' do
780
+ node = parse('factory :admin, class: User do; end')
781
+ range = node.child_node_range('caller.arguments.second')
782
+ expect(range.to_range).to eq(16...27)
783
+ end
784
+
785
+ it "checks array' value" do
786
+ node = parse('factory :admin, class: User do; end')
787
+ range = node.child_node_range('caller.arguments.second.class_value')
788
+ expect(range.to_range).to eq(23...27)
789
+ end
790
+ end
723
791
  end
724
792
 
725
793
  describe '#rewritten_source' do
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.60.0
4
+ version: 0.61.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-12-02 00:00:00.000000000 Z
11
+ date: 2021-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport