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 +4 -4
- data/lib/proc_to_ast.rb +7 -15
- data/lib/proc_to_ast/version.rb +1 -1
- data/spec/proc_to_ast_spec.rb +20 -0
- 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: 7154d3d9104765cba5474b644a7401f3afa599cf
|
4
|
+
data.tar.gz: 2524064259c19cccdb92bb071d42390f874874fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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
|
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
|
-
|
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)
|
data/lib/proc_to_ast/version.rb
CHANGED
data/spec/proc_to_ast_spec.rb
CHANGED
@@ -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
|
|