synvert-core 0.4.1 → 0.4.2

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: 9a3cff654eae485c77411077c8e1ee126a29afb6
4
- data.tar.gz: 21dcd6b6f868b4ccc9c35176578d01a060971e4c
3
+ metadata.gz: d77822c52c87146007c9915c7ae6e6d4c3d539ee
4
+ data.tar.gz: 639a0bbc823cddb2476142ef3035b2ea73c954c8
5
5
  SHA512:
6
- metadata.gz: 3ae01938e470eba926eaaa5d7564bdf2342ad6138938c67c400540f99872dcadf42fb5eb4363623edf133da1870c62b94cf12bf5112da01961730f561341ae5e
7
- data.tar.gz: dab94a1c780431350248d454331144c72a11b4e26e63431a5fe9c5fc03f79f35d7cab1928ec9bf897ce7133a35b849005784120c0ec43008d8216ca5c4d90289
6
+ metadata.gz: 06127d574c1450a0689ab8f43d2403cee8e3aa859c4fd7ae645da98832112f5e70b3f7807f793762a6caae5c64d0f912259a65d945de220e4e92a697907fe529
7
+ data.tar.gz: 7a29c3fa1178fe44056460df4692c5a7b8af7fc7832fd3edd5b35e39f9ea57068faa818611ef2fa043a5244d3b9089e93b2e62e6ab7db8aafff6e32c136ec3b0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.2
4
+
5
+ * do not rewrite code in {{ }} in synvert can't rewrite it
6
+ * fix indent for replace_with action
7
+
3
8
  ## 0.4.1
4
9
 
5
10
  * add Rewriter::Helper module to provide common helper methods.
data/lib/synvert/core.rb CHANGED
@@ -6,7 +6,7 @@ require 'bundler'
6
6
  require 'parser'
7
7
  require 'parser/current'
8
8
  require 'ast'
9
- require 'active_support/inflector'
9
+ require 'active_support/core_ext/object'
10
10
  require 'erubis'
11
11
  require 'synvert/core/node_ext'
12
12
 
@@ -254,22 +254,27 @@ class Parser::AST::Node
254
254
  # @raise [Synvert::Core::MethodNotSupported] if string in block {{ }} does not support.
255
255
  def rewritten_source(code)
256
256
  code.gsub(/{{(.*?)}}/m) do
257
- evaluated = self.instance_eval $1
258
- case evaluated
259
- when Parser::AST::Node
260
- source = evaluated.loc.expression.source_buffer.source
261
- source[evaluated.loc.expression.begin_pos...evaluated.loc.expression.end_pos]
262
- when Array
263
- if evaluated.size > 0
264
- source = evaluated.first.loc.expression.source_buffer.source
265
- source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
257
+ old_code = $1
258
+ if self.respond_to? old_code.split(/\.|\[/).first
259
+ evaluated = self.instance_eval old_code
260
+ case evaluated
261
+ when Parser::AST::Node
262
+ source = evaluated.loc.expression.source_buffer.source
263
+ source[evaluated.loc.expression.begin_pos...evaluated.loc.expression.end_pos]
264
+ when Array
265
+ if evaluated.size > 0
266
+ source = evaluated.first.loc.expression.source_buffer.source
267
+ source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos]
268
+ end
269
+ when String
270
+ evaluated
271
+ when NilClass
272
+ 'nil'
273
+ else
274
+ raise Synvert::Core::MethodNotSupported.new "rewritten_source is not handled for #{evaluated.inspect}"
266
275
  end
267
- when String
268
- evaluated
269
- when NilClass
270
- 'nil'
271
276
  else
272
- raise Synvert::Core::MethodNotSupported.new "rewritten_source is not handled for #{evaluated.inspect}"
277
+ "{{#{old_code}}}"
273
278
  end
274
279
  end
275
280
  end
@@ -70,9 +70,11 @@ module Synvert::Core
70
70
  # @return [String] rewritten code.
71
71
  def rewritten_code
72
72
  if rewritten_source.split("\n").length > 1
73
- "\n\n" + rewritten_source.split("\n").map { |line|
74
- indent(@node) + line
75
- }.join("\n")
73
+ new_code = []
74
+ rewritten_source.split("\n").each_with_index { |line, index|
75
+ new_code << (index == 0 ? line : indent(@node) + line)
76
+ }
77
+ new_code.join("\n")
76
78
  else
77
79
  rewritten_source
78
80
  end
@@ -31,7 +31,9 @@ module Synvert::Core
31
31
  #
32
32
  # strip_brackets("(1..100)") #=> "1..100"
