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