synvert-core 0.40.0 → 0.41.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 -0
- data/lib/synvert/core/rewriter/action.rb +1 -4
- data/lib/synvert/core/rewriter/action/insert_action.rb +12 -1
- 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/rewriter/action/insert_action_spec.rb +37 -14
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +12 -12
- data/spec/synvert/core/rewriter/instance_spec.rb +10 -11
- 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: 4b54b506cc880f8fff9834b131659fd6f32cc60082bfade8a07d2d9a767352dd
|
|
4
|
+
data.tar.gz: 0ec762ecd158a05142674be271a10a0cbbe260ae7d4e0f0cd36ab4d426cefcd7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dabaa021af949aa5b15bbe5830d2e6154eb1679d5144d614c933c48d5767b92f0b1ad10b281d7d3819f755a762677c7dac26d696b501710e29f9730188f4afe9
|
|
7
|
+
data.tar.gz: 319e76c109e66ee558ff5c844ba3c9a1f3b6be1f2e6ca769bb3a97dedb19c6c0e4cf7dd22f7e04ff9a2508722f2a96e77e4f7521356f1c97ad0aef0e9959fe0f
|
data/CHANGELOG.md
CHANGED
|
@@ -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
|
|
|
@@ -3,11 +3,22 @@
|
|
|
3
3
|
module Synvert::Core
|
|
4
4
|
# AddAction to add code to the node.
|
|
5
5
|
class Rewriter::InsertAction < Rewriter::Action
|
|
6
|
+
def initialize(instance, code, at:)
|
|
7
|
+
@instance = instance
|
|
8
|
+
@code = code
|
|
9
|
+
@at = at
|
|
10
|
+
@node = @instance.current_node
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
# Begin position to insert code.
|
|
7
14
|
#
|
|
8
15
|
# @return [Integer] begin position.
|
|
9
16
|
def begin_pos
|
|
10
|
-
@
|
|
17
|
+
if @at == 'end'
|
|
18
|
+
@node.loc.expression.end_pos
|
|
19
|
+
else
|
|
20
|
+
@node.loc.expression.begin_pos
|
|
21
|
+
end
|
|
11
22
|
end
|
|
12
23
|
|
|
13
24
|
# End position, always same to begin position.
|
|
@@ -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
|
@@ -4,23 +4,46 @@ 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
|
-
|
|
7
|
+
context 'at end' do
|
|
8
|
+
subject {
|
|
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
|
+
}
|
|
14
|
+
|
|
15
|
+
it 'gets begin_pos' do
|
|
16
|
+
expect(subject.begin_pos).to eq " User.where(username: 'Richard')".length
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'gets end_pos' do
|
|
20
|
+
expect(subject.end_pos).to eq " User.where(username: 'Richard')".length
|
|
21
|
+
end
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
it 'gets rewritten_code' do
|
|
24
|
+
expect(subject.rewritten_code).to eq '.first'
|
|
25
|
+
end
|
|
20
26
|
end
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
context 'at beginning' do
|
|
29
|
+
subject {
|
|
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
|
+
}
|
|
35
|
+
|
|
36
|
+
it 'gets begin_pos' do
|
|
37
|
+
expect(subject.begin_pos).to eq 2
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'gets end_pos' do
|
|
41
|
+
expect(subject.end_pos).to eq 2
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'gets rewritten_code' do
|
|
45
|
+
expect(subject.rewritten_code).to eq 'URI.'
|
|
46
|
+
end
|
|
24
47
|
end
|
|
25
48
|
end
|
|
26
49
|
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
|
-
instance,
|
|
109
|
-
'.first',
|
|
110
|
-
{}
|
|
111
|
-
)
|
|
106
|
+
expect(Rewriter::InsertAction).to receive(:new).with(instance, '.first', at: 'end')
|
|
112
107
|
instance.insert '.first'
|
|
113
108
|
end
|
|
114
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
|
+
end
|
|
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
|
|
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.41.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-06-
|
|
11
|
+
date: 2021-06-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|