synvert-core 0.40.0 → 0.41.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c729d2f456550a622a39f2e43d6d6c7f701f8b1330af038c436a03ec162d828
4
- data.tar.gz: 8f5e7642d9073bfa38798dbd237a416037047991d530f4ff862623007ffa81f7
3
+ metadata.gz: 4b54b506cc880f8fff9834b131659fd6f32cc60082bfade8a07d2d9a767352dd
4
+ data.tar.gz: 0ec762ecd158a05142674be271a10a0cbbe260ae7d4e0f0cd36ab4d426cefcd7
5
5
  SHA512:
6
- metadata.gz: 1187929417e2611b85d95dad5cc2f985d86cc4a1638d0ebecc4baeacc8dfa124ed5ec6595845a941fe7fe13c1666255333aa9f4249f297899c84ab8518a1b809
7
- data.tar.gz: 729ff991e4a120d655faaa5517915d6d2614396668f08268a93b70c86f00ce038ec12ff09cf7d5cc060e34b7af8604689f3e4948b32a26bf4fc85ae520da1d21
6
+ metadata.gz: dabaa021af949aa5b15bbe5830d2e6154eb1679d5144d614c933c48d5767b92f0b1ad10b281d7d3819f755a762677c7dac26d696b501710e29f9730188f4afe9
7
+ data.tar.gz: 319e76c109e66ee558ff5c844ba3c9a1f3b6be1f2e6ca769bb3a97dedb19c6c0e4cf7dd22f7e04ff9a2508722f2a96e77e4f7521356f1c97ad0aef0e9959fe0f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.41.0 (2021-06-24)
4
+
5
+ * Remove unused autoindent option
6
+ * Add `insert 'xxx', at: 'beginning'`
7
+
3
8
  ## 0.40.0 (2021-06-23)
4
9
 
5
10
  * Rewrite `insert` action
@@ -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
- # @param options [Hash] action options, it includes :autoindent.
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
- @node.loc.expression.end_pos
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.split("\n").length > 1
24
+ if rewritten_source.include?("\n")
25
25
  new_code = []
26
- rewritten_source.split("\n").each_with_index { |line, index|
27
- new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line)
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
- # @param options [Hash] action options.
223
- def append(code, options = {})
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
- # @param options [Hash] action options.
232
- def prepend(code, options = {})
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 options [Hash] action options.
241
- def insert(code, options = {})
242
- @actions << Rewriter::InsertAction.new(self, code, options)
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
- # @param options [Hash] action options.
250
- def insert_after(node, options = {})
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
- # @param options [Hash] action options.
259
- def replace_with(code, options = {})
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '0.40.0'
5
+ VERSION = '0.41.0'
6
6
  end
7
7
  end
@@ -4,23 +4,46 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter::InsertAction do
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
16
- end
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
- it 'gets end_pos' do
19
- expect(subject.end_pos).to eq "User.where(username: 'Richard')".length
23
+ it 'gets rewritten_code' do
24
+ expect(subject.rewritten_code).to eq '.first'
25
+ end
20
26
  end
21
27
 
22
- it 'gets rewritten_code' do
23
- expect(subject.rewritten_code).to eq ".first"
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
- instance,
35
- "describe '#size' do
36
- subject { super().size }
37
- it { {{body}} }
38
- end",
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 "describe '#size' do
53
- subject { super().size }
54
- it { should == 1 }
55
- end"
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.40.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-23 00:00:00.000000000 Z
11
+ date: 2021-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport