synvert-core 0.4.2 → 0.4.3

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
  SHA1:
3
- metadata.gz: d77822c52c87146007c9915c7ae6e6d4c3d539ee
4
- data.tar.gz: 639a0bbc823cddb2476142ef3035b2ea73c954c8
3
+ metadata.gz: 0bde34eb3461990b83ea42cb438131ed34dab7ff
4
+ data.tar.gz: 856cce48e98b4b1930f247125a7dd156a3c0e2b2
5
5
  SHA512:
6
- metadata.gz: 06127d574c1450a0689ab8f43d2403cee8e3aa859c4fd7ae645da98832112f5e70b3f7807f793762a6caae5c64d0f912259a65d945de220e4e92a697907fe529
7
- data.tar.gz: 7a29c3fa1178fe44056460df4692c5a7b8af7fc7832fd3edd5b35e39f9ea57068faa818611ef2fa043a5244d3b9089e93b2e62e6ab7db8aafff6e32c136ec3b0
6
+ metadata.gz: 2abd9040ab9b7f2908c1ef1f3c617bbf367fc177e0fb78a4b088b13520d60dd85cdd1afb93fb39d9bbeca20d25eabac4468dde07b99fc459a90335b1ec41df7a
7
+ data.tar.gz: 7848b2dd867efe89b7ee79207274e59cf33660ea5115a23af4ec20fff9e3400f2df27a23492e8b2e5ecc294e79453e72f021ea207040f302accfdedbda3d9e4b
data/CHANGELOG.md CHANGED
@@ -1,13 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.3
4
+
5
+ * Add parent_class for :class node
6
+ * Move process_with_node to instance
7
+ * Add Instance#process_with_other_node
8
+ * Fix indent for append action
9
+
3
10
  ## 0.4.2
4
11
 
5
- * do not rewrite code in {{ }} in synvert can't rewrite it
6
- * fix indent for replace_with action
12
+ * Do not rewrite code in {{ }} in synvert can't rewrite it
13
+ * Fix indent for replace_with action
7
14
 
8
15
  ## 0.4.1
9
16
 
10
- * add Rewriter::Helper module to provide common helper methods.
17
+ * Add Rewriter::Helper module to provide common helper methods.
11
18
 
12
19
  ## 0.4.0 (2014-07-26)
13
20
 
@@ -15,6 +15,18 @@ class Parser::AST::Node
15
15
  end
16
16
  end
17
17
 
18
+ # Get parent_class node of :class node.
19
+ #
20
+ # @return [Parser::AST::Node] parent_class node.
21
+ # @raise [Synvert::Core::MethodNotSupported] if calls on other node.
22
+ def parent_class
23
+ if :class == self.type
24
+ self.children[1]
25
+ else
26
+ raise Synvert::Core::MethodNotSupported.new "parent_class is not handled for #{self.inspect}"
27
+ end
28
+ end
29
+
18
30
  # Get receiver node of :send node.
19
31
  #
20
32
  # @return [Parser::AST::Node] receiver node.
@@ -266,7 +278,7 @@ class Parser::AST::Node
266
278
  source = evaluated.first.loc.expression.source_buffer.source
267
279
  source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
268
280
  end
269
- when String
281
+ when String, Symbol
270
282
  evaluated
271
283
  when NilClass
272
284
  'nil'
@@ -100,7 +100,7 @@ module Synvert::Core
100
100
  if :begin == @node.type
101
101
  @node.loc.expression.end_pos
102
102
  else
103
- @node.loc.expression.end_pos - 4
103
+ @node.loc.expression.end_pos - @node.indent - 4
104
104
  end
105
105
  end
106
106
 
@@ -55,9 +55,9 @@ module Synvert::Core
55
55
 
56
56
  @current_file = file_path
57
57
  @current_source = source
58
- @current_node = ast
59
- instance_eval &@block
60
- @current_node = ast
58
+ self.process_with_node ast do
59
+ instance_eval &@block
60
+ end
61
61
 
62
62
  @actions.sort!
63
63
  check_conflict_actions
