synvert-core 0.16.0 → 0.20.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/.travis.yml +6 -1
- data/CHANGELOG.md +16 -0
- data/lib/synvert/core.rb +1 -4
- data/lib/synvert/core/configuration.rb +9 -17
- data/lib/synvert/core/engine/erb.rb +29 -22
- data/lib/synvert/core/exceptions.rb +3 -3
- data/lib/synvert/core/node_ext.rb +102 -101
- data/lib/synvert/core/rewriter.rb +33 -19
- data/lib/synvert/core/rewriter/action.rb +3 -5
- data/lib/synvert/core/rewriter/action/append_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/insert_action.rb +7 -3
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +4 -3
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -4
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -2
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/gem_spec.rb +10 -10
- data/lib/synvert/core/rewriter/helper.rb +3 -5
- data/lib/synvert/core/rewriter/instance.rb +22 -19
- data/lib/synvert/core/rewriter/scope/within_scope.rb +18 -11
- data/lib/synvert/core/version.rb +1 -1
- data/spec/spec_helper.rb +1 -6
- data/spec/synvert/core/engine/erb_spec.rb +30 -30
- data/spec/synvert/core/node_ext_spec.rb +53 -52
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +8 -8
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +3 -3
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +15 -11
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +21 -12
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +11 -10
- data/spec/synvert/core/rewriter/helper_spec.rb +34 -31
- data/spec/synvert/core/rewriter/instance_spec.rb +104 -70
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +8 -6
- data/spec/synvert/core/rewriter/scope/within_scope.rb +16 -9
- data/spec/synvert/core/rewriter_spec.rb +105 -68
- metadata +2 -4
- data/spec/synvert/core/configuration_spec.rb +0 -12
@@ -22,18 +22,8 @@ module Synvert::Core
|
|
22
22
|
current_node = @instance.current_node
|
23
23
|
return unless current_node
|
24
24
|
|
25
|
+
matching_nodes = find_matching_nodes(current_node)
|
25
26
|
@instance.process_with_node current_node do
|
26
|
-
matching_nodes = []
|
27
|
-
matching_nodes << current_node if current_node.match? @rules
|
28
|
-
if @options[:recursive]
|
29
|
-
current_node.recursive_children do |child_node|
|
30
|
-
matching_nodes << child_node if child_node.match? @rules
|
31
|
-
end
|
32
|
-
else
|
33
|
-
current_node.children do |child_node|
|
34
|
-
matching_nodes << child_node if child_node.match? @rules
|
35
|
-
end
|
36
|
-
end
|
37
27
|
matching_nodes.each do |matching_node|
|
38
28
|
@instance.process_with_node matching_node do
|
39
29
|
@instance.instance_eval &@block
|
@@ -41,5 +31,22 @@ module Synvert::Core
|
|
41
31
|
end
|
42
32
|
end
|
43
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def find_matching_nodes(current_node)
|
38
|
+
matching_nodes = []
|
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
|
43
|
+
end
|
44
|
+
else
|
45
|
+
current_node.each do |child_node|
|
46
|
+
matching_nodes << child_node if child_node.match? @rules
|
47
|
+
end
|
48
|
+
end
|
49
|
+
matching_nodes
|
50
|
+
end
|
44
51
|
end
|
45
52
|
end
|
data/lib/synvert/core/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
4
|
|
5
5
|
require 'synvert/core'
|
6
6
|
|
@@ -18,9 +18,4 @@ RSpec.configure do |config|
|
|
18
18
|
config.filter_run :focus
|
19
19
|
|
20
20
|
config.order = 'random'
|
21
|
-
|
22
|
-
config.before do
|
23
|
-
Synvert::Core::Configuration.instance.set :path, '.'
|
24
|
-
Synvert::Core::Configuration.instance.set :skip_files, []
|
25
|
-
end
|
26
21
|
end
|
@@ -4,38 +4,38 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Engine::ERB do
|
7
|
-
it
|
8
|
-
source
|
9
|
-
<%content_for :head do%>
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
<%end%>
|
16
|
-
|
17
|
-
<%
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
%>
|
22
|
-
|
23
|
-
<% if User.current &&
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
<% end %>
|
7
|
+
it 'encodes / decodes' do
|
8
|
+
source = <<~EOF
|
9
|
+
<%content_for :head do%>
|
10
|
+
<style>
|
11
|
+
body {
|
12
|
+
background-image: url(<%= asset_path('bg.png') %>);
|
13
|
+
}
|
14
|
+
</style>
|
15
|
+
<%end%>
|
16
|
+
|
17
|
+
<%
|
18
|
+
foo = 'bar'
|
19
|
+
post = Post.find(:first)
|
20
|
+
bar = 'foo'
|
21
|
+
%>
|
22
|
+
|
23
|
+
<% if User.current &&
|
24
|
+
User.current.admin %>
|
25
|
+
<%= rounded_content("page") do %>
|
26
|
+
<div class='test'>
|
27
|
+
<% if post %>
|
28
|
+
<div id="title"><%= foo %></div>
|
29
|
+
<% form_for post do |f| %>
|
30
|
+
<label><%= link_to_function 'test', "confirm('test');" %></label>
|
31
|
+
<%= f.text_field 'bar' %>
|
32
|
+
<% end %>
|
33
|
+
<% end %></div>
|
34
|
+
<% end %>
|
35
|
+
<% end %>
|
36
36
|
EOF
|
37
37
|
encoded_source = Engine::ERB.encode(source)
|
38
|
-
buffer = Parser::Source::Buffer.new
|
38
|
+
buffer = Parser::Source::Buffer.new '(test)'
|
39
39
|
buffer.source = encoded_source
|
40
40
|
parser = Parser::CurrentRuby.new
|
41
41
|
parser.reset
|
@@ -103,12 +103,12 @@ describe Parser::AST::Node do
|
|
103
103
|
|
104
104
|
describe '#arguments' do
|
105
105
|
it 'gets for def node' do
|
106
|
-
node = parse(
|
106
|
+
node = parse('def test(foo, bar); foo + bar; end')
|
107
107
|
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
108
108
|
end
|
109
109
|
|
110
110
|
it 'gets for defs node' do
|
111
|
-
node = parse(
|
111
|
+
node = parse('def self.test(foo, bar); foo + bar; end')
|
112
112
|
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
113
113
|
end
|
114
114
|
|
@@ -172,129 +172,129 @@ describe Parser::AST::Node do
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
|
-
describe
|
175
|
+
describe '#keys' do
|
176
176
|
it 'gets for hash node' do
|
177
177
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
178
178
|
expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
182
|
-
describe
|
182
|
+
describe '#values' do
|
183
183
|
it 'gets for hash node' do
|
184
184
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
185
185
|
expect(node.values).to eq [parse(':bar'), parse("'bar'")]
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
189
|
-
describe
|
190
|
-
it
|
191
|
-
node = parse(
|
189
|
+
describe '#has_key?' do
|
190
|
+
it 'gets true if key exists' do
|
191
|
+
node = parse('{:foo => :bar}')
|
192
192
|
expect(node.has_key?(:foo)).to be_truthy
|
193
193
|
end
|
194
194
|
|
195
|
-
it
|
196
|
-
node = parse(
|
195
|
+
it 'gets false if key does not exist' do
|
196
|
+
node = parse('{:foo => :bar}')
|
197
197
|
expect(node.has_key?('foo')).to be_falsey
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
201
|
-
describe
|
202
|
-
it
|
203
|
-
node = parse(
|
201
|
+
describe '#hash_value' do
|
202
|
+
it 'gets value of specified key' do
|
203
|
+
node = parse('{:foo => :bar}')
|
204
204
|
expect(node.hash_value(:foo)).to eq parse(':bar')
|
205
205
|
end
|
206
206
|
|
207
|
-
it
|
208
|
-
node = parse(
|
207
|
+
it 'gets nil if key does not exist' do
|
208
|
+
node = parse('{:foo => :bar}')
|
209
209
|
expect(node.hash_value(:bar)).to be_nil
|
210
210
|
end
|
211
211
|
end
|
212
212
|
|
213
|
-
describe
|
213
|
+
describe '#key' do
|
214
214
|
it 'gets for pair node' do
|
215
215
|
node = parse("{:foo => 'bar'}").children[0]
|
216
216
|
expect(node.key).to eq parse(':foo')
|
217
217
|
end
|
218
218
|
end
|
219
219
|
|
220
|
-
describe
|
220
|
+
describe '#value' do
|
221
221
|
it 'gets for hash node' do
|
222
222
|
node = parse("{:foo => 'bar'}").children[0]
|
223
223
|
expect(node.value).to eq parse("'bar'")
|
224
224
|
end
|
225
225
|
end
|
226
226
|
|
227
|
-
describe
|
227
|
+
describe '#condition' do
|
228
228
|
it 'gets for if node' do
|
229
229
|
node = parse('if defined?(Bundler); end')
|
230
230
|
expect(node.condition).to eq parse('defined?(Bundler)')
|
231
231
|
end
|
232
232
|
end
|
233
233
|
|
234
|
-
describe
|
234
|
+
describe '#left_value' do
|
235
235
|
it 'gets for masgn' do
|
236
|
-
node = parse(
|
236
|
+
node = parse('a, b = 1, 2')
|
237
237
|
expect(node.left_value.to_source).to eq 'a, b'
|
238
238
|
end
|
239
239
|
|
240
240
|
it 'gets for lvasgn' do
|
241
|
-
node = parse(
|
241
|
+
node = parse('a = 1')
|
242
242
|
expect(node.left_value).to eq :a
|
243
243
|
end
|
244
244
|
|
245
245
|
it 'gets for ivasgn' do
|
246
|
-
node = parse(
|
246
|
+
node = parse('@a = 1')
|
247
247
|
expect(node.left_value).to eq :@a
|
248
248
|
end
|
249
249
|
end
|
250
250
|
|
251
|
-
describe
|
251
|
+
describe '#right_value' do
|
252
252
|
it 'gets for masgn' do
|
253
|
-
node = parse(
|
253
|
+
node = parse('a, b = 1, 2')
|
254
254
|
expect(node.right_value).to eq parse('[1, 2]')
|
255
255
|
end
|
256
256
|
|
257
257
|
it 'gets for masgn' do
|
258
|
-
node = parse(
|
259
|
-
expect(node.right_value).to eq parse(
|
258
|
+
node = parse('a, b = params')
|
259
|
+
expect(node.right_value).to eq parse('params')
|
260
260
|
end
|
261
261
|
|
262
262
|
it 'gets for lvasgn' do
|
263
|
-
node = parse(
|
264
|
-
expect(node.right_value).to eq parse(
|
263
|
+
node = parse('a = 1')
|
264
|
+
expect(node.right_value).to eq parse('1')
|
265
265
|
end
|
266
266
|
|
267
267
|
it 'gets for ivasgn' do
|
268
|
-
node = parse(
|
269
|
-
expect(node.right_value).to eq parse(
|
268
|
+
node = parse('@a = 1')
|
269
|
+
expect(node.right_value).to eq parse('1')
|
270
270
|
end
|
271
271
|
end
|
272
272
|
|
273
|
-
describe
|
273
|
+
describe '#to_value' do
|
274
274
|
it 'gets for int' do
|
275
|
-
node = parse(
|
275
|
+
node = parse('1')
|
276
276
|
expect(node.to_value).to eq 1
|
277
277
|
end
|
278
278
|
|
279
279
|
it 'gets for string' do
|
280
280
|
node = parse("'str'")
|
281
|
-
expect(node.to_value).to eq
|
281
|
+
expect(node.to_value).to eq 'str'
|
282
282
|
end
|
283
283
|
|
284
284
|
it 'gets for symbol' do
|
285
|
-
node = parse(
|
285
|
+
node = parse(':str')
|
286
286
|
expect(node.to_value).to eq :str
|
287
287
|
end
|
288
288
|
|
289
289
|
it 'get for boolean' do
|
290
|
-
node = parse(
|
290
|
+
node = parse('true')
|
291
291
|
expect(node.to_value).to be_truthy
|
292
|
-
node = parse(
|
292
|
+
node = parse('false')
|
293
293
|
expect(node.to_value).to be_falsey
|
294
294
|
end
|
295
295
|
|
296
296
|
it 'get for range' do
|
297
|
-
node = parse(
|
297
|
+
node = parse('(1..10)')
|
298
298
|
expect(node.to_value).to eq (1..10)
|
299
299
|
end
|
300
300
|
|
@@ -384,13 +384,13 @@ describe Parser::AST::Node do
|
|
384
384
|
it 'matches arguments any' do
|
385
385
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
386
386
|
node = parse(source)
|
387
|
-
expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
|
387
|
+
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
388
388
|
end
|
389
389
|
|
390
390
|
it 'matches not' do
|
391
391
|
source = 'class Synvert; end'
|
392
392
|
node = parse(source)
|
393
|
-
expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
|
393
|
+
expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
|
394
394
|
end
|
395
395
|
end
|
396
396
|
|
@@ -415,24 +415,25 @@ describe Parser::AST::Node do
|
|
415
415
|
it 'rewrites for ArgumentsNode' do
|
416
416
|
source = 'test { |a, b| }'
|
417
417
|
node = parse(source)
|
418
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
418
|
+
expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
|
419
419
|
end
|
420
420
|
|
421
|
-
it 'rewrites array with multi line given as argument for method'do
|
422
|
-
source =
|
423
|
-
long_name_method([
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
])
|
421
|
+
it 'rewrites array with multi line given as argument for method' do
|
422
|
+
source = <<~EOS.strip
|
423
|
+
long_name_method([
|
424
|
+
1,
|
425
|
+
2,
|
426
|
+
3
|
427
|
+
])
|
428
428
|
EOS
|
429
|
+
|
429
430
|
node = parse(source)
|
430
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
431
|
-
[
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
]
|
431
|
+
expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
|
432
|
+
[
|
433
|
+
1,
|
434
|
+
2,
|
435
|
+
3
|
436
|
+
]
|
436
437
|
EOS
|
437
438
|
end
|
438
439
|
end
|
@@ -13,11 +13,11 @@ module Synvert::Core
|
|
13
13
|
}
|
14
14
|
|
15
15
|
it 'gets begin_pos' do
|
16
|
-
expect(subject.begin_pos).to eq
|
16
|
+
expect(subject.begin_pos).to eq 'Synvert::Application.configure do'.length
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'gets end_pos' do
|
20
|
-
expect(subject.end_pos).to eq
|
20
|
+
expect(subject.end_pos).to eq 'Synvert::Application.configure do'.length
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'gets rewritten_code' do
|
@@ -34,11 +34,11 @@ module Synvert::Core
|
|
34
34
|
}
|
35
35
|
|
36
36
|
it 'gets begin_pos' do
|
37
|
-
expect(subject.begin_pos).to eq
|
37
|
+
expect(subject.begin_pos).to eq 'RSpec.configure do |config|'.length
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'gets end_pos' do
|
41
|
-
expect(subject.end_pos).to eq
|
41
|
+
expect(subject.end_pos).to eq 'RSpec.configure do |config|'.length
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'gets rewritten_code' do
|
@@ -55,11 +55,11 @@ module Synvert::Core
|
|
55
55
|
}
|
56
56
|
|
57
57
|
it 'gets begin_pos' do
|
58
|
-
expect(subject.begin_pos).to eq
|
58
|
+
expect(subject.begin_pos).to eq 'class User'.length
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'gets end_pos' do
|
62
|
-
expect(subject.end_pos).to eq
|
62
|
+
expect(subject.end_pos).to eq 'class User'.length
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'gets rewritten_code' do
|
@@ -76,11 +76,11 @@ module Synvert::Core
|
|
76
76
|
}
|
77
77
|
|
78
78
|
it 'gets begin_pos' do
|
79
|
-
expect(subject.begin_pos).to eq
|
79
|
+
expect(subject.begin_pos).to eq 'class User < ActionRecord::Base'.length
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'gets end_pos' do
|
83
|
-
expect(subject.end_pos).to eq
|
83
|
+
expect(subject.end_pos).to eq 'class User < ActionRecord::Base'.length
|
84
84
|
end
|
85
85
|
|
86
86
|
it 'gets rewritten_code' do
|
@@ -5,18 +5,18 @@ require 'spec_helper'
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::InsertAfterAction do
|
7
7
|
subject {
|
8
|
-
source =
|
8
|
+
source = ' include Foo'
|
9
9
|
node = Parser::CurrentRuby.parse(source)
|
10
10
|
instance = double(current_node: node)
|
11
11
|
Rewriter::InsertAfterAction.new(instance, 'include Bar')
|
12
12
|
}
|
13
13
|
|
14
14
|
it 'gets begin_pos' do
|
15
|
-
expect(subject.begin_pos).to eq
|
15
|
+
expect(subject.begin_pos).to eq ' include Foo'.length
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'gets end_pos' do
|
19
|
-
expect(subject.end_pos).to eq
|
19
|
+
expect(subject.end_pos).to eq ' include Foo'.length
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'gets rewritten_code' do
|