This repository was archived by the owner on Jun 24, 2022. It is now read-only.
forked from prettier/plugin-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetadata_test.rb
829 lines (706 loc) · 13.8 KB
/
metadata_test.rb
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# frozen_string_literal: true
require 'test_helper'
class MetadataTest < Minitest::Test
def test_BEGIN
assert_metadata :BEGIN, <<~SOURCE
BEGIN {
}
SOURCE
end
def test_END
assert_metadata :END, <<~SOURCE
END {
}
SOURCE
end
def test_alias
assert_metadata :alias, 'alias foo bar'
end
def test_array_args
assert_metadata :array, <<~SOURCE
[
foo,
bar,
baz
]
SOURCE
end
def test_array_args_add_star
assert_metadata :array, <<~SOURCE
[
foo,
*bar,
baz
]
SOURCE
end
def test_array_qwords
assert_metadata :array, <<~SOURCE
%w[
foo
bar
baz
]
SOURCE
end
def test_aref
assert_metadata :aref, 'foo[bar]'
end
def test_aref_field
assert_node_metadata(
:aref_field,
parse('foo[bar] = baz').dig(:body, 0),
sc: 0,
ec: 8
)
end
def test_args
assert_node_metadata(
:args,
parse('foo bar, baz').dig(:body, 1, :body, 0),
sc: 4,
ec: 12
)
end
def test_args_add_block
assert_node_metadata(
:args_add_block,
parse('foo bar, baz').dig(:body, 1),
sc: 4,
ec: 12
)
end
def test_args_add_star
assert_node_metadata(
:args_add_star,
parse('foo *bar').dig(:body, 1, :body, 0),
sc: 4,
ec: 8
)
end
def test_arg_paren
content = <<~SOURCE
foo(
a,
b,
c
)
SOURCE
assert_node_metadata(
:arg_paren,
parse(content).dig(:body, 1),
sc: 3,
ec: 20
)
end
def test_assign
assert_metadata :assign, 'foo = bar'
end
def test_assoc_new
assert_node_metadata(
:assoc_new,
parse('{ foo: bar, bar: baz }').dig(:body, 0, :body, 0),
sc: 2,
ec: 10
)
end
def test_assoc_splat
assert_node_metadata(
:assoc_splat,
parse('foo **bar').dig(:body, 1, :body, 0, :body, 0, :body, 0),
sc: 4,
ec: 9
)
end
def test_assoclist_from_args
assert_node_metadata(
:assoclist_from_args,
parse('{ foo => bar }').dig(:body, 0),
sc: 1,
ec: 13
)
end
def test_bare_assoc_hash
assert_node_metadata(
:bare_assoc_hash,
parse('foo(bar: baz)').dig(:body, 1, :body, 0, :body, 0, :body, 0),
sc: 4,
ec: 12
)
end
def test_begin
assert_metadata :begin, <<~SOURCE
begin
begin; end
end
SOURCE
end
def test_binary
assert_metadata :binary, 'foo + bar'
end
def test_blockarg
assert_node_metadata(
:blockarg,
parse('def foo(&bar) end').dig(:body, 1, :body, 0, :body, 6),
sc: 8,
ec: 12
)
end
def test_block_var
assert_node_metadata(
:block_var,
parse('foo { |bar| }').dig(:body, 1, :body, 0),
sc: 6,
ec: 11
)
end
def test_bodystmt
assert_node_metadata(
:bodystmt,
parse('class Foo; def foo; end; end').dig(:body, 2),
sc: 9,
ec: 25
)
end
def test_brace_block
assert_node_metadata(
:brace_block,
parse('foo { bar }').dig(:body, 1),
sc: 4,
ec: 11
)
end
def test_break
assert_metadata :break, 'break foo'
end
def test_call
assert_metadata :call, 'foo.bar'
end
def test_case
assert_metadata :case, <<~SOURCE
case foo
when bar
case baz
when qux
end
end
SOURCE
end
def test_class
assert_metadata :class, <<~SOURCE
class Foo
class Bar; end
end
SOURCE
end
def test_command
assert_metadata :command, 'foo bar'
end
def test_command_call
assert_metadata :command_call, 'foo.bar baz'
end
def test_const_ref
assert_node_metadata(
:const_ref,
parse('class Foo; end').dig(:body, 0),
sc: 6,
ec: 9
)
end
def test_const_path_field
assert_node_metadata(
:const_path_field,
parse('Foo::Bar = baz').dig(:body, 0),
sc: 0,
ec: 8
)
end
def test_const_path_ref
assert_metadata :const_path_ref, 'Foo::Bar'
end
def test_def
assert_metadata :def, <<~SOURCE
def foo
def bar; end
end
SOURCE
end
def test_defined
assert_metadata :defined, <<~SOURCE
defined?(
Foo
)
SOURCE
end
def test_defs
assert_metadata :defs, <<~SOURCE
def Object.foo
def Object.bar; end
end
SOURCE
end
def test_do_block
assert_node_metadata(
:do_block,
parse('foo do; bar; end').dig(:body, 1),
sc: 4,
ec: 16
)
end
def test_dot2
assert_metadata :dot2, 'foo..bar'
end
def test_dot3
assert_metadata :dot3, 'foo...bar'
end
def test_dyna_symbol
assert_metadata :dyna_symbol, ':"foo #{bar} baz"'
end
def test_else
content = <<~SOURCE
if foo
bar
else
baz
end
SOURCE
assert_node_metadata(:else, parse(content).dig(:body, 2), sc: 13, ec: 27)
end
def test_elsif
content = <<~SOURCE
if foo
bar
elsif bar
qux
end
SOURCE
assert_node_metadata(:elsif, parse(content).dig(:body, 2), sc: 13, ec: 32)
end
def test_ensure
content = <<~SOURCE
begin
foo
ensure
bar
end
SOURCE
assert_node_metadata(
:ensure,
parse(content).dig(:body, 0, :body, 3),
sc: 12,
ec: 28
)
end
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6')
def test_excessed_comma
assert_node_metadata(
:excessed_comma,
parse('foo { |bar,| }').dig(:body, 1, :body, 0, :body, 0, :body, 2),
sc: 10,
ec: 11
)
end
end
def test_fcall
assert_node_metadata(:fcall, parse('foo(bar)').dig(:body, 0), sc: 0, ec: 3)
end
def test_field
assert_node_metadata(
:field,
parse('foo.bar = baz').dig(:body, 0),
sc: 0,
ec: 7
)
end
def test_for
assert_metadata :for, <<~SOURCE
for foo in bar do
for baz in qux do
end
end
SOURCE
end
def test_hash
assert_metadata :hash, <<~SOURCE
{
foo: 'bar'
}
SOURCE
end
def test_if
assert_metadata :if, <<~SOURCE
if foo
if bar; end
end
SOURCE
end
def test_ifop
assert_metadata :ifop, 'foo ? bar : baz'
end
def test_if_mod
assert_metadata :if_mod, 'foo if bar'
end
def test_kwrest_param
assert_node_metadata(
:kwrest_param,
parse('def foo(**bar); end').dig(:body, 1, :body, 0, :body, 5),
sc: 8,
ec: 13
)
end
def test_lambda
assert_metadata :lambda, <<~SOURCE
-> (foo, bar) {
foo + bar
}
SOURCE
end
def test_massign
assert_metadata :massign, 'foo, bar, baz = 1, 2, 3'
end
def test_method_add_arg
assert_metadata :method_add_arg, 'foo(bar)'
end
def test_method_add_block
assert_metadata :method_add_block, 'foo { bar }'
end
def test_mlhs
assert_node_metadata(
:mlhs,
parse('foo, bar, baz = 1, 2, 3').dig(:body, 0),
sc: 0,
ec: 13
)
end
def test_mlhs_add_post
assert_node_metadata(
:mlhs_add_post,
parse('foo, *bar, baz = 1, 2, 3').dig(:body, 0),
sc: 5,
ec: 14
)
end
def test_mlhs_add_star
assert_node_metadata(
:mlhs_add_star,
parse('foo, *bar = 1, 2, 3').dig(:body, 0),
sc: 5,
ec: 9
)
end
def test_mlhs_paren
assert_node_metadata(
:mlhs_paren,
parse('(foo, bar) = baz').dig(:body, 0),
sc: 0,
ec: 10
)
end
def test_module
assert_metadata :module, <<~SOURCE
module Foo
module Bar; end
end
SOURCE
end
def test_mrhs_add_star
assert_node_metadata(
:mrhs_add_star,
parse('foo, bar = *baz').dig(:body, 1),
sc: 11,
ec: 15
)
end
def test_mrhs_new_from_args
assert_node_metadata(
:mrhs_new_from_args,
parse('foo, bar, baz = 1, 2, 3').dig(:body, 1),
sc: 16,
ec: 23
)
end
def test_next
assert_metadata :next, 'next foo'
end
def test_opassign
assert_metadata :opassign, 'foo ||= bar'
end
def test_params
content = <<~SOURCE
def foo(
bar,
baz
); end
SOURCE
assert_node_metadata(
:params,
parse(content).dig(:body, 1, :body, 0),
sc: 8,
ec: 22
)
end
def test_paren
assert_metadata :paren, '()'
end
def test_qsymbols
assert_node_metadata(
:qsymbols,
parse('%i[foo bar baz]').dig(:body, 0),
sc: 0,
ec: 15
)
end
def test_qwords
assert_node_metadata(
:qwords,
parse('%w[foo bar baz]').dig(:body, 0),
sc: 0,
ec: 15
)
end
def test_redo
assert_metadata :redo, 'redo'
end
def test_regexp_literal
assert_metadata :regexp_literal, '/foo/'
assert_metadata :regexp_literal, '%r{foo}'
assert_metadata :regexp_literal, '%r(foo)'
assert_node_metadata(
:regexp_literal,
parse('%r(foo)'),
beging: '%r(',
ending: ')'
)
end
def test_rescue
assert_node_metadata(
:rescue,
parse('begin; foo; rescue => bar; baz; end').dig(:body, 0, :body, 1),
sc: 12,
ec: 35
)
end
def test_rescue_mod
assert_metadata :rescue_mod, 'foo rescue bar'
end
def test_rest_param
assert_node_metadata(
:rest_param,
parse('def foo(*bar); end').dig(:body, 1, :body, 0, :body, 2),
sc: 8,
ec: 12
)
end
def test_retry
assert_metadata :retry, 'retry'
end
def test_return
assert_metadata :return, 'return foo'
end
def test_return0
assert_metadata :return0, 'return'
end
def test_sclass
assert_metadata :sclass, <<~SOURCE
class << Foo
class << Bar; end
end
SOURCE
end
def test_string_concat
assert_metadata :string_concat, <<~SOURCE
'foo' \
'bar'
SOURCE
end
def test_string_dvar
assert_node_metadata(
:string_dvar,
parse('"#$foo"').dig(:body, 0),
sc: 1,
ec: 6
)
end
def test_string_embexpr
assert_node_metadata(
:string_embexpr,
parse('"foo #{bar} baz"').dig(:body, 1),
sc: 5,
ec: 11
)
end
def test_string_literal
assert_metadata :string_literal, '"foo"'
end
def test_super
assert_metadata :super, 'super foo'
end
def test_symbol_literal
assert_metadata :symbol_literal, ':foo'
end
def test_symbols
assert_node_metadata(
:symbols,
parse('%I[f#{o}o b#{a}r b#{a}z]').dig(:body, 0),
sc: 0,
ec: 24
)
end
def test_top_const_field
assert_node_metadata(
:top_const_field,
parse('::Foo = bar').dig(:body, 0),
sc: 0,
ec: 5
)
end
def test_top_const_ref
assert_metadata :top_const_ref, '::Foo'
end
def test_unary
assert_metadata :unary, '-foo'
assert_metadata :unary, 'not foo'
end
def test_undef
assert_metadata :undef, 'undef foo, bar'
end
def test_unless
assert_metadata :unless, <<~SOURCE
unless foo
unless bar; end
end
SOURCE
end
def test_unless_mod
assert_metadata :unless_mod, 'foo unless bar'
end
def test_until
assert_metadata :until, <<~SOURCE
until foo
until bar; end
end
SOURCE
end
def test_until_mod
assert_metadata :until_mod, 'foo until bar'
end
def test_while
assert_metadata :while, <<~SOURCE
while foo
while bar; end
end
SOURCE
end
def test_var_alias
assert_metadata :var_alias, 'alias $foo $bar'
end
def test_var_field
assert_node_metadata(
:var_field,
parse('foo = 1').dig(:body, 0),
sc: 0,
ec: 3
)
end
def test_var_ref
assert_metadata :var_ref, 'true'
end
def test_vcall
assert_metadata :vcall, 'foo'
end
def test_void_stmt
assert_node_metadata(:void_stmt, parse('; ;'), sc: 0, ec: 0)
end
def test_when
assert_node_metadata(
:when,
parse('case foo; when bar; baz; end').dig(:body, 1),
sc: 10,
ec: 28
)
end
def test_while_mod
assert_metadata :while_mod, 'foo while bar'
end
def test_words
assert_node_metadata(
:words,
parse('%W[f#{o}o b#{a}r b#{a}z]').dig(:body, 0),
sc: 0,
ec: 24
)
end
def test_xstring
assert_metadata :xstring_literal, <<~SOURCE
`
foo
bar
`
SOURCE
end
def test_yield
assert_metadata :yield, 'yield foo'
end
def test_yield0
assert_metadata :yield0, 'yield'
end
def test_zsuper
assert_metadata :zsuper, 'super'
end
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7')
def test_args_forward
content = <<~SOURCE
def foo(...)
bar(...)
end
SOURCE
assert_node_metadata(
:args_forward,
parse(content).dig(:body, 1, :body, 0, :body, 2),
sc: 8,
ec: 11
)
end
def test_aryptn
content = <<~SOURCE
case foo
in bar, baz
qux
end
SOURCE
assert_node_metadata(
:aryptn,
parse(content).dig(:body, 1, :body, 0),
sc: 12,
ec: 20
)
end
def test_in
content = <<~SOURCE
case foo
in bar
baz
end
SOURCE
assert_node_metadata(:in, parse(content).dig(:body, 1), sc: 9, ec: 25)
end
end
private
def assert_metadata(type, ruby)
assert_node_metadata(
type,
parse(ruby),
sc: 0,
ec: ruby.chomp.size,
sl: 1,
el: [1, ruby.count("\n")].max
)
end
def assert_node_metadata(type, node, metadata)
assert_equal type, node[:type]
metadata.each { |key, value| assert_equal value, node[key] }
end
def parse(ruby)
Prettier::Parser.parse(ruby).dig(:body, 0, :body, 0)
end
end