proc_to_ast 0.0.1 → 0.0.2
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/lib/proc_to_ast.rb +9 -4
- data/lib/proc_to_ast/version.rb +1 -1
- data/spec/proc_to_ast_spec.rb +36 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08e16687b46d8a606c8490cfdf2046302a98c490
|
4
|
+
data.tar.gz: 8c1d95a9b8fc423b77ea79d5a8132e3d49a496d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4126703d5e1e93a62a64dc7fe795bcd91ea181939d2db7455b1f7b3fd29fc53d9b100db51c4f1e68f692f9d607e193b54880ac39e8a5761202fb8d47550abc61
|
7
|
+
data.tar.gz: b57c0598263cd2bdfeb9e03e47dae6158510ca723159de749ac935939290dd45c702f441e90719e96d984e0c28eb422c497b2e2c7f7157039680051bc50888f1
|
data/lib/proc_to_ast.rb
CHANGED
@@ -5,6 +5,8 @@ require 'unparser'
|
|
5
5
|
require 'coderay'
|
6
6
|
|
7
7
|
module ProcToAst
|
8
|
+
class MultiMatchError < StandardError; end
|
9
|
+
|
8
10
|
class Traverser
|
9
11
|
def traverse_node(node)
|
10
12
|
if node.type != :block
|
@@ -14,9 +16,7 @@ module ProcToAst
|
|
14
16
|
end
|
15
17
|
}.compact
|
16
18
|
else
|
17
|
-
|
18
|
-
node
|
19
|
-
end
|
19
|
+
node
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -62,7 +62,12 @@ class Proc
|
|
62
62
|
source_buffer = Parser::Source::Buffer.new(filename, linenum)
|
63
63
|
source_buffer.source = source
|
64
64
|
node = parser.parse(source_buffer)
|
65
|
-
ProcToAst::Traverser.new.traverse_node(node)
|
65
|
+
block_nodes = ProcToAst::Traverser.new.traverse_node(node)
|
66
|
+
if block_nodes.length == 1
|
67
|
+
block_nodes.first
|
68
|
+
else
|
69
|
+
raise ProcToAst::MultiMatchError
|
70
|
+
end
|
66
71
|
rescue Parser::SyntaxError
|
67
72
|
retry if try_count < retry_limit
|
68
73
|
end
|
data/lib/proc_to_ast/version.rb
CHANGED
data/spec/proc_to_ast_spec.rb
CHANGED
@@ -8,27 +8,52 @@ describe Proc do
|
|
8
8
|
expect(ast.type).to eq(:block)
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
context "proc variation" do
|
12
|
+
it "converts Kernel#proc" do
|
13
|
+
pr = proc { p 1 }
|
12
14
|
|
13
|
-
|
15
|
+
expect(pr.to_ast).to be_a(AST::Node)
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
it "converts Proc.new" do
|
19
|
+
pr = Proc.new do |b|
|
20
|
+
puts b
|
21
|
+
end
|
22
|
+
|
23
|
+
expect(pr.to_ast).to be_a(AST::Node)
|
24
|
+
end
|
18
25
|
|
19
|
-
|
20
|
-
|
26
|
+
it "converts block passing method" do
|
27
|
+
def receive_block(&block)
|
28
|
+
block.to_ast
|
29
|
+
end
|
30
|
+
|
31
|
+
block_pass = receive_block do |n|
|
32
|
+
puts n
|
33
|
+
[1, 2, 3].map do |i|
|
34
|
+
i * 2
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
block_pass2 = receive_block { %w(a b c).map(&:upcase) }
|
39
|
+
|
40
|
+
expect(block_pass).to be_a(AST::Node)
|
41
|
+
expect(block_pass2).to be_a(AST::Node)
|
21
42
|
end
|
22
43
|
|
23
|
-
|
24
|
-
|
25
|
-
|
44
|
+
it "raise ProcToAst::MultiMatchError, when other block exists on same line " do
|
45
|
+
_ = [1].map {|i| i * 2}; fuga = ->(a) {
|
46
|
+
p a
|
47
|
+
}
|
48
|
+
|
49
|
+
expect{ fuga.to_ast }.to raise_error(ProcToAst::MultiMatchError)
|
50
|
+
end
|
26
51
|
end
|
27
52
|
end
|
28
53
|
|
29
54
|
describe "#to_source" do
|
30
55
|
it "return source code string" do
|
31
|
-
|
56
|
+
fuga = ->(a) {
|
32
57
|
p a
|
33
58
|
}
|
34
59
|
|