proc_to_ast 0.0.4 → 0.0.5

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: af92479d75ab4508025af6183f02826935da753f
4
- data.tar.gz: e7d4661d31825657ab4d3f87b5f5f773a083d89f
3
+ metadata.gz: 7154d3d9104765cba5474b644a7401f3afa599cf
4
+ data.tar.gz: 2524064259c19cccdb92bb071d42390f874874fe
5
5
  SHA512:
6
- metadata.gz: bb3e438c238c4e153e9cb24adfcc11fc090a6dc713bad95dcd72748062983a62fc0eae58cc9a79721a365e4ffa7022babeb74188b3208acc010d0d13717a2048
7
- data.tar.gz: ea3ca120b291b266d5bda7b2fc0541400e38d696c80ac836d36d9b48bc90f3171e976c3f2e18efbe107ee2480ae5870cca16e45330db32832c12712f34f3438c
6
+ metadata.gz: 5fae7228287b64ab740e6e1b725c75f95ffeaf30560fa3ef6a085b60e52de05ab70df542184026922e6b75eac79fe01bafd0f340d5f4fb5a8601ebfefa730a4f
7
+ data.tar.gz: b983db0c5643c7589ee5329958a5f0b627e67bdbcf7a54656c25a69dfc610186ff752f3f1b364914319651aad58c69fdf6d3c2f2445d91c189daf77de950a8bc
data/lib/proc_to_ast.rb CHANGED
@@ -34,7 +34,7 @@ module ProcToAst
34
34
  try_count += 1
35
35
 
36
36
  buf << file.gets
37
- do_parse(buf)
37
+ do_parse(buf.join)
38
38
  rescue ::Parser::SyntaxError => e
39
39
  node = trim_and_retry(buf)
40
40
 
@@ -49,9 +49,8 @@ module ProcToAst
49
49
 
50
50
  private
51
51
 
52
- def do_parse(buf)
52
+ def do_parse(source)
53
53
  parser.reset
54
- source = buf.join.force_encoding(parser.default_encoding)
55
54
 
56
55
  source_buffer = ::Parser::Source::Buffer.new(@filename, @linenum)
57
56
  source_buffer.source = source
@@ -65,22 +64,15 @@ module ProcToAst
65
64
  end
66
65
  end
67
66
 
68
- # Remove tail comma or Hash syntax, and retry parsing
67
+ # Remove tail comma and wrap dummy method, and retry parsing
68
+ # For proc inner Array or Hash
69
69
  def trim_and_retry(buf)
70
70
  *lines, last = buf
71
71
 
72
72
  # For inner Array or Hash or Arguments list.
73
- lines << last.gsub(/,\s*/, "")
74
-
75
- lines[0] = lines[0]
76
- .gsub(/[0-9a-zA-Z_]+:\s*/, "")
77
- .gsub(/[^\s]+\s*=>\s*/, "")
78
-
79
- begin
80
- do_parse(lines)
81
- rescue ::Parser::SyntaxError
82
- nil
83
- end
73
+ lines << last.gsub(/,\s*$/, "")
74
+ do_parse("a(#{lines.join})") # wrap dummy method
75
+ rescue ::Parser::SyntaxError
84
76
  end
85
77
 
86
78
  def traverse_node(node)
@@ -1,3 +1,3 @@
1
1
  module ProcToAst
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -70,11 +70,22 @@ describe Proc do
70
70
  expect(array[3].to_source).to eq("proc do |a|\n [a]\nend")
71
71
  end
72
72
 
73
+ it "inner two dimension array" do
74
+ array = [
75
+ [1, 2, -> { 3 }],
76
+ [4, 5, -> { 6 }]
77
+ ]
78
+ expect(array[0][2].to_ast).to be_a(AST::Node)
79
+ expect(array[1][2].to_ast).to be_a(AST::Node)
80
+ end
81
+
73
82
  it "inner hash" do
74
83
  hash_oneline = {a: -> { 1 }}
75
84
  expect(hash_oneline[:a].to_ast).to be_a(AST::Node)
76
85
  expect(hash_oneline[:a].to_source).to eq("lambda do\n 1\nend")
86
+ end
77
87
 
88
+ it "inner multiline hash" do
78
89
  hash = {
79
90
  a: -> { 1 },
80
91
  :b => -> { 2 },
@@ -92,6 +103,15 @@ describe Proc do
92
103
  expect(hash[:d].to_ast).to be_a(AST::Node)
93
104
  expect(hash[:d].to_source).to eq("lambda do\n 4\nend")
94
105
  end
106
+
107
+ it "inner another hash" do
108
+ hash = {
109
+ a: 1, :b => -> { 2 },
110
+ }
111
+
112
+ expect(hash[:b].to_ast).to be_a(AST::Node)
113
+ expect(hash[:b].to_source).to eq("lambda do\n 2\nend")
114
+ end
95
115
  end
96
116
  end
97
117
 
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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007