code_analyzer 0.5.4 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +2 -2
- data/.gitignore +0 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +127 -0
- data/Gemfile.lock +38 -0
- data/code_analyzer.gemspec +2 -1
- data/lib/code_analyzer/checker.rb +7 -7
- data/lib/code_analyzer/checking_visitor/default.rb +7 -7
- data/lib/code_analyzer/node.rb +1186 -0
- data/lib/code_analyzer/version.rb +1 -1
- data/lib/code_analyzer.rb +1 -3
- data/spec/code_analyzer/{sexp_spec.rb → node_spec.rb} +194 -195
- data/spec/spec_helper.rb +1 -1
- metadata +12 -16
- data/lib/code_analyzer/nil.rb +0 -36
- data/lib/code_analyzer/sexp.rb +0 -911
- data/spec/code_analyzer/nil_spec.rb +0 -39
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
|
-
describe
|
|
5
|
+
describe CodeAnalyzer::Node do
|
|
6
6
|
describe 'line_number' do
|
|
7
7
|
before :each do
|
|
8
8
|
content = <<-EOF
|
|
@@ -33,75 +33,75 @@ describe Sexp do
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it 'should return class line' do
|
|
36
|
-
expect(@node.grep_node(
|
|
36
|
+
expect(@node.grep_node(node_type: :class).line_number).to eq 1
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it 'should return def line' do
|
|
40
|
-
expect(@node.grep_node(
|
|
40
|
+
expect(@node.grep_node(node_type: :def).line_number).to eq 2
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
it 'should return const line' do
|
|
44
|
-
expect(@node.grep_node(
|
|
44
|
+
expect(@node.grep_node(node_type: :const_ref).line_number).to eq 1
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
it 'should return top const line' do
|
|
48
|
-
expect(@node.grep_node(
|
|
48
|
+
expect(@node.grep_node(node_type: :top_const_ref).line_number).to eq 20
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
it 'should return const path line' do
|
|
52
|
-
expect(@node.grep_node(
|
|
52
|
+
expect(@node.grep_node(node_type: :const_path_ref).line_number).to eq 3
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it 'should return alias line' do
|
|
56
|
-
expect(@node.grep_node(
|
|
56
|
+
expect(@node.grep_node(node_type: :alias).line_number).to eq 5
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
it 'should return hash line' do
|
|
60
|
-
expect(@node.grep_node(
|
|
60
|
+
expect(@node.grep_node(node_type: :hash).line_number).to eq 6
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
it 'should return massign line' do
|
|
64
|
-
expect(@node.grep_node(
|
|
64
|
+
expect(@node.grep_node(node_type: :massign).line_number).to eq 8
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
it 'should return opassign line' do
|
|
68
|
-
expect(@node.grep_node(
|
|
68
|
+
expect(@node.grep_node(node_type: :opassign).line_number).to eq 11
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
it 'should return if line' do
|
|
72
|
-
expect(@node.grep_node(
|
|
72
|
+
expect(@node.grep_node(node_type: :if).line_number).to eq 14
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it 'should return elsif line' do
|
|
76
|
-
expect(@node.grep_node(
|
|
76
|
+
expect(@node.grep_node(node_type: :elsif).line_number).to eq 16
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
it 'should return if_mod line' do
|
|
80
|
-
expect(@node.grep_node(
|
|
80
|
+
expect(@node.grep_node(node_type: :if_mod).line_number).to eq 15
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
it 'should return unary line' do
|
|
84
|
-
expect(@node.grep_node(
|
|
84
|
+
expect(@node.grep_node(node_type: :unary).line_number).to eq 15
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
it 'should return assign line' do
|
|
88
|
-
expect(@node.grep_node(
|
|
88
|
+
expect(@node.grep_node(node_type: :assign).line_number).to eq 6
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
it 'should return paren line' do
|
|
92
|
-
expect(@node.grep_node(
|
|
92
|
+
expect(@node.grep_node(node_type: :paren).line_number).to eq 10
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it 'should return dot2 line' do
|
|
96
|
-
expect(@node.grep_node(
|
|
96
|
+
expect(@node.grep_node(node_type: :dot2).line_number).to eq 17
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
it 'should return dot3 line' do
|
|
100
|
-
expect(@node.grep_node(
|
|
100
|
+
expect(@node.grep_node(node_type: :dot3).line_number).to eq 18
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
it 'should return params line' do
|
|
104
|
-
expect(@node.grep_node(
|
|
104
|
+
expect(@node.grep_node(node_type: :params).line_number).to be_nil
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
it 'should return params line if not empty' do
|
|
@@ -110,18 +110,18 @@ describe Sexp do
|
|
|
110
110
|
def foo(a, b)
|
|
111
111
|
end
|
|
112
112
|
CODE
|
|
113
|
-
expect(@node.grep_node(
|
|
113
|
+
expect(@node.grep_node(node_type: :params).line_number).to eq 2
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
it 'should return stmts_add line' do
|
|
117
|
-
expect(@node.grep_node(
|
|
117
|
+
expect(@node.grep_node(node_type: :stmts_add).line_number).to eq 13
|
|
118
118
|
end
|
|
119
119
|
|
|
120
120
|
context 'when a complex code is given' do
|
|
121
121
|
before :each do
|
|
122
122
|
@node = parse_content(<<~CODE)
|
|
123
123
|
def foo(num)
|
|
124
|
-
unless (num == 0 ? :zero
|
|
124
|
+
unless (num == 0 ? :zero : :other) || !@opts.right?
|
|
125
125
|
@bar = {}
|
|
126
126
|
end
|
|
127
127
|
end
|
|
@@ -129,27 +129,27 @@ describe Sexp do
|
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
it 'should return unless line' do
|
|
132
|
-
expect(@node.grep_node(
|
|
132
|
+
expect(@node.grep_node(node_type: :unless).line_number).to eq 2
|
|
133
133
|
end
|
|
134
134
|
|
|
135
135
|
it 'should return paren line' do
|
|
136
|
-
expect(@node.grep_node(
|
|
136
|
+
expect(@node.grep_node(node_type: :paren).line_number).to eq 1
|
|
137
137
|
end
|
|
138
138
|
|
|
139
139
|
it 'should return stmts_add line' do
|
|
140
|
-
expect(@node.grep_node(
|
|
140
|
+
expect(@node.grep_node(node_type: :stmts_add).line_number).to eq 2
|
|
141
141
|
end
|
|
142
142
|
|
|
143
143
|
it 'should return binary line' do
|
|
144
|
-
expect(@node.grep_node(
|
|
144
|
+
expect(@node.grep_node(node_type: :binary).line_number).to eq 2
|
|
145
145
|
end
|
|
146
146
|
|
|
147
147
|
it 'should return unary line' do
|
|
148
|
-
expect(@node.grep_node(
|
|
148
|
+
expect(@node.grep_node(node_type: :unary).line_number).to eq 2
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
it 'should return assign line' do
|
|
152
|
-
expect(@node.grep_node(
|
|
152
|
+
expect(@node.grep_node(node_type: :assign).line_number).to eq 3
|
|
153
153
|
end
|
|
154
154
|
end
|
|
155
155
|
end
|
|
@@ -166,45 +166,28 @@ describe Sexp do
|
|
|
166
166
|
|
|
167
167
|
it 'should get the call nodes with receiver current_user' do
|
|
168
168
|
nodes = []
|
|
169
|
-
@node.grep_nodes(
|
|
170
|
-
expect(nodes).to eq
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
s(:@period, '.', s(2, 20)),
|
|
175
|
-
s(:@ident, 'posts', s(2, 21))
|
|
176
|
-
)
|
|
177
|
-
]
|
|
169
|
+
@node.grep_nodes(node_type: :call, receiver: 'current_user') { |node| nodes << node }
|
|
170
|
+
expect(nodes.size).to eq 1
|
|
171
|
+
expect(nodes.first.node_type).to eq :call
|
|
172
|
+
expect(nodes.first.message.to_s).to eq 'posts'
|
|
173
|
+
expect(nodes.first.to_s).to eq 'current_user.posts'
|
|
178
174
|
end
|
|
179
175
|
|
|
180
176
|
it 'should get the call nodes with different messages' do
|
|
181
177
|
nodes = []
|
|
182
|
-
@node.grep_nodes(
|
|
183
|
-
expect(nodes).to eq [
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
s(
|
|
187
|
-
:call,
|
|
188
|
-
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
|
189
|
-
s(:@period, '.', s(2, 20)),
|
|
190
|
-
s(:@ident, 'posts', s(2, 21))
|
|
191
|
-
),
|
|
192
|
-
s(:@period, '.', s(2, 26)),
|
|
193
|
-
s(:@ident, 'find', s(2, 27))
|
|
194
|
-
),
|
|
195
|
-
s(
|
|
196
|
-
:call,
|
|
197
|
-
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
|
198
|
-
s(:@period, '.', s(2, 20)),
|
|
199
|
-
s(:@ident, 'posts', s(2, 21))
|
|
200
|
-
)
|
|
178
|
+
@node.grep_nodes(node_type: :call, message: %w[posts find]) { |node| nodes << node }
|
|
179
|
+
expect(nodes.map { |node| [node.receiver.to_s, node.message.to_s] }).to eq [
|
|
180
|
+
['current_user.posts', 'find'],
|
|
181
|
+
['current_user', 'posts']
|
|
201
182
|
]
|
|
202
183
|
end
|
|
203
184
|
|
|
204
185
|
it 'should get the vcall node with to_s' do
|
|
205
186
|
nodes = []
|
|
206
|
-
@node.grep_nodes(
|
|
207
|
-
expect(nodes).to eq
|
|
187
|
+
@node.grep_nodes(node_type: :vcall, to_s: 'current_user') { |node| nodes << node }
|
|
188
|
+
expect(nodes.size).to eq 1
|
|
189
|
+
expect(nodes.first.node_type).to eq :vcall
|
|
190
|
+
expect(nodes.first.to_s).to eq 'current_user'
|
|
208
191
|
end
|
|
209
192
|
end
|
|
210
193
|
|
|
@@ -219,13 +202,9 @@ describe Sexp do
|
|
|
219
202
|
end
|
|
220
203
|
|
|
221
204
|
it 'should get first node with empty argument' do
|
|
222
|
-
node = @node.grep_node(
|
|
223
|
-
expect(node).to eq
|
|
224
|
-
|
|
225
|
-
s(:vcall, s(:@ident, 'current_user', s(2, 8))),
|
|
226
|
-
s(:@period, '.', s(2, 20)),
|
|
227
|
-
s(:@ident, 'posts', s(2, 21))
|
|
228
|
-
)
|
|
205
|
+
node = @node.grep_node(node_type: :call, receiver: 'current_user')
|
|
206
|
+
expect(node.node_type).to eq :call
|
|
207
|
+
expect(node.to_s).to eq 'current_user.posts'
|
|
229
208
|
end
|
|
230
209
|
end
|
|
231
210
|
|
|
@@ -240,31 +219,31 @@ describe Sexp do
|
|
|
240
219
|
end
|
|
241
220
|
|
|
242
221
|
it 'should get the count of call nodes' do
|
|
243
|
-
expect(@node.grep_nodes_count(
|
|
222
|
+
expect(@node.grep_nodes_count(node_type: :call)).to eq 2
|
|
244
223
|
end
|
|
245
224
|
end
|
|
246
225
|
|
|
247
226
|
describe 'receiver' do
|
|
248
227
|
it 'should get receiver of assign node' do
|
|
249
|
-
node = parse_content('user.name = params[:name]').grep_node(
|
|
228
|
+
node = parse_content('user.name = params[:name]').grep_node(node_type: :assign)
|
|
250
229
|
receiver = node.receiver
|
|
251
|
-
expect(receiver.
|
|
230
|
+
expect(receiver.node_type).to eq :field
|
|
252
231
|
expect(receiver.receiver.to_s).to eq 'user'
|
|
253
232
|
expect(receiver.message.to_s).to eq 'name'
|
|
254
233
|
end
|
|
255
234
|
|
|
256
235
|
it 'should get receiver of field node' do
|
|
257
|
-
node = parse_content('user.name = params[:name]').grep_node(
|
|
236
|
+
node = parse_content('user.name = params[:name]').grep_node(node_type: :field)
|
|
258
237
|
expect(node.receiver.to_s).to eq 'user'
|
|
259
238
|
end
|
|
260
239
|
|
|
261
240
|
it 'should get receiver of call node' do
|
|
262
|
-
node = parse_content('user.name').grep_node(
|
|
241
|
+
node = parse_content('user.name').grep_node(node_type: :call)
|
|
263
242
|
expect(node.receiver.to_s).to eq 'user'
|
|
264
243
|
end
|
|
265
244
|
|
|
266
245
|
it 'should get receiver of binary' do
|
|
267
|
-
node = parse_content("user == 'user_name'").grep_node(
|
|
246
|
+
node = parse_content("user == 'user_name'").grep_node(node_type: :binary)
|
|
268
247
|
expect(node.receiver.to_s).to eq 'user'
|
|
269
248
|
end
|
|
270
249
|
|
|
@@ -273,298 +252,293 @@ describe Sexp do
|
|
|
273
252
|
map.resources :posts do
|
|
274
253
|
end
|
|
275
254
|
EOF
|
|
276
|
-
node = parse_content(content).grep_node(
|
|
255
|
+
node = parse_content(content).grep_node(node_type: :command_call)
|
|
277
256
|
expect(node.receiver.to_s).to eq 'map'
|
|
278
257
|
end
|
|
279
258
|
|
|
280
259
|
it 'should get receiver of method_add_arg' do
|
|
281
|
-
node = parse_content('Post.find(:all)').grep_node(
|
|
260
|
+
node = parse_content('Post.find(:all)').grep_node(node_type: :method_add_arg)
|
|
282
261
|
expect(node.receiver.to_s).to eq 'Post'
|
|
283
262
|
end
|
|
284
263
|
|
|
285
264
|
it 'should get receiver of method_add_block' do
|
|
286
|
-
node = parse_content('Post.save do; end').grep_node(
|
|
265
|
+
node = parse_content('Post.save do; end').grep_node(node_type: :method_add_block)
|
|
287
266
|
expect(node.receiver.to_s).to eq 'Post'
|
|
288
267
|
end
|
|
289
268
|
end
|
|
290
269
|
|
|
291
270
|
describe 'module_name' do
|
|
292
271
|
it 'should get module name of module node' do
|
|
293
|
-
node = parse_content('module Admin; end').grep_node(
|
|
272
|
+
node = parse_content('module Admin; end').grep_node(node_type: :module)
|
|
294
273
|
expect(node.module_name.to_s).to eq 'Admin'
|
|
295
274
|
end
|
|
296
275
|
end
|
|
297
276
|
|
|
298
277
|
describe 'class_name' do
|
|
299
278
|
it 'should get class name of class node' do
|
|
300
|
-
node = parse_content('class User; end').grep_node(
|
|
279
|
+
node = parse_content('class User; end').grep_node(node_type: :class)
|
|
301
280
|
expect(node.class_name.to_s).to eq 'User'
|
|
302
281
|
end
|
|
303
282
|
end
|
|
304
283
|
|
|
305
284
|
describe 'base_class' do
|
|
306
285
|
it 'should get base class of class node' do
|
|
307
|
-
node = parse_content('class User < ActiveRecord::Base; end').grep_node(
|
|
286
|
+
node = parse_content('class User < ActiveRecord::Base; end').grep_node(node_type: :class)
|
|
308
287
|
expect(node.base_class.to_s).to eq 'ActiveRecord::Base'
|
|
309
288
|
end
|
|
310
289
|
end
|
|
311
290
|
|
|
312
291
|
describe 'left_value' do
|
|
313
292
|
it 'should get the left value of assign' do
|
|
314
|
-
node = parse_content('user = current_user').grep_node(
|
|
293
|
+
node = parse_content('user = current_user').grep_node(node_type: :assign)
|
|
315
294
|
expect(node.left_value.to_s).to eq 'user'
|
|
316
295
|
end
|
|
317
296
|
end
|
|
318
297
|
|
|
319
298
|
describe 'right_value' do
|
|
320
299
|
it 'should get the right value of assign' do
|
|
321
|
-
node = parse_content('user = current_user').grep_node(
|
|
300
|
+
node = parse_content('user = current_user').grep_node(node_type: :assign)
|
|
322
301
|
expect(node.right_value.to_s).to eq 'current_user'
|
|
323
302
|
end
|
|
324
303
|
end
|
|
325
304
|
|
|
326
305
|
describe 'message' do
|
|
327
306
|
it 'should get the message of command' do
|
|
328
|
-
node = parse_content('has_many :projects').grep_node(
|
|
307
|
+
node = parse_content('has_many :projects').grep_node(node_type: :command)
|
|
329
308
|
expect(node.message.to_s).to eq 'has_many'
|
|
330
309
|
end
|
|
331
310
|
|
|
332
311
|
it 'should get the message of command_call' do
|
|
333
|
-
node = parse_content('map.resources :posts do; end').grep_node(
|
|
312
|
+
node = parse_content('map.resources :posts do; end').grep_node(node_type: :command_call)
|
|
334
313
|
expect(node.message.to_s).to eq 'resources'
|
|
335
314
|
end
|
|
336
315
|
|
|
337
316
|
it 'should get the message of field' do
|
|
338
|
-
node = parse_content("user.name = 'test'").grep_node(
|
|
317
|
+
node = parse_content("user.name = 'test'").grep_node(node_type: :field)
|
|
339
318
|
expect(node.message.to_s).to eq 'name'
|
|
340
319
|
end
|
|
341
320
|
|
|
342
321
|
it 'should get the message of call' do
|
|
343
|
-
node = parse_content('user.name').grep_node(
|
|
322
|
+
node = parse_content('user.name').grep_node(node_type: :call)
|
|
344
323
|
expect(node.message.to_s).to eq 'name'
|
|
345
324
|
end
|
|
346
325
|
|
|
347
326
|
it 'should get the message of binary' do
|
|
348
|
-
node = parse_content("user.name == 'test'").grep_node(
|
|
327
|
+
node = parse_content("user.name == 'test'").grep_node(node_type: :binary)
|
|
349
328
|
expect(node.message.to_s).to eq '=='
|
|
350
329
|
end
|
|
351
330
|
|
|
352
331
|
it 'should get the message of fcall' do
|
|
353
|
-
node = parse_content("test?('world')").grep_node(
|
|
332
|
+
node = parse_content("test?('world')").grep_node(node_type: :fcall)
|
|
354
333
|
expect(node.message.to_s).to eq 'test?'
|
|
355
334
|
end
|
|
356
335
|
|
|
357
336
|
it 'should get the message of method_add_arg' do
|
|
358
|
-
node = parse_content('Post.find(:all)').grep_node(
|
|
337
|
+
node = parse_content('Post.find(:all)').grep_node(node_type: :method_add_arg)
|
|
359
338
|
expect(node.message.to_s).to eq 'find'
|
|
360
339
|
end
|
|
361
340
|
|
|
362
341
|
it 'should get the message of method_add_block' do
|
|
363
|
-
node = parse_content('Post.save do; end').grep_node(
|
|
342
|
+
node = parse_content('Post.save do; end').grep_node(node_type: :method_add_block)
|
|
364
343
|
expect(node.message.to_s).to eq 'save'
|
|
365
344
|
end
|
|
366
345
|
end
|
|
367
346
|
|
|
368
347
|
describe 'arguments' do
|
|
369
348
|
it 'should get the arguments of command' do
|
|
370
|
-
node = parse_content('resources :posts do; end').grep_node(
|
|
371
|
-
expect(node.arguments.
|
|
349
|
+
node = parse_content('resources :posts do; end').grep_node(node_type: :command)
|
|
350
|
+
expect(node.arguments.node_type).to eq :args_add_block
|
|
372
351
|
end
|
|
373
352
|
|
|
374
353
|
it 'should get the arguments of command_call' do
|
|
375
|
-
node = parse_content('map.resources :posts do; end').grep_node(
|
|
376
|
-
expect(node.arguments.
|
|
354
|
+
node = parse_content('map.resources :posts do; end').grep_node(node_type: :command_call)
|
|
355
|
+
expect(node.arguments.node_type).to eq :args_add_block
|
|
377
356
|
end
|
|
378
357
|
|
|
379
358
|
it 'should get the arguments of method_add_arg' do
|
|
380
|
-
node = parse_content('User.find(:all)').grep_node(
|
|
381
|
-
expect(node.arguments.
|
|
359
|
+
node = parse_content('User.find(:all)').grep_node(node_type: :method_add_arg)
|
|
360
|
+
expect(node.arguments.node_type).to eq :args_add_block
|
|
382
361
|
end
|
|
383
362
|
|
|
384
363
|
it 'should get the arguments of method_add_block' do
|
|
385
|
-
node = parse_content('Post.save(false) do; end').grep_node(
|
|
386
|
-
expect(node.arguments.
|
|
364
|
+
node = parse_content('Post.save(false) do; end').grep_node(node_type: :method_add_block)
|
|
365
|
+
expect(node.arguments.node_type).to eq :args_add_block
|
|
387
366
|
end
|
|
388
367
|
end
|
|
389
368
|
|
|
390
369
|
describe 'argument' do
|
|
391
370
|
it 'should get the argument of binary' do
|
|
392
|
-
node = parse_content('user == current_user').grep_node(
|
|
371
|
+
node = parse_content('user == current_user').grep_node(node_type: :binary)
|
|
393
372
|
expect(node.argument.to_s).to eq 'current_user'
|
|
394
373
|
end
|
|
395
374
|
end
|
|
396
375
|
|
|
397
376
|
describe 'all' do
|
|
398
377
|
it 'should get all arguments' do
|
|
399
|
-
node = parse_content("puts 'hello', 'world'").grep_node(
|
|
378
|
+
node = parse_content("puts 'hello', 'world'").grep_node(node_type: :args_add_block)
|
|
400
379
|
expect(node.all.map(&:to_s)).to eq %w[hello world]
|
|
401
380
|
end
|
|
402
381
|
|
|
403
382
|
it 'should get all arguments with &:' do
|
|
404
|
-
node = parse_content('user.posts.map(&:title)').grep_node(
|
|
383
|
+
node = parse_content('user.posts.map(&:title)').grep_node(node_type: :args_add_block)
|
|
405
384
|
expect(node.all.map(&:to_s)).to eq %w[title]
|
|
406
385
|
end
|
|
407
386
|
|
|
408
387
|
it 'should get all arguments with command_call node' do
|
|
409
|
-
node = parse_content('options_for_select(Account.get_business current_user)').grep_node(
|
|
410
|
-
expect(node.all).to eq
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
s(:@period, '.', s(1, 26)),
|
|
415
|
-
s(:@ident, 'get_business', s(1, 27)),
|
|
416
|
-
s(:args_add_block, s(:args_add, s(:args_new), s(:vcall, s(:@ident, 'current_user', s(1, 40)))), false)
|
|
417
|
-
)
|
|
418
|
-
]
|
|
388
|
+
node = parse_content('options_for_select(Account.get_business current_user)').grep_node(node_type: :args_add)
|
|
389
|
+
expect(node.all.size).to eq 1
|
|
390
|
+
expect(node.all.first.node_type).to eq :command_call
|
|
391
|
+
expect(node.all.first.message.to_s).to eq 'get_business'
|
|
392
|
+
expect(node.all.first.receiver.to_s).to eq 'Account'
|
|
419
393
|
end
|
|
420
394
|
|
|
421
395
|
it 'no error for args_add_star' do
|
|
422
|
-
node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(
|
|
396
|
+
node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(node_type: :args_add_block)
|
|
423
397
|
expect { node.all }.not_to raise_error
|
|
424
398
|
end
|
|
425
399
|
end
|
|
426
400
|
|
|
427
401
|
describe 'conditional_statement' do
|
|
428
402
|
it 'should get conditional statement of if' do
|
|
429
|
-
node = parse_content('if true; end').grep_node(
|
|
403
|
+
node = parse_content('if true; end').grep_node(node_type: :if)
|
|
430
404
|
expect(node.conditional_statement.to_s).to eq 'true'
|
|
431
405
|
end
|
|
432
406
|
|
|
433
407
|
it 'should get conditional statement of unless' do
|
|
434
|
-
node = parse_content('unless false; end').grep_node(
|
|
408
|
+
node = parse_content('unless false; end').grep_node(node_type: :unless)
|
|
435
409
|
expect(node.conditional_statement.to_s).to eq 'false'
|
|
436
410
|
end
|
|
437
411
|
|
|
438
412
|
it 'should get conditional statement of elsif' do
|
|
439
|
-
node = parse_content('if true; elsif false; end').grep_node(
|
|
413
|
+
node = parse_content('if true; elsif false; end').grep_node(node_type: :elsif)
|
|
440
414
|
expect(node.conditional_statement.to_s).to eq 'false'
|
|
441
415
|
end
|
|
442
416
|
|
|
443
417
|
it 'should get conditional statement of if_mod' do
|
|
444
|
-
node = parse_content("'OK' if true").grep_node(
|
|
418
|
+
node = parse_content("'OK' if true").grep_node(node_type: :if_mod)
|
|
445
419
|
expect(node.conditional_statement.to_s).to eq 'true'
|
|
446
420
|
end
|
|
447
421
|
|
|
448
422
|
it 'should get conditional statement of unless_mod' do
|
|
449
|
-
node = parse_content("'OK' unless false").grep_node(
|
|
423
|
+
node = parse_content("'OK' unless false").grep_node(node_type: :unless_mod)
|
|
450
424
|
expect(node.conditional_statement.to_s).to eq 'false'
|
|
451
425
|
end
|
|
452
426
|
|
|
453
427
|
it 'should get conditional statement of ifop' do
|
|
454
|
-
node = parse_content("true ? 'OK' : 'NO'").grep_node(
|
|
428
|
+
node = parse_content("true ? 'OK' : 'NO'").grep_node(node_type: :ifop)
|
|
455
429
|
expect(node.conditional_statement.to_s).to eq 'true'
|
|
456
430
|
end
|
|
457
431
|
end
|
|
458
432
|
|
|
459
433
|
describe 'all_conditions' do
|
|
460
434
|
it 'should get all conditions' do
|
|
461
|
-
node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(
|
|
435
|
+
node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(node_type: :binary)
|
|
462
436
|
expect(node.all_conditions.size).to eq 3
|
|
463
437
|
end
|
|
464
438
|
end
|
|
465
439
|
|
|
466
440
|
describe 'method_name' do
|
|
467
441
|
it 'should get the method name of def' do
|
|
468
|
-
node = parse_content('def show; end').grep_node(
|
|
442
|
+
node = parse_content('def show; end').grep_node(node_type: :def)
|
|
469
443
|
expect(node.method_name.to_s).to eq 'show'
|
|
470
444
|
end
|
|
471
445
|
|
|
472
446
|
it 'should get the method name of defs' do
|
|
473
|
-
node = parse_content('def self.find; end').grep_node(
|
|
447
|
+
node = parse_content('def self.find; end').grep_node(node_type: :defs)
|
|
474
448
|
expect(node.method_name.to_s).to eq 'find'
|
|
475
449
|
end
|
|
476
450
|
end
|
|
477
451
|
|
|
478
452
|
describe 'body' do
|
|
479
453
|
it 'should get body of class' do
|
|
480
|
-
node = parse_content('class User; end').grep_node(
|
|
481
|
-
expect(node.body.
|
|
454
|
+
node = parse_content('class User; end').grep_node(node_type: :class)
|
|
455
|
+
expect(node.body.node_type).to eq :bodystmt
|
|
482
456
|
end
|
|
483
457
|
|
|
484
458
|
it 'should get body of def' do
|
|
485
|
-
node = parse_content('def login; end').grep_node(
|
|
486
|
-
expect(node.body.
|
|
459
|
+
node = parse_content('def login; end').grep_node(node_type: :def)
|
|
460
|
+
expect(node.body.node_type).to eq :bodystmt
|
|
487
461
|
end
|
|
488
462
|
|
|
489
463
|
it 'should get body of defs' do
|
|
490
|
-
node = parse_content('def self.login; end').grep_node(
|
|
491
|
-
expect(node.body.
|
|
464
|
+
node = parse_content('def self.login; end').grep_node(node_type: :defs)
|
|
465
|
+
expect(node.body.node_type).to eq :bodystmt
|
|
492
466
|
end
|
|
493
467
|
|
|
494
468
|
it 'should get body of module' do
|
|
495
|
-
node = parse_content('module Enumerable; end').grep_node(
|
|
496
|
-
expect(node.body.
|
|
469
|
+
node = parse_content('module Enumerable; end').grep_node(node_type: :module)
|
|
470
|
+
expect(node.body.node_type).to eq :bodystmt
|
|
497
471
|
end
|
|
498
472
|
|
|
499
473
|
it 'should get body of if' do
|
|
500
|
-
node = parse_content("if true; 'OK'; end").grep_node(
|
|
501
|
-
expect(node.body.
|
|
474
|
+
node = parse_content("if true; 'OK'; end").grep_node(node_type: :if)
|
|
475
|
+
expect(node.body.node_type).to eq :stmts_add
|
|
502
476
|
end
|
|
503
477
|
|
|
504
478
|
it 'should get body of elsif' do
|
|
505
|
-
node = parse_content("if true; elsif true; 'OK'; end").grep_node(
|
|
506
|
-
expect(node.body.
|
|
479
|
+
node = parse_content("if true; elsif true; 'OK'; end").grep_node(node_type: :elsif)
|
|
480
|
+
expect(node.body.node_type).to eq :stmts_add
|
|
507
481
|
end
|
|
508
482
|
|
|
509
483
|
it 'should get body of unless' do
|
|
510
|
-
node = parse_content("unless true; 'OK'; end").grep_node(
|
|
511
|
-
expect(node.body.
|
|
484
|
+
node = parse_content("unless true; 'OK'; end").grep_node(node_type: :unless)
|
|
485
|
+
expect(node.body.node_type).to eq :stmts_add
|
|
512
486
|
end
|
|
513
487
|
|
|
514
488
|
it 'should get body of else' do
|
|
515
|
-
node = parse_content("if true; else; 'OK'; end").grep_node(
|
|
516
|
-
expect(node.body.
|
|
489
|
+
node = parse_content("if true; else; 'OK'; end").grep_node(node_type: :else)
|
|
490
|
+
expect(node.body.node_type).to eq :stmts_add
|
|
517
491
|
end
|
|
518
492
|
|
|
519
493
|
it 'should get body of if_mod' do
|
|
520
|
-
node = parse_content("'OK' if true").grep_node(
|
|
494
|
+
node = parse_content("'OK' if true").grep_node(node_type: :if_mod)
|
|
521
495
|
expect(node.body.to_s).to eq 'OK'
|
|
522
496
|
end
|
|
523
497
|
|
|
524
498
|
it 'should get body of unless_mod' do
|
|
525
|
-
node = parse_content("'OK' unless false").grep_node(
|
|
499
|
+
node = parse_content("'OK' unless false").grep_node(node_type: :unless_mod)
|
|
526
500
|
expect(node.body.to_s).to eq 'OK'
|
|
527
501
|
end
|
|
528
502
|
|
|
529
503
|
it 'should get body of if_op' do
|
|
530
|
-
node = parse_content("true ? 'OK' : 'NO'").grep_node(
|
|
504
|
+
node = parse_content("true ? 'OK' : 'NO'").grep_node(node_type: :ifop)
|
|
531
505
|
expect(node.body.to_s).to eq 'OK'
|
|
532
506
|
end
|
|
533
507
|
end
|
|
534
508
|
|
|
535
509
|
describe 'block' do
|
|
536
510
|
it 'sould get block of method_add_block node' do
|
|
537
|
-
node = parse_content('resources :posts do; resources :comments; end').grep_node(
|
|
538
|
-
expect(node.block_node.
|
|
511
|
+
node = parse_content('resources :posts do; resources :comments; end').grep_node(node_type: :method_add_block)
|
|
512
|
+
expect(node.block_node.node_type).to eq :do_block
|
|
539
513
|
end
|
|
540
514
|
end
|
|
541
515
|
|
|
542
516
|
describe 'statements' do
|
|
543
517
|
it 'should get statements of do_block node' do
|
|
544
518
|
node =
|
|
545
|
-
parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(
|
|
519
|
+
parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(node_type: :do_block)
|
|
546
520
|
expect(node.statements.size).to eq 2
|
|
547
521
|
end
|
|
548
522
|
|
|
549
523
|
it 'should get statements of bodystmt node' do
|
|
550
|
-
node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(
|
|
524
|
+
node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(node_type: :bodystmt)
|
|
551
525
|
expect(node.statements.size).to eq 2
|
|
552
526
|
end
|
|
553
527
|
end
|
|
554
528
|
|
|
555
529
|
describe 'exception_classes' do
|
|
556
530
|
it 'should get exception classes of rescue node' do
|
|
557
|
-
node = parse_content('def test; rescue CustomException; end').grep_node(
|
|
531
|
+
node = parse_content('def test; rescue CustomException; end').grep_node(node_type: :rescue)
|
|
558
532
|
expect(node.exception_classes.first.to_s).to eq 'CustomException'
|
|
559
533
|
end
|
|
560
534
|
|
|
561
535
|
it 'should get empty of empty rescue node' do
|
|
562
|
-
node = parse_content('def test; rescue; end').grep_node(
|
|
536
|
+
node = parse_content('def test; rescue; end').grep_node(node_type: :rescue)
|
|
563
537
|
expect(node.exception_classes.first.to_s).to eq ''
|
|
564
538
|
end
|
|
565
539
|
|
|
566
540
|
it 'should get exception classes of rescue node for multiple exceptions' do
|
|
567
|
-
node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(
|
|
541
|
+
node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(node_type: :rescue)
|
|
568
542
|
expect(node.exception_classes.first.to_s).to eq 'StandardError'
|
|
569
543
|
expect(node.exception_classes.last.to_s).to eq 'CustomException'
|
|
570
544
|
end
|
|
@@ -572,19 +546,19 @@ describe Sexp do
|
|
|
572
546
|
|
|
573
547
|
describe 'exception_variable' do
|
|
574
548
|
it 'should get exception varible of rescue node' do
|
|
575
|
-
node = parse_content('def test; rescue => e; end').grep_node(
|
|
549
|
+
node = parse_content('def test; rescue => e; end').grep_node(node_type: :rescue)
|
|
576
550
|
expect(node.exception_variable.to_s).to eq 'e'
|
|
577
551
|
end
|
|
578
552
|
|
|
579
553
|
it 'should get empty of empty rescue node' do
|
|
580
|
-
node = parse_content('def test; rescue; end').grep_node(
|
|
554
|
+
node = parse_content('def test; rescue; end').grep_node(node_type: :rescue)
|
|
581
555
|
expect(node.exception_variable.to_s).to eq ''
|
|
582
556
|
end
|
|
583
557
|
end
|
|
584
558
|
|
|
585
559
|
describe 'hash_value' do
|
|
586
560
|
it 'should get value for hash node' do
|
|
587
|
-
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
|
561
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
|
|
588
562
|
expect(node.hash_value('first_name').to_s).to eq 'Richard'
|
|
589
563
|
expect(node.hash_value('last_name').to_s).to eq 'Huang'
|
|
590
564
|
end
|
|
@@ -592,7 +566,7 @@ describe Sexp do
|
|
|
592
566
|
it 'should get value for bare_assoc_hash' do
|
|
593
567
|
node =
|
|
594
568
|
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
|
595
|
-
|
|
569
|
+
node_type: :bare_assoc_hash
|
|
596
570
|
)
|
|
597
571
|
expect(node.hash_value('first_name').to_s).to eq 'Richard'
|
|
598
572
|
expect(node.hash_value('last_name').to_s).to eq 'Huang'
|
|
@@ -601,14 +575,14 @@ describe Sexp do
|
|
|
601
575
|
|
|
602
576
|
describe 'hash_size' do
|
|
603
577
|
it 'should get value for hash node' do
|
|
604
|
-
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
|
578
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
|
|
605
579
|
expect(node.hash_size).to eq 2
|
|
606
580
|
end
|
|
607
581
|
|
|
608
582
|
it 'should get value for bare_assoc_hash' do
|
|
609
583
|
node =
|
|
610
584
|
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
|
611
|
-
|
|
585
|
+
node_type: :bare_assoc_hash
|
|
612
586
|
)
|
|
613
587
|
expect(node.hash_size).to eq 2
|
|
614
588
|
end
|
|
@@ -616,14 +590,14 @@ describe Sexp do
|
|
|
616
590
|
|
|
617
591
|
describe 'hash_keys' do
|
|
618
592
|
it 'should get hash_keys for hash node' do
|
|
619
|
-
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
|
593
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
|
|
620
594
|
expect(node.hash_keys).to eq %w[first_name last_name]
|
|
621
595
|
end
|
|
622
596
|
|
|
623
597
|
it 'should get hash_keys for bare_assoc_hash' do
|
|
624
598
|
node =
|
|
625
599
|
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
|
626
|
-
|
|
600
|
+
node_type: :bare_assoc_hash
|
|
627
601
|
)
|
|
628
602
|
expect(node.hash_keys).to eq %w[first_name last_name]
|
|
629
603
|
end
|
|
@@ -631,14 +605,14 @@ describe Sexp do
|
|
|
631
605
|
|
|
632
606
|
describe 'hash_values' do
|
|
633
607
|
it 'should get hash_values for hash node' do
|
|
634
|
-
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
|
|
608
|
+
node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
|
|
635
609
|
expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
|
|
636
610
|
end
|
|
637
611
|
|
|
638
612
|
it 'should get hash_values for bare_assoc_hash' do
|
|
639
613
|
node =
|
|
640
614
|
parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
|
|
641
|
-
|
|
615
|
+
node_type: :bare_assoc_hash
|
|
642
616
|
)
|
|
643
617
|
expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
|
|
644
618
|
end
|
|
@@ -648,7 +622,7 @@ describe Sexp do
|
|
|
648
622
|
it 'should get key for assoc_new' do
|
|
649
623
|
node =
|
|
650
624
|
parse_content("add_user :user, first_name: 'Richard'").grep_node(
|
|
651
|
-
|
|
625
|
+
node_type: :assoc_new
|
|
652
626
|
)
|
|
653
627
|
expect(node.key).to eq 'first_name'
|
|
654
628
|
end
|
|
@@ -658,15 +632,25 @@ describe Sexp do
|
|
|
658
632
|
it 'should get value for assoc_new' do
|
|
659
633
|
node =
|
|
660
634
|
parse_content("add_user :user, first_name: 'Richard'").grep_node(
|
|
661
|
-
|
|
635
|
+
node_type: :assoc_new
|
|
662
636
|
)
|
|
663
637
|
expect(node.value).to eq 'Richard'
|
|
664
638
|
end
|
|
665
639
|
|
|
640
|
+
if RUBY_VERSION.to_f >= 3.1
|
|
641
|
+
it 'should get value for assoc_new' do
|
|
642
|
+
node =
|
|
643
|
+
parse_content("add_user :user, first_name:").grep_node(
|
|
644
|
+
node_type: :assoc_new
|
|
645
|
+
)
|
|
646
|
+
expect(node.value).to be_nil
|
|
647
|
+
end
|
|
648
|
+
end
|
|
649
|
+
|
|
666
650
|
it 'should get array value for assoc_new' do
|
|
667
651
|
node =
|
|
668
652
|
parse_content("add_user :user, name: %w[Richard Huang]").grep_node(
|
|
669
|
-
|
|
653
|
+
node_type: :assoc_new
|
|
670
654
|
)
|
|
671
655
|
expect(node.value.array_values.map(&:to_s)).to eq ['Richard', 'Huang']
|
|
672
656
|
end
|
|
@@ -674,65 +658,65 @@ describe Sexp do
|
|
|
674
658
|
|
|
675
659
|
describe 'array_size' do
|
|
676
660
|
it 'should get array size' do
|
|
677
|
-
node = parse_content("['first_name', 'last_name']").grep_node(
|
|
661
|
+
node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
|
|
678
662
|
expect(node.array_size).to eq 2
|
|
679
663
|
end
|
|
680
664
|
|
|
681
665
|
it 'should get 0' do
|
|
682
|
-
node = parse_content('[]').grep_node(
|
|
666
|
+
node = parse_content('[]').grep_node(node_type: :array)
|
|
683
667
|
expect(node.array_size).to eq 0
|
|
684
668
|
end
|
|
685
669
|
end
|
|
686
670
|
|
|
687
671
|
describe 'array_values' do
|
|
688
672
|
it 'should get array values' do
|
|
689
|
-
node = parse_content("['first_name', 'last_name']").grep_node(
|
|
673
|
+
node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
|
|
690
674
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
|
691
675
|
end
|
|
692
676
|
|
|
693
677
|
it 'should get empty array values' do
|
|
694
|
-
node = parse_content('[]').grep_node(
|
|
678
|
+
node = parse_content('[]').grep_node(node_type: :array)
|
|
695
679
|
expect(node.array_values).to eq []
|
|
696
680
|
end
|
|
697
681
|
|
|
698
682
|
it 'should get array value with array and words_add' do
|
|
699
|
-
node = parse_content('%W{day week fortnight}').grep_node(
|
|
683
|
+
node = parse_content('%W{day week fortnight}').grep_node(node_type: :array)
|
|
700
684
|
expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
|
|
701
685
|
end
|
|
702
686
|
|
|
703
687
|
it 'should get empty array values with array and words_add' do
|
|
704
|
-
node = parse_content('%W{}').grep_node(
|
|
688
|
+
node = parse_content('%W{}').grep_node(node_type: :array)
|
|
705
689
|
expect(node.array_values.map(&:to_s)).to eq []
|
|
706
690
|
end
|
|
707
691
|
|
|
708
692
|
it 'should get array value with array and qwords_add' do
|
|
709
|
-
node = parse_content('%w(first_name last_name)').grep_node(
|
|
693
|
+
node = parse_content('%w(first_name last_name)').grep_node(node_type: :array)
|
|
710
694
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
|
711
695
|
end
|
|
712
696
|
|
|
713
697
|
it 'should get empty array values with array and qwords_add' do
|
|
714
|
-
node = parse_content('%w()').grep_node(
|
|
698
|
+
node = parse_content('%w()').grep_node(node_type: :array)
|
|
715
699
|
expect(node.array_values.map(&:to_s)).to eq []
|
|
716
700
|
end
|
|
717
701
|
|
|
718
702
|
if RUBY_VERSION.to_i > 1
|
|
719
703
|
it 'should get array value with array and symbols_add' do
|
|
720
|
-
node = parse_content('%I(first_name last_name)').grep_node(
|
|
704
|
+
node = parse_content('%I(first_name last_name)').grep_node(node_type: :array)
|
|
721
705
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
|
722
706
|
end
|
|
723
707
|
|
|
724
708
|
it 'should get empty array value with array and symbols_add' do
|
|
725
|
-
node = parse_content('%I()').grep_node(
|
|
709
|
+
node = parse_content('%I()').grep_node(node_type: :array)
|
|
726
710
|
expect(node.array_values.map(&:to_s)).to eq []
|
|
727
711
|
end
|
|
728
712
|
|
|
729
713
|
it 'should get array value with array and qsymbols_add' do
|
|
730
|
-
node = parse_content('%i(first_name last_name)').grep_node(
|
|
714
|
+
node = parse_content('%i(first_name last_name)').grep_node(node_type: :array)
|
|
731
715
|
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
|
732
716
|
end
|
|
733
717
|
|
|
734
718
|
it 'should get empty array values with array and qsymbols_new' do
|
|
735
|
-
node = parse_content('%i()').grep_node(
|
|
719
|
+
node = parse_content('%i()').grep_node(node_type: :array)
|
|
736
720
|
expect(node.array_values.map(&:to_s)).to eq []
|
|
737
721
|
end
|
|
738
722
|
end
|
|
@@ -740,7 +724,7 @@ describe Sexp do
|
|
|
740
724
|
|
|
741
725
|
describe 'alias' do
|
|
742
726
|
context 'method' do
|
|
743
|
-
before { @node = parse_content('alias new old').grep_node(
|
|
727
|
+
before { @node = parse_content('alias new old').grep_node(node_type: :alias) }
|
|
744
728
|
|
|
745
729
|
it 'should get old_method' do
|
|
746
730
|
expect(@node.old_method.to_s).to eq 'old'
|
|
@@ -752,7 +736,7 @@ describe Sexp do
|
|
|
752
736
|
end
|
|
753
737
|
|
|
754
738
|
context 'symbol' do
|
|
755
|
-
before { @node = parse_content('alias :new :old').grep_node(
|
|
739
|
+
before { @node = parse_content('alias :new :old').grep_node(node_type: :alias) }
|
|
756
740
|
|
|
757
741
|
it 'should get old_method' do
|
|
758
742
|
expect(@node.old_method.to_s).to eq 'old'
|
|
@@ -766,91 +750,94 @@ describe Sexp do
|
|
|
766
750
|
|
|
767
751
|
describe 'to_object' do
|
|
768
752
|
it 'should to array' do
|
|
769
|
-
node = parse_content("['first_name', 'last_name']").grep_node(
|
|
753
|
+
node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
|
|
770
754
|
expect(node.to_object).to eq %w[first_name last_name]
|
|
771
755
|
end
|
|
772
756
|
|
|
773
757
|
it 'should to array with %w()' do
|
|
774
|
-
node = parse_content('%w(new create)').grep_node(
|
|
758
|
+
node = parse_content('%w(new create)').grep_node(node_type: :array)
|
|
775
759
|
expect(node.to_object).to eq %w[new create]
|
|
776
760
|
end
|
|
777
761
|
|
|
778
762
|
it 'should to array with symbols' do
|
|
779
|
-
node = parse_content('[:first_name, :last_name]').grep_node(
|
|
763
|
+
node = parse_content('[:first_name, :last_name]').grep_node(node_type: :array)
|
|
780
764
|
expect(node.to_object).to eq %w[first_name last_name]
|
|
781
765
|
end
|
|
782
766
|
|
|
783
767
|
it 'should to empty array' do
|
|
784
|
-
node = parse_content('[]').grep_node(
|
|
768
|
+
node = parse_content('[]').grep_node(node_type: :array)
|
|
785
769
|
expect(node.to_object).to eq []
|
|
786
770
|
end
|
|
787
771
|
|
|
788
772
|
it 'should to string' do
|
|
789
|
-
node = parse_content("'richard'").grep_node(
|
|
773
|
+
node = parse_content("'richard'").grep_node(node_type: :string_literal)
|
|
790
774
|
expect(node.to_object).to eq 'richard'
|
|
791
775
|
end
|
|
792
776
|
end
|
|
793
777
|
|
|
794
778
|
describe 'to_s' do
|
|
795
779
|
it 'should get to_s for string' do
|
|
796
|
-
node = parse_content("'user'").grep_node(
|
|
780
|
+
node = parse_content("'user'").grep_node(node_type: :string_literal)
|
|
797
781
|
expect(node.to_s).to eq 'user'
|
|
798
782
|
end
|
|
799
783
|
|
|
800
784
|
it 'should get to_s for symbol' do
|
|
801
|
-
node = parse_content(':user').grep_node(
|
|
785
|
+
node = parse_content(':user').grep_node(node_type: :symbol_literal)
|
|
802
786
|
expect(node.to_s).to eq 'user'
|
|
803
787
|
end
|
|
804
788
|
|
|
805
789
|
it 'should get to_s for const' do
|
|
806
|
-
node = parse_content('User').grep_node(
|
|
790
|
+
node = parse_content('User').grep_node(node_type: :var_ref)
|
|
807
791
|
expect(node.to_s).to eq 'User'
|
|
792
|
+
expect(node).to be_const
|
|
808
793
|
end
|
|
809
794
|
|
|
810
795
|
it 'should get to_s for ivar' do
|
|
811
|
-
node = parse_content('@user').grep_node(
|
|
796
|
+
node = parse_content('@user').grep_node(node_type: :var_ref)
|
|
812
797
|
expect(node.to_s).to eq '@user'
|
|
798
|
+
expect(node).not_to be_const
|
|
813
799
|
end
|
|
814
800
|
|
|
815
|
-
it 'should get to_s for
|
|
816
|
-
node = parse_content('@user.name').grep_node(
|
|
817
|
-
expect(node.to_s).to eq '
|
|
801
|
+
it 'should get to_s for message' do
|
|
802
|
+
node = parse_content('@user.name').grep_node(node_type: :call)
|
|
803
|
+
expect(node.message.to_s).to eq 'name'
|
|
818
804
|
end
|
|
819
805
|
|
|
820
806
|
it 'should get to_s for class with module' do
|
|
821
|
-
node = parse_content('ActiveRecord::Base').grep_node(
|
|
807
|
+
node = parse_content('ActiveRecord::Base').grep_node(node_type: :const_path_ref)
|
|
822
808
|
expect(node.to_s).to eq 'ActiveRecord::Base'
|
|
823
809
|
end
|
|
824
810
|
|
|
825
811
|
it 'should get to_s for label' do
|
|
826
|
-
node = parse_content("{first_name: 'Richard'}").grep_node(
|
|
812
|
+
node = parse_content("{first_name: 'Richard'}").grep_node(node_type: :@label)
|
|
827
813
|
expect(node.to_s).to eq 'first_name'
|
|
828
814
|
end
|
|
829
815
|
|
|
830
816
|
it 'should get to_s for call' do
|
|
831
|
-
node = parse_content('current_user.post').grep_node(
|
|
817
|
+
node = parse_content('current_user.post').grep_node(node_type: :call)
|
|
832
818
|
expect(node.to_s).to eq 'current_user.post'
|
|
833
819
|
end
|
|
834
820
|
|
|
835
821
|
it 'should get to_s for top_const_ref' do
|
|
836
|
-
node = parse_content('::User').grep_node(
|
|
822
|
+
node = parse_content('::User').grep_node(node_type: :top_const_ref)
|
|
837
823
|
expect(node.to_s).to eq '::User'
|
|
838
824
|
end
|
|
839
825
|
end
|
|
840
826
|
|
|
841
827
|
describe 'const?' do
|
|
842
828
|
it 'should return true for const with var_ref' do
|
|
843
|
-
node = parse_content('User.find').grep_node(
|
|
829
|
+
node = parse_content('User.find').grep_node(node_type: :var_ref)
|
|
844
830
|
expect(node).to be_const
|
|
845
831
|
end
|
|
846
832
|
|
|
847
|
-
it 'should return true for const
|
|
848
|
-
node = parse_content('User.find').grep_node(
|
|
833
|
+
it 'should return true for const node' do
|
|
834
|
+
node = parse_content('User.find').grep_node(node_type: :var_ref)
|
|
835
|
+
expect(node.to_s).to eq 'User'
|
|
849
836
|
expect(node).to be_const
|
|
850
837
|
end
|
|
851
838
|
|
|
852
839
|
it 'should return false for ivar' do
|
|
853
|
-
node = parse_content('@user.find').grep_node(
|
|
840
|
+
node = parse_content('@user.find').grep_node(node_type: :var_ref)
|
|
854
841
|
expect(node).not_to be_const
|
|
855
842
|
end
|
|
856
843
|
end
|
|
@@ -870,12 +857,24 @@ describe Sexp do
|
|
|
870
857
|
end
|
|
871
858
|
|
|
872
859
|
describe 'remove_line_and_column' do
|
|
873
|
-
it 'should
|
|
874
|
-
|
|
860
|
+
it 'should keep the node accessible' do
|
|
861
|
+
node = parse_content('test_name').grep_node(node_type: :vcall)
|
|
862
|
+
expect(node.remove_line_and_column.node_type).to eq :vcall
|
|
863
|
+
expect(node.remove_line_and_column.to_s).to eq 'test_name'
|
|
864
|
+
end
|
|
865
|
+
|
|
866
|
+
it 'should consider structurally equal nodes as equal' do
|
|
867
|
+
node1 = parse_content('def a; foo.bar; end').grep_node(node_type: :call)
|
|
868
|
+
node2 = parse_content('def b; foo.bar; end').grep_node(node_type: :call)
|
|
869
|
+
expect(node1.remove_line_and_column).to eq node2.remove_line_and_column
|
|
870
|
+
expect(node1.remove_line_and_column.hash).to eq node2.remove_line_and_column.hash
|
|
875
871
|
end
|
|
876
872
|
|
|
877
|
-
it 'should
|
|
878
|
-
|
|
873
|
+
it 'should consider nodes at different positions as different' do
|
|
874
|
+
node1 = parse_content("def a\n foo.bar\nend").grep_node(node_type: :call)
|
|
875
|
+
node2 = parse_content("def b\n foo.bar\nend").grep_node(node_type: :call)
|
|
876
|
+
expect(node1).not_to eq node2
|
|
877
|
+
expect(node1.remove_line_and_column).to eq node2.remove_line_and_column
|
|
879
878
|
end
|
|
880
879
|
end
|
|
881
880
|
end
|