synvert-core 0.15.2 → 0.19.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 +16 -2
- data/Gemfile +2 -0
- data/Guardfile +2 -0
- data/Rakefile +2 -0
- data/lib/synvert/core.rb +2 -3
- data/lib/synvert/core/configuration.rb +10 -17
- data/lib/synvert/core/engine.rb +1 -1
- data/lib/synvert/core/engine/erb.rb +31 -23
- data/lib/synvert/core/exceptions.rb +5 -3
- data/lib/synvert/core/node_ext.rb +107 -101
- data/lib/synvert/core/rewriter.rb +22 -16
- data/lib/synvert/core/rewriter/action.rb +4 -6
- data/lib/synvert/core/rewriter/action/append_action.rb +3 -3
- data/lib/synvert/core/rewriter/action/insert_action.rb +9 -5
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +2 -2
- data/lib/synvert/core/rewriter/action/remove_action.rb +2 -2
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +5 -4
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +7 -5
- data/lib/synvert/core/rewriter/condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +2 -2
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +2 -3
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +2 -2
- data/lib/synvert/core/rewriter/gem_spec.rb +11 -11
- data/lib/synvert/core/rewriter/helper.rb +4 -6
- data/lib/synvert/core/rewriter/instance.rb +37 -23
- data/lib/synvert/core/rewriter/ruby_version.rb +1 -1
- data/lib/synvert/core/rewriter/scope.rb +1 -1
- data/lib/synvert/core/rewriter/scope/goto_scope.rb +2 -1
- data/lib/synvert/core/rewriter/scope/within_scope.rb +23 -7
- data/lib/synvert/core/rewriter/warning.rb +1 -1
- data/lib/synvert/core/version.rb +2 -2
- data/spec/spec_helper.rb +3 -6
- data/spec/support/parser_helper.rb +2 -0
- data/spec/synvert/core/engine/erb_spec.rb +32 -30
- data/spec/synvert/core/node_ext_spec.rb +57 -54
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +2 -0
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +10 -8
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +5 -3
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +3 -1
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +2 -0
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +17 -11
- data/spec/synvert/core/rewriter/action_spec.rb +2 -0
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +19 -9
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +23 -12
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +19 -9
- data/spec/synvert/core/rewriter/condition_spec.rb +2 -0
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +13 -10
- data/spec/synvert/core/rewriter/helper_spec.rb +36 -31
- data/spec/synvert/core/rewriter/instance_spec.rb +118 -66
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +10 -6
- data/spec/synvert/core/rewriter/scope/within_scope.rb +18 -9
- data/spec/synvert/core/rewriter/scope_spec.rb +2 -0
- data/spec/synvert/core/rewriter/warning_spec.rb +2 -0
- data/spec/synvert/core/rewriter_spec.rb +106 -73
- metadata +2 -4
- data/spec/synvert/core/configuration_spec.rb +0 -10
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Go to and change its scope to a child node.
|
@@ -18,6 +18,7 @@ module Synvert::Core
|
|
18
18
|
def process
|
19
19
|
current_node = @instance.current_node
|
20
20
|
return unless current_node
|
21
|
+
|
21
22
|
child_node = current_node.send @child_node_name
|
22
23
|
@instance.process_with_other_node child_node do
|
23
24
|
@instance.instance_eval &@block
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# WithinScope finds out nodes which match rules, then change its scope to matching node.
|
@@ -7,10 +7,12 @@ module Synvert::Core
|
|
7
7
|
#
|
8
8
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
9
9
|
# @param rules [Hash]
|
10
|
+
# @param options [Hash]
|
10
11
|
# @param block [Block]
|
11
|
-
def initialize(instance, rules, &block)
|
12
|
+
def initialize(instance, rules, options = { recursive: true }, &block)
|
12
13
|
@instance = instance
|
13
14
|
@rules = rules
|
15
|
+
@options = options
|
14
16
|
@block = block
|
15
17
|
end
|
16
18
|
|
@@ -19,12 +21,9 @@ module Synvert::Core
|
|
19
21
|
def process
|
20
22
|
current_node = @instance.current_node
|
21
23
|
return unless current_node
|
24
|
+
|
25
|
+
matching_nodes = find_matching_nodes(current_node)
|
22
26
|
@instance.process_with_node current_node do
|
23
|
-
matching_nodes = []
|
24
|
-
matching_nodes << current_node if current_node.match? @rules
|
25
|
-
current_node.recursive_children do |child_node|
|
26
|
-
matching_nodes << child_node if child_node.match? @rules
|
27
|
-
end
|
28
27
|
matching_nodes.each do |matching_node|
|
29
28
|
@instance.process_with_node matching_node do
|
30
29
|
@instance.instance_eval &@block
|
@@ -32,5 +31,22 @@ module Synvert::Core
|
|
32
31
|
end
|
33
32
|
end
|
34
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
|
35
51
|
end
|
36
52
|
end
|
data/lib/synvert/core/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
4
|
|
3
5
|
require 'synvert/core'
|
4
6
|
|
@@ -16,9 +18,4 @@ RSpec.configure do |config|
|
|
16
18
|
config.filter_run :focus
|
17
19
|
|
18
20
|
config.order = 'random'
|
19
|
-
|
20
|
-
config.before do
|
21
|
-
Synvert::Core::Configuration.instance.set :path, '.'
|
22
|
-
Synvert::Core::Configuration.instance.set :skip_files, []
|
23
|
-
end
|
24
21
|
end
|
@@ -1,39 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Synvert::Core
|
4
6
|
describe Engine::ERB do
|
5
|
-
it
|
6
|
-
source
|
7
|
-
<%content_for :head do%>
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
<%end%>
|
14
|
-
|
15
|
-
<%
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
%>
|
20
|
-
|
21
|
-
<% if User.current &&
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
<% 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 %>
|
34
36
|
EOF
|
35
37
|
encoded_source = Engine::ERB.encode(source)
|
36
|
-
buffer = Parser::Source::Buffer.new
|
38
|
+
buffer = Parser::Source::Buffer.new '(test)'
|
37
39
|
buffer.source = encoded_source
|
38
40
|
parser = Parser::CurrentRuby.new
|
39
41
|
parser.reset
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Parser::AST::Node do
|
@@ -101,13 +103,13 @@ describe Parser::AST::Node do
|
|
101
103
|
|
102
104
|
describe '#arguments' do
|
103
105
|
it 'gets for def node' do
|
104
|
-
node = parse(
|
105
|
-
expect(node.arguments.map { |argument| argument.to_source }).to eq [
|
106
|
+
node = parse('def test(foo, bar); foo + bar; end')
|
107
|
+
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
106
108
|
end
|
107
109
|
|
108
110
|
it 'gets for defs node' do
|
109
|
-
node = parse(
|
110
|
-
expect(node.arguments.map { |argument| argument.to_source }).to eq [
|
111
|
+
node = parse('def self.test(foo, bar); foo + bar; end')
|
112
|
+
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
111
113
|
end
|
112
114
|
|
113
115
|
it 'gets for block node' do
|
@@ -170,129 +172,129 @@ describe Parser::AST::Node do
|
|
170
172
|
end
|
171
173
|
end
|
172
174
|
|
173
|
-
describe
|
175
|
+
describe '#keys' do
|
174
176
|
it 'gets for hash node' do
|
175
177
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
176
178
|
expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
|
177
179
|
end
|
178
180
|
end
|
179
181
|
|
180
|
-
describe
|
182
|
+
describe '#values' do
|
181
183
|
it 'gets for hash node' do
|
182
184
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
183
185
|
expect(node.values).to eq [parse(':bar'), parse("'bar'")]
|
184
186
|
end
|
185
187
|
end
|
186
188
|
|
187
|
-
describe
|
188
|
-
it
|
189
|
-
node = parse(
|
189
|
+
describe '#has_key?' do
|
190
|
+
it 'gets true if key exists' do
|
191
|
+
node = parse('{:foo => :bar}')
|
190
192
|
expect(node.has_key?(:foo)).to be_truthy
|
191
193
|
end
|
192
194
|
|
193
|
-
it
|
194
|
-
node = parse(
|
195
|
+
it 'gets false if key does not exist' do
|
196
|
+
node = parse('{:foo => :bar}')
|
195
197
|
expect(node.has_key?('foo')).to be_falsey
|
196
198
|
end
|
197
199
|
end
|
198
200
|
|
199
|
-
describe
|
200
|
-
it
|
201
|
-
node = parse(
|
201
|
+
describe '#hash_value' do
|
202
|
+
it 'gets value of specified key' do
|
203
|
+
node = parse('{:foo => :bar}')
|
202
204
|
expect(node.hash_value(:foo)).to eq parse(':bar')
|
203
205
|
end
|
204
206
|
|
205
|
-
it
|
206
|
-
node = parse(
|
207
|
+
it 'gets nil if key does not exist' do
|
208
|
+
node = parse('{:foo => :bar}')
|
207
209
|
expect(node.hash_value(:bar)).to be_nil
|
208
210
|
end
|
209
211
|
end
|
210
212
|
|
211
|
-
describe
|
213
|
+
describe '#key' do
|
212
214
|
it 'gets for pair node' do
|
213
215
|
node = parse("{:foo => 'bar'}").children[0]
|
214
216
|
expect(node.key).to eq parse(':foo')
|
215
217
|
end
|
216
218
|
end
|
217
219
|
|
218
|
-
describe
|
220
|
+
describe '#value' do
|
219
221
|
it 'gets for hash node' do
|
220
222
|
node = parse("{:foo => 'bar'}").children[0]
|
221
223
|
expect(node.value).to eq parse("'bar'")
|
222
224
|
end
|
223
225
|
end
|
224
226
|
|
225
|
-
describe
|
227
|
+
describe '#condition' do
|
226
228
|
it 'gets for if node' do
|
227
229
|
node = parse('if defined?(Bundler); end')
|
228
230
|
expect(node.condition).to eq parse('defined?(Bundler)')
|
229
231
|
end
|
230
232
|
end
|
231
233
|
|
232
|
-
describe
|
234
|
+
describe '#left_value' do
|
233
235
|
it 'gets for masgn' do
|
234
|
-
node = parse(
|
236
|
+
node = parse('a, b = 1, 2')
|
235
237
|
expect(node.left_value.to_source).to eq 'a, b'
|
236
238
|
end
|
237
239
|
|
238
240
|
it 'gets for lvasgn' do
|
239
|
-
node = parse(
|
241
|
+
node = parse('a = 1')
|
240
242
|
expect(node.left_value).to eq :a
|
241
243
|
end
|
242
244
|
|
243
245
|
it 'gets for ivasgn' do
|
244
|
-
node = parse(
|
246
|
+
node = parse('@a = 1')
|
245
247
|
expect(node.left_value).to eq :@a
|
246
248
|
end
|
247
249
|
end
|
248
250
|
|
249
|
-
describe
|
251
|
+
describe '#right_value' do
|
250
252
|
it 'gets for masgn' do
|
251
|
-
node = parse(
|
253
|
+
node = parse('a, b = 1, 2')
|
252
254
|
expect(node.right_value).to eq parse('[1, 2]')
|
253
255
|
end
|
254
256
|
|
255
257
|
it 'gets for masgn' do
|
256
|
-
node = parse(
|
257
|
-
expect(node.right_value).to eq parse(
|
258
|
+
node = parse('a, b = params')
|
259
|
+
expect(node.right_value).to eq parse('params')
|
258
260
|
end
|
259
261
|
|
260
262
|
it 'gets for lvasgn' do
|
261
|
-
node = parse(
|
262
|
-
expect(node.right_value).to eq parse(
|
263
|
+
node = parse('a = 1')
|
264
|
+
expect(node.right_value).to eq parse('1')
|
263
265
|
end
|
264
266
|
|
265
267
|
it 'gets for ivasgn' do
|
266
|
-
node = parse(
|
267
|
-
expect(node.right_value).to eq parse(
|
268
|
+
node = parse('@a = 1')
|
269
|
+
expect(node.right_value).to eq parse('1')
|
268
270
|
end
|
269
271
|
end
|
270
272
|
|
271
|
-
describe
|
273
|
+
describe '#to_value' do
|
272
274
|
it 'gets for int' do
|
273
|
-
node = parse(
|
275
|
+
node = parse('1')
|
274
276
|
expect(node.to_value).to eq 1
|
275
277
|
end
|
276
278
|
|
277
279
|
it 'gets for string' do
|
278
280
|
node = parse("'str'")
|
279
|
-
expect(node.to_value).to eq
|
281
|
+
expect(node.to_value).to eq 'str'
|
280
282
|
end
|
281
283
|
|
282
284
|
it 'gets for symbol' do
|
283
|
-
node = parse(
|
285
|
+
node = parse(':str')
|
284
286
|
expect(node.to_value).to eq :str
|
285
287
|
end
|
286
288
|
|
287
289
|
it 'get for boolean' do
|
288
|
-
node = parse(
|
290
|
+
node = parse('true')
|
289
291
|
expect(node.to_value).to be_truthy
|
290
|
-
node = parse(
|
292
|
+
node = parse('false')
|
291
293
|
expect(node.to_value).to be_falsey
|
292
294
|
end
|
293
295
|
|
294
296
|
it 'get for range' do
|
295
|
-
node = parse(
|
297
|
+
node = parse('(1..10)')
|
296
298
|
expect(node.to_value).to eq (1..10)
|
297
299
|
end
|
298
300
|
|
@@ -382,13 +384,13 @@ describe Parser::AST::Node do
|
|
382
384
|
it 'matches arguments any' do
|
383
385
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
384
386
|
node = parse(source)
|
385
|
-
expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
|
387
|
+
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
386
388
|
end
|
387
389
|
|
388
390
|
it 'matches not' do
|
389
391
|
source = 'class Synvert; end'
|
390
392
|
node = parse(source)
|
391
|
-
expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
|
393
|
+
expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
|
392
394
|
end
|
393
395
|
end
|
394
396
|
|
@@ -413,24 +415,25 @@ describe Parser::AST::Node do
|
|
413
415
|
it 'rewrites for ArgumentsNode' do
|
414
416
|
source = 'test { |a, b| }'
|
415
417
|
node = parse(source)
|
416
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
418
|
+
expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
|
417
419
|
end
|
418
420
|
|
419
|
-
it 'rewrites array with multi line given as argument for method'do
|
420
|
-
source =
|
421
|
-
long_name_method([
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
])
|
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
|
+
])
|
426
428
|
EOS
|
429
|
+
|
427
430
|
node = parse(source)
|
428
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
429
|
-
[
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
]
|
431
|
+
expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
|
432
|
+
[
|
433
|
+
1,
|
434
|
+
2,
|
435
|
+
3
|
436
|
+
]
|
434
437
|
EOS
|
435
438
|
end
|
436
439
|
end
|