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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/synvert/core.rb +1 -1
- data/lib/synvert/core/node_ext.rb +19 -14
- data/lib/synvert/core/rewriter/action.rb +5 -3
- data/lib/synvert/core/rewriter/helper.rb +3 -1
- data/lib/synvert/core/version.rb +1 -1
- data/spec/synvert/core/node_ext_spec.rb +22 -0
- data/spec/synvert/core/rewriter/action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/helper_spec.rb +45 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d77822c52c87146007c9915c7ae6e6d4c3d539ee
|
4
|
+
data.tar.gz: 639a0bbc823cddb2476142ef3035b2ea73c954c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06127d574c1450a0689ab8f43d2403cee8e3aa859c4fd7ae645da98832112f5e70b3f7807f793762a6caae5c64d0f912259a65d945de220e4e92a697907fe529
|
7
|
+
data.tar.gz: 7a29c3fa1178fe44056460df4692c5a7b8af7fc7832fd3edd5b35e39f9ea57068faa818611ef2fa043a5244d3b9089e93b2e62e6ab7db8aafff6e32c136ec3b0
|
data/CHANGELOG.md
CHANGED
data/lib/synvert/core.rb
CHANGED
@@ -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
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
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
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
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
|
data/lib/synvert/core/version.rb
CHANGED
@@ -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
|
@@ -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.
|
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-
|
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
|