proc_to_ast 0.0.1 → 0.0.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: 91c5fe173fcebfbe9b300cae9ec8c670fe5faafe
4
- data.tar.gz: 79c1ff86ee61c98ea86e6c5fcb09c651a461adb8
3
+ metadata.gz: 08e16687b46d8a606c8490cfdf2046302a98c490
4
+ data.tar.gz: 8c1d95a9b8fc423b77ea79d5a8132e3d49a496d2
5
5
  SHA512:
6
- metadata.gz: 09568ba9f1b51ffde7d256d08f2e145a548379f38bba040ee40debb5a80a66a79ecaf7e60a0cc0358c3185d58da4ff6fdd7014c60ba193f9e9f06c750bb644e5
7
- data.tar.gz: 4d077ea1bdb11c48a0e518d3252bc5a763e6306033c4f6cc22fe42102bb4fb66cfa7f3b0b246d0887b411292673e408f643486a5b77ccbe269a400f2624afe15
6
+ metadata.gz: 4126703d5e1e93a62a64dc7fe795bcd91ea181939d2db7455b1f7b3fd29fc53d9b100db51c4f1e68f692f9d607e193b54880ac39e8a5761202fb8d47550abc61
7
+ data.tar.gz: b57c0598263cd2bdfeb9e03e47dae6158510ca723159de749ac935939290dd45c702f441e90719e96d984e0c28eb422c497b2e2c7f7157039680051bc50888f1
@@ -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
- if proc_block?(node)
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).first
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
@@ -1,3 +1,3 @@
1
1
  module ProcToAst
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,27 +8,52 @@ describe Proc do
8
8
  expect(ast.type).to eq(:block)
9
9
  end
10
10
 
11
- it "converts proc variation" do
11
+ context "proc variation" do
12
+ it "converts Kernel#proc" do
13
+ pr = proc { p 1 }
12
14
 
13
- hoge = proc { p 1 }
15
+ expect(pr.to_ast).to be_a(AST::Node)
16
+ end
14
17
 
15
- _ = [1].map {|i| i * 2}; fuga = ->(a) {
16
- p a
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
- foo = Proc.new do |b|
20
- puts b
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
- expect(hoge.to_ast).to be_a(AST::Node)
24
- expect(fuga.to_ast).to be_a(AST::Node)
25
- expect(foo.to_ast).to be_a(AST::Node)
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
- _ = [1].map {|i| i * 2}; fuga = ->(a) {
56
+ fuga = ->(a) {
32
57
  p a
33
58
  }
34
59
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proc_to_ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007