synvert-core 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile +0 -3
- data/Guardfile +0 -9
- data/README.md +30 -12
- data/Rakefile +1 -15
- data/lib/synvert/core/engine/erb.rb +1 -1
- data/lib/synvert/core/engine.rb +1 -1
- data/lib/synvert/core/node_ext.rb +0 -466
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +20 -17
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/instance.rb +83 -133
- data/lib/synvert/core/rewriter/scope/query_scope.rb +2 -2
- data/lib/synvert/core/rewriter/scope/within_scope.rb +4 -4
- data/lib/synvert/core/rewriter.rb +0 -10
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +4 -6
- data/spec/synvert/core/engine/erb_spec.rb +3 -3
- data/spec/synvert/core/node_ext_spec.rb +0 -795
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +21 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +47 -115
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +1 -4
- data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +1 -4
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +1 -4
- data/synvert-core-ruby.gemspec +4 -2
- metadata +44 -61
- data/lib/synvert/core/array_ext.rb +0 -48
- data/lib/synvert/core/node_query/compiler/array.rb +0 -34
- data/lib/synvert/core/node_query/compiler/attribute.rb +0 -39
- data/lib/synvert/core/node_query/compiler/attribute_list.rb +0 -24
- data/lib/synvert/core/node_query/compiler/basic_selector.rb +0 -28
- data/lib/synvert/core/node_query/compiler/boolean.rb +0 -23
- data/lib/synvert/core/node_query/compiler/comparable.rb +0 -86
- data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +0 -51
- data/lib/synvert/core/node_query/compiler/expression.rb +0 -41
- data/lib/synvert/core/node_query/compiler/float.rb +0 -23
- data/lib/synvert/core/node_query/compiler/identifier.rb +0 -41
- data/lib/synvert/core/node_query/compiler/integer.rb +0 -23
- data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/nil.rb +0 -23
- data/lib/synvert/core/node_query/compiler/parse_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/regexp.rb +0 -37
- data/lib/synvert/core/node_query/compiler/selector.rb +0 -113
- data/lib/synvert/core/node_query/compiler/string.rb +0 -23
- data/lib/synvert/core/node_query/compiler/symbol.rb +0 -23
- data/lib/synvert/core/node_query/compiler.rb +0 -25
- data/lib/synvert/core/node_query/lexer.rex +0 -99
- data/lib/synvert/core/node_query/lexer.rex.rb +0 -299
- data/lib/synvert/core/node_query/parser.racc.rb +0 -306
- data/lib/synvert/core/node_query/parser.y +0 -60
- data/lib/synvert/core/node_query.rb +0 -36
- data/lib/synvert/core/rewriter/action/append_action.rb +0 -28
- data/lib/synvert/core/rewriter/action/delete_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +0 -22
- data/lib/synvert/core/rewriter/action/prepend_action.rb +0 -44
- data/lib/synvert/core/rewriter/action/remove_action.rb +0 -56
- data/lib/synvert/core/rewriter/action/replace_action.rb +0 -33
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +0 -36
- data/lib/synvert/core/rewriter/action/wrap_action.rb +0 -37
- data/lib/synvert/core/rewriter/action.rb +0 -102
- data/spec/synvert/core/node_query/lexer_spec.rb +0 -580
- data/spec/synvert/core/node_query/parser_spec.rb +0 -337
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/delete_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +0 -175
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/replace_action_spec.rb +0 -28
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +0 -59
- data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +0 -31
- data/spec/synvert/core/rewriter/action_spec.rb +0 -14
@@ -1,337 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
def assert_parser(source)
|
4
|
-
expect(parser.parse(source).to_s).to eq source
|
5
|
-
end
|
6
|
-
|
7
|
-
module Synvert::Core::NodeQuery
|
8
|
-
RSpec.describe Parser do
|
9
|
-
let(:parser) { described_class.new }
|
10
|
-
|
11
|
-
describe '#toString' do
|
12
|
-
it 'parses one selector' do
|
13
|
-
source = '.send[message=:create]'
|
14
|
-
assert_parser(source)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'parses two selectors' do
|
18
|
-
source = '.class[name=Synvert] .def[name="foobar"]'
|
19
|
-
assert_parser(source)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'parses three selectors' do
|
23
|
-
source = '.class[name=Synvert] .def[name="foobar"] .send[message=create]'
|
24
|
-
assert_parser(source)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'parses child selector' do
|
28
|
-
source = '.class[name=Synvert] > .def[name="foobar"]'
|
29
|
-
assert_parser(source)
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'parses scope' do
|
33
|
-
source = '.block body > .send'
|
34
|
-
assert_parser(source)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'parses :has selector' do
|
38
|
-
source = '.class :has(> .def)'
|
39
|
-
assert_parser(source)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'parses :not_has selector' do
|
43
|
-
source = '.class :not_has(> .def)'
|
44
|
-
assert_parser(source)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'parses root :has selector' do
|
48
|
-
source = ':has(.def)'
|
49
|
-
assert_parser(source)
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'parses multiple attributes' do
|
53
|
-
source = '.send[receiver=nil][message=:create]'
|
54
|
-
assert_parser(source)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'parses nested attributes' do
|
58
|
-
source = '.send[receiver.message=:create]'
|
59
|
-
assert_parser(source)
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'parses selector value' do
|
63
|
-
source = '.send[receiver=.send[message=:create]]'
|
64
|
-
assert_parser(source)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'parses start with operator' do
|
68
|
-
source = '.def[name^=synvert]'
|
69
|
-
assert_parser(source)
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'parses end with operator' do
|
73
|
-
source = '.def[name$=synvert]'
|
74
|
-
assert_parser(source)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'parses contain operator' do
|
78
|
-
source = '.def[name*=synvert]'
|
79
|
-
assert_parser(source)
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'parses not equal operator' do
|
83
|
-
source = '.send[receiver=.send[message!=:create]]'
|
84
|
-
assert_parser(source)
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'parses greater than operator' do
|
88
|
-
source = '.send[receiver=.send[arguments.size>1]]'
|
89
|
-
assert_parser(source)
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'parses greater than or equal operator' do
|
93
|
-
source = '.send[receiver=.send[arguments.size>=1]]'
|
94
|
-
assert_parser(source)
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'parses less than operator' do
|
98
|
-
source = '.send[receiver=.send[arguments.size<1]]'
|
99
|
-
assert_parser(source)
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'parses less than or equal operator' do
|
103
|
-
source = '.send[receiver=.send[arguments.size<=1]]'
|
104
|
-
assert_parser(source)
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'parses in operator' do
|
108
|
-
source = '.def[name in (foo bar)]'
|
109
|
-
assert_parser(source)
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'parses not_in operator' do
|
113
|
-
source = '.def[name not in (foo bar)]'
|
114
|
-
assert_parser(source)
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'parses includes operator' do
|
118
|
-
source = '.def[arguments includes &block]'
|
119
|
-
assert_parser(source)
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'parses empty string' do
|
123
|
-
source = '.send[arguments.first=""]'
|
124
|
-
assert_parser(source)
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'parses []=' do
|
128
|
-
source = '.send[message=[]=]'
|
129
|
-
assert_parser(source)
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'parses :[]' do
|
133
|
-
source = '.send[message=:[]]'
|
134
|
-
assert_parser(source)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
describe '#query_nodes' do
|
139
|
-
let(:node) {
|
140
|
-
parse(<<~EOS)
|
141
|
-
class Synvert
|
142
|
-
def foo
|
143
|
-
FactoryBot.create(:user, name: 'foo')
|
144
|
-
end
|
145
|
-
|
146
|
-
def bar
|
147
|
-
FactoryBot.create(:user, name: 'bar')
|
148
|
-
end
|
149
|
-
|
150
|
-
def foobar(a, b)
|
151
|
-
{ a: a, b: b }
|
152
|
-
arr[index]
|
153
|
-
arr[index] = value
|
154
|
-
nil?
|
155
|
-
call('')
|
156
|
-
end
|
157
|
-
end
|
158
|
-
EOS
|
159
|
-
}
|
160
|
-
|
161
|
-
let(:test_node) {
|
162
|
-
parse(<<~EOS)
|
163
|
-
RSpec.describe Synvert do
|
164
|
-
end
|
165
|
-
EOS
|
166
|
-
}
|
167
|
-
|
168
|
-
it 'matches class node' do
|
169
|
-
expression = parser.parse('.class[name=Synvert]')
|
170
|
-
expect(expression.query_nodes(node)).to eq [node]
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'matches def node' do
|
174
|
-
expression = parser.parse('.def')
|
175
|
-
expect(expression.query_nodes(node)).to eq node.body
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'matches start with' do
|
179
|
-
expression = parser.parse('.def[name^=foo]')
|
180
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.last]
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'matches end with' do
|
184
|
-
expression = parser.parse('.def[name$=bar]')
|
185
|
-
expect(expression.query_nodes(node)).to eq [node.body.second, node.body.last]
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'matches contain' do
|
189
|
-
expression = parser.parse('.def[name*=oob]')
|
190
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
191
|
-
end
|
192
|
-
|
193
|
-
it 'matches not equal' do
|
194
|
-
expression = parser.parse('.def[name!=foobar]')
|
195
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second]
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'matches in' do
|
199
|
-
expression = parser.parse('.def[name IN (foo bar)]')
|
200
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second]
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'matches not in' do
|
204
|
-
expression = parser.parse('.def[name NOT IN (foo bar)]')
|
205
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'matches includes' do
|
209
|
-
expression = parser.parse('.def[arguments INCLUDES a]')
|
210
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'matches equal array' do
|
214
|
-
expression = parser.parse('.def[arguments=(a b)]')
|
215
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
216
|
-
|
217
|
-
expression = parser.parse('.def[arguments=(b a)]')
|
218
|
-
expect(expression.query_nodes(node)).to eq []
|
219
|
-
end
|
220
|
-
|
221
|
-
it 'matches not equal array' do
|
222
|
-
expression = parser.parse('.def[arguments!=(a b)]')
|
223
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second]
|
224
|
-
|
225
|
-
expression = parser.parse('.def[arguments!=(b a)]')
|
226
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second, node.body.last]
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'matches descendant node' do
|
230
|
-
expression = parser.parse('.class .send[message=:create]')
|
231
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
232
|
-
end
|
233
|
-
|
234
|
-
it 'matches three level descendant node' do
|
235
|
-
expression = parser.parse('.class .def .send[message=:create]')
|
236
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
237
|
-
end
|
238
|
-
|
239
|
-
it 'matches child node' do
|
240
|
-
expression = parser.parse('.def > .send[message=:create]')
|
241
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
242
|
-
end
|
243
|
-
|
244
|
-
it 'matches next sibling node' do
|
245
|
-
expression = parser.parse('.def[name=foo] + .def[name=bar]')
|
246
|
-
expect(expression.query_nodes(node)).to eq [node.body.second]
|
247
|
-
end
|
248
|
-
|
249
|
-
it 'matches sebsequent sibling node' do
|
250
|
-
expression = parser.parse('.def[name=foo] ~ .def[name=foobar]')
|
251
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'matches goto scope' do
|
255
|
-
expression = parser.parse('.def body > .send[message=:create]')
|
256
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
257
|
-
|
258
|
-
expression = parser.parse('.def body .send[message=:create]')
|
259
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
260
|
-
end
|
261
|
-
|
262
|
-
it 'matches multiple goto scope' do
|
263
|
-
expression = parser.parse('.block caller.arguments .const[name=Synvert]')
|
264
|
-
expect(expression.query_nodes(test_node)).to eq [test_node.caller.arguments.first]
|
265
|
-
end
|
266
|
-
|
267
|
-
it 'matches has selector' do
|
268
|
-
expression = parser.parse('.def:has(> .send[receiver=FactoryBot])')
|
269
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.second]
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'matches not_has selector' do
|
273
|
-
expression = parser.parse('.def:not_has(> .send[receiver=FactoryBot])')
|
274
|
-
expect(expression.query_nodes(node)).to eq [node.body.last]
|
275
|
-
end
|
276
|
-
|
277
|
-
it 'matches root has selector' do
|
278
|
-
expression = parser.parse(':has(.def[name=foobar])')
|
279
|
-
expect(expression.query_nodes(node)).to eq [node]
|
280
|
-
end
|
281
|
-
|
282
|
-
it 'matches arguments.size' do
|
283
|
-
expression = parser.parse('.def .send[arguments.size=2]')
|
284
|
-
expect(expression.query_nodes(node)).to eq [
|
285
|
-
node.body.first.body.last,
|
286
|
-
node.body.second.body.last,
|
287
|
-
node.body.third.body.third
|
288
|
-
]
|
289
|
-
expression = parser.parse('.def .send[arguments.size>2]')
|
290
|
-
expect(expression.query_nodes(node)).to eq []
|
291
|
-
expression = parser.parse('.def .send[arguments.size>=2]')
|
292
|
-
expect(expression.query_nodes(node)).to eq [
|
293
|
-
node.body.first.body.last,
|
294
|
-
node.body.second.body.last,
|
295
|
-
node.body.third.body.third
|
296
|
-
]
|
297
|
-
end
|
298
|
-
|
299
|
-
it 'matches arguments' do
|
300
|
-
expression = parser.parse('.send[arguments.size=2][arguments.first=.sym][arguments.last=.hash]')
|
301
|
-
expect(expression.query_nodes(node)).to eq [node.body.first.body.last, node.body.second.body.last]
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'matches regexp value' do
|
305
|
-
expression = parser.parse('.def[name=~/foo/]')
|
306
|
-
expect(expression.query_nodes(node)).to eq [node.body.first, node.body.last]
|
307
|
-
expression = parser.parse('.def[name!~/bar/]')
|
308
|
-
expect(expression.query_nodes(node)).to eq [node.body.first]
|
309
|
-
end
|
310
|
-
|
311
|
-
it 'matches attribute value' do
|
312
|
-
expression = parser.parse('.pair[key={{value}}]')
|
313
|
-
expect(expression.query_nodes(node)).to eq node.body.last.body.first.children
|
314
|
-
end
|
315
|
-
|
316
|
-
it 'matches []' do
|
317
|
-
expression = parser.parse('.send[message=[]]')
|
318
|
-
expect(expression.query_nodes(node)).to eq [node.body.last.body.second]
|
319
|
-
end
|
320
|
-
|
321
|
-
it 'matches []=' do
|
322
|
-
expression = parser.parse('.send[message=:[]=]')
|
323
|
-
expect(expression.query_nodes(node)).to eq [node.body.last.body.third]
|
324
|
-
end
|
325
|
-
|
326
|
-
it 'matches nil and nil?' do
|
327
|
-
expression = parser.parse('.send[receiver=nil][message=nil?]')
|
328
|
-
expect(expression.query_nodes(node)).to eq [node.body.last.body.fourth]
|
329
|
-
end
|
330
|
-
|
331
|
-
it 'matches empty string' do
|
332
|
-
expression = parser.parse('.send[message=call][arguments.first=""]')
|
333
|
-
expect(expression.query_nodes(node)).to eq [node.body.last.body.last]
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::AppendAction do
|
7
|
-
describe 'class node' do
|
8
|
-
subject do
|
9
|
-
source = "class User\n has_many :posts\nend"
|
10
|
-
class_node = Parser::CurrentRuby.parse(source)
|
11
|
-
instance = double(current_node: class_node)
|
12
|
-
Rewriter::AppendAction.new(instance, "def as_json\n super\nend").process
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'gets begin_pos' do
|
16
|
-
expect(subject.begin_pos).to eq "class User\n has_many :posts".length
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'gets end_pos' do
|
20
|
-
expect(subject.end_pos).to eq "class User\n has_many :posts".length
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'gets rewritten_code' do
|
24
|
-
expect(subject.rewritten_code).to eq "\n\n def as_json\n super\n end"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'begin node' do
|
29
|
-
subject do
|
30
|
-
source = "gem 'rails'\ngem 'mysql2'"
|
31
|
-
begin_node = Parser::CurrentRuby.parse(source)
|
32
|
-
instance = double(current_node: begin_node)
|
33
|
-
Rewriter::AppendAction.new(instance, "gem 'twitter'").process
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'gets begin_pos' do
|
37
|
-
expect(subject.begin_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'gets end_pos' do
|
41
|
-
expect(subject.end_pos).to eq "gem 'rails'\ngem 'mysql2'".length
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'gets rewritten_code' do
|
45
|
-
expect(subject.rewritten_code).to eq "\ngem 'twitter'"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'def node' do
|
50
|
-
subject do
|
51
|
-
source = "def teardown\n do_something\nend"
|
52
|
-
class_node = Parser::CurrentRuby.parse(source)
|
53
|
-
instance = double(current_node: class_node)
|
54
|
-
Rewriter::AppendAction.new(instance, 'super').process
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'gets begin_pos' do
|
58
|
-
expect(subject.begin_pos).to eq "def teardown\n do_something".length
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'gets end_pos' do
|
62
|
-
expect(subject.end_pos).to eq "def teardown\n do_something".length
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'gets rewritten_code' do
|
66
|
-
expect(subject.rewritten_code).to eq "\n super"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::DeleteAction do
|
7
|
-
subject {
|
8
|
-
source = 'arr.map {}.flatten'
|
9
|
-
node = Parser::CurrentRuby.parse(source)
|
10
|
-
instance = double(current_node: node, file_source: source)
|
11
|
-
Rewriter::DeleteAction.new(instance, :dot, :message).process
|
12
|
-
}
|
13
|
-
|
14
|
-
it 'gets begin_pos' do
|
15
|
-
expect(subject.begin_pos).to eq 'arr.map {}'.length
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'gets end_pos' do
|
19
|
-
expect(subject.end_pos).to eq 'arr.map {}.flatten'.length
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'gets rewritten_code' do
|
23
|
-
expect(subject.rewritten_code).to eq ''
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::InsertAction do
|
7
|
-
context 'at end' do
|
8
|
-
subject {
|
9
|
-
source = " User.where(username: 'Richard')"
|
10
|
-
node = Parser::CurrentRuby.parse(source)
|
11
|
-
instance = double(current_node: node)
|
12
|
-
Rewriter::InsertAction.new(instance, '.first', at: 'end').process
|
13
|
-
}
|
14
|
-
|
15
|
-
it 'gets begin_pos' do
|
16
|
-
expect(subject.begin_pos).to eq " User.where(username: 'Richard')".length
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'gets end_pos' do
|
20
|
-
expect(subject.end_pos).to eq " User.where(username: 'Richard')".length
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'gets rewritten_code' do
|
24
|
-
expect(subject.rewritten_code).to eq '.first'
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'at beginning' do
|
29
|
-
subject {
|
30
|
-
source = " open('http://test.com')"
|
31
|
-
node = Parser::CurrentRuby.parse(source)
|
32
|
-
instance = double(current_node: node)
|
33
|
-
Rewriter::InsertAction.new(instance, 'URI.', at: 'beginning').process
|
34
|
-
}
|
35
|
-
|
36
|
-
it 'gets begin_pos' do
|
37
|
-
expect(subject.begin_pos).to eq 2
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'gets end_pos' do
|
41
|
-
expect(subject.end_pos).to eq 2
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'gets rewritten_code' do
|
45
|
-
expect(subject.rewritten_code).to eq 'URI.'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'to receiver' do
|
50
|
-
subject {
|
51
|
-
source = "User.where(username: 'Richard')"
|
52
|
-
node = Parser::CurrentRuby.parse(source)
|
53
|
-
instance = double(current_node: node)
|
54
|
-
Rewriter::InsertAction.new(instance, '.active', to: 'receiver', at: 'end').process
|
55
|
-
}
|
56
|
-
|
57
|
-
it 'gets begin_pos' do
|
58
|
-
expect(subject.begin_pos).to eq "User".length
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'gets end_pos' do
|
62
|
-
expect(subject.end_pos).to eq "User".length
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'gets rewritten_code' do
|
66
|
-
expect(subject.rewritten_code).to eq '.active'
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::InsertAfterAction do
|
7
|
-
subject {
|
8
|
-
source = ' include Foo'
|
9
|
-
node = Parser::CurrentRuby.parse(source)
|
10
|
-
instance = double(current_node: node)
|
11
|
-
Rewriter::InsertAfterAction.new(instance, 'include Bar').process
|
12
|
-
}
|
13
|
-
|
14
|
-
it 'gets begin_pos' do
|
15
|
-
expect(subject.begin_pos).to eq ' include Foo'.length
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'gets end_pos' do
|
19
|
-
expect(subject.end_pos).to eq ' include Foo'.length
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'gets rewritten_code' do
|
23
|
-
expect(subject.rewritten_code).to eq "\n include Bar"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,175 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Synvert::Core
|
6
|
-
describe Rewriter::PrependAction do
|
7
|
-
describe 'block node without args' do
|
8
|
-
subject {
|
9
|
-
source = "Synvert::Application.configure do\nend"
|
10
|
-
block_node = Parser::CurrentRuby.parse(source)
|
11
|
-
instance = double(current_node: block_node)
|
12
|
-
Rewriter::PrependAction.new(instance, 'config.eager_load = true').process
|
13
|
-
}
|
14
|
-
|
15
|
-
it 'gets begin_pos' do
|
16
|
-
expect(subject.begin_pos).to eq 'Synvert::Application.configure do'.length
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'gets end_pos' do
|
20
|
-
expect(subject.end_pos).to eq 'Synvert::Application.configure do'.length
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'gets rewritten_code' do
|
24
|
-
expect(subject.rewritten_code).to eq "\n config.eager_load = true"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'block node with args' do
|
29
|
-
subject {
|
30
|
-
source = "RSpec.configure do |config|\nend"
|
31
|
-
block_node = Parser::CurrentRuby.parse(source)
|
32
|
-
instance = double(current_node: block_node)
|
33
|
-
Rewriter::PrependAction.new(instance, '{{arguments.first}}.include FactoryGirl::Syntax::Methods').process
|
34
|
-
}
|
35
|
-
|
36
|
-
it 'gets begin_pos' do
|
37
|
-
expect(subject.begin_pos).to eq 'RSpec.configure do |config|'.length
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'gets end_pos' do
|
41
|
-
expect(subject.end_pos).to eq 'RSpec.configure do |config|'.length
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'gets rewritten_code' do
|
45
|
-
expect(subject.rewritten_code).to eq "\n config.include FactoryGirl::Syntax::Methods"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'class node without superclass' do
|
50
|
-
subject {
|
51
|
-
source = "class User\n has_many :posts\nend"
|
52
|
-
class_node = Parser::CurrentRuby.parse(source)
|
53
|
-
instance = double(current_node: class_node)
|
54
|
-
Rewriter::PrependAction.new(instance, 'include Deletable').process
|
55
|
-
}
|
56
|
-
|
57
|
-
it 'gets begin_pos' do
|
58
|
-
expect(subject.begin_pos).to eq 'class User'.length
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'gets end_pos' do
|
62
|
-
expect(subject.end_pos).to eq 'class User'.length
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'gets rewritten_code' do
|
66
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe 'class node with superclass' do
|
71
|
-
subject {
|
72
|
-
source = "class User < ActiveRecord::Base\n has_many :posts\nend"
|
73
|
-
class_node = Parser::CurrentRuby.parse(source)
|
74
|
-
instance = double(current_node: class_node)
|
75
|
-
Rewriter::PrependAction.new(instance, 'include Deletable').process
|
76
|
-
}
|
77
|
-
|
78
|
-
it 'gets begin_pos' do
|
79
|
-
expect(subject.begin_pos).to eq 'class User < ActionRecord::Base'.length
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'gets end_pos' do
|
83
|
-
expect(subject.end_pos).to eq 'class User < ActionRecord::Base'.length
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'gets rewritten_code' do
|
87
|
-
expect(subject.rewritten_code).to eq "\n include Deletable"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'def node without args' do
|
92
|
-
subject do
|
93
|
-
source = "def setup\n do_something\nend"
|
94
|
-
def_node = Parser::CurrentRuby.parse(source)
|
95
|
-
instance = double(current_node: def_node)
|
96
|
-
Rewriter::PrependAction.new(instance, 'super').process
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'gets begin_pos' do
|
100
|
-
expect(subject.begin_pos).to eq 'def setup'.length
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'gets end_pos' do
|
104
|
-
expect(subject.end_pos).to eq 'def setup'.length
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'gets rewritten_code' do
|
108
|
-
expect(subject.rewritten_code).to eq "\n super"
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
describe 'def node with args' do
|
113
|
-
subject do
|
114
|
-
source = "def setup(foobar)\n do_something\nend"
|
115
|
-
def_node = Parser::CurrentRuby.parse(source)
|
116
|
-
instance = double(current_node: def_node)
|
117
|
-
Rewriter::PrependAction.new(instance, 'super').process
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'gets begin_pos' do
|
121
|
-
expect(subject.begin_pos).to eq 'def setup(foobar)'.length
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'gets end_pos' do
|
125
|
-
expect(subject.end_pos).to eq 'def setup(foobar)'.length
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'gets rewritten_code' do
|
129
|
-
expect(subject.rewritten_code).to eq "\n super"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe 'defs node without args' do
|
134
|
-
subject do
|
135
|
-
source = "def self.foo\n do_something\nend"
|
136
|
-
def_node = Parser::CurrentRuby.parse(source)
|
137
|
-
instance = double(current_node: def_node)
|
138
|
-
Rewriter::PrependAction.new(instance, 'do_something_first').process
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'gets begin_pos' do
|
142
|
-
expect(subject.begin_pos).to eq 'def self.foo'.length
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'gets end_pos' do
|
146
|
-
expect(subject.end_pos).to eq 'def self.foo'.length
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'gets rewritten_code' do
|
150
|
-
expect(subject.rewritten_code).to eq "\n do_something_first"
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe 'defs node with args' do
|
155
|
-
subject do
|
156
|
-
source = "def self.foo(bar)\n do_something\nend"
|
157
|
-
def_node = Parser::CurrentRuby.parse(source)
|
158
|
-
instance = double(current_node: def_node)
|
159
|
-
Rewriter::PrependAction.new(instance, 'do_something_first').process
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'gets begin_pos' do
|
163
|
-
expect(subject.begin_pos).to eq 'def self.foo(bar)'.length
|
164
|
-
end
|
165
|
-
|
166
|
-
it 'gets end_pos' do
|
167
|
-
expect(subject.end_pos).to eq 'def self.foo(bar)'.length
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'gets rewritten_code' do
|
171
|
-
expect(subject.rewritten_code).to eq "\n do_something_first"
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
end
|