synvert-core 1.11.0 → 1.12.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 +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +2 -2
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition.rb +4 -4
- data/lib/synvert/core/rewriter/instance.rb +22 -31
- data/lib/synvert/core/rewriter/scope/within_scope.rb +6 -4
- data/lib/synvert/core/rewriter.rb +0 -1
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +1 -1
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +105 -60
- metadata +2 -5
- data/lib/synvert/core/rewriter/scope/query_scope.rb +0 -38
- data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e526dbe6f12418da40df7cb5b945f7ed957820573455b575f2a609c334b58e72
|
4
|
+
data.tar.gz: 1f22528b636b548c9ddb05466d366e62dce813794d173c11b8de11ef522b97a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46f29de95717e764a33c0303c20086fb3fc3dcc43c7548c174b4eb46f368e2b0cec6b1ea6ff88eed04654f58cf9dad3066aa679ece8d7a6cfb2a7676095fe4aa
|
7
|
+
data.tar.gz: 7622e84c396751bd49b57464424efee84ba3f45d727c5653528fb11a5da7d26e856c0008ea1dc5967e7ee3afe8f238a8dbb6c67bc5071970312523a426a98dd3
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -73,16 +73,16 @@ DSLs are as follows
|
|
73
73
|
|
74
74
|
Scopes:
|
75
75
|
|
76
|
-
* [find_node](./Synvert/Core/Rewriter/Instance.html#find_node-instance_method) - recursively find matching ast nodes by node query language
|
77
76
|
* [within_node](./Synvert/Core/Rewriter/Instance.html#within_node-instance_method) - recursively find matching ast nodes
|
78
77
|
* [with_node](./Synvert/Core/Rewriter/Instance.html#with_node-instance_method) - alias to within_node
|
78
|
+
* [find_node](./Synvert/Core/Rewriter/Instance.html#find_node-instance_method) - alias to within_node
|
79
79
|
* [goto_node](./Synvert/Core/Rewriter/Instance.html#goto_node-instance_method) - go to a child node
|
80
80
|
|
81
81
|
Conditions:
|
82
82
|
|
83
83
|
* [if_exist_node](./Synvert/Core/Rewriter/Instance.html#if_exist_node-instance_method) - check if matching node exist in the child nodes
|
84
84
|
* [unless_exist_node](./Synvert/Core/Rewriter/Instance.html#unless_exist_node-instance_method) - check if matching node doesn't exist in the child nodes
|
85
|
-
* [if_only_exist_node](./Synvert/Core/Rewriter/Instance.html#if_only_exist_node-instance_method) - check if current node has only one child node and the child node matches
|
85
|
+
* [if_only_exist_node](./Synvert/Core/Rewriter/Instance.html#if_only_exist_node-instance_method) - check if current node has only one child node and the child node matches
|
86
86
|
|
87
87
|
Actions:
|
88
88
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
# IfOnlyExistCondition checks if node has only one child node and the child node matches
|
4
|
+
# IfOnlyExistCondition checks if node has only one child node and the child node matches.
|
5
5
|
class Rewriter::IfOnlyExistCondition < Rewriter::Condition
|
6
6
|
private
|
7
7
|
|
8
|
-
# check if only have one child node and the child node matches
|
8
|
+
# check if only have one child node and the child node matches.
|
9
9
|
#
|
10
10
|
# @return [Boolean]
|
11
11
|
def match?
|
@@ -1,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
# Condition checks if rules matches.
|
4
|
+
# Condition checks if nql or rules matches.
|
5
5
|
class Rewriter::Condition
|
6
6
|
# Initialize a Condition.
|
7
7
|
#
|
8
8
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
9
|
-
# @param
|
9
|
+
# @param nql_or_rules [String|Hash]
|
10
10
|
# @yield run when condition matches
|
11
|
-
def initialize(instance,
|
11
|
+
def initialize(instance, nql_or_rules, &block)
|
12
12
|
@instance = instance
|
13
|
-
@node_query = NodeQuery.new(
|
13
|
+
@node_query = NodeQuery.new(nql_or_rules)
|
14
14
|
@block = block
|
15
15
|
end
|
16
16
|
|
@@ -85,38 +85,29 @@ module Synvert::Core
|
|
85
85
|
# DSL #
|
86
86
|
#######
|
87
87
|
|
88
|
-
# Parse +find_node+ dsl, it creates {Synvert::Core::Rewriter::QueryScope} to recursively find matching ast nodes,
|
89
|
-
# then continue operating on each matching ast node.
|
90
|
-
# @example
|
91
|
-
# # matches FactoryBot.create(:user)
|
92
|
-
# find_node '.send[receiver=FactoryBot][message=create][arguments.size=1]' do
|
93
|
-
# end
|
94
|
-
# @param nql [String] node query language to find matching ast nodes.
|
95
|
-
# @yield run on the matching nodes.
|
96
|
-
# @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if query string is invalid.
|
97
|
-
def find_node(nql, options = {}, &block)
|
98
|
-
Rewriter::QueryScope.new(self, nql, options, &block).process
|
99
|
-
rescue NodeQueryLexer::ScanError, Racc::ParseError => e
|
100
|
-
raise NodeQuery::Compiler::ParseError, "Invalid query string: #{nql}"
|
101
|
-
end
|
102
|
-
|
103
88
|
# Parse +within_node+ dsl, it creates a {Synvert::Core::Rewriter::WithinScope} to recursively find matching ast nodes,
|
104
89
|
# then continue operating on each matching ast node.
|
105
90
|
# @example
|
106
91
|
# # matches User.find_by_login('test')
|
107
92
|
# with_node type: 'send', message: /^find_by_/ do
|
108
93
|
# end
|
109
|
-
#
|
94
|
+
# # matches FactoryBot.create(:user)
|
95
|
+
# with_node '.send[receiver=FactoryBot][message=create][arguments.size=1]' do
|
96
|
+
# end
|
97
|
+
# @param nql_or_rules [String|Hash] nql or rules to find mathing ast nodes.
|
110
98
|
# @param options [Hash] optional
|
111
99
|
# @option including_self [Boolean] set if query the current node, default is true
|
112
100
|
# @option stop_at_first_match [Boolean] set if stop at first match, default is false
|
113
101
|
# @option recursive [Boolean] set if recursively query child nodes, default is true
|
114
102
|
# @yield run on the matching nodes.
|
115
|
-
def within_node(
|
116
|
-
Rewriter::WithinScope.new(self,
|
103
|
+
def within_node(nql_or_rules, options = {}, &block)
|
104
|
+
Rewriter::WithinScope.new(self, nql_or_rules, options, &block).process
|
105
|
+
rescue NodeQueryLexer::ScanError, Racc::ParseError => e
|
106
|
+
raise NodeQuery::Compiler::ParseError, "Invalid query string: #{nql_or_rules}"
|
117
107
|
end
|
118
108
|
|
119
109
|
alias with_node within_node
|
110
|
+
alias find_node within_node
|
120
111
|
|
121
112
|
# Parse +goto_node+ dsl, it creates a {Synvert::Core::Rewriter::GotoScope} to go to a child node,
|
122
113
|
# then continue operating on the child node.
|
@@ -140,10 +131,10 @@ module Synvert::Core
|
|
140
131
|
# if_exist_node type: 'send', message: 'any_instance' do
|
141
132
|
# end
|
142
133
|
# end
|
143
|
-
# @param
|
134
|
+
# @param nql_or_rules [String|Hash] nql or rules to check mathing ast nodes.
|
144
135
|
# @param block [Block] block code to continue operating on the matching nodes.
|
145
|
-
def if_exist_node(
|
146
|
-
Rewriter::IfExistCondition.new(self,
|
136
|
+
def if_exist_node(nql_or_rules, &block)
|
137
|
+
Rewriter::IfExistCondition.new(self, nql_or_rules, &block).process
|
147
138
|
end
|
148
139
|
|
149
140
|
# Parse +unless_exist_node+ dsl, it creates a {Synvert::Core::Rewriter::UnlessExistCondition} to check
|
@@ -154,14 +145,14 @@ module Synvert::Core
|
|
154
145
|
# unless_exist_node type: 'send', message: 'any_instance' do
|
155
146
|
# end
|
156
147
|
# end
|
157
|
-
# @param
|
148
|
+
# @param nql_or_rules [String|Hash] nql or rules to check mathing ast nodes.
|
158
149
|
# @param block [Block] block code to continue operating on the matching nodes.
|
159
|
-
def unless_exist_node(
|
160
|
-
Rewriter::UnlessExistCondition.new(self,
|
150
|
+
def unless_exist_node(nql_or_rules, &block)
|
151
|
+
Rewriter::UnlessExistCondition.new(self, nql_or_rules, &block).process
|
161
152
|
end
|
162
153
|
|
163
154
|
# Parse +if_only_exist_node+ dsl, it creates a {Synvert::Core::Rewriter::IfOnlyExistCondition} to check
|
164
|
-
# if current node has only one child node and the child node matches
|
155
|
+
# if current node has only one child node and the child node matches,
|
165
156
|
# if so, then continue operating on each matching ast node.
|
166
157
|
# @example
|
167
158
|
# # it { should matcher }
|
@@ -169,10 +160,10 @@ module Synvert::Core
|
|
169
160
|
# if_only_exist_node type: 'send', receiver: nil, message: 'should' do
|
170
161
|
# end
|
171
162
|
# end
|
172
|
-
# @param
|
163
|
+
# @param nql_or_rules [String|Hash] nql or rules to check mathing ast nodes.
|
173
164
|
# @param block [Block] block code to continue operating on the matching nodes.
|
174
|
-
def if_only_exist_node(
|
175
|
-
Rewriter::IfOnlyExistCondition.new(self,
|
165
|
+
def if_only_exist_node(nql_or_rules, &block)
|
166
|
+
Rewriter::IfOnlyExistCondition.new(self, nql_or_rules, &block).process
|
176
167
|
end
|
177
168
|
|
178
169
|
# Parse +append+ dsl, it creates a {Synvert::Core::Rewriter::AppendAction} to
|
@@ -434,16 +425,16 @@ module Synvert::Core
|
|
434
425
|
only_paths.flat_map do |only_path|
|
435
426
|
@file_patterns.flat_map do |file_pattern|
|
436
427
|
pattern = only_path == "." ? file_pattern : File.join(only_path, file_pattern)
|
437
|
-
Dir.glob(pattern)
|
428
|
+
Dir.glob(pattern)
|
438
429
|
end
|
439
|
-
end
|
430
|
+
end - get_skip_files
|
440
431
|
end
|
441
432
|
end
|
442
433
|
|
443
434
|
# Get skip files.
|
444
435
|
# @return [Array<String>] skip files
|
445
436
|
def get_skip_files
|
446
|
-
|
437
|
+
Configuration.skip_paths.flat_map do |skip_path|
|
447
438
|
if File.directory?(skip_path)
|
448
439
|
Dir.glob(File.join(skip_path, "**/*"))
|
449
440
|
elsif File.file?(skip_path)
|
@@ -1,22 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
# WithinScope finds out nodes which match rules, then changes its scope to matching node.
|
4
|
+
# WithinScope finds out nodes which match nql or rules, then changes its scope to matching node.
|
5
5
|
class Rewriter::WithinScope < Rewriter::Scope
|
6
6
|
# Initialize a WithinScope.
|
7
7
|
#
|
8
8
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
9
|
-
# @param
|
9
|
+
# @param nql_or_rules [String|Hash]
|
10
10
|
# @param options [Hash]
|
11
11
|
# @yield run on all matching nodes
|
12
|
-
|
12
|
+
# @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if the query string is invalid.
|
13
|
+
def initialize(instance, nql_or_rules, options = {}, &block)
|
13
14
|
super(instance, &block)
|
14
15
|
|
15
16
|
@options = { including_self: true, stop_at_first_match: false, recursive: true }.merge(options)
|
16
|
-
@node_query = NodeQuery.new(
|
17
|
+
@node_query = NodeQuery.new(nql_or_rules)
|
17
18
|
end
|
18
19
|
|
19
20
|
# Find out the matching nodes.
|
21
|
+
#
|
20
22
|
# It checks the current node and iterates all child nodes,
|
21
23
|
# then run the block code on each matching node.
|
22
24
|
def process
|
@@ -17,7 +17,6 @@ module Synvert::Core
|
|
17
17
|
autoload :Instance, 'synvert/core/rewriter/instance'
|
18
18
|
|
19
19
|
autoload :Scope, 'synvert/core/rewriter/scope'
|
20
|
-
autoload :QueryScope, 'synvert/core/rewriter/scope/query_scope'
|
21
20
|
autoload :WithinScope, 'synvert/core/rewriter/scope/within_scope'
|
22
21
|
autoload :GotoScope, 'synvert/core/rewriter/scope/goto_scope'
|
23
22
|
|
data/lib/synvert/core/version.rb
CHANGED
@@ -12,7 +12,7 @@ module Synvert::Core
|
|
12
12
|
it 'parses find_node' do
|
13
13
|
scope = double
|
14
14
|
block = proc {}
|
15
|
-
expect(Rewriter::
|
15
|
+
expect(Rewriter::WithinScope).to receive(:new).with(instance, '.send[message=create]', {}, &block).and_return(scope)
|
16
16
|
expect(scope).to receive(:process)
|
17
17
|
instance.find_node('.send[message=create]', &block)
|
18
18
|
end
|
@@ -23,72 +23,117 @@ module Synvert::Core
|
|
23
23
|
before { instance.current_node = node }
|
24
24
|
|
25
25
|
describe '#process' do
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
context 'rules' do
|
27
|
+
it 'not call block if no matching node' do
|
28
|
+
run = false
|
29
|
+
scope =
|
30
|
+
Rewriter::WithinScope.new instance, type: 'send', message: 'missing' do
|
31
|
+
run = true
|
32
|
+
end
|
33
|
+
scope.process
|
34
|
+
expect(run).to be_falsey
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
37
|
+
it 'call block if there is matching node' do
|
38
|
+
run = false
|
39
|
+
type_in_scope = nil
|
40
|
+
scope =
|
41
|
+
Rewriter::WithinScope.new instance,
|
42
|
+
type: 'send',
|
43
|
+
receiver: 'FactoryGirl',
|
44
|
+
message: 'create',
|
45
|
+
arguments: [':user'] do
|
46
|
+
run = true
|
47
|
+
type_in_scope = node.type
|
48
|
+
end
|
49
|
+
scope.process
|
50
|
+
expect(run).to be_truthy
|
51
|
+
expect(type_in_scope).to eq :send
|
52
|
+
expect(instance.current_node.type).to eq :block
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
it 'matches multiple block nodes' do
|
56
|
+
block_nodes = []
|
57
|
+
scope =
|
58
|
+
Rewriter::WithinScope.new(instance, { type: 'block' }) do
|
59
|
+
block_nodes << node
|
60
|
+
end
|
61
|
+
scope.process
|
62
|
+
expect(block_nodes.size).to eq 3
|
63
|
+
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
65
|
+
it 'matches only 2 block nodes if including_self is false' do
|
66
|
+
block_nodes = []
|
67
|
+
scope =
|
68
|
+
Rewriter::WithinScope.new(instance, { type: 'block' }, { including_self: false }) do
|
69
|
+
block_nodes << node
|
70
|
+
end
|
71
|
+
scope.process
|
72
|
+
expect(block_nodes.size).to eq 2
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
75
|
+
it 'matches only one block node if recursive is false' do
|
76
|
+
block_nodes = []
|
77
|
+
scope =
|
78
|
+
Rewriter::WithinScope.new(instance, { type: 'block' }, { recursive: false }) do
|
79
|
+
block_nodes << node
|
80
|
+
end
|
81
|
+
scope.process
|
82
|
+
expect(block_nodes.size).to eq 1
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'matches only one block node if stop_at_first_match is true' do
|
86
|
+
block_nodes = []
|
87
|
+
scope =
|
88
|
+
Rewriter::WithinScope.new(instance, { type: 'block' }, { stop_at_first_match: true }) do
|
89
|
+
block_nodes << node
|
90
|
+
end
|
91
|
+
scope.process
|
92
|
+
expect(block_nodes.size).to eq 1
|
93
|
+
end
|
82
94
|
end
|
83
95
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
96
|
+
context 'nql' do
|
97
|
+
it 'not call block if no matching node' do
|
98
|
+
run = false
|
99
|
+
scope =
|
100
|
+
described_class.new instance, '.send[message=missing]' do
|
101
|
+
run = true
|
102
|
+
end
|
103
|
+
scope.process
|
104
|
+
expect(run).to be_falsey
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'call block if there is matching node' do
|
108
|
+
run = false
|
109
|
+
type_in_scope = nil
|
110
|
+
scope =
|
111
|
+
described_class.new instance, '.send[receiver=FactoryGirl][message=create][arguments=(:user)]' do
|
112
|
+
run = true
|
113
|
+
type_in_scope = node.type
|
114
|
+
end
|
115
|
+
scope.process
|
116
|
+
expect(run).to be_truthy
|
117
|
+
expect(type_in_scope).to eq :send
|
118
|
+
expect(instance.current_node.type).to eq :block
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'matches multiple block nodes' do
|
122
|
+
block_nodes = []
|
123
|
+
scope =
|
124
|
+
described_class.new(instance, '.block') do
|
125
|
+
block_nodes << node
|
126
|
+
end
|
127
|
+
scope.process
|
128
|
+
expect(block_nodes.size).to eq 3
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'raises InvalidOperatorError' do
|
132
|
+
scope = described_class.new(instance, '.send[receiver IN FactoryGirl]') {}
|
133
|
+
expect {
|
134
|
+
scope.process
|
135
|
+
}.to raise_error(NodeQuery::Compiler::InvalidOperatorError)
|
136
|
+
end
|
92
137
|
end
|
93
138
|
end
|
94
139
|
end
|
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: 1.
|
4
|
+
version: 1.12.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: 2022-10-
|
11
|
+
date: 2022-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -144,7 +144,6 @@ files:
|
|
144
144
|
- lib/synvert/core/rewriter/ruby_version.rb
|
145
145
|
- lib/synvert/core/rewriter/scope.rb
|
146
146
|
- lib/synvert/core/rewriter/scope/goto_scope.rb
|
147
|
-
- lib/synvert/core/rewriter/scope/query_scope.rb
|
148
147
|
- lib/synvert/core/rewriter/scope/within_scope.rb
|
149
148
|
- lib/synvert/core/rewriter/warning.rb
|
150
149
|
- lib/synvert/core/utils.rb
|
@@ -163,7 +162,6 @@ files:
|
|
163
162
|
- spec/synvert/core/rewriter/instance_spec.rb
|
164
163
|
- spec/synvert/core/rewriter/ruby_version_spec.rb
|
165
164
|
- spec/synvert/core/rewriter/scope/goto_scope_spec.rb
|
166
|
-
- spec/synvert/core/rewriter/scope/query_scope_spec.rb
|
167
165
|
- spec/synvert/core/rewriter/scope/within_scope_spec.rb
|
168
166
|
- spec/synvert/core/rewriter/scope_spec.rb
|
169
167
|
- spec/synvert/core/rewriter/warning_spec.rb
|
@@ -208,7 +206,6 @@ test_files:
|
|
208
206
|
- spec/synvert/core/rewriter/instance_spec.rb
|
209
207
|
- spec/synvert/core/rewriter/ruby_version_spec.rb
|
210
208
|
- spec/synvert/core/rewriter/scope/goto_scope_spec.rb
|
211
|
-
- spec/synvert/core/rewriter/scope/query_scope_spec.rb
|
212
209
|
- spec/synvert/core/rewriter/scope/within_scope_spec.rb
|
213
210
|
- spec/synvert/core/rewriter/scope_spec.rb
|
214
211
|
- spec/synvert/core/rewriter/warning_spec.rb
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core
|
4
|
-
# QueryScope finds out nodes by using node query language, then changes its scope to matching node.
|
5
|
-
class Rewriter::QueryScope < Rewriter::Scope
|
6
|
-
# Initialize a QueryScope.
|
7
|
-
#
|
8
|
-
# @param instance [Synvert::Core::Rewriter::Instance]
|
9
|
-
# @param nql [String]
|
10
|
-
# @param options [Hash]
|
11
|
-
# @yield run on all matching nodes
|
12
|
-
def initialize(instance, nql, options = {}, &block)
|
13
|
-
super(instance, &block)
|
14
|
-
|
15
|
-
@options = { including_self: true, stop_at_first_match: false, recursive: true }.merge(options)
|
16
|
-
@node_query = NodeQuery.new(nql)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Find out the matching nodes.
|
20
|
-
#
|
21
|
-
# It checks the current node and iterates all child nodes,
|
22
|
-
# then run the block code on each matching node.
|
23
|
-
# @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if the query string is invalid.
|
24
|
-
def process
|
25
|
-
current_node = @instance.current_node
|
26
|
-
return unless current_node
|
27
|
-
|
28
|
-
matching_nodes = @node_query.query_nodes(current_node, @options)
|
29
|
-
@instance.process_with_node(current_node) do
|
30
|
-
matching_nodes.each do |node|
|
31
|
-
@instance.process_with_node(node) do
|
32
|
-
@instance.instance_eval(&@block)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::QueryScope do
|
7
|
-
let(:instance) {
|
8
|
-
rewriter = Rewriter.new('foo', 'bar')
|
9
|
-
Rewriter::Instance.new(rewriter, 'file pattern')
|
10
|
-
}
|
11
|
-
let(:source) { <<~EOS }
|
12
|
-
describe Post do
|
13
|
-
it 'gets post' do
|
14
|
-
FactoryGirl.create :post
|
15
|
-
end
|
16
|
-
end
|
17
|
-
EOS
|
18
|
-
|
19
|
-
let(:node) { Parser::CurrentRuby.parse(source) }
|
20
|
-
|
21
|
-
before { instance.current_node = node }
|
22
|
-
|
23
|
-
describe '#process' do
|
24
|
-
it 'not call block if no matching node' do
|
25
|
-
run = false
|
26
|
-
scope =
|
27
|
-
described_class.new instance, '.send[message=missing]' do
|
28
|
-
run = true
|
29
|
-
end
|
30
|
-
scope.process
|
31
|
-
expect(run).to be_falsey
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'call block if there is matching node' do
|
35
|
-
run = false
|
36
|
-
type_in_scope = nil
|
37
|
-
scope =
|
38
|
-
described_class.new instance, '.send[receiver=FactoryGirl][message=create][arguments=(:post)]' do
|
39
|
-
run = true
|
40
|
-
type_in_scope = node.type
|
41
|
-
end
|
42
|
-
scope.process
|
43
|
-
expect(run).to be_truthy
|
44
|
-
expect(type_in_scope).to eq :send
|
45
|
-
expect(instance.current_node.type).to eq :block
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'matches multiple block nodes' do
|
49
|
-
block_nodes = []
|
50
|
-
scope =
|
51
|
-
described_class.new(instance, '.block') do
|
52
|
-
block_nodes << node
|
53
|
-
end
|
54
|
-
scope.process
|
55
|
-
expect(block_nodes.size).to eq 2
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'raises InvalidOperatorError' do
|
59
|
-
scope = described_class.new(instance, '.send[receiver IN FactoryGirl]') {}
|
60
|
-
expect {
|
61
|
-
scope.process
|
62
|
-
}.to raise_error(NodeQuery::Compiler::InvalidOperatorError)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|