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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/synvert/core/node_ext.rb +43 -6
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +68 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff83abb1009ca5d9fe97a1c2e64701c31942a1c235e2b5a9bfe6cec1875f0956
|
4
|
+
data.tar.gz: b983862aa7a7861801104d43dc19cd794a5b83e4c2be5b137888b2e92a6f8068
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6978c684ad91c8a59c9774129c5ab21162b66f4f3b4eb043337a008c2dd5a5df3634b7781442d03147ae8829b250bd73f279b31379f6b3861e852da75209ba26
|
7
|
+
data.tar.gz: 96299eb5b9b6254a3781abaff6e3d9f2ac6e1da8e1216b17990471558408bf6ceecb439175183819f3aadd860de94754639c4434705910469318e91abd57f124
|
data/CHANGELOG.md
CHANGED
@@ -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,
|
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
|
388
|
-
return child_direct_child_node.child_node_range(child_nested_child_name
|
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?
|
490
|
-
evaluated =
|
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
|
data/lib/synvert/core/version.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2021-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|