synvert-core 0.39.0 → 0.42.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 +15 -1
- data/lib/synvert/core/node_ext.rb +16 -9
- data/lib/synvert/core/rewriter/action.rb +1 -4
- data/lib/synvert/core/rewriter/action/insert_action.rb +14 -23
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +4 -4
- data/lib/synvert/core/rewriter/instance.rb +11 -15
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +29 -10
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +16 -58
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +12 -12
- data/spec/synvert/core/rewriter/instance_spec.rb +59 -68
- metadata +2 -3
- data/lib/synvert/core/rewriter/action/add_action.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19c77660c79cc18a6c2d720d7a8bada136dc0397a2cc73610cc87d3412ac5306
|
4
|
+
data.tar.gz: '083cc96fa1b41d83a4bd5c3512440ec88223bbc67969aca2d03dd58f87a26b24'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6403332404a070851c4817321702097cbff8cc34f75bc23d8dba236be878c83948012f79ba36ed51bea4e275f61f4adf4a5b638e35b0c8fd88dfd370b720854f
|
7
|
+
data.tar.gz: dc6bbc40dc208bb9ed9b8f9003b7a11e59ece0e79fb8db3cc4466f7c5b6f70574f71a5c7e4e161e762498854954293105b2023a996802b2906df04af51286f9a
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,22 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
# 0.42.0 (2021-07-11)
|
4
|
+
|
5
|
+
* Match string with quote
|
6
|
+
* `match_value?` returns true if actual and expected are the same
|
7
|
+
|
8
|
+
## 0.41.0 (2021-06-24)
|
9
|
+
|
10
|
+
* Remove unused autoindent option
|
11
|
+
* Add `insert 'xxx', at: 'beginning'`
|
12
|
+
|
13
|
+
## 0.40.0 (2021-06-23)
|
14
|
+
|
15
|
+
* Rewrite `insert` action
|
16
|
+
|
3
17
|
## 0.39.0 (2021-06-23)
|
4
18
|
|
5
|
-
* Add `prepend`
|
19
|
+
* Add `prepend` action instead of `insert`
|
6
20
|
|
7
21
|
## 0.38.0 (2021-06-21)
|
8
22
|
|
@@ -240,7 +240,7 @@ module Parser::AST
|
|
240
240
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
241
241
|
def to_value
|
242
242
|
case type
|
243
|
-
when :int, :str, :sym
|
243
|
+
when :int, :float, :str, :sym
|
244
244
|
children.last
|
245
245
|
when :true
|
246
246
|
true
|
@@ -253,7 +253,7 @@ module Parser::AST
|
|
253
253
|
when :begin
|
254
254
|
children.first.to_value
|
255
255
|
else
|
256
|
-
|
256
|
+
self
|
257
257
|
end
|
258
258
|
end
|
259
259
|
|
@@ -388,7 +388,7 @@ module Parser::AST
|
|
388
388
|
end
|
389
389
|
|
390
390
|
raise Synvert::Core::MethodNotSupported,
|
391
|
-
"child_node_range is not handled for #{
|
391
|
+
"child_node_range is not handled for #{debug_info}, child_name: #{child_name}"
|
392
392
|
end
|
393
393
|
end
|
394
394
|
|
@@ -493,6 +493,8 @@ module Parser::AST
|
|
493
493
|
# @return [Integer] -1, 0 or 1.
|
494
494
|
# @raise [Synvert::Core::MethodNotSupported] if expected class is not supported.
|
495
495
|
def match_value?(actual, expected)
|
496
|
+
return true if actual == expected
|
497
|
+
|
496
498
|
case expected
|
497
499
|
when Symbol
|
498
500
|
if actual.is_a?(Parser::AST::Node)
|
@@ -502,10 +504,11 @@ module Parser::AST
|
|
502
504
|
end
|
503
505
|
when String
|
504
506
|
if actual.is_a?(Parser::AST::Node)
|
507
|
+
return true if (Parser::CurrentRuby.parse(expected) == actual rescue nil)
|
505
508
|
actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
|
506
509
|
actual.to_source[1...-1] == expected
|
507
510
|
else
|
508
|
-
actual.to_s == expected
|
511
|
+
actual.to_s == expected || wrap_quote(actual.to_s) == expected
|
509
512
|
end
|
510
513
|
when Regexp
|
511
514
|
if actual.is_a?(Parser::AST::Node)
|
@@ -563,11 +566,7 @@ module Parser::AST
|
|
563
566
|
# @param multi_keys [Array<Symbol>]
|
564
567
|
# @return [Object] actual value.
|
565
568
|
def actual_value(node, multi_keys)
|
566
|
-
multi_keys.inject(node) { |n, key|
|
567
|
-
if n
|
568
|
-
key == :source ? n.send(key) : n.send(key)
|
569
|
-
end
|
570
|
-
}
|
569
|
+
multi_keys.inject(node) { |n, key| n.send(key) if n }
|
571
570
|
end
|
572
571
|
|
573
572
|
# Get expected value from rules.
|
@@ -578,5 +577,13 @@ module Parser::AST
|
|
578
577
|
def expected_value(rules, multi_keys)
|
579
578
|
multi_keys.inject(rules) { |o, key| o[key] }
|
580
579
|
end
|
580
|
+
|
581
|
+
def wrap_quote(string)
|
582
|
+
if string.include?("'")
|
583
|
+
"\"#{string}\""
|
584
|
+
else
|
585
|
+
"'#{string}'"
|
586
|
+
end
|
587
|
+
end
|
581
588
|
end
|
582
589
|
end
|
@@ -3,18 +3,15 @@
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Action defines rewriter action, add, replace or remove code.
|
5
5
|
class Rewriter::Action
|
6
|
-
DEFAULT_OPTIONS = { autoindent: true }.freeze
|
7
6
|
DEFAULT_INDENT = 2
|
8
7
|
|
9
8
|
# Initialize an action.
|
10
9
|
#
|
11
10
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
12
11
|
# @param code [String] new code to add, replace or remove.
|
13
|
-
|
14
|
-
def initialize(instance, code, options = {})
|
12
|
+
def initialize(instance, code)
|
15
13
|
@instance = instance
|
16
14
|
@code = code
|
17
|
-
@options = DEFAULT_OPTIONS.merge(options)
|
18
15
|
@node = @instance.current_node
|
19
16
|
end
|
20
17
|
|
@@ -1,25 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
|
-
#
|
4
|
+
# AddAction to add code to the node.
|
5
5
|
class Rewriter::InsertAction < Rewriter::Action
|
6
|
-
|
6
|
+
def initialize(instance, code, at:)
|
7
|
+
@instance = instance
|
8
|
+
@code = code
|
9
|
+
@at = at
|
10
|
+
@node = @instance.current_node
|
11
|
+
end
|
7
12
|
|
8
13
|
# Begin position to insert code.
|
9
14
|
#
|
10
15
|
# @return [Integer] begin position.
|
11
16
|
def begin_pos
|
12
|
-
|
13
|
-
|
14
|
-
if @node.children[1].children.empty?
|
15
|
-
@node.children[0].loc.expression.end_pos + DO_LENGTH
|
16
|
-
else
|
17
|
-
@node.children[1].loc.expression.end_pos
|
18
|
-
end
|
19
|
-
when :class
|
20
|
-
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
|
17
|
+
if @at == 'end'
|
18
|
+
@node.loc.expression.end_pos
|
21
19
|
else
|
22
|
-
@node.
|
20
|
+
@node.loc.expression.begin_pos
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
@@ -30,18 +28,11 @@ module Synvert::Core
|
|
30
28
|
begin_pos
|
31
29
|
end
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
# Indent of the node.
|
31
|
+
# The rewritten source code.
|
36
32
|
#
|
37
|
-
# @
|
38
|
-
|
39
|
-
|
40
|
-
if %i[block class].include? node.type
|
41
|
-
' ' * (node.indent + DEFAULT_INDENT)
|
42
|
-
else
|
43
|
-
' ' * node.indent
|
44
|
-
end
|
33
|
+
# @return [String] rewritten code.
|
34
|
+
def rewritten_code
|
35
|
+
rewritten_source
|
45
36
|
end
|
46
37
|
end
|
47
38
|
end
|
@@ -21,11 +21,11 @@ module Synvert::Core
|
|
21
21
|
#
|
22
22
|
# @return [String] rewritten code.
|
23
23
|
def rewritten_code
|
24
|
-
if rewritten_source.
|
24
|
+
if rewritten_source.include?("\n")
|
25
25
|
new_code = []
|
26
|
-
rewritten_source.split("\n").each_with_index
|
27
|
-
new_code << (index == 0
|
28
|
-
|
26
|
+
rewritten_source.split("\n").each_with_index do |line, index|
|
27
|
+
new_code << (index == 0 ? line : indent(@node) + line)
|
28
|
+
end
|
29
29
|
new_code.join("\n")
|
30
30
|
else
|
31
31
|
rewritten_source
|
@@ -219,45 +219,41 @@ module Synvert::Core
|
|
219
219
|
# append the code to the bottom of current node body.
|
220
220
|
#
|
221
221
|
# @param code [String] code need to be appended.
|
222
|
-
|
223
|
-
|
224
|
-
@actions << Rewriter::AppendAction.new(self, code, options)
|
222
|
+
def append(code)
|
223
|
+
@actions << Rewriter::AppendAction.new(self, code)
|
225
224
|
end
|
226
225
|
|
227
226
|
# Parse prepend dsl, it creates a [Synvert::Core::Rewriter::PrependAction] to
|
228
227
|
# prepend the code to the top of current node body.
|
229
228
|
#
|
230
229
|
# @param code [String] code need to be prepended.
|
231
|
-
|
232
|
-
|
233
|
-
@actions << Rewriter::PrependAction.new(self, code, options)
|
230
|
+
def prepend(code)
|
231
|
+
@actions << Rewriter::PrependAction.new(self, code)
|
234
232
|
end
|
235
233
|
|
236
234
|
# Parse insert dsl, it creates a [Synvert::Core::Rewriter::InsertAction] to
|
237
235
|
# insert the code to the top of current node body.
|
238
236
|
#
|
239
237
|
# @param code [String] code need to be inserted.
|
240
|
-
# @param
|
241
|
-
def insert(code,
|
242
|
-
@actions << Rewriter::InsertAction.new(self, code,
|
238
|
+
# @param at [String] insert position, beginning or end, end is the default.
|
239
|
+
def insert(code, at: 'end')
|
240
|
+
@actions << Rewriter::InsertAction.new(self, code, at: at)
|
243
241
|
end
|
244
242
|
|
245
243
|
# Parse insert_after dsl, it creates a [Synvert::Core::Rewriter::InsertAfterAction] to
|
246
244
|
# insert the code next to the current node.
|
247
245
|
#
|
248
246
|
# @param code [String] code need to be inserted.
|
249
|
-
|
250
|
-
|
251
|
-
@actions << Rewriter::InsertAfterAction.new(self, node, options)
|
247
|
+
def insert_after(node)
|
248
|
+
@actions << Rewriter::InsertAfterAction.new(self, node)
|
252
249
|
end
|
253
250
|
|
254
251
|
# Parse replace_with dsl, it creates a [Synvert::Core::Rewriter::ReplaceWithAction] to
|
255
252
|
# replace current node with code.
|
256
253
|
#
|
257
254
|
# @param code [String] code need to be replaced with.
|
258
|
-
|
259
|
-
|
260
|
-
@actions << Rewriter::ReplaceWithAction.new(self, code, options)
|
255
|
+
def replace_with(code)
|
256
|
+
@actions << Rewriter::ReplaceWithAction.new(self, code)
|
261
257
|
end
|
262
258
|
|
263
259
|
# Parse replace with dsl, it creates a [Synvert::Core::Rewriter::ReplaceAction] to
|
data/lib/synvert/core/version.rb
CHANGED
@@ -291,6 +291,11 @@ describe Parser::AST::Node do
|
|
291
291
|
expect(node.to_value).to eq 1
|
292
292
|
end
|
293
293
|
|
294
|
+
it 'gets for float' do
|
295
|
+
node = parse('1.5')
|
296
|
+
expect(node.to_value).to eq 1.5
|
297
|
+
end
|
298
|
+
|
294
299
|
it 'gets for string' do
|
295
300
|
node = parse("'str'")
|
296
301
|
expect(node.to_value).to eq 'str'
|
@@ -385,11 +390,6 @@ describe Parser::AST::Node do
|
|
385
390
|
end
|
386
391
|
|
387
392
|
describe '#match?' do
|
388
|
-
let(:instance) {
|
389
|
-
rewriter = Synvert::Rewriter.new('foo', 'bar')
|
390
|
-
Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
|
391
|
-
}
|
392
|
-
|
393
393
|
it 'matches class name' do
|
394
394
|
source = 'class Synvert; end'
|
395
395
|
node = parse(source)
|
@@ -414,18 +414,42 @@ describe Parser::AST::Node do
|
|
414
414
|
expect(node).to be_match(type: 'send', arguments: [0])
|
415
415
|
end
|
416
416
|
|
417
|
+
it 'matches assign float' do
|
418
|
+
source = 'at_least(1.5)'
|
419
|
+
node = parse(source)
|
420
|
+
expect(node).to be_match(type: 'send', arguments: [1.5])
|
421
|
+
end
|
422
|
+
|
417
423
|
it 'matches arguments with string' do
|
418
424
|
source = 'params["user"]'
|
419
425
|
node = parse(source)
|
420
426
|
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
|
421
427
|
end
|
422
428
|
|
429
|
+
it 'matches arguments with string 2' do
|
430
|
+
source = 'params["user"]'
|
431
|
+
node = parse(source)
|
432
|
+
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ["'user'"])
|
433
|
+
end
|
434
|
+
|
435
|
+
it 'matches arguments with string 3' do
|
436
|
+
source = "{ notice: 'Welcome' }"
|
437
|
+
node = parse(source)
|
438
|
+
expect(node).to be_match(type: 'hash', notice_value: "'Welcome'")
|
439
|
+
end
|
440
|
+
|
423
441
|
it 'matches arguments any' do
|
424
442
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
425
443
|
node = parse(source)
|
426
444
|
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
427
445
|
end
|
428
446
|
|
447
|
+
it 'matches arguments with nested hash' do
|
448
|
+
source = '{ user_id: user.id }'
|
449
|
+
node = parse(source)
|
450
|
+
expect(node).to be_match(type: 'hash', user_id_value: { type: 'send', receiver: { type: 'send', message: 'user' }, message: 'id' })
|
451
|
+
end
|
452
|
+
|
429
453
|
it 'matches arguments contain' do
|
430
454
|
source = 'def slow(foo, bar, &block); end'
|
431
455
|
node = parse(source)
|
@@ -594,11 +618,6 @@ describe Parser::AST::Node do
|
|
594
618
|
end
|
595
619
|
|
596
620
|
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
621
|
it 'does not rewrite with unknown method' do
|
603
622
|
source = 'class Synvert; end'
|
604
623
|
node = parse(source)
|
@@ -4,87 +4,45 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::InsertAction do
|
7
|
-
|
7
|
+
context 'at end' do
|
8
8
|
subject {
|
9
|
-
source = "
|
10
|
-
|
11
|
-
instance = double(current_node:
|
12
|
-
Rewriter::InsertAction.new(instance, '
|
9
|
+
source = " User.where(username: 'Richard')"
|
10
|
+
node = Parser::CurrentRuby.parse(source)
|
11
|
+
instance = double(current_node: node)
|
12
|
+
Rewriter::InsertAction.new(instance, '.first', at: 'end')
|
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 " User.where(username: 'Richard')".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 " User.where(username: 'Richard')".length
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'gets rewritten_code' do
|
24
|
-
expect(subject.rewritten_code).to eq
|
24
|
+
expect(subject.rewritten_code).to eq '.first'
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
context 'at beginning' do
|
29
29
|
subject {
|
30
|
-
source = "
|
31
|
-
|
32
|
-
instance = double(current_node:
|
33
|
-
Rewriter::InsertAction.new(instance, '
|
30
|
+
source = " open('http://test.com')"
|
31
|
+
node = Parser::CurrentRuby.parse(source)
|
32
|
+
instance = double(current_node: node)
|
33
|
+
Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning')
|
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 2
|
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 2
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'gets rewritten_code' do
|
45
|
-
expect(subject.rewritten_code).to eq
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'class node without superclass' do
|
50
|
-
subject {
|
51
|
-
source = "class User\n has_many :posts\nend"
|
52
|
-
class_node = Parser::CurrentRuby.parse(source)
|
53
|
-
instance = double(current_node: class_node)
|
54
|
-
Rewriter::InsertAction.new(instance, 'include Deletable')
|
55
|
-
}
|
56
|
-
|
57
|
-
it 'gets begin_pos' do
|
58
|
-
expect(subject.begin_pos).to eq 'class User'.length
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'gets end_pos' do
|
62
|
-
expect(subject.end_pos).to eq 'class User'.length
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'gets rewritten_code' do
|
66
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe 'class node with superclass' do
|
71
|
-
subject {
|
72
|
-
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
73
|
-
class_node = Parser::CurrentRuby.parse(source)
|
74
|
-
instance = double(current_node: class_node)
|
75
|
-
Rewriter::InsertAction.new(instance, 'include Deletable')
|
76
|
-
}
|
77
|
-
|
78
|
-
it 'gets begin_pos' do
|
79
|
-
expect(subject.begin_pos).to eq 'class User < ActionRecord::Base'.length
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'gets end_pos' do
|
83
|
-
expect(subject.end_pos).to eq 'class User < ActionRecord::Base'.length
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'gets rewritten_code' do
|
87
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
45
|
+
expect(subject.rewritten_code).to eq 'URI.'
|
88
46
|
end
|
89
47
|
end
|
90
48
|
end
|
@@ -30,14 +30,12 @@ module Synvert::Core
|
|
30
30
|
source = ' its(:size) { should == 1 }'
|
31
31
|
send_node = Parser::CurrentRuby.parse(source)
|
32
32
|
instance = double(current_node: send_node)
|
33
|
-
Rewriter::ReplaceWithAction.new(
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
autoindent: false
|
40
|
-
)
|
33
|
+
Rewriter::ReplaceWithAction.new(instance, <<~EOS)
|
34
|
+
describe '#size' do
|
35
|
+
subject { super().size }
|
36
|
+
it { {{body}} }
|
37
|
+
end
|
38
|
+
EOS
|
41
39
|
}
|
42
40
|
|
43
41
|
it 'gets begin_pos' do
|
@@ -49,10 +47,12 @@ end",
|
|
49
47
|
end
|
50
48
|
|
51
49
|
it 'gets rewritten_code' do
|
52
|
-
expect(subject.rewritten_code).to eq
|
53
|
-
|
54
|
-
|
55
|
-
|
50
|
+
expect(subject.rewritten_code).to eq <<~EOS.strip
|
51
|
+
describe '#size' do
|
52
|
+
subject { super().size }
|
53
|
+
it { should == 1 }
|
54
|
+
end
|
55
|
+
EOS
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -90,39 +90,38 @@ module Synvert::Core
|
|
90
90
|
end
|
91
91
|
|
92
92
|
it 'parses append' do
|
93
|
-
expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods'
|
93
|
+
expect(Rewriter::AppendAction).to receive(:new).with(instance, 'include FactoryGirl::Syntax::Methods')
|
94
94
|
instance.append 'include FactoryGirl::Syntax::Methods'
|
95
95
|
end
|
96
96
|
|
97
97
|
it 'parses prepend' do
|
98
98
|
expect(Rewriter::PrependAction).to receive(:new).with(
|
99
99
|
instance,
|
100
|
-
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
101
|
-
{}
|
100
|
+
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
102
101
|
)
|
103
102
|
instance.prepend '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
104
103
|
end
|
105
104
|
|
106
105
|
it 'parses insert' do
|
107
|
-
expect(Rewriter::InsertAction).to receive(:new).with(
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
106
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end')
|
107
|
+
instance.insert '.first'
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'parses insert' do
|
111
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, 'URI.', at: 'beginning')
|
112
|
+
instance.insert 'URI.', at: 'beginning'
|
113
113
|
end
|
114
114
|
|
115
115
|
it 'parses insert_after' do
|
116
116
|
expect(Rewriter::InsertAfterAction).to receive(:new).with(
|
117
117
|
instance,
|
118
|
-
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
119
|
-
{}
|
118
|
+
'{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
120
119
|
)
|
121
120
|
instance.insert_after '{{arguments.first}}.include FactoryGirl::Syntax::Methods'
|
122
121
|
end
|
123
122
|
|
124
123
|
it 'parses replace_with' do
|
125
|
-
expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}'
|
124
|
+
expect(Rewriter::ReplaceWithAction).to receive(:new).with(instance, 'create {{arguments}}')
|
126
125
|
instance.replace_with 'create {{arguments}}'
|
127
126
|
end
|
128
127
|
|
@@ -156,22 +155,20 @@ module Synvert::Core
|
|
156
155
|
replace_with 'create {{arguments}}'
|
157
156
|
end
|
158
157
|
end
|
159
|
-
input =
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
end
|
174
|
-
"
|
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
|
175
172
|
expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb'])
|
176
173
|
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
|
177
174
|
expect(File).to receive(:write).with('spec/models/post_spec.rb', output)
|
@@ -187,18 +184,16 @@ end
|
|
187
184
|
end
|
188
185
|
end
|
189
186
|
end
|
190
|
-
input =
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
end
|
201
|
-
'
|
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
|
202
197
|
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb'])
|
203
198
|
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input)
|
204
199
|
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
@@ -214,18 +209,16 @@ end
|
|
214
209
|
end
|
215
210
|
end
|
216
211
|
end
|
217
|
-
input =
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
end
|
228
|
-
'
|
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
|
229
222
|
expect(Dir).to receive(:glob).with('./spec/spec_helper.rb').and_return(['spec/spec_helper.rb']).twice
|
230
223
|
expect(File).to receive(:read).with('spec/spec_helper.rb').and_return(input).once
|
231
224
|
expect(File).not_to receive(:write).with('spec/spec_helper.rb', output)
|
@@ -240,22 +233,20 @@ end
|
|
240
233
|
replace_with 'create {{arguments}}'
|
241
234
|
end
|
242
235
|
end
|
243
|
-
input =
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
end
|
258
|
-
"
|
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
|
259
250
|
expect(Dir).to receive(:glob).with('./spec/**/*_spec.rb').and_return(['spec/models/post_spec.rb']).twice
|
260
251
|
expect(File).to receive(:read).with('spec/models/post_spec.rb').and_return(input)
|
261
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.42.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: 2021-
|
11
|
+
date: 2021-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -146,7 +146,6 @@ files:
|
|
146
146
|
- lib/synvert/core/node_ext.rb
|
147
147
|
- lib/synvert/core/rewriter.rb
|
148
148
|
- lib/synvert/core/rewriter/action.rb
|
149
|
-
- lib/synvert/core/rewriter/action/add_action.rb
|
150
149
|
- lib/synvert/core/rewriter/action/append_action.rb
|
151
150
|
- lib/synvert/core/rewriter/action/delete_action.rb
|
152
151
|
- lib/synvert/core/rewriter/action/insert_action.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Synvert::Core
|
4
|
-
# AddAction to add code to the node.
|
5
|
-
class Rewriter::AddAction < Rewriter::Action
|
6
|
-
# Begin position to insert code.
|
7
|
-
#
|
8
|
-
# @return [Integer] begin position.
|
9
|
-
def begin_pos
|
10
|
-
@node.loc.expression.end_pos
|
11
|
-
end
|
12
|
-
|
13
|
-
# End position, always same to begin position.
|
14
|
-
#
|
15
|
-
# @return [Integer] end position.
|
16
|
-
def end_pos
|
17
|
-
begin_pos
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
# The rewritten source code.
|
23
|
-
#
|
24
|
-
# @return [String] rewritten code.
|
25
|
-
def rewritten_code
|
26
|
-
rewritten_source
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|