synvert-core 0.41.2 → 0.44.1
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 +15 -0
- data/lib/synvert/core/node_ext.rb +20 -9
- data/lib/synvert/core/rewriter.rb +3 -1
- data/lib/synvert/core/rewriter/action/delete_action.rb +2 -2
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +53 -10
- data/spec/synvert/core/rewriter/instance_spec.rb +48 -56
- 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: 252c2e9347770f9363ca9d317d574fa6e1d9faad4826d2853816a669ad13dab1
|
|
4
|
+
data.tar.gz: 1d19aac0f1dac17a84dd4a85bcbb8606d9391afeb9c4493eff516cf2b5ed948f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 795910a3044cc4cdbf02e0412a5784235b7c8fc0892a1243cf58677c53d09e1cdf097d7f56cf8f818864de0eab6ec65d829c59e2ba246e1d1150bdd3fd5e2b40
|
|
7
|
+
data.tar.gz: fc2f006fd4b269f598cd23d05ebfcf72b098ce4a7ccfaa1a5d6004517d058caabea1005e999d2ccaf92a73bae34941b4bc8b37124db98ae90a71730c7e749098
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
# 0.44.1 (2021-07-19)
|
|
4
|
+
|
|
5
|
+
* Handle `nil` child node for `begin_pos` and `end_pos`
|
|
6
|
+
|
|
7
|
+
# 0.44.0 (2021-07-19)
|
|
8
|
+
|
|
9
|
+
* Return rewrtier after executing snippet
|
|
10
|
+
* `left_value` and `right_value` support `or_asgn` node
|
|
11
|
+
* `child_node_range` supports send `parentheses`
|
|
12
|
+
|
|
13
|
+
# 0.42.0 (2021-07-11)
|
|
14
|
+
|
|
15
|
+
* Match string with quote
|
|
16
|
+
* `match_value?` returns true if actual and expected are the same
|
|
17
|
+
|
|
3
18
|
## 0.41.0 (2021-06-24)
|
|
4
19
|
|
|
5
20
|
* Remove unused autoindent option
|
|
@@ -217,6 +217,8 @@ module Parser::AST
|
|
|
217
217
|
def left_value
|
|
218
218
|
if %i[masgn lvasgn ivasgn].include? type
|
|
219
219
|
children[0]
|
|
220
|
+
elsif :or_asgn == type
|
|
221
|
+
children[0].children[0]
|
|
220
222
|
else
|
|
221
223
|
raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}"
|
|
222
224
|
end
|
|
@@ -227,7 +229,7 @@ module Parser::AST
|
|
|
227
229
|
# @return [Array<Parser::AST::Node>] variable nodes.
|
|
228
230
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
|
229
231
|
def right_value
|
|
230
|
-
if %i[masgn lvasgn ivasgn].include? type
|
|
232
|
+
if %i[masgn lvasgn ivasgn or_asgn].include? type
|
|
231
233
|
children[1]
|
|
232
234
|
else
|
|
233
235
|
raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
|
|
@@ -240,7 +242,7 @@ module Parser::AST
|
|
|
240
242
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
|
241
243
|
def to_value
|
|
242
244
|
case type
|
|
243
|
-
when :int, :str, :sym
|
|
245
|
+
when :int, :float, :str, :sym
|
|
244
246
|
children.last
|
|
245
247
|
when :true
|
|
246
248
|
true
|
|
@@ -253,7 +255,7 @@ module Parser::AST
|
|
|
253
255
|
when :begin
|
|
254
256
|
children.first.to_value
|
|
255
257
|
else
|
|
256
|
-
|
|
258
|
+
self
|
|
257
259
|
end
|
|
258
260
|
end
|
|
259
261
|
|
|
@@ -360,6 +362,10 @@ module Parser::AST
|
|
|
360
362
|
else
|
|
361
363
|
loc.selector
|
|
362
364
|
end
|
|
365
|
+
when %i[send parentheses]
|
|
366
|
+
if loc.begin && loc.end
|
|
367
|
+
Parser::Source::Range.new('(string)', loc.begin.begin_pos, loc.end.end_pos)
|
|
368
|
+
end
|
|
363
369
|
else
|
|
364
370
|
if respond_to?(child_name)
|
|
365
371
|
child_node = send(child_name)
|
|
@@ -504,10 +510,11 @@ module Parser::AST
|
|
|
504
510
|
end
|
|
505
511
|
when String
|
|
506
512
|
if actual.is_a?(Parser::AST::Node)
|
|
513
|
+
return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
|
|
507
514
|
actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
|
|
508
515
|
actual.to_source[1...-1] == expected
|
|
509
516
|
else
|
|
510
|
-
actual.to_s == expected
|
|
517
|
+
actual.to_s == expected || wrap_quote(actual.to_s) == expected
|
|
511
518
|
end
|
|
512
519
|
when Regexp
|
|
513
520
|
if actual.is_a?(Parser::AST::Node)
|
|
@@ -565,11 +572,7 @@ module Parser::AST
|
|
|
565
572
|
# @param multi_keys [Array<Symbol>]
|
|
566
573
|
# @return [Object] actual value.
|
|
567
574
|
def actual_value(node, multi_keys)
|
|
568
|
-
multi_keys.inject(node) { |n, key|
|
|
569
|
-
if n
|
|
570
|
-
key == :source ? n.send(key) : n.send(key)
|
|
571
|
-
end
|
|
572
|
-
}
|
|
575
|
+
multi_keys.inject(node) { |n, key| n.send(key) if n }
|
|
573
576
|
end
|
|
574
577
|
|
|
575
578
|
# Get expected value from rules.
|
|
@@ -580,5 +583,13 @@ module Parser::AST
|
|
|
580
583
|
def expected_value(rules, multi_keys)
|
|
581
584
|
multi_keys.inject(rules) { |o, key| o[key] }
|
|
582
585
|
end
|
|
586
|
+
|
|
587
|
+
def wrap_quote(string)
|
|
588
|
+
if string.include?("'")
|
|
589
|
+
"\"#{string}\""
|
|
590
|
+
else
|
|
591
|
+
"'#{string}'"
|
|
592
|
+
end
|
|
593
|
+
end
|
|
583
594
|
end
|
|
584
595
|
end
|
|
@@ -52,7 +52,9 @@ module Synvert::Core
|
|
|
52
52
|
#
|
|
53
53
|
# @param block [Block] a block defines the behaviors of the rewriter.
|
|
54
54
|
def execute(&block)
|
|
55
|
-
Rewriter.new('', '', &block)
|
|
55
|
+
rewriter = Rewriter.new('', '', &block)
|
|
56
|
+
rewriter.process
|
|
57
|
+
rewriter
|
|
56
58
|
end
|
|
57
59
|
|
|
58
60
|
# Register a rewriter with its group and name.
|
|
@@ -12,14 +12,14 @@ module Synvert::Core
|
|
|
12
12
|
#
|
|
13
13
|
# @return [Integer] begin position.
|
|
14
14
|
def begin_pos
|
|
15
|
-
@selectors.map { |selector| @node.child_node_range(selector)
|
|
15
|
+
@selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
# End position of code to replace.
|
|
19
19
|
#
|
|
20
20
|
# @return [Integer] end position.
|
|
21
21
|
def end_pos
|
|
22
|
-
@selectors.map { |selector| @node.child_node_range(selector)
|
|
22
|
+
@selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
# The rewritten code, always empty string.
|
data/lib/synvert/core/version.rb
CHANGED
|
@@ -261,6 +261,11 @@ describe Parser::AST::Node do
|
|
|
261
261
|
node = parse('@a = 1')
|
|
262
262
|
expect(node.left_value).to eq :@a
|
|
263
263
|
end
|
|
264
|
+
|
|
265
|
+
it 'gets for or_asgn' do
|
|
266
|
+
node = parse('a ||= 1')
|
|
267
|
+
expect(node.left_value).to eq :a
|
|
268
|
+
end
|
|
264
269
|
end
|
|
265
270
|
|
|
266
271
|
describe '#right_value' do
|
|
@@ -283,6 +288,11 @@ describe Parser::AST::Node do
|
|
|
283
288
|
node = parse('@a = 1')
|
|
284
289
|
expect(node.right_value).to eq parse('1')
|
|
285
290
|
end
|
|
291
|
+
|
|
292
|
+
it 'gets for or_asgn' do
|
|
293
|
+
node = parse('a ||= 1')
|
|
294
|
+
expect(node.right_value).to eq parse('1')
|
|
295
|
+
end
|
|
286
296
|
end
|
|
287
297
|
|
|
288
298
|
describe '#to_value' do
|
|
@@ -291,6 +301,11 @@ describe Parser::AST::Node do
|
|
|
291
301
|
expect(node.to_value).to eq 1
|
|
292
302
|
end
|
|
293
303
|
|
|
304
|
+
it 'gets for float' do
|
|
305
|
+
node = parse('1.5')
|
|
306
|
+
expect(node.to_value).to eq 1.5
|
|
307
|
+
end
|
|
308
|
+
|
|
294
309
|
it 'gets for string' do
|
|
295
310
|
node = parse("'str'")
|
|
296
311
|
expect(node.to_value).to eq 'str'
|
|
@@ -385,11 +400,6 @@ describe Parser::AST::Node do
|
|
|
385
400
|
end
|
|
386
401
|
|
|
387
402
|
describe '#match?' do
|
|
388
|
-
let(:instance) {
|
|
389
|
-
rewriter = Synvert::Rewriter.new('foo', 'bar')
|
|
390
|
-
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
|
391
|
-
}
|
|
392
|
-
|
|
393
403
|
it 'matches class name' do
|
|
394
404
|
source = 'class Synvert; end'
|
|
395
405
|
node = parse(source)
|
|
@@ -414,18 +424,42 @@ describe Parser::AST::Node do
|
|
|
414
424
|
expect(node).to be_match(type: 'send', arguments: [0])
|
|
415
425
|
end
|
|
416
426
|
|
|
427
|
+
it 'matches assign float' do
|
|
428
|
+
source = 'at_least(1.5)'
|
|
429
|
+
node = parse(source)
|
|
430
|
+
expect(node).to be_match(type: 'send', arguments: [1.5])
|
|
431
|
+
end
|
|
432
|
+
|
|
417
433
|
it 'matches arguments with string' do
|
|
418
434
|
source = 'params["user"]'
|
|
419
435
|
node = parse(source)
|
|
420
436
|
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
|
|
421
437
|
end
|
|
422
438
|
|
|
439
|
+
it 'matches arguments with string 2' do
|
|
440
|
+
source = 'params["user"]'
|
|
441
|
+
node = parse(source)
|
|
442
|
+
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ["'user'"])
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
it 'matches arguments with string 3' do
|
|
446
|
+
source = "{ notice: 'Welcome' }"
|
|
447
|
+
node = parse(source)
|
|
448
|
+
expect(node).to be_match(type: 'hash', notice_value: "'Welcome'")
|
|
449
|
+
end
|
|
450
|
+
|
|
423
451
|
it 'matches arguments any' do
|
|
424
452
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
|
425
453
|
node = parse(source)
|
|
426
454
|
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
|
427
455
|
end
|
|
428
456
|
|
|
457
|
+
it 'matches arguments with nested hash' do
|
|
458
|
+
source = '{ user_id: user.id }'
|
|
459
|
+
node = parse(source)
|
|
460
|
+
expect(node).to be_match(type: 'hash', user_id_value: { type: 'send', receiver: { type: 'send', message: 'user' }, message: 'id' })
|
|
461
|
+
end
|
|
462
|
+
|
|
429
463
|
it 'matches arguments contain' do
|
|
430
464
|
source = 'def slow(foo, bar, &block); end'
|
|
431
465
|
node = parse(source)
|
|
@@ -590,15 +624,24 @@ describe Parser::AST::Node do
|
|
|
590
624
|
range = node.child_node_range(:arguments)
|
|
591
625
|
expect(range).to be_nil
|
|
592
626
|
end
|
|
627
|
+
|
|
628
|
+
it 'checks parentheses' do
|
|
629
|
+
node = parse('foo.bar(test)')
|
|
630
|
+
range = node.child_node_range(:parentheses)
|
|
631
|
+
expect(range.to_range).to eq(7...13)
|
|
632
|
+
|
|
633
|
+
node = parse('foobar(test)')
|
|
634
|
+
range = node.child_node_range(:parentheses)
|
|
635
|
+
expect(range.to_range).to eq(6...12)
|
|
636
|
+
|
|
637
|
+
node = parse('foo.bar')
|
|
638
|
+
range = node.child_node_range(:parentheses)
|
|
639
|
+
expect(range).to be_nil
|
|
640
|
+
end
|
|
593
641
|
end
|
|
594
642
|
end
|
|
595
643
|
|
|
596
644
|
describe '#rewritten_source' do
|
|
597
|
-
let(:instance) {
|
|
598
|
-
rewriter = Synvert::Rewriter.new('foo', 'bar')
|
|
599
|
-
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
|
600
|
-
}
|
|
601
|
-
|
|
602
645
|
it 'does not rewrite with unknown method' do
|
|
603
646
|
source = 'class Synvert; end'
|
|
604
647
|
node = parse(source)
|
|
@@ -155,22 +155,20 @@ module Synvert::Core
|
|
|
155
155
|
replace_with 'create {{arguments}}'
|
|
156
156
|
end
|
|
157
157
|
end
|
|
158
|
-
input =
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
end
|
|
173
|
-
"
|
|
158
|
+
input = <<~EOS
|
|
159
|
+
it 'uses factory_girl' do
|
|
160
|
+
user = FactoryGirl.create :user
|
|
161
|
+
post = FactoryGirl.create :post, user: user
|
|
162
|
+
assert post.valid?
|
|
163
|
+
end
|
|
164
|
+
EOS
|
|
165
|
+
output = <<~EOS
|
|
166
|
+
it 'uses factory_girl' do
|
|
167
|
+
user = create :user
|
|
168
|
+
post = create :post, user: user
|
|
169
|
+
assert post.valid?
|
|
170
|
+
end
|
|
171
|
+
EOS
|
|
174
172
|
expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
|
|
175
173
|
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
|
|
176
174
|
expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
|
|
@@ -186,18 +184,16 @@ end
|
|
|
186
184
|
end
|
|
187
185
|
end
|
|
188
186
|
end
|
|
189
|
-
input =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
end
|
|
200
|
-
'
|
|
187
|
+
input = <<~EOS
|
|
188
|
+
RSpec.configure do |config|
|
|
189
|
+
config.include FactoryGirl::Syntax::Methods
|
|
190
|
+
end
|
|
191
|
+
EOS
|
|
192
|
+
output = <<~EOS
|
|
193
|
+
RSpec.configure do |config|
|
|
194
|
+
config.include FactoryGirl::Syntax::Methods
|
|
195
|
+
end
|
|
196
|
+
EOS
|
|
201
197
|
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
|
|
202
198
|
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
|
|
203
199
|
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
|
@@ -213,18 +209,16 @@ end
|
|
|
213
209
|
end
|
|
214
210
|
end
|
|
215
211
|
end
|
|
216
|
-
input =
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
end
|
|
227
|
-
'
|
|
212
|
+
input = <<~EOS
|
|
213
|
+
RSpec.configure do |config|
|
|
214
|
+
config.include FactoryGirl::Syntax::Methods
|
|
215
|
+
end
|
|
216
|
+
EOS
|
|
217
|
+
output = <<~EOS
|
|
218
|
+
RSpec.configure do |config|
|
|
219
|
+
config.include FactoryGirl::Syntax::Methods
|
|
220
|
+
end
|
|
221
|
+
EOS
|
|
228
222
|
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
|
|
229
223
|
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
|
|
230
224
|
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
|
@@ -239,22 +233,20 @@ end
|
|
|
239
233
|
replace_with 'create {{arguments}}'
|
|
240
234
|
end
|
|
241
235
|
end
|
|
242
|
-
input =
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
end
|
|
257
|
-
"
|
|
236
|
+
input = <<~EOS
|
|
237
|
+
it 'uses factory_girl' do
|
|
238
|
+
user = FactoryGirl.create :user
|
|
239
|
+
post = FactoryGirl.create :post, user: user
|
|
240
|
+
assert post.valid?
|
|
241
|
+
end
|
|
242
|
+
EOS
|
|
243
|
+
output = <<~EOS
|
|
244
|
+
it 'uses factory_girl' do
|
|
245
|
+
user = create :user
|
|
246
|
+
post = create :post, user: user
|
|
247
|
+
assert post.valid?
|
|
248
|
+
end
|
|
249
|
+
EOS
|
|
258
250
|
expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
|
|
259
251
|
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
|
|
260
252
|
expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
|
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.44.1
|
|
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-07-
|
|
11
|
+
date: 2021-07-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|