33
33
  def strip_brackets(code)
34
- code.sub(/^[{(\[]/, '').sub(/[})\]]$/, '')
34
+ code.sub(/^\((.*)\)$/) { $1 }
35
+ .sub(/^\[(.*)\]$/) { $1 }
36
+ .sub(/^{(.*)}$/) { $1 }
35
37
  end
36
38
  end
37
39
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = "0.4.1"
5
+ VERSION = "0.4.2"
6
6
  end
7
7
  end
@@ -267,4 +267,26 @@ describe Parser::AST::Node do
267
267
  expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
268
268
  end
269
269
  end
270
+
271
+ describe '#rewritten_source' do
272
+ let(:instance) {
273
+ rewriter = Synvert::Rewriter.new('foobar')
274
+ Synvert::Rewriter::Instance.new(rewriter, 'file pattern')
275
+ }
276
+ before { Synvert::Rewriter::Instance.current = instance }
277
+
278
+ it 'does not rewrite with unknown method' do
279
+ source = 'class Synvert; end'
280
+ instance.current_source = source
281
+ node = parse(source)
282
+ expect(node.rewritten_source('{{foobar}}')).to eq '{{foobar}}'
283
+ end
284
+
285
+ it 'rewrites with node known method' do
286
+ source = 'class Synvert; end'
287
+ instance.current_source = source
288
+ node = parse(source)
289
+ expect(node.rewritten_source('{{name}}')).to eq 'Synvert'
290
+ end
291
+ end
270
292
  end
@@ -56,7 +56,7 @@ end""")
56
56
  end
57
57
 
58
58
  it 'gets rewritten_code' do
59
- expect(subject.rewritten_code).to eq """\n\n describe '#size' do
59
+ expect(subject.rewritten_code).to eq """describe '#size' do
60
60
  subject { super().size }
61
61
  it { should == 1 }
62
62
  end"""
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ describe Rewriter::Helper do
5
+ let(:dummy_instance) { Class.new { include Rewriter::Helper }.new }
6
+
7
+ describe "add_receiver_if_necessary" do
8
+ context "with receiver" do
9
+ let(:node) { parse("User.save(false)") }
10
+
11
+ it "adds reciever" do
12
+ allow(dummy_instance).to receive(:node).and_return(node)
13
+ expect(dummy_instance.add_receiver_if_necessary("save(validate: false)")).to eq "{{receiver}}.save(validate: false)"
14
+ end
15
+ end
16
+
17
+ context "without receiver" do
18
+ let(:node) { parse("save(false)") }
19
+
20
+ it "doesn't add reciever" do
21
+ allow(dummy_instance).to receive(:node).and_return(node)
22
+ expect(dummy_instance.add_receiver_if_necessary("save(validate: false)")).to eq "save(validate: false)"
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "strip_brackets" do
28
+ it "strip ()" do
29
+ expect(dummy_instance.strip_brackets("(123)")).to eq "123"
30
+ end
31
+
32
+ it "strip {}" do
33
+ expect(dummy_instance.strip_brackets("{123}")).to eq "123"
34
+ end
35
+
36
+ it "strip []" do
37
+ expect(dummy_instance.strip_brackets("[123]")).to eq "123"
38
+ end
39
+
40
+ it "not strip unmatched (]" do
41
+ expect(dummy_instance.strip_brackets("(123]")).to eq "(123]"
42
+ end
43
+ end
44
+ end
45
+ end
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.1
4
+ version: 0.4.2
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-10 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -132,6 +132,7 @@ files:
132
132
  - spec/synvert/core/rewriter/action_spec.rb
133
133
  - spec/synvert/core/rewriter/condition_spec.rb
134
134
  - spec/synvert/core/rewriter/gem_spec_spec.rb
135
+ - spec/synvert/core/rewriter/helper_spec.rb
135
136
  - spec/synvert/core/rewriter/instance_spec.rb
136
137
  - spec/synvert/core/rewriter/scope_spec.rb
137
138
  - spec/synvert/core/rewriter/warning_spec.rb
@@ -170,6 +171,7 @@ test_files:
170
171
  - spec/synvert/core/rewriter/action_spec.rb
171
172
  - spec/synvert/core/rewriter/condition_spec.rb
172
173
  - spec/synvert/core/rewriter/gem_spec_spec.rb
174
+ - spec/synvert/core/rewriter/helper_spec.rb
173
175
  - spec/synvert/core/rewriter/instance_spec.rb
174
176
  - spec/synvert/core/rewriter/scope_spec.rb
175
177
  - spec/synvert/core/rewriter/warning_spec.rb