synvert-core 0.7.5 → 0.8.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 +4 -16
- data/lib/synvert/core/rewriter/action/append_action.rb +38 -0
- data/lib/synvert/core/rewriter/action/insert_action.rb +48 -0
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +30 -0
- data/lib/synvert/core/rewriter/action/remove_action.rb +29 -0
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +40 -0
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +45 -0
- data/lib/synvert/core/rewriter/action.rb +1 -212
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +15 -0
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +12 -0
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +15 -0
- data/lib/synvert/core/rewriter/condition.rb +0 -33
- data/lib/synvert/core/rewriter/instance.rb +3 -0
- data/lib/synvert/core/rewriter/scope/goto_scope.rb +27 -0
- data/lib/synvert/core/rewriter/scope/within_scope.rb +36 -0
- data/lib/synvert/core/rewriter/scope.rb +0 -57
- data/lib/synvert/core/rewriter.rb +11 -11
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +47 -0
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +89 -0
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +24 -0
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +24 -0
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +6 -0
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +53 -0
- data/spec/synvert/core/rewriter/action_spec.rb +0 -222
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +36 -0
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +43 -0
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +36 -0
- data/spec/synvert/core/rewriter/condition_spec.rb +0 -83
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +34 -0
- data/spec/synvert/core/rewriter/scope/within_scope.rb +46 -0
- data/spec/synvert/core/rewriter/scope_spec.rb +1 -72
- metadata +35 -2
@@ -4,61 +4,4 @@ module Synvert::Core
|
|
4
4
|
# Scope finds out nodes which match rules.
|
5
5
|
class Rewriter::Scope
|
6
6
|
end
|
7
|
-
|
8
|
-
# WithinScope finds out nodes which match rules, then change its scope to matching node.
|
9
|
-
class Rewriter::WithinScope < Rewriter::Scope
|
10
|
-
# Initialize a scope
|
11
|
-
#
|
12
|
-
# @param instance [Synvert::Core::Rewriter::Instance]
|
13
|
-
# @param rules [Hash]
|
14
|
-
# @param block [Block]
|
15
|
-
def initialize(instance, rules, &block)
|
16
|
-
@instance = instance
|
17
|
-
@rules = rules
|
18
|
-
@block = block
|
19
|
-
end
|
20
|
-
|
21
|
-
# Find out the matching nodes. It checks the current node and iterates all child nodes,
|
22
|
-
# then run the block code with each matching node.
|
23
|
-
def process
|
24
|
-
current_node = @instance.current_node
|
25
|
-
return unless current_node
|
26
|
-
@instance.process_with_node current_node do
|
27
|
-
matching_nodes = []
|
28
|
-
matching_nodes << current_node if current_node.match? @rules
|
29
|
-
current_node.recursive_children do |child_node|
|
30
|
-
matching_nodes << child_node if child_node.match? @rules
|
31
|
-
end
|
32
|
-
matching_nodes.each do |matching_node|
|
33
|
-
@instance.process_with_node matching_node do
|
34
|
-
@instance.instance_eval &@block
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Go to and change its scope to a child node.
|
42
|
-
class Rewriter::GotoScope < Rewriter::Scope
|
43
|
-
# Initialize a scope
|
44
|
-
#
|
45
|
-
# @param instance [Synvert::Core::Rewriter::Instance]
|
46
|
-
# @param child_node_name [String]
|
47
|
-
# @param block [Block]
|
48
|
-
def initialize(instance, child_node_name, &block)
|
49
|
-
@instance = instance
|
50
|
-
@child_node_name = child_node_name
|
51
|
-
@block = block
|
52
|
-
end
|
53
|
-
|
54
|
-
# Go to a child now, then run the block code with the the child node.
|
55
|
-
def process
|
56
|
-
current_node = @instance.current_node
|
57
|
-
return unless current_node
|
58
|
-
child_node = current_node.send @child_node_name
|
59
|
-
@instance.process_with_other_node child_node do
|
60
|
-
@instance.instance_eval &@block
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
7
|
end
|
@@ -17,25 +17,25 @@ module Synvert::Core
|
|
17
17
|
# end
|
18
18
|
class Rewriter
|
19
19
|
autoload :Action, 'synvert/core/rewriter/action'
|
20
|
-
autoload :AppendAction, 'synvert/core/rewriter/action'
|
21
|
-
autoload :InsertAction, 'synvert/core/rewriter/action'
|
22
|
-
autoload :InsertAfterAction, 'synvert/core/rewriter/action'
|
23
|
-
autoload :ReplaceWithAction, 'synvert/core/rewriter/action'
|
24
|
-
autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action'
|
25
|
-
autoload :RemoveAction, 'synvert/core/rewriter/action'
|
20
|
+
autoload :AppendAction, 'synvert/core/rewriter/action/append_action'
|
21
|
+
autoload :InsertAction, 'synvert/core/rewriter/action/insert_action'
|
22
|
+
autoload :InsertAfterAction, 'synvert/core/rewriter/action/insert_after_action'
|
23
|
+
autoload :ReplaceWithAction, 'synvert/core/rewriter/action/replace_with_action'
|
24
|
+
autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action/replace_erb_stmt_with_expr_action'
|
25
|
+
autoload :RemoveAction, 'synvert/core/rewriter/action/remove_action'
|
26
26
|
|
27
27
|
autoload :Warning, 'synvert/core/rewriter/warning'
|
28
28
|
|
29
29
|
autoload :Instance, 'synvert/core/rewriter/instance'
|
30
30
|
|
31
31
|
autoload :Scope, 'synvert/core/rewriter/scope'
|
32
|
-
autoload :WithinScope, 'synvert/core/rewriter/scope'
|
33
|
-
autoload :GotoScope, 'synvert/core/rewriter/scope'
|
32
|
+
autoload :WithinScope, 'synvert/core/rewriter/scope/within_scope'
|
33
|
+
autoload :GotoScope, 'synvert/core/rewriter/scope/goto_scope'
|
34
34
|
|
35
35
|
autoload :Condition, 'synvert/core/rewriter/condition'
|
36
|
-
autoload :IfExistCondition, 'synvert/core/rewriter/condition'
|
37
|
-
autoload :UnlessExistCondition, 'synvert/core/rewriter/condition'
|
38
|
-
autoload :IfOnlyExistCondition, 'synvert/core/rewriter/condition'
|
36
|
+
autoload :IfExistCondition, 'synvert/core/rewriter/condition/if_exist_condition'
|
37
|
+
autoload :UnlessExistCondition, 'synvert/core/rewriter/condition/unless_exist_condition'
|
38
|
+
autoload :IfOnlyExistCondition, 'synvert/core/rewriter/condition/if_only_exist_condition'
|
39
39
|
|
40
40
|
autoload :Helper, 'synvert/core/rewriter/helper'
|
41
41
|
|
data/lib/synvert/core/version.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::AppendAction do
|
5
|
+
describe 'class node' do
|
6
|
+
subject do
|
7
|
+
source = "class User\n has_many :posts\nend"
|
8
|
+
class_node = Parser::CurrentRuby.parse(source)
|
9
|
+
instance = double(current_node: class_node)
|
10
|
+
Rewriter::AppendAction.new(instance, "def as_json\n super\nend")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'gets begin_pos' do
|
14
|
+
expect(subject.begin_pos).to eq "calss User\n has_many :posts".length
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'gets end_pos' do
|
18
|
+
expect(subject.end_pos).to eq "class User\n has_many :posts".length
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'gets rewritten_code' do
|
22
|
+
expect(subject.rewritten_code).to eq "\n\n def as_json\n super\n end"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'begin node' do
|
27
|
+
subject do
|
28
|
+
source = "gem 'rails'\ngem 'mysql2'"
|
29
|
+
begin_node = Parser::CurrentRuby.parse(source)
|
30
|
+
instance = double(current_node: begin_node)
|
31
|
+
Rewriter::AppendAction.new(instance, "gem 'twitter'")
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'gets begin_pos' do
|
35
|
+
expect(subject.begin_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'gets end_pos' do
|
39
|
+
expect(subject.end_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'gets rewritten_code' do
|
43
|
+
expect(subject.rewritten_code).to eq "\ngem 'twitter'"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::InsertAction do
|
5
|
+
describe 'block node without args' do
|
6
|
+
subject {
|
7
|
+
source = "Synvert::Application.configure do\nend"
|
8
|
+
block_node = Parser::CurrentRuby.parse(source)
|
9
|
+
instance = double(current_node: block_node)
|
10
|
+
Rewriter::InsertAction.new(instance, 'config.eager_load = true')
|
11
|
+
}
|
12
|
+
|
13
|
+
it 'gets begin_pos' do
|
14
|
+
expect(subject.begin_pos).to eq "Synvert::Application.configure do".length
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'gets end_pos' do
|
18
|
+
expect(subject.end_pos).to eq "Synvert::Application.configure do".length
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'gets rewritten_code' do
|
22
|
+
expect(subject.rewritten_code).to eq "\n config.eager_load = true"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'block node with args' do
|
27
|
+
subject {
|
28
|
+
source = "RSpec.configure do |config|\nend"
|
29
|
+
block_node = Parser::CurrentRuby.parse(source)
|
30
|
+
instance = double(current_node: block_node)
|
31
|
+
Rewriter::InsertAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
32
|
+
}
|
33
|
+
|
34
|
+
it 'gets begin_pos' do
|
35
|
+
expect(subject.begin_pos).to eq "RSpec.configure do |config|".length
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'gets end_pos' do
|
39
|
+
expect(subject.end_pos).to eq "RSpec.configure do |config|".length
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'gets rewritten_code' do
|
43
|
+
expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'class node without superclass' do
|
48
|
+
subject {
|
49
|
+
source = "class User\n has_many :posts\nend"
|
50
|
+
class_node = Parser::CurrentRuby.parse(source)
|
51
|
+
instance = double(current_node: class_node)
|
52
|
+
Rewriter::InsertAction.new(instance, 'include Deletable')
|
53
|
+
}
|
54
|
+
|
55
|
+
it 'gets begin_pos' do
|
56
|
+
expect(subject.begin_pos).to eq "class User".length
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'gets end_pos' do
|
60
|
+
expect(subject.end_pos).to eq "class User".length
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'gets rewritten_code' do
|
64
|
+
expect(subject.rewritten_code).to eq "\n include Deletable"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'class node with superclass' do
|
69
|
+
subject {
|
70
|
+
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
71
|
+
class_node = Parser::CurrentRuby.parse(source)
|
72
|
+
instance = double(current_node: class_node)
|
73
|
+
Rewriter::InsertAction.new(instance, 'include Deletable')
|
74
|
+
}
|
75
|
+
|
76
|
+
it 'gets begin_pos' do
|
77
|
+
expect(subject.begin_pos).to eq "class User < ActionRecord::Base".length
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'gets end_pos' do
|
81
|
+
expect(subject.end_pos).to eq "class User < ActionRecord::Base".length
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'gets rewritten_code' do
|
85
|
+
expect(subject.rewritten_code).to eq "\n include Deletable"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::InsertAfterAction do
|
5
|
+
subject {
|
6
|
+
source = " include Foo"
|
7
|
+
node = Parser::CurrentRuby.parse(source)
|
8
|
+
instance = double(current_node: node)
|
9
|
+
Rewriter::InsertAfterAction.new(instance, 'include Bar')
|
10
|
+
}
|
11
|
+
|
12
|
+
it 'gets begin_pos' do
|
13
|
+
expect(subject.begin_pos).to eq " include Foo".length
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'gets end_pos' do
|
17
|
+
expect(subject.end_pos).to eq " include Foo".length
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'gets rewritten_code' do
|
21
|
+
expect(subject.rewritten_code).to eq "\n include Bar"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::RemoveAction do
|
5
|
+
subject {
|
6
|
+
source = "user = User.new params[:user]\nuser.save\nrender\n"
|
7
|
+
send_node = Parser::CurrentRuby.parse(source).children[1]
|
8
|
+
instance = double(current_node: send_node)
|
9
|
+
Rewriter::RemoveAction.new(instance)
|
10
|
+
}
|
11
|
+
|
12
|
+
it 'gets begin_pos' do
|
13
|
+
expect(subject.begin_pos).to eq "user = User.new params[:user]\n".length
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'gets end_pos' do
|
17
|
+
expect(subject.end_pos).to eq "user = User.new params[:user]\nuser.save".length
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'gets rewritten_code' do
|
21
|
+
expect(subject.rewritten_code).to eq ""
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::ReplaceWithAction do
|
5
|
+
context "replace with single line" do
|
6
|
+
subject {
|
7
|
+
source = "post = FactoryGirl.create_list :post, 2"
|
8
|
+
send_node = Parser::CurrentRuby.parse(source).children[1]
|
9
|
+
instance = double(current_node: send_node)
|
10
|
+
Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
|
11
|
+
}
|
12
|
+
|
13
|
+
it 'gets begin_pos' do
|
14
|
+
expect(subject.begin_pos).to eq "post = ".length
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'gets end_pos' do
|
18
|
+
expect(subject.end_pos).to eq "post = FactoryGirl.create_list :post, 2".length
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'gets rewritten_code' do
|
22
|
+
expect(subject.rewritten_code).to eq 'create_list :post, 2'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "#replace with multiple line" do
|
27
|
+
subject {
|
28
|
+
source = " its(:size) { should == 1 }"
|
29
|
+
send_node = Parser::CurrentRuby.parse(source)
|
30
|
+
instance = double(current_node: send_node)
|
31
|
+
Rewriter::ReplaceWithAction.new(instance, """describe '#size' do
|
32
|
+
subject { super().size }
|
33
|
+
it { {{body}} }
|
34
|
+
end""")
|
35
|
+
}
|
36
|
+
|
37
|
+
it 'gets begin_pos' do
|
38
|
+
expect(subject.begin_pos).to eq 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'gets end_pos' do
|
42
|
+
expect(subject.end_pos).to eq " its(:size) { should == 1 }".length
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'gets rewritten_code' do
|
46
|
+
expect(subject.rewritten_code).to eq """describe '#size' do
|
47
|
+
subject { super().size }
|
48
|
+
it { should == 1 }
|
49
|
+
end"""
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -13,226 +13,4 @@ module Synvert::Core
|
|
13
13
|
expect(subject.line).to eq 2
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
17
|
-
describe Rewriter::ReplaceWithAction do
|
18
|
-
context "replace with single line" do
|
19
|
-
subject {
|
20
|
-
source = "post = FactoryGirl.create_list :post, 2"
|
21
|
-
send_node = Parser::CurrentRuby.parse(source).children[1]
|
22
|
-
instance = double(current_node: send_node)
|
23
|
-
Rewriter::ReplaceWithAction.new(instance, 'create_list {{arguments}}')
|
24
|
-
}
|
25
|
-
|
26
|
-
it 'gets begin_pos' do
|
27
|
-
expect(subject.begin_pos).to eq "post = ".length
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'gets end_pos' do
|
31
|
-
expect(subject.end_pos).to eq "post = FactoryGirl.create_list :post, 2".length
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'gets rewritten_code' do
|
35
|
-
expect(subject.rewritten_code).to eq 'create_list :post, 2'
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context "#replace with multiple line" do
|
40
|
-
subject {
|
41
|
-
source = " its(:size) { should == 1 }"
|
42
|
-
send_node = Parser::CurrentRuby.parse(source)
|
43
|
-
instance = double(current_node: send_node)
|
44
|
-
Rewriter::ReplaceWithAction.new(instance, """describe '#size' do
|
45
|
-
subject { super().size }
|
46
|
-
it { {{body}} }
|
47
|
-
end""")
|
48
|
-
}
|
49
|
-
|
50
|
-
it 'gets begin_pos' do
|
51
|
-
expect(subject.begin_pos).to eq 2
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'gets end_pos' do
|
55
|
-
expect(subject.end_pos).to eq " its(:size) { should == 1 }".length
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'gets rewritten_code' do
|
59
|
-
expect(subject.rewritten_code).to eq """describe '#size' do
|
60
|
-
subject { super().size }
|
61
|
-
it { should == 1 }
|
62
|
-
end"""
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe Rewriter::AppendAction < Rewriter::Action do
|
68
|
-
describe 'class node' do
|
69
|
-
subject do
|
70
|
-
source = "class User\n has_many :posts\nend"
|
71
|
-
class_node = Parser::CurrentRuby.parse(source)
|
72
|
-
instance = double(current_node: class_node)
|
73
|
-
Rewriter::AppendAction.new(instance, "def as_json\n super\nend")
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'gets begin_pos' do
|
77
|
-
expect(subject.begin_pos).to eq "calss User\n has_many :posts".length
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'gets end_pos' do
|
81
|
-
expect(subject.end_pos).to eq "class User\n has_many :posts".length
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'gets rewritten_code' do
|
85
|
-
expect(subject.rewritten_code).to eq "\n\n def as_json\n super\n end"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
describe 'begin node' do
|
90
|
-
subject do
|
91
|
-
source = "gem 'rails'\ngem 'mysql2'"
|
92
|
-
begin_node = Parser::CurrentRuby.parse(source)
|
93
|
-
instance = double(current_node: begin_node)
|
94
|
-
Rewriter::AppendAction.new(instance, "gem 'twitter'")
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'gets begin_pos' do
|
98
|
-
expect(subject.begin_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'gets end_pos' do
|
102
|
-
expect(subject.end_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'gets rewritten_code' do
|
106
|
-
expect(subject.rewritten_code).to eq "\ngem 'twitter'"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
describe Rewriter::InsertAction do
|
112
|
-
describe 'block node without args' do
|
113
|
-
subject {
|
114
|
-
source = "Synvert::Application.configure do\nend"
|
115
|
-
block_node = Parser::CurrentRuby.parse(source)
|
116
|
-
instance = double(current_node: block_node)
|
117
|
-
Rewriter::InsertAction.new(instance, 'config.eager_load = true')
|
118
|
-
}
|
119
|
-
|
120
|
-
it 'gets begin_pos' do
|
121
|
-
expect(subject.begin_pos).to eq "Synvert::Application.configure do".length
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'gets end_pos' do
|
125
|
-
expect(subject.end_pos).to eq "Synvert::Application.configure do".length
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'gets rewritten_code' do
|
129
|
-
expect(subject.rewritten_code).to eq "\n config.eager_load = true"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe 'block node with args' do
|
134
|
-
subject {
|
135
|
-
source = "RSpec.configure do |config|\nend"
|
136
|
-
block_node = Parser::CurrentRuby.parse(source)
|
137
|
-
instance = double(current_node: block_node)
|
138
|
-
Rewriter::InsertAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
139
|
-
}
|
140
|
-
|
141
|
-
it 'gets begin_pos' do
|
142
|
-
expect(subject.begin_pos).to eq "RSpec.configure do |config|".length
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'gets end_pos' do
|
146
|
-
expect(subject.end_pos).to eq "RSpec.configure do |config|".length
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'gets rewritten_code' do
|
150
|
-
expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe 'class node without superclass' do
|
155
|
-
subject {
|
156
|
-
source = "class User\n has_many :posts\nend"
|
157
|
-
class_node = Parser::CurrentRuby.parse(source)
|
158
|
-
instance = double(current_node: class_node)
|
159
|
-
Rewriter::InsertAction.new(instance, 'include Deletable')
|
160
|
-
}
|
161
|
-
|
162
|
-
it 'gets begin_pos' do
|
163
|
-
expect(subject.begin_pos).to eq "class User".length
|
164
|
-
end
|
165
|
-
|
166
|
-
it 'gets end_pos' do
|
167
|
-
expect(subject.end_pos).to eq "class User".length
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'gets rewritten_code' do
|
171
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe 'class node with superclass' do
|
176
|
-
subject {
|
177
|
-
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
178
|
-
class_node = Parser::CurrentRuby.parse(source)
|
179
|
-
instance = double(current_node: class_node)
|
180
|
-
Rewriter::InsertAction.new(instance, 'include Deletable')
|
181
|
-
}
|
182
|
-
|
183
|
-
it 'gets begin_pos' do
|
184
|
-
expect(subject.begin_pos).to eq "class User < ActionRecord::Base".length
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'gets end_pos' do
|
188
|
-
expect(subject.end_pos).to eq "class User < ActionRecord::Base".length
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'gets rewritten_code' do
|
192
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
describe Rewriter::InsertAfterAction do
|
198
|
-
subject {
|
199
|
-
source = " include Foo"
|
200
|
-
node = Parser::CurrentRuby.parse(source)
|
201
|
-
instance = double(current_node: node)
|
202
|
-
Rewriter::InsertAfterAction.new(instance, 'include Bar')
|
203
|
-
}
|
204
|
-
|
205
|
-
it 'gets begin_pos' do
|
206
|
-
expect(subject.begin_pos).to eq " include Foo".length
|
207
|
-
end
|
208
|
-
|
209
|
-
it 'gets end_pos' do
|
210
|
-
expect(subject.end_pos).to eq " include Foo".length
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'gets rewritten_code' do
|
214
|
-
expect(subject.rewritten_code).to eq "\n include Bar"
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
describe Rewriter::RemoveAction do
|
219
|
-
subject {
|
220
|
-
source = "user = User.new params[:user]\nuser.save\nrender\n"
|
221
|
-
send_node = Parser::CurrentRuby.parse(source).children[1]
|
222
|
-
instance = double(current_node: send_node)
|
223
|
-
Rewriter::RemoveAction.new(instance)
|
224
|
-
}
|
225
|
-
|
226
|
-
it 'gets begin_pos' do
|
227
|
-
expect(subject.begin_pos).to eq "user = User.new params[:user]\n".length
|
228
|
-
end
|
229
|
-
|
230
|
-
it 'gets end_pos' do
|
231
|
-
expect(subject.end_pos).to eq "user = User.new params[:user]\nuser.save".length
|
232
|
-
end
|
233
|
-
|
234
|
-
it 'gets rewritten_code' do
|
235
|
-
expect(subject.rewritten_code).to eq ""
|
236
|
-
end
|
237
|
-
end
|
238
16
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::IfExistCondition do
|
5
|
+
let(:source) {
|
6
|
+
"""
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.include EmailSpec::Helpers
|
9
|
+
config.include EmailSpec::Methods
|
10
|
+
end
|
11
|
+
"""
|
12
|
+
}
|
13
|
+
let(:node) { Parser::CurrentRuby.parse(source) }
|
14
|
+
let(:instance) { double(:current_node => node) }
|
15
|
+
|
16
|
+
describe '#process' do
|
17
|
+
it 'call block if match anything' do
|
18
|
+
run = false
|
19
|
+
condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
20
|
+
run = true
|
21
|
+
end
|
22
|
+
condition.process
|
23
|
+
expect(run).to be_truthy
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'not call block if not match anything' do
|
27
|
+
run = false
|
28
|
+
condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::SyntaxMethods'] do
|
29
|
+
run = true
|
30
|
+
end
|
31
|
+
condition.process
|
32
|
+
expect(run).to be_falsey
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
describe Rewriter::IfOnlyExistCondition do
|
5
|
+
let(:source) {
|
6
|
+
"""
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.include EmailSpec::Helpers
|
9
|
+
config.include EmailSpec::Methods
|
10
|
+
end
|
11
|
+
"""
|
12
|
+
}
|
13
|
+
let(:node) { Parser::CurrentRuby.parse(source) }
|
14
|
+
let(:instance) { double(:current_node => node) }
|
15
|
+
|
16
|
+
describe '#process' do
|
17
|
+
it 'gets matching nodes' do
|
18
|
+
source = """
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.include EmailSpec::Helpers
|
21
|
+
end
|
22
|
+
"""
|
23
|
+
node = Parser::CurrentRuby.parse(source)
|
24
|
+
instance = double(:current_node => node)
|
25
|
+
run = false
|
26
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
27
|
+
run = true
|
28
|
+
end
|
29
|
+
condition.process
|
30
|
+
expect(run).to be_truthy
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'not call block if does not match' do
|
34
|
+
run = false
|
35
|
+
condition = Rewriter::IfOnlyExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
|
36
|
+
run = true
|
37
|
+
end
|
38
|
+
condition.process
|
39
|
+
expect(run).to be_falsey
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|