@@ -174,6 +174,23 @@ module Synvert::Core
174
174
  @rewriter.add_warning Rewriter::Warning.new(self, message)
175
175
  end
176
176
 
177
+ # Set current_node to node and process.
178
+ # @param node [Parser::AST::Node] node set to current_node
179
+ def process_with_node(node)
180
+ self.current_node = node
181
+ yield
182
+ self.current_node = node
183
+ end
184
+
185
+ # Set current_node properly, process and set current_node back to original current_node.
186
+ # @param node [Parser::AST::Node] node set to current_node
187
+ def process_with_other_node(node)
188
+ original_node = self.current_node
189
+ self.current_node = node
190
+ yield
191
+ self.current_node = original_node
192
+ end
193
+
177
194
  private
178
195
 
179
196
  # It changes source code from bottom to top, and it can change source code twice at the same time.
@@ -19,28 +19,18 @@ module Synvert::Core
19
19
  def process
20
20
  current_node = @instance.current_node
21
21
  return unless current_node
22
- process_with_node current_node do
22
+ @instance.process_with_node current_node do
23
23
  matching_nodes = []
24
24
  matching_nodes << current_node if current_node.match? @rules
25
25
  current_node.recursive_children do |child_node|
26
26
  matching_nodes << child_node if child_node.match? @rules
27
27
  end
28
28
  matching_nodes.each do |matching_node|
29
- process_with_node matching_node do
29
+ @instance.process_with_node matching_node do
30
30
  @instance.instance_eval &@block
31
31
  end
32
32
  end
33
33
  end
34
34
  end
35
-
36
- private
37
-
38
- # Set instance current node properly and process.
39
- # @param node [Parser::AST::Node]
40
- def process_with_node(node)
41
- @instance.current_node = node
42
- yield
43
- @instance.current_node = node
44
- end
45
35
  end
46
36
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.4.2"
5
+ VERSION = "0.4.3"
6
6
  end
7
7
  end
@@ -26,6 +26,13 @@ describe Parser::AST::Node do
26
26
  end
27
27
  end
28
28
 
29
+ describe '#parent_class' do
30
+ it 'gets for class node' do
31
+ node = parse('class Post < ActiveRecord::Base; end')
32
+ expect(node.parent_class).to eq parse('ActiveRecord::Base')
33
+ end
34
+ end
35
+
29
36
  describe '#receiver' do
30
37
  it 'gets for send node' do
31
38
  node = parse('FactoryGirl.create :post')
@@ -191,5 +191,37 @@ end
191
191
  end
192
192
  end
193
193
  end
194
+
195
+ describe '#process_with_node' do
196
+ let(:rewriter) { Rewriter.new('foobar') }
197
+
198
+ it 'resets current_node' do
199
+ instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do; end
200
+ node1 = double()
201
+ node2 = double()
202
+ instance.process_with_node(node1) do
203
+ instance.current_node = node2
204
+ expect(instance.current_node).to eq node2
205
+ end
206
+ expect(instance.current_node).to eq node1
207
+ end
208
+ end
209
+
210
+ describe '#process_with_other_node' do
211
+ let(:rewriter) { Rewriter.new('foobar') }
212
+
213
+ it 'resets current_node' do
214
+ instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do; end
215
+ node1 = double()
216
+ node2 = double()
217
+ node3 = double()
218
+ instance.current_node = node1
219
+ instance.process_with_other_node(node2) do
220
+ instance.current_node = node3
221
+ expect(instance.current_node).to eq node3
222
+ end
223
+ expect(instance.current_node).to eq node1
224
+ end
225
+ end
194
226
  end
195
227
  end
@@ -18,7 +18,10 @@ end
18
18
  }
19
19
  let(:node) { Parser::CurrentRuby.parse(source) }
20
20
  let(:instance) { double(:current_node => node, :current_node= => node, :current_source => source) }
21
- before { Rewriter::Instance.current = instance }
21
+ before do
22
+ allow(instance).to receive(:process_with_node).and_yield
23
+ Rewriter::Instance.current = instance
24
+ end
22
25
 
23
26
  describe '#process' do
24
27
  it 'not call block if no matching node' do
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.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser