synvert-core 0.16.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -1
- data/CHANGELOG.md +16 -0
- data/lib/synvert/core.rb +1 -4
- data/lib/synvert/core/configuration.rb +9 -17
- data/lib/synvert/core/engine/erb.rb +29 -22
- data/lib/synvert/core/exceptions.rb +3 -3
- data/lib/synvert/core/node_ext.rb +102 -101
- data/lib/synvert/core/rewriter.rb +33 -19
- data/lib/synvert/core/rewriter/action.rb +3 -5
- data/lib/synvert/core/rewriter/action/append_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/insert_action.rb +7 -3
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/remove_action.rb +1 -1
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +4 -3
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +6 -4
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -2
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/gem_spec.rb +10 -10
- data/lib/synvert/core/rewriter/helper.rb +3 -5
- data/lib/synvert/core/rewriter/instance.rb +22 -19
- data/lib/synvert/core/rewriter/scope/within_scope.rb +18 -11
- data/lib/synvert/core/version.rb +1 -1
- data/spec/spec_helper.rb +1 -6
- data/spec/synvert/core/engine/erb_spec.rb +30 -30
- data/spec/synvert/core/node_ext_spec.rb +53 -52
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +8 -8
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +3 -3
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +1 -1
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +15 -11
- data/spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +21 -12
- data/spec/synvert/core/rewriter/condition/unless_exist_condition_spec.rb +17 -9
- data/spec/synvert/core/rewriter/gem_spec_spec.rb +11 -10
- data/spec/synvert/core/rewriter/helper_spec.rb +34 -31
- data/spec/synvert/core/rewriter/instance_spec.rb +104 -70
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +8 -6
- data/spec/synvert/core/rewriter/scope/within_scope.rb +16 -9
- data/spec/synvert/core/rewriter_spec.rb +105 -68
- metadata +2 -4
- data/spec/synvert/core/configuration_spec.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43a4d413d049428517fba9bfe79d57aadec0fa3b98d72a377170888005fe8e50
|
4
|
+
data.tar.gz: c0efe87cdfcbac1e837126f6525c45bfaba5213fee0688ca774666a60ba62851
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b81c56d7e16caf4d1409e896dd405a6b6c242df8512da45ca83b43040e74edb34a11b1197e90d6d58e05c5225ff695992d52bbaae0862c45e68ad151791e29b
|
7
|
+
data.tar.gz: 56f14b2292823f227193faf06c727c819b0adcb684f6d3db2429b2cb8cc01cc7a0f9a2147eb98e7c2ea8c23297dcf415a9c27a59ba5ad02b2dbd187b1d08a393
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.20.0 (2021-02-15)
|
4
|
+
|
5
|
+
* Call snippet in sandbox mode
|
6
|
+
|
7
|
+
## 0.19.0 (2021-02-07)
|
8
|
+
|
9
|
+
* Simplify `Configuration`
|
10
|
+
|
11
|
+
## 0.18.0 (2021-02-07)
|
12
|
+
|
13
|
+
* Add `Rewriter.execute`
|
14
|
+
|
15
|
+
## 0.17.0 (2021-01-29)
|
16
|
+
|
17
|
+
* Ignore `gem_spec` check if `Gemfile.lock` does not eixst
|
18
|
+
|
3
19
|
## 0.16.0 (2021-01-17)
|
4
20
|
|
5
21
|
* Use parser 3.0.0
|
data/lib/synvert/core.rb
CHANGED
@@ -1,26 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'singleton'
|
4
|
-
|
5
3
|
module Synvert::Core
|
6
4
|
# Synvert global configuration.
|
7
|
-
class Configuration
|
8
|
-
|
5
|
+
class Configuration
|
6
|
+
class << self
|
7
|
+
attr_writer :path, :skip_files
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# @param value [Object] configuration value.
|
14
|
-
def set(key, value)
|
15
|
-
self[key] = value
|
16
|
-
end
|
9
|
+
def path
|
10
|
+
@path || '.'
|
11
|
+
end
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# @return [Object] configuration value.
|
22
|
-
def get(key)
|
23
|
-
self[key]
|
13
|
+
def skip_files
|
14
|
+
@skip_files || []
|
15
|
+
end
|
24
16
|
end
|
25
17
|
end
|
26
18
|
end
|
@@ -4,17 +4,17 @@ require 'erubis'
|
|
4
4
|
|
5
5
|
module Synvert::Core
|
6
6
|
module Engine
|
7
|
-
ERUBY_EXPR_SPLITTER =
|
8
|
-
ERUBY_STMT_SPLITTER =
|
7
|
+
ERUBY_EXPR_SPLITTER = '; ;'
|
8
|
+
ERUBY_STMT_SPLITTER = '; ;'
|
9
9
|
|
10
10
|
class ERB
|
11
|
-
class <<self
|
11
|
+
class << self
|
12
12
|
# convert erb to ruby code.
|
13
13
|
#
|
14
14
|
# @param source [String] erb source code.
|
15
15
|
# @return [String] ruby source code.
|
16
16
|
def encode(source)
|
17
|
-
Erubis.new(source.gsub(
|
17
|
+
Erubis.new(source.gsub('-%>', '%>'), escape: false, trim: false).src
|
18
18
|
end
|
19
19
|
|
20
20
|
# convert ruby code to erb.
|
@@ -28,25 +28,30 @@ module Synvert::Core
|
|
28
28
|
source = remove_erubis_buf(source)
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
private
|
32
32
|
|
33
33
|
def decode_ruby_stmt(source)
|
34
34
|
source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/m) { "<%#{$1}%>" }
|
35
35
|
end
|
36
36
|
|
37
37
|
def decode_ruby_output(source)
|
38
|
-
source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$1}%>" }
|
39
|
-
|
38
|
+
source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$1}%>" }.gsub(
|
39
|
+
/@output_buffer.append= (.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m
|
40
|
+
) { |m| "<%=#{m.sub('@output_buffer.append= ', '').sub(ERUBY_EXPR_SPLITTER, '')}%>" }
|
40
41
|
end
|
41
42
|
|
42
43
|
def decode_html_output(source)
|
43
|
-
source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text($1) }
|
44
|
-
|
45
|
-
|
44
|
+
source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text($1) }.gsub(
|
45
|
+
/@output_buffer.safe_append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m
|
46
|
+
) { reverse_escape_text($1) }.gsub(
|
47
|
+
/@output_buffer.safe_append=(.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m
|
48
|
+
) { reverse_escape_text($1) }
|
46
49
|
end
|
47
50
|
|
48
51
|
def remove_erubis_buf(source)
|
49
|
-
source
|
52
|
+
source
|
53
|
+
.sub('@output_buffer = output_buffer || ActionView::OutputBuffer.new;', '')
|
54
|
+
.sub('@output_buffer.to_s', '')
|
50
55
|
end
|
51
56
|
|
52
57
|
def reverse_escape_text(source)
|
@@ -59,7 +64,7 @@ module Synvert::Core
|
|
59
64
|
class Erubis < ::Erubis::Eruby
|
60
65
|
def add_preamble(src)
|
61
66
|
@newline_pending = 0
|
62
|
-
src <<
|
67
|
+
src << '@output_buffer = output_buffer || ActionView::OutputBuffer.new;'
|
63
68
|
end
|
64
69
|
|
65
70
|
def add_text(src, text)
|
@@ -102,22 +107,24 @@ module Synvert::Core
|
|
102
107
|
def add_expr_escaped(src, code)
|
103
108
|
flush_newline_if_pending(src)
|
104
109
|
if code =~ BLOCK_EXPR
|
105
|
-
src <<
|
110
|
+
src << '@output_buffer.safe_append= ' << code << ERUBY_EXPR_SPLITTER
|
106
111
|
else
|
107
|
-
src <<
|
112
|
+
src << '@output_buffer.safe_append=(' << code << ');' << ERUBY_EXPR_SPLITTER
|
108
113
|
end
|
109
114
|
end
|
110
115
|
|
111
116
|
def add_stmt(src, code)
|
112
117
|
flush_newline_if_pending(src)
|
113
|
-
if code != "\n" && code !=
|
114
|
-
index =
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
118
|
+
if code != "\n" && code != ''
|
119
|
+
index =
|
120
|
+
case code
|
121
|
+
when /\A(\s*)\r?\n/
|
122
|
+
$1.length
|
123
|
+
when /\A(\s+)/
|
124
|
+
$1.end_with?(' ') ? $1.length - 1 : $1.length
|
125
|
+
else
|
126
|
+
0
|
127
|
+
end
|
121
128
|
code.insert(index, ERUBY_STMT_SPLITTER)
|
122
129
|
code.insert(-1, ERUBY_STMT_SPLITTER[0...-1])
|
123
130
|
end
|
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Rewriter not found exception.
|
5
|
-
class RewriterNotFound <
|
5
|
+
class RewriterNotFound < RuntimeError
|
6
6
|
end
|
7
7
|
|
8
8
|
# Gemfile.lock not found exception.
|
9
|
-
class GemfileLockNotFound <
|
9
|
+
class GemfileLockNotFound < RuntimeError
|
10
10
|
end
|
11
11
|
|
12
12
|
# Method not supported exception.
|
13
|
-
class MethodNotSupported <
|
13
|
+
class MethodNotSupported < RuntimeError
|
14
14
|
end
|
15
15
|
end
|
@@ -31,15 +31,15 @@ module Parser::AST
|
|
31
31
|
# @return [Parser::AST::Node] name node.
|
32
32
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
33
33
|
def name
|
34
|
-
case
|
34
|
+
case type
|
35
35
|
when :class, :module, :def, :arg, :blockarg, :restarg
|
36
|
-
|
36
|
+
children[0]
|
37
37
|
when :defs, :const
|
38
|
-
|
38
|
+
children[1]
|
39
39
|
when :mlhs
|
40
40
|
self
|
41
41
|
else
|
42
|
-
raise Synvert::Core::MethodNotSupported
|
42
|
+
raise Synvert::Core::MethodNotSupported, "name is not handled for #{debug_info}"
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -48,10 +48,10 @@ module Parser::AST
|
|
48
48
|
# @return [Parser::AST::Node] parent_class node.
|
49
49
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
50
50
|
def parent_class
|
51
|
-
if :class ==
|
52
|
-
|
51
|
+
if :class == type
|
52
|
+
children[1]
|
53
53
|
else
|
54
|
-
raise Synvert::Core::MethodNotSupported
|
54
|
+
raise Synvert::Core::MethodNotSupported, "parent_class is not handled for #{debug_info}"
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -60,10 +60,10 @@ module Parser::AST
|
|
60
60
|
# @return [Parser::AST::Node] parent const node.
|
61
61
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
62
62
|
def parent_const
|
63
|
-
if :const ==
|
64
|
-
|
63
|
+
if :const == type
|
64
|
+
children[0]
|
65
65
|
else
|
66
|
-
raise Synvert::Core::MethodNotSupported
|
66
|
+
raise Synvert::Core::MethodNotSupported, "parent_const is not handled for #{debug_info}"
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -72,10 +72,10 @@ module Parser::AST
|
|
72
72
|
# @return [Parser::AST::Node] receiver node.
|
73
73
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
74
74
|
def receiver
|
75
|
-
if :send ==
|
76
|
-
|
75
|
+
if :send == type
|
76
|
+
children[0]
|
77
77
|
else
|
78
|
-
raise Synvert::Core::MethodNotSupported
|
78
|
+
raise Synvert::Core::MethodNotSupported, "receiver is not handled for #{debug_info}"
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
@@ -84,13 +84,13 @@ module Parser::AST
|
|
84
84
|
# @return [Parser::AST::Node] mesage node.
|
85
85
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
86
86
|
def message
|
87
|
-
case
|
87
|
+
case type
|
88
88
|
when :super, :zsuper
|
89
89
|
:super
|
90
90
|
when :send
|
91
|
-
|
91
|
+
children[1]
|
92
92
|
else
|
93
|
-
raise Synvert::Core::MethodNotSupported
|
93
|
+
raise Synvert::Core::MethodNotSupported, "message is not handled for #{debug_info}"
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -99,17 +99,17 @@ module Parser::AST
|
|
99
99
|
# @return [Array<Parser::AST::Node>] arguments node.
|
100
100
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
101
101
|
def arguments
|
102
|
-
case
|
102
|
+
case type
|
103
103
|
when :def, :block
|
104
|
-
ArgumentsNode.new
|
104
|
+
ArgumentsNode.new children[1]
|
105
105
|
when :defs
|
106
|
-
ArgumentsNode.new
|
106
|
+
ArgumentsNode.new children[2]
|
107
107
|
when :send
|
108
|
-
|
108
|
+
children[2..-1]
|
109
109
|
when :defined?
|
110
|
-
|
110
|
+
children
|
111
111
|
else
|
112
|
-
raise Synvert::Core::MethodNotSupported
|
112
|
+
raise Synvert::Core::MethodNotSupported, "arguments is not handled for #{debug_info}"
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
@@ -118,10 +118,10 @@ module Parser::AST
|
|
118
118
|
# @return [Parser::AST::Node] caller node.
|
119
119
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
120
120
|
def caller
|
121
|
-
if :block ==
|
122
|
-
|
121
|
+
if :block == type
|
122
|
+
children[0]
|
123
123
|
else
|
124
|
-
raise Synvert::Core::MethodNotSupported
|
124
|
+
raise Synvert::Core::MethodNotSupported, "caller is not handled for #{debug_info}"
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
@@ -130,19 +130,19 @@ module Parser::AST
|
|
130
130
|
# @return [Array<Parser::AST::Node>] body node.
|
131
131
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
132
132
|
def body
|
133
|
-
case
|
133
|
+
case type
|
134
134
|
when :begin
|
135
|
-
|
135
|
+
children
|
136
136
|
when :def, :block
|
137
|
-
return [] if
|
137
|
+
return [] if children[2].nil?
|
138
138
|
|
139
|
-
:begin ==
|
139
|
+
:begin == children[2].type ? children[2].body : children[2..-1]
|
140
140
|
when :defs
|
141
|
-
return [] if
|
141
|
+
return [] if children[3].nil?
|
142
142
|
|
143
|
-
:begin ==
|
143
|
+
:begin == children[3].type ? children[3].body : children[3..-1]
|
144
144
|
else
|
145
|
-
raise Synvert::Core::MethodNotSupported
|
145
|
+
raise Synvert::Core::MethodNotSupported, "body is not handled for #{debug_info}"
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -151,10 +151,10 @@ module Parser::AST
|
|
151
151
|
# @return [Parser::AST::Node] condition node.
|
152
152
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
153
153
|
def condition
|
154
|
-
if :if ==
|
155
|
-
|
154
|
+
if :if == type
|
155
|
+
children[0]
|
156
156
|
else
|
157
|
-
raise Synvert::Core::MethodNotSupported
|
157
|
+
raise Synvert::Core::MethodNotSupported, "condition is not handled for #{debug_info}"
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
@@ -163,10 +163,10 @@ module Parser::AST
|
|
163
163
|
# @return [Array<Parser::AST::Node>] keys node.
|
164
164
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
165
165
|
def keys
|
166
|
-
if :hash ==
|
167
|
-
|
166
|
+
if :hash == type
|
167
|
+
children.map { |child| child.children[0] }
|
168
168
|
else
|
169
|
-
raise Synvert::Core::MethodNotSupported
|
169
|
+
raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}"
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
@@ -175,10 +175,10 @@ module Parser::AST
|
|
175
175
|
# @return [Array<Parser::AST::Node>] values node.
|
176
176
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
177
177
|
def values
|
178
|
-
if :hash ==
|
179
|
-
|
178
|
+
if :hash == type
|
179
|
+
children.map { |child| child.children[1] }
|
180
180
|
else
|
181
|
-
raise Synvert::Core::MethodNotSupported
|
181
|
+
raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}"
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
@@ -188,10 +188,10 @@ module Parser::AST
|
|
188
188
|
# @return [Boolean] true if specified key exists.
|
189
189
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
190
190
|
def has_key?(key)
|
191
|
-
if :hash ==
|
192
|
-
|
191
|
+
if :hash == type
|
192
|
+
children.any? { |pair_node| pair_node.key.to_value == key }
|
193
193
|
else
|
194
|
-
raise Synvert::Core::MethodNotSupported
|
194
|
+
raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{debug_info}"
|
195
195
|
end
|
196
196
|
end
|
197
197
|
|
@@ -201,11 +201,11 @@ module Parser::AST
|
|
201
201
|
# @return [Parser::AST::Node] value node.
|
202
202
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
203
203
|
def hash_value(key)
|
204
|
-
if :hash ==
|
205
|
-
value_node =
|
204
|
+
if :hash == type
|
205
|
+
value_node = children.find { |pair_node| pair_node.key.to_value == key }
|
206
206
|
value_node ? value_node.value : nil
|
207
207
|
else
|
208
|
-
raise Synvert::Core::MethodNotSupported
|
208
|
+
raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{debug_info}"
|
209
209
|
end
|
210
210
|
end
|
211
211
|
|
@@ -214,10 +214,10 @@ module Parser::AST
|
|
214
214
|
# @return [Parser::AST::Node] key node.
|
215
215
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
216
216
|
def key
|
217
|
-
if :pair ==
|
218
|
-
|
217
|
+
if :pair == type
|
218
|
+
children.first
|
219
219
|
else
|
220
|
-
raise Synvert::Core::MethodNotSupported
|
220
|
+
raise Synvert::Core::MethodNotSupported, "key is not handled for #{debug_info}"
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
@@ -226,10 +226,10 @@ module Parser::AST
|
|
226
226
|
# @return [Parser::AST::Node] value node.
|
227
227
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
228
228
|
def value
|
229
|
-
if :pair ==
|
230
|
-
|
229
|
+
if :pair == type
|
230
|
+
children.last
|
231
231
|
else
|
232
|
-
raise Synvert::Core::MethodNotSupported
|
232
|
+
raise Synvert::Core::MethodNotSupported, "value is not handled for #{debug_info}"
|
233
233
|
end
|
234
234
|
end
|
235
235
|
|
@@ -238,10 +238,10 @@ module Parser::AST
|
|
238
238
|
# @return [Parser::AST::Node] variable nodes.
|
239
239
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
240
240
|
def left_value
|
241
|
-
if %i[masgn lvasgn ivasgn].include?
|
242
|
-
|
241
|
+
if %i[masgn lvasgn ivasgn].include? type
|
242
|
+
children[0]
|
243
243
|
else
|
244
|
-
raise Synvert::Core::MethodNotSupported
|
244
|
+
raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}"
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
@@ -250,10 +250,10 @@ module Parser::AST
|
|
250
250
|
# @return [Array<Parser::AST::Node>] variable nodes.
|
251
251
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
252
252
|
def right_value
|
253
|
-
if %i[masgn lvasgn ivasgn].include?
|
254
|
-
|
253
|
+
if %i[masgn lvasgn ivasgn].include? type
|
254
|
+
children[1]
|
255
255
|
else
|
256
|
-
raise Synvert::Core::MethodNotSupported
|
256
|
+
raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}"
|
257
257
|
end
|
258
258
|
end
|
259
259
|
|
@@ -262,60 +262,59 @@ module Parser::AST
|
|
262
262
|
# @return [Object] exact value.
|
263
263
|
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.
|
264
264
|
def to_value
|
265
|
-
case
|
265
|
+
case type
|
266
266
|
when :int, :str, :sym
|
267
|
-
|
267
|
+
children.last
|
268
268
|
when :true
|
269
269
|
true
|
270
270
|
when :false
|
271
271
|
false
|
272
272
|
when :array
|
273
|
-
|
273
|
+
children.map(&:to_value)
|
274
274
|
when :irange
|
275
|
-
(
|
275
|
+
(children.first.to_value..children.last.to_value)
|
276
276
|
when :begin
|
277
|
-
|
277
|
+
children.first.to_value
|
278
278
|
else
|
279
|
-
raise Synvert::Core::MethodNotSupported
|
279
|
+
raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{debug_info}"
|
280
280
|
end
|
281
281
|
end
|
282
282
|
|
283
283
|
def to_s
|
284
|
-
if :mlhs ==
|
285
|
-
"(#{
|
284
|
+
if :mlhs == type
|
285
|
+
"(#{children.map(&:name).join(', ')})"
|
286
286
|
end
|
287
287
|
end
|
288
288
|
|
289
289
|
def debug_info
|
290
|
-
"\n" +
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
290
|
+
"\n" +
|
291
|
+
[
|
292
|
+
"file: #{loc.expression.source_buffer.name}",
|
293
|
+
"line: #{loc.expression.line}",
|
294
|
+
"source: #{to_source}",
|
295
|
+
"node: #{inspect}"
|
296
|
+
].join("\n")
|
296
297
|
end
|
297
298
|
|
298
299
|
# Get the source code of current node.
|
299
300
|
#
|
300
301
|
# @return [String] source code.
|
301
302
|
def to_source
|
302
|
-
|
303
|
-
self.loc.expression.source
|
304
|
-
end
|
303
|
+
loc.expression&.source
|
305
304
|
end
|
306
305
|
|
307
306
|
# Get the indent of current node.
|
308
307
|
#
|
309
308
|
# @return [Integer] indent.
|
310
309
|
def indent
|
311
|
-
|
310
|
+
loc.expression.column
|
312
311
|
end
|
313
312
|
|
314
313
|
# Get the line of current node.
|
315
314
|
#
|
316
315
|
# @return [Integer] line.
|
317
316
|
def line
|
318
|
-
|
317
|
+
loc.expression.line
|
319
318
|
end
|
320
319
|
|
321
320
|
# Recursively iterate all child nodes of current node.
|
@@ -323,7 +322,7 @@ module Parser::AST
|
|
323
322
|
# @yield [child] Gives a child node.
|
324
323
|
# @yieldparam child [Parser::AST::Node] child node
|
325
324
|
def recursive_children
|
326
|
-
|
325
|
+
children.each do |child|
|
327
326
|
if Parser::AST::Node === child
|
328
327
|
yield child
|
329
328
|
child.recursive_children { |c| yield c }
|
@@ -336,21 +335,24 @@ module Parser::AST
|
|
336
335
|
# @param rules [Hash] rules to match.
|
337
336
|
# @return true if matches.
|
338
337
|
def match?(rules)
|
339
|
-
flat_hash(rules)
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
338
|
+
flat_hash(rules)
|
339
|
+
.keys
|
340
|
+
.all? do |multi_keys|
|
341
|
+
case multi_keys.last
|
342
|
+
when :any
|
343
|
+
actual_values = actual_value(self, multi_keys[0...-1])
|
344
|
+
expected = expected_value(rules, multi_keys)
|
345
|
+
actual_values.any? { |actual| match_value?(actual, expected) }
|
346
|
+
when :not
|
347
|
+
actual = actual_value(self, multi_keys[0...-1])
|
348
|
+
expected = expected_value(rules, multi_keys)
|
349
|
+
!match_value?(actual, expected)
|
350
|
+
else
|
351
|
+
actual = actual_value(self, multi_keys)
|
352
|
+
expected = expected_value(rules, multi_keys)
|
353
|
+
match_value?(actual, expected)
|
354
|
+
end
|
352
355
|
end
|
353
|
-
end
|
354
356
|
end
|
355
357
|
|
356
358
|
# Get rewritten source code.
|
@@ -363,8 +365,8 @@ module Parser::AST
|
|
363
365
|
def rewritten_source(code)
|
364
366
|
code.gsub(/{{(.*?)}}/m) do
|
365
367
|
old_code = $1
|
366
|
-
if
|
367
|
-
evaluated =
|
368
|
+
if respond_to? old_code.split(/\.|\[/).first
|
369
|
+
evaluated = instance_eval old_code
|
368
370
|
case evaluated
|
369
371
|
when Parser::AST::Node
|
370
372
|
evaluated.loc.expression.source
|
@@ -377,7 +379,7 @@ module Parser::AST
|
|
377
379
|
if lines_count > 1 && lines_count == evaluated.size
|
378
380
|
new_code = []
|
379
381
|
lines.each_with_index { |line, index|
|
380
|
-
new_code << (index == 0 ? line : line[evaluated.first.indent-2..-1])
|
382
|
+
new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1])
|
381
383
|
}
|
382
384
|
new_code.join("\n")
|
383
385
|
else
|
@@ -389,7 +391,7 @@ module Parser::AST
|
|
389
391
|
when NilClass
|
390
392
|
'nil'
|
391
393
|
else
|
392
|
-
raise Synvert::Core::MethodNotSupported
|
394
|
+
raise Synvert::Core::MethodNotSupported, "rewritten_source is not handled for #{evaluated.inspect}"
|
393
395
|
end
|
394
396
|
else
|
395
397
|
"{{#{old_code}}}"
|
@@ -397,7 +399,7 @@ module Parser::AST
|
|
397
399
|
end
|
398
400
|
end
|
399
401
|
|
400
|
-
|
402
|
+
private
|
401
403
|
|
402
404
|
# Compare actual value with expected value.
|
403
405
|
#
|
@@ -415,8 +417,7 @@ module Parser::AST
|
|
415
417
|
end
|
416
418
|
when String
|
417
419
|
if Parser::AST::Node === actual
|
418
|
-
actual.to_source == expected ||
|
419
|
-
(actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
|
420
|
+
actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) ||
|
420
421
|
actual.to_source[1...-1] == expected
|
421
422
|
else
|
422
423
|
actual.to_s == expected
|
@@ -446,7 +447,7 @@ module Parser::AST
|
|
446
447
|
when Parser::AST::Node
|
447
448
|
actual == expected
|
448
449
|
else
|
449
|
-
raise Synvert::Core::MethodNotSupported
|
450
|
+
raise Synvert::Core::MethodNotSupported, "#{expected.class} is not handled for match_value?"
|
450
451
|
end
|
451
452
|
end
|
452
453
|
|