/
parse.js
490 lines (456 loc) · 11.8 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
'use strict';
let expect = require('chai').expect;
let parse = require('../lib/parse');
const util = require('util');
describe('parse', function() {
context('simple parsing', function() {
it('finds methods', function() {
const contextString = `
def fn;end
def こんにちは
end
def other!
end`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('こんにちは');
expect(result.method).to.have.property('other!');
expect(result.method.fn).to.have.property('posn', 5);
expect(result.method['こんにちは']).to.have.property('posn', 16);
expect(result.method['other!']).to.have.property('posn', 30);
});
it('finds classes with methods', function() {
const contextString = `
class Class;def test;end;end
class Other
def test
end
end`;
const result = parse(contextString);
expect(result.class).to.have.property('Class');
expect(result.class).to.have.property('Other');
expect(result.class.Class.method).to.have.property('test');
expect(result.class.Other.method).to.have.property('test');
});
});
context('with comments and strings', function() {
it('finds methods', function() {
const contextString = `
def fn #a comment here with a quote: " and an end
"an escaped quote \\" with an end and a def here"
end# with a comment
def other!
"an escaped quote \\" with an end"
end`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('other!');
});
it('finds classes', function() {
const contextString = `
class Class;end # with end in a comment
class Other
\`String with an end\`
def test
end# with a comment
end`;
const result = parse(contextString);
expect(result.class).to.have.property('Class');
expect(result.class).to.have.property('Other');
expect(result.class.Other.method).to.have.property('test');
});
});
context('with blocks', function() {
function check(contextString) {
const result = parse(contextString);
expect(result.method.fn).to.eql({
posn: 5
});
expect(result.method).to.have.property('other!');
}
it("doesn't keep begin;end", function() {
const contextString = `
def fn
begin
rescue => e
end
end
def other!
end`;
check(contextString);
});
it("get around modifiers", function() {
const contextString = `
def fn
a = 1 if true #gets ignored even the word end here
b = 1 unless false
begin
puts 'test'
end if true
end
def other!
end`;
check(contextString);
});
it("works with a modifier and a block on the same line", function() {
const contextString = `
def fn
a = 1 if true; if true
b = 1
end
begin
puts 'test'
end if true
end
def other!
end`;
check(contextString);
});
it("recognises an assignment to a block result", function() {
const contextString = `
def fn
a = if true
1
end
b = 5 ** if true
2
else
1
end
end
def other!
end`;
check(contextString);
});
it("recognises a block following a semi colon", function() {
const contextString = `
def fn
a = 1; if true
1
end
b = 5; if true
2
else
1
end
end
def other!
end`;
check(contextString);
});
it("recognises two whiles", function() {
const contextString = `
def fn
a = 1 while false
while true
b = 1
break
end
end
def other!
end`;
check(contextString);
});
it("recognises a single while", function() {
const contextString = `
def fn
while true
a = 1
break
end
end
def other!
end`;
check(contextString);
});
it("recognises a while modifier", function() {
const contextString = `
def fn
a = 1 while false
end
def other!
end`;
check(contextString);
});
it("recognises an until", function() {
const contextString = `
def fn
begin
a = 1
end until false
until true
b = 1
break
end
end
def other!
end`;
check(contextString);
});
it("recognises a for", function() {
const contextString = `
def fn
for a in [1,2]
puts a
end
end
def other!
end`;
check(contextString);
});
it("recognises a do", function() {
const contextString = `
def fn
(1..3).each do |a|
puts a
end
end
def other!
end`;
check(contextString);
});
it("recognises a case", function() {
const contextString = `
def fn(x)
case x
when 5
puts x
else
puts "none"
end
end
def other!
end`;
check(contextString);
});
});
context('with nested modules and classes', function() {
it('finds all depth elements', function() {
const contextString = `
module Module
class Class
def initialize
end
end
def other!
end
end
`;
const result = parse(contextString);
expect(result.module).to.have.property('Module');
expect(result.module.Module.class).to.have.property('Class');
expect(result.module.Module.class.Class.method).to.have.property('initialize');
expect(result.module.Module.method).to.have.property('other!');
});
});
context('with class methods', function() {
it('finds them', function() {
const contextString = `
module Module
class Class
def initialize
end
def self.mine
end
end
def other!
end
end
`;
const result = parse(contextString);
expect(result.module).to.have.property('Module');
expect(result.module.Module.class).to.have.property('Class');
expect(result.module.Module.class.Class.method).to.have.property('initialize');
expect(result.module.Module.class.Class.classMethod).to.have.property('mine');
expect(result.module.Module.method).to.have.property('other!');
});
});
context('when defining methods on the class', function() {
it('finds them', function() {
const contextString = `
module Module
class Class
def initialize
end
class << self
def mine
end
end
end
def other!
end
end
`;
const result = parse(contextString);
expect(result.module).to.have.property('Module');
expect(result.module.Module.class).to.have.property('Class');
expect(result.module.Module.class.Class.method).to.have.property('initialize');
expect(result.module.Module.class.Class.classMethod).to.have.property('mine');
expect(result.module.Module.method).to.have.property('other!');
});
});
context("when defining modules and classes with '::'", function() {
it('finds them', function() {
const contextString = `
module Module::First
module Second
def two
end
end
class Second::Class
def initialize
end
end
def other!
end
end
`;
const result = parse(contextString);
expect(result.module).to.have.property('Module');
expect(result.module.Module.module).to.have.property('First');
expect(result.module.Module.module.First.module).to.have.property('Second');
expect(result.module.Module.module.First.module.Second.method).to.have.property('two');
expect(result.module.Module.module.First.module.Second.class).to.have.property('Class');
expect(result.module.Module.module.First.module.Second.class.Class.method).to.have.property('initialize');
expect(result.module.Module.module.First.method).to.have.property('other!');
});
});
context("inheriting", function() {
it('includes', function() {
const contextString = `
class Class
include Other, OtherTwo
end
`;
const result = parse(contextString);
expect(result.class).to.have.property('Class');
expect(result.class.Class.include).to.include('Other');
expect(result.class.Class.include).to.include('OtherTwo');
});
it('inherits', function() {
const contextString = `
class Class < Other
end
`;
const result = parse(contextString);
expect(result.class).to.have.property('Class');
expect(result.class.Class.inherit).to.eql('Other');
});
});
context('aliasing', function() {
it('uses an alias_method', function() {
const contextString = `
class Class
alias_method :m2 :m1
alias_method :m3, :m1
end
`;
const result = parse(contextString);
expect(result.class.Class.method).to.have.property('m2');
expect(result.class.Class.method).to.have.property('m3');
});
it('uses an alias to a (potential) method', function() {
const contextString = `
class Class
alias m2 m1
alias :m3 :m1
end
`;
const result = parse(contextString);
expect(result.class.Class.method).to.have.property('m2');
expect(result.class.Class.method).to.have.property('m3');
});
it("doesn't use an alias to a local or global", function() {
const contextString = `
class Class
alias @m1 @m2
alias $m3 $m4
end
`;
const result = parse(contextString);
expect(result.class.Class.method).to.be.an('undefined');
});
});
context("HEREDOC", function() {
it("skips through a HEREDOC", function() {
const contextString = `
def fn
<<HEREDOC
end
def other
HEREDOC
end
def fn2
end
`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('fn2');
expect(result.method).not.to.have.property('other');
});
it("skips through a ~HEREDOC", function() {
const contextString = `
def fn
<<~HEREDOC
end
def other
HEREDOC
end
def fn2
end
`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('fn2');
expect(result.method).not.to.have.property('other');
});
it("skips through a -'HEREDOC'", function() {
const contextString = `
def fn
<<-'delimiter'
end
def other
delimiter
end
def fn2
end
`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('fn2');
expect(result.method).not.to.have.property('other');
});
it("skips through a HEREDOC with a method call", function() {
const contextString = `
def fn
a = <<delimiter.test
end
def other
delimiter
end
def fn2
end
`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('fn2');
expect(result.method).not.to.have.property('other');
});
it("skips through a HEREDOC as an argument", function() {
const contextString = `
def fn
call(<<argument)
end
def other
argument
end
def fn2
end
`;
const result = parse(contextString);
expect(result.method).to.have.property('fn');
expect(result.method).to.have.property('fn2');
expect(result.method).not.to.have.property('other');
});
});
});