synvert-core 0.39.0 → 0.40.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 +5 -1
- data/lib/synvert/core/rewriter/action/insert_action.rb +6 -26
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +13 -78
- data/spec/synvert/core/rewriter/instance_spec.rb +2 -2
- metadata +1 -2
- 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: 5c729d2f456550a622a39f2e43d6d6c7f701f8b1330af038c436a03ec162d828
|
4
|
+
data.tar.gz: 8f5e7642d9073bfa38798dbd237a416037047991d530f4ff862623007ffa81f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1187929417e2611b85d95dad5cc2f985d86cc4a1638d0ebecc4baeacc8dfa124ed5ec6595845a941fe7fe13c1666255333aa9f4249f297899c84ab8518a1b809
|
7
|
+
data.tar.gz: 729ff991e4a120d655faaa5517915d6d2614396668f08268a93b70c86f00ce038ec12ff09cf7d5cc060e34b7af8604689f3e4948b32a26bf4fc85ae520da1d21
|
data/CHANGELOG.md
CHANGED
@@ -1,26 +1,13 @@
|
|
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
|
-
DO_LENGTH = ' do'.length
|
7
|
-
|
8
6
|
# Begin position to insert code.
|
9
7
|
#
|
10
8
|
# @return [Integer] begin position.
|
11
9
|
def begin_pos
|
12
|
-
|
13
|
-
when :block
|
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
|
21
|
-
else
|
22
|
-
@node.children.last.loc.expression.end_pos
|
23
|
-
end
|
10
|
+
@node.loc.expression.end_pos
|
24
11
|
end
|
25
12
|
|
26
13
|
# End position, always same to begin position.
|
@@ -30,18 +17,11 @@ module Synvert::Core
|
|
30
17
|
begin_pos
|
31
18
|
end
|
32
19
|
|
33
|
-
|
34
|
-
|
35
|
-
# Indent of the node.
|
20
|
+
# The rewritten source code.
|
36
21
|
#
|
37
|
-
# @
|
38
|
-
|
39
|
-
|
40
|
-
if %i[block class].include? node.type
|
41
|
-
' ' * (node.indent + DEFAULT_INDENT)
|
42
|
-
else
|
43
|
-
' ' * node.indent
|
44
|
-
end
|
22
|
+
# @return [String] rewritten code.
|
23
|
+
def rewritten_code
|
24
|
+
rewritten_source
|
45
25
|
end
|
46
26
|
end
|
47
27
|
end
|
data/lib/synvert/core/version.rb
CHANGED
@@ -4,88 +4,23 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
describe Rewriter::InsertAction do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
expect(subject.begin_pos).to eq 'Synvert::Application.configure do'.length
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'gets end_pos' do
|
20
|
-
expect(subject.end_pos).to eq 'Synvert::Application.configure do'.length
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'gets rewritten_code' do
|
24
|
-
expect(subject.rewritten_code).to eq "\n config.eager_load = true"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'block node with args' do
|
29
|
-
subject {
|
30
|
-
source = "RSpec.configure do |config|\nend"
|
31
|
-
block_node = Parser::CurrentRuby.parse(source)
|
32
|
-
instance = double(current_node: block_node)
|
33
|
-
Rewriter::InsertAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods')
|
34
|
-
}
|
35
|
-
|
36
|
-
it 'gets begin_pos' do
|
37
|
-
expect(subject.begin_pos).to eq 'RSpec.configure do |config|'.length
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'gets end_pos' do
|
41
|
-
expect(subject.end_pos).to eq 'RSpec.configure do |config|'.length
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'gets rewritten_code' do
|
45
|
-
expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
|
46
|
-
end
|
7
|
+
subject {
|
8
|
+
source = "User.where(username: 'Richard')"
|
9
|
+
node = Parser::CurrentRuby.parse(source)
|
10
|
+
instance = double(current_node: node)
|
11
|
+
Rewriter::InsertAction.new(instance, '.first')
|
12
|
+
}
|
13
|
+
|
14
|
+
it 'gets begin_pos' do
|
15
|
+
expect(subject.begin_pos).to eq "User.where(username: 'Richard')".length
|
47
16
|
end
|
48
17
|
|
49
|
-
|
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
|
18
|
+
it 'gets end_pos' do
|
19
|
+
expect(subject.end_pos).to eq "User.where(username: 'Richard')".length
|
68
20
|
end
|
69
21
|
|
70
|
-
|
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"
|
88
|
-
end
|
22
|
+
it 'gets rewritten_code' do
|
23
|
+
expect(subject.rewritten_code).to eq ".first"
|
89
24
|
end
|
90
25
|
end
|
91
26
|
end
|
@@ -106,10 +106,10 @@ module Synvert::Core
|
|
106
106
|
it 'parses insert' do
|
107
107
|
expect(Rewriter::InsertAction).to receive(:new).with(
|
108
108
|
instance,
|
109
|
-
'
|
109
|
+
'.first',
|
110
110
|
{}
|
111
111
|
)
|
112
|
-
instance.insert '
|
112
|
+
instance.insert '.first'
|
113
113
|
end
|
114
114
|
|
115
115
|
it 'parses insert_after' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.40.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
@@ -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
|