synvert-core 0.15.1 → 0.18.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 +12 -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 +2 -1
- 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 +20 -14
- data/lib/synvert/core/rewriter/action.rb +5 -7
- data/lib/synvert/core/rewriter/action/append_action.rb +8 -6
- data/lib/synvert/core/rewriter/action/insert_action.rb +18 -19
- 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 +10 -10
- data/lib/synvert/core/rewriter/helper.rb +4 -6
- data/lib/synvert/core/rewriter/instance.rb +36 -22
- 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 -1
- data/spec/support/parser_helper.rb +2 -0
- data/spec/synvert/core/configuration_spec.rb +3 -1
- 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
- data/synvert-core.gemspec +2 -2
- metadata +12 -12
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Instance is an execution unit, it finds specified ast nodes,
|
@@ -8,7 +8,7 @@ module Synvert::Core
|
|
8
8
|
class Rewriter::Instance
|
9
9
|
include Rewriter::Helper
|
10
10
|
|
11
|
-
class <<self
|
11
|
+
class << self
|
12
12
|
# Cached file source.
|
13
13
|
#
|
14
14
|
# @param file_path [String] file path
|
@@ -64,7 +64,7 @@ module Synvert::Core
|
|
64
64
|
# @return current filename
|
65
65
|
attr_accessor :current_node, :current_file
|
66
66
|
|
67
|
-
DEFAULT_OPTIONS = { sort_by: 'begin_pos' }
|
67
|
+
DEFAULT_OPTIONS = { sort_by: 'begin_pos' }.freeze
|
68
68
|
|
69
69
|
# Initialize an instance.
|
70
70
|
#
|
@@ -73,13 +73,13 @@ module Synvert::Core
|
|
73
73
|
# @param options [Hash] instance options, it includes :sort_by.
|
74
74
|
# @param block [Block] block code to find nodes, match conditions and rewrite code.
|
75
75
|
# @return [Synvert::Core::Rewriter::Instance]
|
76
|
-
def initialize(rewriter, file_pattern, options={}, &block)
|
76
|
+
def initialize(rewriter, file_pattern, options = {}, &block)
|
77
77
|
@rewriter = rewriter
|
78
78
|
@actions = []
|
79
79
|
@file_pattern = file_pattern
|
80
80
|
@options = DEFAULT_OPTIONS.merge(options)
|
81
81
|
@block = block
|
82
|
-
rewriter.helpers.each { |helper|
|
82
|
+
rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) }
|
83
83
|
end
|
84
84
|
|
85
85
|
# Process the instance.
|
@@ -87,16 +87,19 @@ module Synvert::Core
|
|
87
87
|
# and rewrite source code back to original file.
|
88
88
|
def process
|
89
89
|
file_pattern = File.join(Configuration.instance.get(:path), @file_pattern)
|
90
|
-
Dir
|
91
|
-
|
90
|
+
Dir
|
91
|
+
.glob(file_pattern)
|
92
|
+
.each do |file_path|
|
93
|
+
next if Configuration.instance.get(:skip_files).include? file_path
|
94
|
+
|
92
95
|
begin
|
93
96
|
conflict_actions = []
|
94
|
-
source = self.class.file_source(file_path)
|
97
|
+
source = +self.class.file_source(file_path)
|
95
98
|
ast = self.class.file_ast(file_path)
|
96
99
|
|
97
100
|
@current_file = file_path
|
98
101
|
|
99
|
-
|
102
|
+
process_with_node ast do
|
100
103
|
begin
|
101
104
|
instance_eval &@block
|
102
105
|
rescue NoMethodError
|
@@ -108,7 +111,7 @@ module Synvert::Core
|
|
108
111
|
if @actions.length > 0
|
109
112
|
@actions.sort_by! { |action| action.send(@options[:sort_by]) }
|
110
113
|
conflict_actions = get_conflict_actions
|
111
|
-
@actions.
|
114
|
+
@actions.reverse_each do |action|
|
112
115
|
source[action.begin_pos...action.end_pos] = action.rewritten_code
|
113
116
|
source = remove_code_or_whole_line(source, action.line)
|
114
117
|
end
|
@@ -121,7 +124,6 @@ module Synvert::Core
|
|
121
124
|
# do nothing, iterate next file
|
122
125
|
end while !conflict_actions.empty?
|
123
126
|
end
|
124
|
-
end
|
125
127
|
end
|
126
128
|
|
127
129
|
# Gets current node, it allows to get current node in block code.
|
@@ -146,7 +148,7 @@ module Synvert::Core
|
|
146
148
|
# @param node [Parser::AST::Node] node set to current_node
|
147
149
|
# @yield process
|
148
150
|
def process_with_other_node(node)
|
149
|
-
original_node =
|
151
|
+
original_node = current_node
|
150
152
|
self.current_node = node
|
151
153
|
yield
|
152
154
|
self.current_node = original_node
|
@@ -156,15 +158,28 @@ module Synvert::Core
|
|
156
158
|
# DSL #
|
157
159
|
#######
|
158
160
|
|
159
|
-
# Parse within_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find matching ast nodes,
|
161
|
+
# Parse within_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find recursive matching ast nodes,
|
160
162
|
# then continue operating on each matching ast node.
|
161
163
|
#
|
162
164
|
# @param rules [Hash] rules to find mathing ast nodes.
|
163
165
|
# @param block [Block] block code to continue operating on the matching nodes.
|
164
166
|
def within_node(rules, &block)
|
165
|
-
Rewriter::WithinScope.new(self, rules, &block).process
|
167
|
+
Rewriter::WithinScope.new(self, rules, { recursive: true }, &block).process
|
166
168
|
end
|
167
169
|
|
170
|
+
alias with_node within_node
|
171
|
+
|
172
|
+
# Parse within_direct_node dsl, it creates a [Synvert::Core::Rewriter::WithinScope] to find direct matching ast nodes,
|
173
|
+
# then continue operating on each matching ast node.
|
174
|
+
#
|
175
|
+
# @param rules [Hash] rules to find mathing ast nodes.
|
176
|
+
# @param block [Block] block code to continue operating on the matching nodes.
|
177
|
+
def within_direct_node(rules, &block)
|
178
|
+
Rewriter::WithinScope.new(self, rules, { recursive: false }, &block).process
|
179
|
+
end
|
180
|
+
|
181
|
+
alias with_direct_node within_direct_node
|
182
|
+
|
168
183
|
# Parse goto_node dsl, it creates a [Synvert::Core::Rewriter::GotoScope] to go to a child node,
|
169
184
|
# then continue operating on the child node.
|
170
185
|
#
|
@@ -174,8 +189,6 @@ module Synvert::Core
|
|
174
189
|
Rewriter::GotoScope.new(self, child_node_name, &block).process
|
175
190
|
end
|
176
191
|
|
177
|
-
alias_method :with_node, :within_node
|
178
|
-
|
179
192
|
# Parse if_exist_node dsl, it creates a [Synvert::Core::Rewriter::IfExistCondition] to check
|
180
193
|
# if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
|
181
194
|
#
|
@@ -209,7 +222,7 @@ module Synvert::Core
|
|
209
222
|
#
|
210
223
|
# @param code [String] code need to be appended.
|
211
224
|
# @param options [Hash] action options.
|
212
|
-
def append(code, options={})
|
225
|
+
def append(code, options = {})
|
213
226
|
@actions << Rewriter::AppendAction.new(self, code, options)
|
214
227
|
end
|
215
228
|
|
@@ -218,7 +231,7 @@ module Synvert::Core
|
|
218
231
|
#
|
219
232
|
# @param code [String] code need to be inserted.
|
220
233
|
# @param options [Hash] action options.
|
221
|
-
def insert(code, options={})
|
234
|
+
def insert(code, options = {})
|
222
235
|
@actions << Rewriter::InsertAction.new(self, code, options)
|
223
236
|
end
|
224
237
|
|
@@ -227,7 +240,7 @@ module Synvert::Core
|
|
227
240
|
#
|
228
241
|
# @param code [String] code need to be inserted.
|
229
242
|
# @param options [Hash] action options.
|
230
|
-
def insert_after(node, options={})
|
243
|
+
def insert_after(node, options = {})
|
231
244
|
@actions << Rewriter::InsertAfterAction.new(self, node, options)
|
232
245
|
end
|
233
246
|
|
@@ -236,7 +249,7 @@ module Synvert::Core
|
|
236
249
|
#
|
237
250
|
# @param code [String] code need to be replaced with.
|
238
251
|
# @param options [Hash] action options.
|
239
|
-
def replace_with(code, options={})
|
252
|
+
def replace_with(code, options = {})
|
240
253
|
@actions << Rewriter::ReplaceWithAction.new(self, code, options)
|
241
254
|
end
|
242
255
|
|
@@ -258,7 +271,7 @@ module Synvert::Core
|
|
258
271
|
@rewriter.add_warning Rewriter::Warning.new(self, message)
|
259
272
|
end
|
260
273
|
|
261
|
-
|
274
|
+
private
|
262
275
|
|
263
276
|
# It changes source code from bottom to top, and it can change source code twice at the same time,
|
264
277
|
# So if there is an overlap between two actions, it removes the conflict actions and operate them in the next loop.
|
@@ -290,7 +303,8 @@ module Synvert::Core
|
|
290
303
|
source_arr = source.split("\n")
|
291
304
|
if source_arr[line - 1] && source_arr[line - 1].strip.empty?
|
292
305
|
source_arr.delete_at(line - 1)
|
293
|
-
if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] &&
|
306
|
+
if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] &&
|
307
|
+
source_arr[line - 1].strip.empty?
|
294
308
|
source_arr.delete_at(line - 1)
|
295
309
|
end
|
296
310
|
source_arr.join("\n") + (newline_at_end_of_line ? "\n" : '')
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# Go to and change its scope to a child node.
|
@@ -18,6 +18,7 @@ module Synvert::Core
|
|
18
18
|
def process
|
19
19
|
current_node = @instance.current_node
|
20
20
|
return unless current_node
|
21
|
+
|
21
22
|
child_node = current_node.send @child_node_name
|
22
23
|
@instance.process_with_other_node child_node do
|
23
24
|
@instance.instance_eval &@block
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Synvert::Core
|
4
4
|
# WithinScope finds out nodes which match rules, then change its scope to matching node.
|
@@ -7,10 +7,12 @@ module Synvert::Core
|
|
7
7
|
#
|
8
8
|
# @param instance [Synvert::Core::Rewriter::Instance]
|
9
9
|
# @param rules [Hash]
|
10
|
+
# @param options [Hash]
|
10
11
|
# @param block [Block]
|
11
|
-
def initialize(instance, rules, &block)
|
12
|
+
def initialize(instance, rules, options = { recursive: true }, &block)
|
12
13
|
@instance = instance
|
13
14
|
@rules = rules
|
15
|
+
@options = options
|
14
16
|
@block = block
|
15
17
|
end
|
16
18
|
|
@@ -19,12 +21,9 @@ module Synvert::Core
|
|
19
21
|
def process
|
20
22
|
current_node = @instance.current_node
|
21
23
|
return unless current_node
|
24
|
+
|
25
|
+
matching_nodes = find_matching_nodes(current_node)
|
22
26
|
@instance.process_with_node current_node do
|
23
|
-
matching_nodes = []
|
24
|
-
matching_nodes << current_node if current_node.match? @rules
|
25
|
-
current_node.recursive_children do |child_node|
|
26
|
-
matching_nodes << child_node if child_node.match? @rules
|
27
|
-
end
|
28
27
|
matching_nodes.each do |matching_node|
|
29
28
|
@instance.process_with_node matching_node do
|
30
29
|
@instance.instance_eval &@block
|
@@ -32,5 +31,22 @@ module Synvert::Core
|
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def find_matching_nodes(current_node)
|
38
|
+
matching_nodes = []
|
39
|
+
if @options[:recursive]
|
40
|
+
matching_nodes << current_node if current_node.match? @rules
|
41
|
+
current_node.recursive_children do |child_node|
|
42
|
+
matching_nodes << child_node if child_node.match? @rules
|
43
|
+
end
|
44
|
+
else
|
45
|
+
current_node.each do |child_node|
|
46
|
+
matching_nodes << child_node if child_node.match? @rules
|
47
|
+
end
|
48
|
+
end
|
49
|
+
matching_nodes
|
50
|
+
end
|
35
51
|
end
|
36
52
|
end
|
data/lib/synvert/core/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Synvert::Core
|
4
6
|
describe Configuration do
|
5
7
|
it 'sets / gets' do
|
6
8
|
Configuration.instance.set :key, 'value'
|
7
|
-
expect(Configuration.instance.get
|
9
|
+
expect(Configuration.instance.get(:key)).to eq 'value'
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
@@ -1,39 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module Synvert::Core
|
4
6
|
describe Engine::ERB do
|
5
|
-
it
|
6
|
-
source
|
7
|
-
<%content_for :head do%>
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
<%end%>
|
14
|
-
|
15
|
-
<%
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
%>
|
20
|
-
|
21
|
-
<% if User.current &&
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
<% end %>
|
7
|
+
it 'encodes / decodes' do
|
8
|
+
source = <<~EOF
|
9
|
+
<%content_for :head do%>
|
10
|
+
<style>
|
11
|
+
body {
|
12
|
+
background-image: url(<%= asset_path('bg.png') %>);
|
13
|
+
}
|
14
|
+
</style>
|
15
|
+
<%end%>
|
16
|
+
|
17
|
+
<%
|
18
|
+
foo = 'bar'
|
19
|
+
post = Post.find(:first)
|
20
|
+
bar = 'foo'
|
21
|
+
%>
|
22
|
+
|
23
|
+
<% if User.current &&
|
24
|
+
User.current.admin %>
|
25
|
+
<%= rounded_content("page") do %>
|
26
|
+
<div class='test'>
|
27
|
+
<% if post %>
|
28
|
+
<div id="title"><%= foo %></div>
|
29
|
+
<% form_for post do |f| %>
|
30
|
+
<label><%= link_to_function 'test', "confirm('test');" %></label>
|
31
|
+
<%= f.text_field 'bar' %>
|
32
|
+
<% end %>
|
33
|
+
<% end %></div>
|
34
|
+
<% end %>
|
35
|
+
<% end %>
|
34
36
|
EOF
|
35
37
|
encoded_source = Engine::ERB.encode(source)
|
36
|
-
buffer = Parser::Source::Buffer.new
|
38
|
+
buffer = Parser::Source::Buffer.new '(test)'
|
37
39
|
buffer.source = encoded_source
|
38
40
|
parser = Parser::CurrentRuby.new
|
39
41
|
parser.reset
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Parser::AST::Node do
|
@@ -101,13 +103,13 @@ describe Parser::AST::Node do
|
|
101
103
|
|
102
104
|
describe '#arguments' do
|
103
105
|
it 'gets for def node' do
|
104
|
-
node = parse(
|
105
|
-
expect(node.arguments.map { |argument| argument.to_source }).to eq [
|
106
|
+
node = parse('def test(foo, bar); foo + bar; end')
|
107
|
+
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
106
108
|
end
|
107
109
|
|
108
110
|
it 'gets for defs node' do
|
109
|
-
node = parse(
|
110
|
-
expect(node.arguments.map { |argument| argument.to_source }).to eq [
|
111
|
+
node = parse('def self.test(foo, bar); foo + bar; end')
|
112
|
+
expect(node.arguments.map { |argument| argument.to_source }).to eq %w[foo bar]
|
111
113
|
end
|
112
114
|
|
113
115
|
it 'gets for block node' do
|
@@ -170,129 +172,129 @@ describe Parser::AST::Node do
|
|
170
172
|
end
|
171
173
|
end
|
172
174
|
|
173
|
-
describe
|
175
|
+
describe '#keys' do
|
174
176
|
it 'gets for hash node' do
|
175
177
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
176
178
|
expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
|
177
179
|
end
|
178
180
|
end
|
179
181
|
|
180
|
-
describe
|
182
|
+
describe '#values' do
|
181
183
|
it 'gets for hash node' do
|
182
184
|
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
183
185
|
expect(node.values).to eq [parse(':bar'), parse("'bar'")]
|
184
186
|
end
|
185
187
|
end
|
186
188
|
|
187
|
-
describe
|
188
|
-
it
|
189
|
-
node = parse(
|
189
|
+
describe '#has_key?' do
|
190
|
+
it 'gets true if key exists' do
|
191
|
+
node = parse('{:foo => :bar}')
|
190
192
|
expect(node.has_key?(:foo)).to be_truthy
|
191
193
|
end
|
192
194
|
|
193
|
-
it
|
194
|
-
node = parse(
|
195
|
+
it 'gets false if key does not exist' do
|
196
|
+
node = parse('{:foo => :bar}')
|
195
197
|
expect(node.has_key?('foo')).to be_falsey
|
196
198
|
end
|
197
199
|
end
|
198
200
|
|
199
|
-
describe
|
200
|
-
it
|
201
|
-
node = parse(
|
201
|
+
describe '#hash_value' do
|
202
|
+
it 'gets value of specified key' do
|
203
|
+
node = parse('{:foo => :bar}')
|
202
204
|
expect(node.hash_value(:foo)).to eq parse(':bar')
|
203
205
|
end
|
204
206
|
|
205
|
-
it
|
206
|
-
node = parse(
|
207
|
+
it 'gets nil if key does not exist' do
|
208
|
+
node = parse('{:foo => :bar}')
|
207
209
|
expect(node.hash_value(:bar)).to be_nil
|
208
210
|
end
|
209
211
|
end
|
210
212
|
|
211
|
-
describe
|
213
|
+
describe '#key' do
|
212
214
|
it 'gets for pair node' do
|
213
215
|
node = parse("{:foo => 'bar'}").children[0]
|
214
216
|
expect(node.key).to eq parse(':foo')
|
215
217
|
end
|
216
218
|
end
|
217
219
|
|
218
|
-
describe
|
220
|
+
describe '#value' do
|
219
221
|
it 'gets for hash node' do
|
220
222
|
node = parse("{:foo => 'bar'}").children[0]
|
221
223
|
expect(node.value).to eq parse("'bar'")
|
222
224
|
end
|
223
225
|
end
|
224
226
|
|
225
|
-
describe
|
227
|
+
describe '#condition' do
|
226
228
|
it 'gets for if node' do
|
227
229
|
node = parse('if defined?(Bundler); end')
|
228
230
|
expect(node.condition).to eq parse('defined?(Bundler)')
|
229
231
|
end
|
230
232
|
end
|
231
233
|
|
232
|
-
describe
|
234
|
+
describe '#left_value' do
|
233
235
|
it 'gets for masgn' do
|
234
|
-
node = parse(
|
236
|
+
node = parse('a, b = 1, 2')
|
235
237
|
expect(node.left_value.to_source).to eq 'a, b'
|
236
238
|
end
|
237
239
|
|
238
240
|
it 'gets for lvasgn' do
|
239
|
-
node = parse(
|
241
|
+
node = parse('a = 1')
|
240
242
|
expect(node.left_value).to eq :a
|
241
243
|
end
|
242
244
|
|
243
245
|
it 'gets for ivasgn' do
|
244
|
-
node = parse(
|
246
|
+
node = parse('@a = 1')
|
245
247
|
expect(node.left_value).to eq :@a
|
246
248
|
end
|
247
249
|
end
|
248
250
|
|
249
|
-
describe
|
251
|
+
describe '#right_value' do
|
250
252
|
it 'gets for masgn' do
|
251
|
-
node = parse(
|
253
|
+
node = parse('a, b = 1, 2')
|
252
254
|
expect(node.right_value).to eq parse('[1, 2]')
|
253
255
|
end
|
254
256
|
|
255
257
|
it 'gets for masgn' do
|
256
|
-
node = parse(
|
257
|
-
expect(node.right_value).to eq parse(
|
258
|
+
node = parse('a, b = params')
|
259
|
+
expect(node.right_value).to eq parse('params')
|
258
260
|
end
|
259
261
|
|
260
262
|
it 'gets for lvasgn' do
|
261
|
-
node = parse(
|
262
|
-
expect(node.right_value).to eq parse(
|
263
|
+
node = parse('a = 1')
|
264
|
+
expect(node.right_value).to eq parse('1')
|
263
265
|
end
|
264
266
|
|
265
267
|
it 'gets for ivasgn' do
|
266
|
-
node = parse(
|
267
|
-
expect(node.right_value).to eq parse(
|
268
|
+
node = parse('@a = 1')
|
269
|
+
expect(node.right_value).to eq parse('1')
|
268
270
|
end
|
269
271
|
end
|
270
272
|
|
271
|
-
describe
|
273
|
+
describe '#to_value' do
|
272
274
|
it 'gets for int' do
|
273
|
-
node = parse(
|
275
|
+
node = parse('1')
|
274
276
|
expect(node.to_value).to eq 1
|
275
277
|
end
|
276
278
|
|
277
279
|
it 'gets for string' do
|
278
280
|
node = parse("'str'")
|
279
|
-
expect(node.to_value).to eq
|
281
|
+
expect(node.to_value).to eq 'str'
|
280
282
|
end
|
281
283
|
|
282
284
|
it 'gets for symbol' do
|
283
|
-
node = parse(
|
285
|
+
node = parse(':str')
|
284
286
|
expect(node.to_value).to eq :str
|
285
287
|
end
|
286
288
|
|
287
289
|
it 'get for boolean' do
|
288
|
-
node = parse(
|
290
|
+
node = parse('true')
|
289
291
|
expect(node.to_value).to be_truthy
|
290
|
-
node = parse(
|
292
|
+
node = parse('false')
|
291
293
|
expect(node.to_value).to be_falsey
|
292
294
|
end
|
293
295
|
|
294
296
|
it 'get for range' do
|
295
|
-
node = parse(
|
297
|
+
node = parse('(1..10)')
|
296
298
|
expect(node.to_value).to eq (1..10)
|
297
299
|
end
|
298
300
|
|
@@ -382,13 +384,13 @@ describe Parser::AST::Node do
|
|
382
384
|
it 'matches arguments any' do
|
383
385
|
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
384
386
|
node = parse(source)
|
385
|
-
expect(node).to be_match(type: 'send', arguments: {any: 'Lifo::Cache'})
|
387
|
+
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
386
388
|
end
|
387
389
|
|
388
390
|
it 'matches not' do
|
389
391
|
source = 'class Synvert; end'
|
390
392
|
node = parse(source)
|
391
|
-
expect(node).not_to be_match(type: 'class', name: {not: 'Synvert'})
|
393
|
+
expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
|
392
394
|
end
|
393
395
|
end
|
394
396
|
|
@@ -413,24 +415,25 @@ describe Parser::AST::Node do
|
|
413
415
|
it 'rewrites for ArgumentsNode' do
|
414
416
|
source = 'test { |a, b| }'
|
415
417
|
node = parse(source)
|
416
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
418
|
+
expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
|
417
419
|
end
|
418
420
|
|
419
|
-
it 'rewrites array with multi line given as argument for method'do
|
420
|
-
source =
|
421
|
-
long_name_method([
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
])
|
421
|
+
it 'rewrites array with multi line given as argument for method' do
|
422
|
+
source = <<~EOS.strip
|
423
|
+
long_name_method([
|
424
|
+
1,
|
425
|
+
2,
|
426
|
+
3
|
427
|
+
])
|
426
428
|
EOS
|
429
|
+
|
427
430
|
node = parse(source)
|
428
|
-
expect(node.rewritten_source('{{arguments}}')).to eq
|
429
|
-
[
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
]
|
431
|
+
expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
|
432
|
+
[
|
433
|
+
1,
|
434
|
+
2,
|
435
|
+
3
|
436
|
+
]
|
434
437
|
EOS
|
435
438
|
end
|
436
439
|
end
|