synvert-core 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -3
- data/lib/synvert/core/node_ext.rb +58 -19
- data/lib/synvert/core/rewriter/condition.rb +3 -3
- data/lib/synvert/core/rewriter/instance.rb +17 -4
- data/lib/synvert/core/rewriter/scope.rb +2 -2
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +62 -18
- data/spec/synvert/core/rewriter/condition_spec.rb +56 -71
- data/spec/synvert/core/rewriter/instance_spec.rb +24 -0
- data/spec/synvert/core/rewriter/scope_spec.rb +3 -2
- data/spec/synvert/core/rewriter_spec.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ee7de00fd1f401d3d4ea506c941871da5714229
|
4
|
+
data.tar.gz: 406b2f2ecdfdb8b837ad0e8839c5e37b1c9d821f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1db8ef41e1b179c38df52c22091418183e40b879921b462bc47d9f9821a47cb4d4823983bb54024624717cbcf39bfa5b4a86575c4cd69a6ae9b179c7eb1bab6
|
7
|
+
data.tar.gz: f394394861d482865963fccd5024a340ddabf9e43077a0467a79ef9de77285348905e09d70f5e334d1e1ae759737dfd709bb5c49a955ff7b331eaacb13082482
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.3.0
|
4
|
+
|
5
|
+
* Rename node.source(instance) to node.to_source
|
6
|
+
* Add has_key? and hash_value helper methods for hash node
|
7
|
+
* Fix Instance#check_conflict_actions
|
8
|
+
|
3
9
|
## 0.2.0
|
4
10
|
|
5
11
|
* Add remove_file dsl
|
6
12
|
* Add warn dsl
|
7
|
-
|
8
|
-
## 0.1.1
|
9
|
-
|
10
13
|
* Return empty array if no available rewriters
|
11
14
|
|
12
15
|
## 0.1.0
|
@@ -119,6 +119,33 @@ class Parser::AST::Node
|
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
|
+
# Test if hash node contains specified key.
|
123
|
+
#
|
124
|
+
# @param [Object] key value.
|
125
|
+
# @return [Boolean] true if specified key exists.
|
126
|
+
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
127
|
+
def has_key?(key)
|
128
|
+
if :hash == self.type
|
129
|
+
self.children.any? { |pair_node| pair_node.key.to_value == key }
|
130
|
+
else
|
131
|
+
raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.inspect}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Get hash value node according to specified key.
|
136
|
+
#
|
137
|
+
# @param [Object] key value.
|
138
|
+
# @return [Parser::AST::Node] value node.
|
139
|
+
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
140
|
+
def hash_value(key)
|
141
|
+
if :hash == self.type
|
142
|
+
value_node = self.children.find { |pair_node| pair_node.key.to_value == key }
|
143
|
+
value_node ? value_node.value : nil
|
144
|
+
else
|
145
|
+
raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.inspect}"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
122
149
|
# Get key node of hash :pair node.
|
123
150
|
#
|
124
151
|
# @return [Parser::AST::Node] key node.
|
@@ -143,11 +170,26 @@ class Parser::AST::Node
|
|
143
170
|
end
|
144
171
|
end
|
145
172
|
|
173
|
+
# Return the exact value.
|
174
|
+
#
|
175
|
+
# @return [Object] exact value.
|
176
|
+
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
177
|
+
def to_value
|
178
|
+
case self.type
|
179
|
+
when :str, :sym
|
180
|
+
self.children.last
|
181
|
+
when :array
|
182
|
+
self.children.map(&:to_value)
|
183
|
+
else
|
184
|
+
raise Synvert::Core::MethodNotSupported.new "to_value is not handled for #{self.inspect}"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
146
188
|
# Get the source code of current node.
|
147
189
|
#
|
148
|
-
# @param instance [Synvert::Core::Rewriter::Instance]
|
149
190
|
# @return [String] source code.
|
150
|
-
def
|
191
|
+
def to_source
|
192
|
+
instance = Synvert::Rewriter::Instance.current
|
151
193
|
if self.loc.expression
|
152
194
|
instance.current_source[self.loc.expression.begin_pos...self.loc.expression.end_pos]
|
153
195
|
end
|
@@ -175,23 +217,22 @@ class Parser::AST::Node
|
|
175
217
|
|
176
218
|
# Match current node with rules.
|
177
219
|
#
|
178
|
-
# @param instance [Synvert::Core::Rewriter::Instance] used to get crrent source code.
|
179
220
|
# @param rules [Hash] rules to match.
|
180
221
|
# @return true if matches.
|
181
|
-
def match?(
|
222
|
+
def match?(rules)
|
182
223
|
flat_hash(rules).keys.all? do |multi_keys|
|
183
224
|
if multi_keys.last == :any
|
184
|
-
actual_values = actual_value(self,
|
225
|
+
actual_values = actual_value(self, multi_keys[0...-1])
|
185
226
|
expected = expected_value(rules, multi_keys)
|
186
|
-
actual_values.any? { |actual| match_value?(
|
227
|
+
actual_values.any? { |actual| match_value?(actual, expected) }
|
187
228
|
elsif multi_keys.last == :not
|
188
|
-
actual = actual_value(self,
|
229
|
+
actual = actual_value(self, multi_keys[0...-1])
|
189
230
|
expected = expected_value(rules, multi_keys)
|
190
|
-
!match_value?(
|
231
|
+
!match_value?(actual, expected)
|
191
232
|
else
|
192
|
-
actual = actual_value(self,
|
233
|
+
actual = actual_value(self, multi_keys)
|
193
234
|
expected = expected_value(rules, multi_keys)
|
194
|
-
match_value?(
|
235
|
+
match_value?(actual, expected)
|
195
236
|
end
|
196
237
|
end
|
197
238
|
end
|
@@ -229,33 +270,32 @@ private
|
|
229
270
|
|
230
271
|
# Compare actual value with expected value.
|
231
272
|
#
|
232
|
-
# @param instance [Synvert::Core::Rewriter::Instance] used to get source code.
|
233
273
|
# @param actual [Object] actual value.
|
234
274
|
# @param expected [Object] expected value.
|
235
275
|
# @return [Integer] -1, 0 or 1.
|
236
276
|
# @raise [Synvert::Core::MethodNotSupported] if expected class is not supported.
|
237
|
-
def match_value?(
|
277
|
+
def match_value?(actual, expected)
|
238
278
|
case expected
|
239
279
|
when Symbol
|
240
280
|
if Parser::AST::Node === actual
|
241
|
-
actual.
|
281
|
+
actual.to_source == ":#{expected}"
|
242
282
|
else
|
243
283
|
actual.to_sym == expected
|
244
284
|
end
|
245
285
|
when String
|
246
286
|
if Parser::AST::Node === actual
|
247
|
-
actual.
|
287
|
+
actual.to_source == expected || actual.to_source[1...-1] == expected
|
248
288
|
else
|
249
289
|
actual.to_s == expected
|
250
290
|
end
|
251
291
|
when Regexp
|
252
292
|
if Parser::AST::Node === actual
|
253
|
-
actual.
|
293
|
+
actual.to_source =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
|
254
294
|
else
|
255
295
|
actual.to_s =~ Regexp.new(expected.to_s, Regexp::MULTILINE)
|
256
296
|
end
|
257
297
|
when Array
|
258
|
-
actual.zip(expected).all? { |a, e| match_value?(
|
298
|
+
actual.zip(expected).all? { |a, e| match_value?(a, e) }
|
259
299
|
when NilClass
|
260
300
|
actual.nil?
|
261
301
|
when Numeric
|
@@ -297,13 +337,12 @@ private
|
|
297
337
|
# Get actual value from the node.
|
298
338
|
#
|
299
339
|
# @param node [Parser::AST::Node]
|
300
|
-
# @param instance [Synvert::Core::Rewriter::Instance]
|
301
340
|
# @param multi_keys [Array<Symbol>]
|
302
341
|
# @return [Object] actual value.
|
303
|
-
def actual_value(node,
|
342
|
+
def actual_value(node, multi_keys)
|
304
343
|
multi_keys.inject(node) { |n, key|
|
305
344
|
if n
|
306
|
-
key == :source ? n.send(key
|
345
|
+
key == :source ? n.send(key) : n.send(key)
|
307
346
|
end
|
308
347
|
}
|
309
348
|
end
|
@@ -27,7 +27,7 @@ module Synvert::Core
|
|
27
27
|
def match?
|
28
28
|
match = false
|
29
29
|
@instance.current_node.recursive_children do |child_node|
|
30
|
-
match = match || (child_node && child_node.match?(@
|
30
|
+
match = match || (child_node && child_node.match?(@rules))
|
31
31
|
end
|
32
32
|
match
|
33
33
|
end
|
@@ -39,7 +39,7 @@ module Synvert::Core
|
|
39
39
|
def match?
|
40
40
|
match = false
|
41
41
|
@instance.current_node.recursive_children do |child_node|
|
42
|
-
match = match || (child_node && child_node.match?(@
|
42
|
+
match = match || (child_node && child_node.match?(@rules))
|
43
43
|
end
|
44
44
|
!match
|
45
45
|
end
|
@@ -50,7 +50,7 @@ module Synvert::Core
|
|
50
50
|
# check if only have one child node and the child node matches rules.
|
51
51
|
def match?
|
52
52
|
@instance.current_node.body.size == 1 &&
|
53
|
-
@instance.current_node.body.first.match?(@
|
53
|
+
@instance.current_node.body.first.match?(@rules)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -6,6 +6,10 @@ module Synvert::Core
|
|
6
6
|
#
|
7
7
|
# One instance can contains one or many [Synvert::Core::Rewriter::Scope] and [Synvert::Rewriter::Condition].
|
8
8
|
class Rewriter::Instance
|
9
|
+
class <<self
|
10
|
+
attr_accessor :current
|
11
|
+
end
|
12
|
+
|
9
13
|
# @!attribute [rw] current_node
|
10
14
|
# @return current parsing node
|
11
15
|
# @!attribute [rw] current_source
|
@@ -32,6 +36,8 @@ module Synvert::Core
|
|
32
36
|
# It finds all files, for each file, it executes the block code, gets all rewrite actions,
|
33
37
|
# and rewrite source code back to original file.
|
34
38
|
def process
|
39
|
+
self.class.current = self
|
40
|
+
|
35
41
|
parser = Parser::CurrentRuby.new
|
36
42
|
file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
|
37
43
|
Dir.glob(file_pattern).each do |file_path|
|
@@ -164,12 +170,19 @@ module Synvert::Core
|
|
164
170
|
# So if there is an overlap between two actions, it removes the conflict actions and operate them in the next loop.
|
165
171
|
def check_conflict_actions
|
166
172
|
i = @actions.length - 1
|
173
|
+
j = i - 1
|
167
174
|
@conflict_actions = []
|
168
|
-
|
169
|
-
|
170
|
-
|
175
|
+
return if i < 0
|
176
|
+
|
177
|
+
begin_pos = @actions[i].begin_pos
|
178
|
+
while j > -1
|
179
|
+
if begin_pos <= @actions[j].end_pos
|
180
|
+
@conflict_actions << @actions.delete_at(j)
|
181
|
+
else
|
182
|
+
i = j
|
183
|
+
begin_pos = @actions[i].begin_pos
|
171
184
|
end
|
172
|
-
|
185
|
+
j -= 1
|
173
186
|
end
|
174
187
|
@conflict_actions
|
175
188
|
end
|
@@ -21,9 +21,9 @@ module Synvert::Core
|
|
21
21
|
return unless current_node
|
22
22
|
process_with_node current_node do
|
23
23
|
matching_nodes = []
|
24
|
-
matching_nodes << current_node if current_node.match? @
|
24
|
+
matching_nodes << current_node if current_node.match? @rules
|
25
25
|
current_node.recursive_children do |child_node|
|
26
|
-
matching_nodes << child_node if child_node.match? @
|
26
|
+
matching_nodes << child_node if child_node.match? @rules
|
27
27
|
end
|
28
28
|
matching_nodes.each do |matching_node|
|
29
29
|
process_with_node matching_node do
|
data/lib/synvert/core/version.rb
CHANGED
@@ -6,8 +6,8 @@ describe Parser::AST::Node do
|
|
6
6
|
node = parse('class Synvert; end')
|
7
7
|
expect(node.name).to eq parse('Synvert')
|
8
8
|
|
9
|
-
node = parse('class Synvert::
|
10
|
-
expect(node.name).to eq parse('Synvert::
|
9
|
+
node = parse('class Synvert::Rewriter::Instance; end')
|
10
|
+
expect(node.name).to eq parse('Synvert::Rewriter::Instance')
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'gets for module node' do
|
@@ -50,7 +50,8 @@ describe Parser::AST::Node do
|
|
50
50
|
source = 'RSpec.configure do |config|; end'
|
51
51
|
node = parse(source)
|
52
52
|
instance = double(current_source: source)
|
53
|
-
|
53
|
+
Synvert::Rewriter::Instance.current = instance
|
54
|
+
expect(node.arguments.map { |argument| argument.to_source }).to eq ['config']
|
54
55
|
end
|
55
56
|
|
56
57
|
it 'gets for defined? node' do
|
@@ -91,28 +92,52 @@ describe Parser::AST::Node do
|
|
91
92
|
describe "#keys" do
|
92
93
|
it 'gets for hash node' do
|
93
94
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
94
|
-
expect(node.keys).to eq [
|
95
|
+
expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
|
95
96
|
end
|
96
97
|
end
|
97
98
|
|
98
99
|
describe "#values" do
|
99
100
|
it 'gets for hash node' do
|
100
101
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
101
|
-
expect(node.values).to eq [
|
102
|
+
expect(node.values).to eq [parse(':bar'), parse("'bar'")]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#has_key?" do
|
107
|
+
it "gets true if key exists" do
|
108
|
+
node = parse("{:foo => :bar}")
|
109
|
+
expect(node.has_key?(:foo)).to be_truthy
|
110
|
+
end
|
111
|
+
|
112
|
+
it "gets false if key does not exist" do
|
113
|
+
node = parse("{:foo => :bar}")
|
114
|
+
expect(node.has_key?('foo')).to be_falsey
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#hash_value" do
|
119
|
+
it "gets value of specified key" do
|
120
|
+
node = parse("{:foo => :bar}")
|
121
|
+
expect(node.hash_value(:foo)).to eq parse(':bar')
|
122
|
+
end
|
123
|
+
|
124
|
+
it "gets nil if key does not exist" do
|
125
|
+
node = parse("{:foo => :bar}")
|
126
|
+
expect(node.hash_value(:bar)).to be_nil
|
102
127
|
end
|
103
128
|
end
|
104
129
|
|
105
130
|
describe "#key" do
|
106
131
|
it 'gets for pair node' do
|
107
132
|
node = parse("{:foo => 'bar'}").children[0]
|
108
|
-
expect(node.key).to eq
|
133
|
+
expect(node.key).to eq parse(':foo')
|
109
134
|
end
|
110
135
|
end
|
111
136
|
|
112
137
|
describe "#value" do
|
113
138
|
it 'gets for hash node' do
|
114
139
|
node = parse("{:foo => 'bar'}").children[0]
|
115
|
-
expect(node.value).to eq
|
140
|
+
expect(node.value).to eq parse("'bar'")
|
116
141
|
end
|
117
142
|
end
|
118
143
|
|
@@ -123,12 +148,30 @@ describe Parser::AST::Node do
|
|
123
148
|
end
|
124
149
|
end
|
125
150
|
|
126
|
-
describe
|
151
|
+
describe "#to_value" do
|
152
|
+
it 'gets for string' do
|
153
|
+
node = parse("'str'")
|
154
|
+
expect(node.to_value).to eq "str"
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'gets for symbol' do
|
158
|
+
node = parse(":str")
|
159
|
+
expect(node.to_value).to eq :str
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'gets for array' do
|
163
|
+
node = parse("['str', :str]")
|
164
|
+
expect(node.to_value).to eq ['str', :str]
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe '#to_source' do
|
127
169
|
it 'gets for node' do
|
128
170
|
source = 'params[:user][:email]'
|
129
171
|
instance = double(current_source: source)
|
172
|
+
Synvert::Rewriter::Instance.current = instance
|
130
173
|
node = parse(source)
|
131
|
-
expect(node.
|
174
|
+
expect(node.to_source).to eq 'params[:user][:email]'
|
132
175
|
end
|
133
176
|
end
|
134
177
|
|
@@ -153,57 +196,58 @@ describe Parser::AST::Node do
|
|
153
196
|
|
154
197
|
describe '#match?' do
|
155
198
|
let(:instance) {
|
156
|
-
rewriter = Synvert::
|
157
|
-
Synvert::
|
199
|
+
rewriter = Synvert::Rewriter.new('foobar')
|
200
|
+
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
158
201
|
}
|
202
|
+
before { Synvert::Rewriter::Instance.current = instance }
|
159
203
|
|
160
204
|
it 'matches class name' do
|
161
205
|
source = 'class Synvert; end'
|
162
206
|
instance.current_source = source
|
163
207
|
node = parse(source)
|
164
|
-
expect(node).to be_match(
|
208
|
+
expect(node).to be_match(type: 'class', name: 'Synvert')
|
165
209
|
end
|
166
210
|
|
167
211
|
it 'matches message with regexp' do
|
168
212
|
source = 'User.find_by_login(login)'
|
169
213
|
instance.current_source = source
|
170
214
|
node = parse(source)
|
171
|
-
expect(node).to be_match(
|
215
|
+
expect(node).to be_match(type: 'send', message: /^find_by_/)
|
172
216
|
end
|
173
217
|
|
174
218
|
it 'matches arguments with symbol' do
|
175
219
|
source = 'params[:user]'
|
176
220
|
instance.current_source = source
|
177
221
|
node = parse(source)
|
178
|
-
expect(node).to be_match(
|
222
|
+
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: [:user])
|
179
223
|
end
|
180
224
|
|
181
225
|
it 'matches assign number' do
|
182
226
|
source = 'at_least(0)'
|
183
227
|
instance.current_source = source
|
184
228
|
node = parse(source)
|
185
|
-
expect(node).to be_match(
|
229
|
+
expect(node).to be_match(type: 'send', arguments: [0])
|
186
230
|
end
|
187
231
|
|
188
232
|
it 'matches arguments with string' do
|
189
233
|
source = 'params["user"]'
|
190
234
|
instance.current_source = source
|
191
235
|
node = parse(source)
|
192
|
-
expect(node).to be_match(
|
236
|
+
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
|
193
237
|
end
|
194
238
|
|
195
239
|
it 'matches arguments any' do
|
196
240
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
197
241
|
instance.current_source = source
|
198
242
|
node = parse(source)
|
199
|
-
expect(node).to be_match(
|
243
|
+
expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
|
200
244
|
end
|
201
245
|
|
202
246
|
it 'matches not' do
|
203
247
|
source = 'class Synvert; end'
|
204
248
|
instance.current_source = source
|
205
249
|
node = parse(source)
|
206
|
-
expect(node).not_to be_match(
|
250
|
+
expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
|
207
251
|
end
|
208
252
|
end
|
209
253
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
describe Rewriter::
|
4
|
+
describe Rewriter::Condition do
|
5
5
|
let(:source) {
|
6
6
|
"""
|
7
7
|
RSpec.configure do |config|
|
@@ -12,94 +12,79 @@ module Synvert::Core
|
|
12
12
|
}
|
13
13
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
14
14
|
let(:instance) { double(:current_node => node, :current_source => source) }
|
15
|
+
before { Rewriter::Instance.current = instance }
|
15
16
|
|
16
|
-
describe
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
describe Rewriter::IfExistCondition do
|
18
|
+
describe '#process' do
|
19
|
+
it 'call block if match anything' do
|
20
|
+
run = false
|
21
|
+
condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
22
|
+
run = true
|
23
|
+
end
|
24
|
+
condition.process
|
25
|
+
expect(run).to be_truthy
|
21
26
|
end
|
22
|
-
condition.process
|
23
|
-
expect(run).to be_true
|
24
|
-
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
it 'not call block if not match anything' do
|
29
|
+
run = false
|
30
|
+
condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::SyntaxMethods'] do
|
31
|
+
run = true
|
32
|
+
end
|
33
|
+
condition.process
|
34
|
+
expect(run).to be_falsey
|
30
35
|
end
|
31
|
-
condition.process
|
32
|
-
expect(run).to be_false
|
33
36
|
end
|
34
37
|
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe Rewriter::UnlessExistCondition do
|
38
|
-
let(:source) {
|
39
|
-
"""
|
40
|
-
RSpec.configure do |config|
|
41
|
-
config.include EmailSpec::Helpers
|
42
|
-
config.include EmailSpec::Methods
|
43
|
-
end
|
44
|
-
"""
|
45
|
-
}
|
46
|
-
let(:node) { Parser::CurrentRuby.parse(source) }
|
47
|
-
let(:instance) { double(:current_node => node, :current_source => source) }
|
48
38
|
|
49
|
-
describe
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
39
|
+
describe Rewriter::UnlessExistCondition do
|
40
|
+
describe '#process' do
|
41
|
+
it 'call block if match anything' do
|
42
|
+
run = false
|
43
|
+
condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
|
44
|
+
run = true
|
45
|
+
end
|
46
|
+
condition.process
|
47
|
+
expect(run).to be_truthy
|
54
48
|
end
|
55
|
-
condition.process
|
56
|
-
expect(run).to be_true
|
57
|
-
end
|
58
49
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
50
|
+
it 'not call block if not match anything' do
|
51
|
+
run = false
|
52
|
+
condition = Rewriter::UnlessExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
53
|
+
run = true
|
54
|
+
end
|
55
|
+
condition.process
|
56
|
+
expect(run).to be_falsey
|
63
57
|
end
|
64
|
-
condition.process
|
65
|
-
expect(run).to be_false
|
66
58
|
end
|
67
59
|
end
|
68
|
-
end
|
69
60
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
61
|
+
describe Rewriter::IfOnlyExistCondition do
|
62
|
+
describe '#process' do
|
63
|
+
it 'gets matching nodes' do
|
64
|
+
source = """
|
65
|
+
RSpec.configure do |config|
|
66
|
+
config.include EmailSpec::Helpers
|
67
|
+
end
|
68
|
+
"""
|
69
|
+
node = Parser::CurrentRuby.parse(source)
|
70
|
+
instance = double(:current_node => node, :current_source => source)
|
71
|
+
Rewriter::Instance.current = instance
|
72
|
+
run = false
|
73
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
74
|
+
run = true
|
76
75
|
end
|
77
|
-
|
78
|
-
|
79
|
-
instance = double(:current_node => node, :current_source => source)
|
80
|
-
run = false
|
81
|
-
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
82
|
-
run = true
|
76
|
+
condition.process
|
77
|
+
expect(run).to be_truthy
|
83
78
|
end
|
84
|
-
condition.process
|
85
|
-
expect(run).to be_true
|
86
|
-
end
|
87
79
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
config.include EmailSpec::Methods
|
80
|
+
it 'not call block if does not match' do
|
81
|
+
run = false
|
82
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
83
|
+
run = true
|
93
84
|
end
|
94
|
-
|
95
|
-
|
96
|
-
instance = double(:current_node => node, :current_source => source)
|
97
|
-
run = false
|
98
|
-
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
99
|
-
run = true
|
85
|
+
condition.process
|
86
|
+
expect(run).to be_falsey
|
100
87
|
end
|
101
|
-
condition.process
|
102
|
-
expect(run).to be_false
|
103
88
|
end
|
104
89
|
end
|
105
90
|
end
|
@@ -166,6 +166,30 @@ end
|
|
166
166
|
expect(File).to receive(:write).with('config/environments/production.rb', output)
|
167
167
|
instance.process
|
168
168
|
end
|
169
|
+
|
170
|
+
describe "#check_conflict_actions" do
|
171
|
+
it "has no conflict" do
|
172
|
+
action1 = double(begin_pos: 10, end_pos: 20)
|
173
|
+
action2 = double(begin_pos: 30, end_pos: 40)
|
174
|
+
action3 = double(begin_pos: 50, end_pos: 60)
|
175
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
176
|
+
instance.instance_variable_set :@actions, [action1, action2, action3]
|
177
|
+
instance.send(:check_conflict_actions)
|
178
|
+
expect(instance.instance_variable_get :@conflict_actions).to eq []
|
179
|
+
expect(instance.instance_variable_get :@actions).to eq [action1, action2, action3]
|
180
|
+
end
|
181
|
+
|
182
|
+
it "has no conflict" do
|
183
|
+
action1 = double(begin_pos: 30, end_pos: 40)
|
184
|
+
action2 = double(begin_pos: 50, end_pos: 60)
|
185
|
+
action3 = double(begin_pos: 10, end_pos: 20)
|
186
|
+
instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb'
|
187
|
+
instance.instance_variable_set :@actions, [action1, action2, action3]
|
188
|
+
instance.send(:check_conflict_actions)
|
189
|
+
expect(instance.instance_variable_get :@conflict_actions).to eq [action2, action1]
|
190
|
+
expect(instance.instance_variable_get :@actions).to eq [action3]
|
191
|
+
end
|
192
|
+
end
|
169
193
|
end
|
170
194
|
end
|
171
195
|
end
|
@@ -18,6 +18,7 @@ end
|
|
18
18
|
}
|
19
19
|
let(:node) { Parser::CurrentRuby.parse(source) }
|
20
20
|
let(:instance) { double(:current_node => node, :current_node= => node, :current_source => source) }
|
21
|
+
before { Rewriter::Instance.current = instance }
|
21
22
|
|
22
23
|
describe '#process' do
|
23
24
|
it 'not call block if no matching node' do
|
@@ -26,7 +27,7 @@ end
|
|
26
27
|
run = true
|
27
28
|
end
|
28
29
|
scope.process
|
29
|
-
expect(run).to
|
30
|
+
expect(run).to be_falsey
|
30
31
|
end
|
31
32
|
|
32
33
|
it 'call block if there is matching node' do
|
@@ -35,7 +36,7 @@ end
|
|
35
36
|
run = true
|
36
37
|
end
|
37
38
|
scope.process
|
38
|
-
expect(run).to
|
39
|
+
expect(run).to be_truthy
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
@@ -73,7 +73,7 @@ module Synvert::Core
|
|
73
73
|
add_file 'foo.bar', 'FooBar'
|
74
74
|
end
|
75
75
|
rewriter.process_with_sandbox
|
76
|
-
expect(File.exist?('./foo.bar')).to
|
76
|
+
expect(File.exist?('./foo.bar')).to be_falsey
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -84,7 +84,7 @@ module Synvert::Core
|
|
84
84
|
remove_file 'foo.bar'
|
85
85
|
end
|
86
86
|
rewriter.process
|
87
|
-
expect(File.exist? './foo.bar').to
|
87
|
+
expect(File.exist? './foo.bar').to be_falsey
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'does nothing if file not exist' do
|
@@ -92,7 +92,7 @@ module Synvert::Core
|
|
92
92
|
remove_file 'foo.bar'
|
93
93
|
end
|
94
94
|
rewriter.process
|
95
|
-
expect(File.exist? './foo.bar').to
|
95
|
+
expect(File.exist? './foo.bar').to be_falsey
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'does nothing in sandbox mode' do
|
@@ -101,7 +101,7 @@ module Synvert::Core
|
|
101
101
|
add_file 'foo.bar', 'FooBar'
|
102
102
|
end
|
103
103
|
rewriter.process_with_sandbox
|
104
|
-
expect(File.exist?('./foo.bar')).to
|
104
|
+
expect(File.exist?('./foo.bar')).to be_truthy
|
105
105
|
FileUtils.rm './foo.bar'
|
106
106
|
end
|
107
107
|
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: 0.
|
4
|
+
version: 0.3.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: 2014-
|
11
|
+
date: 2014-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|