parser 2.6.3.0 → 2.6.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2457,7 +2457,7 @@ class TestParser < Minitest::Test
2457
2457
  # f_arg opt_f_block_arg
2458
2458
  # f_arg tCOMMA
2459
2459
  assert_parses_blockargs(
2460
- s(:args, s(:procarg0, :a)),
2460
+ s(:args, s(:procarg0, s(:arg, :a))),
2461
2461
  %q{|a|},
2462
2462
  SINCE_1_9)
2463
2463
 
@@ -2700,6 +2700,39 @@ class TestParser < Minitest::Test
2700
2700
  Parser::Builders::Default.emit_procarg0 = true
2701
2701
  end
2702
2702
 
2703
+ def test_emit_arg_inside_procarg0_legacy
2704
+ Parser::Builders::Default.emit_arg_inside_procarg0 = false
2705
+ assert_parses_blockargs(
2706
+ s(:args,
2707
+ s(:procarg0, :a)),
2708
+ %q{|a|},
2709
+ SINCE_1_9)
2710
+ ensure
2711
+ Parser::Builders::Default.emit_arg_inside_procarg0 = true
2712
+ end
2713
+
2714
+ def test_procarg0
2715
+ assert_parses(
2716
+ s(:block,
2717
+ s(:send, nil, :m),
2718
+ s(:args,
2719
+ s(:procarg0, s(:arg, :foo))), nil),
2720
+ %q{m { |foo| } },
2721
+ %q{ ^^^ expression (args.procarg0)},
2722
+ SINCE_1_9)
2723
+
2724
+ assert_parses(
2725
+ s(:block,
2726
+ s(:send, nil, :m),
2727
+ s(:args,
2728
+ s(:procarg0, s(:arg, :foo), s(:arg, :bar))), nil),
2729
+ %q{m { |(foo, bar)| } },
2730
+ %q{ ^ begin (args.procarg0)
2731
+ | ^ end (args.procarg0)
2732
+ | ^^^^^^^^^^ expression (args.procarg0)},
2733
+ SINCE_1_9)
2734
+ end
2735
+
2703
2736
  def test_block_kwarg_combinations
2704
2737
  # f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
2705
2738
  assert_parses_blockargs(
@@ -5116,6 +5149,26 @@ class TestParser < Minitest::Test
5116
5149
  |~~~~~~~~~~~~~~~~~~~~~ expression})
5117
5150
  end
5118
5151
 
5152
+ def test_rescue_mod_masgn
5153
+ assert_parses(
5154
+ s(:masgn,
5155
+ s(:mlhs,
5156
+ s(:lvasgn, :foo),
5157
+ s(:lvasgn, :bar)),
5158
+ s(:rescue,
5159
+ s(:send, nil, :meth),
5160
+ s(:resbody, nil, nil,
5161
+ s(:array,
5162
+ s(:int, 1),
5163
+ s(:int, 2))), nil)),
5164
+ %q{foo, bar = meth rescue [1, 2]},
5165
+ %q{ ~~~~~~ keyword (rescue.resbody)
5166
+ | ~~~~~~~~~~~~~ expression (rescue.resbody)
5167
+ | ~~~~~~~~~~~~~~~~~~ expression (rescue)
5168
+ |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression},
5169
+ SINCE_2_7)
5170
+ end
5171
+
5119
5172
  def test_rescue_mod_op_assign
5120
5173
  assert_parses(
5121
5174
  s(:op_asgn,
@@ -5675,7 +5728,7 @@ class TestParser < Minitest::Test
5675
5728
  s(:send,
5676
5729
  s(:int, 1), :tap),
5677
5730
  s(:args,
5678
- s(:procarg0, :n)),
5731
+ s(:procarg0, s(:arg, :n))),
5679
5732
  s(:send, nil, :p,
5680
5733
  s(:lvar, :n))),
5681
5734
  s(:int, 0)),
@@ -6286,7 +6339,7 @@ class TestParser < Minitest::Test
6286
6339
  s(:send, nil, :a,
6287
6340
  s(:ivar, :@b)),
6288
6341
  s(:args,
6289
- s(:procarg0, :c)), nil),
6342
+ s(:procarg0, s(:arg, :c))), nil),
6290
6343
  %q{a @b do |c|;end},
6291
6344
  %q{},
6292
6345
  SINCE_1_9)
@@ -7153,4 +7206,330 @@ class TestParser < Minitest::Test
7153
7206
  %q{ ^ location},
7154
7207
  SINCE_2_7)
7155
7208
  end
7209
+
7210
+ def test_unterimated_heredoc_id__27
7211
+ assert_diagnoses(
7212
+ [:error, :unterminated_heredoc_id],
7213
+ %Q{<<\"EOS\n\nEOS\n},
7214
+ %q{^ location},
7215
+ SINCE_2_7)
7216
+
7217
+ assert_diagnoses(
7218
+ [:error, :unterminated_heredoc_id],
7219
+ %Q{<<\"EOS\n\"\nEOS\n},
7220
+ %q{^ location},
7221
+ SINCE_2_7)
7222
+
7223
+ %W[\r\n \n].each do |nl|
7224
+ assert_diagnoses(
7225
+ [:error, :unterminated_heredoc_id],
7226
+ %Q{<<\"\r\"#{nl}\r#{nl}},
7227
+ %q{^ location},
7228
+ SINCE_2_7)
7229
+ end
7230
+ end
7231
+
7232
+ def test_numbered_args_before_27
7233
+ assert_diagnoses(
7234
+ [:error, :ivar_name, { :name => '@1' }],
7235
+ %q{m { @1 }},
7236
+ %q{ ^^ location},
7237
+ ALL_VERSIONS - SINCE_2_7
7238
+ )
7239
+ end
7240
+
7241
+ def test_numbered_args_after_27
7242
+ assert_parses(
7243
+ s(:numblock,
7244
+ s(:send, nil, :m),
7245
+ 15,
7246
+ s(:send,
7247
+ s(:numparam, 1), :+,
7248
+ s(:numparam, 15))),
7249
+ %q{m { @1 + @15 }},
7250
+ %q{^^^^^^^^^^^^^^ expression
7251
+ | ^^ name (send/2.numparam/1)
7252
+ | ^^ expression (send/2.numparam/1)
7253
+ | ^^^ name (send/2.numparam/2)
7254
+ | ^^^ expression (send/2.numparam/2)},
7255
+ SINCE_2_7)
7256
+
7257
+ assert_parses(
7258
+ s(:numblock,
7259
+ s(:send, nil, :m),
7260
+ 15,
7261
+ s(:send,
7262
+ s(:numparam, 1), :+,
7263
+ s(:numparam, 15))),
7264
+ %q{m do @1 + @15 end},
7265
+ %q{^^^^^^^^^^^^^^^^^ expression
7266
+ | ^^ name (send/2.numparam/1)
7267
+ | ^^ expression (send/2.numparam/1)
7268
+ | ^^^ name (send/2.numparam/2)
7269
+ | ^^^ expression (send/2.numparam/2)},
7270
+ SINCE_2_7)
7271
+
7272
+ # Lambdas
7273
+
7274
+ assert_parses(
7275
+ s(:numblock,
7276
+ s(:lambda),
7277
+ 15,
7278
+ s(:send,
7279
+ s(:numparam, 1), :+,
7280
+ s(:numparam, 15))),
7281
+ %q{-> { @1 + @15}},
7282
+ %q{^^^^^^^^^^^^^^ expression
7283
+ | ^^ name (send.numparam/1)
7284
+ | ^^ expression (send.numparam/1)
7285
+ | ^^^ name (send.numparam/2)
7286
+ | ^^^ expression (send.numparam/2)},
7287
+ SINCE_2_7)
7288
+
7289
+ assert_parses(
7290
+ s(:numblock,
7291
+ s(:lambda),
7292
+ 15,
7293
+ s(:send,
7294
+ s(:numparam, 1), :+,
7295
+ s(:numparam, 15))),
7296
+ %q{-> do @1 + @15 end},
7297
+ %q{^^^^^^^^^^^^^^^^^^ expression
7298
+ | ^^ name (send.numparam/1)
7299
+ | ^^ expression (send.numparam/1)
7300
+ | ^^^ name (send.numparam/2)
7301
+ | ^^^ expression (send.numparam/2)},
7302
+ SINCE_2_7)
7303
+ end
7304
+
7305
+ def test_numbered_and_ordinary_parameters
7306
+ # Blocks
7307
+
7308
+ assert_diagnoses(
7309
+ [:error, :ordinary_param_defined],
7310
+ %q{m { || @1 } },
7311
+ %q{ ^^ location},
7312
+ SINCE_2_7)
7313
+
7314
+ assert_diagnoses(
7315
+ [:error, :ordinary_param_defined],
7316
+ %q{m { |a| @1 } },
7317
+ %q{ ^^ location},
7318
+ SINCE_2_7)
7319
+
7320
+ assert_diagnoses(
7321
+ [:error, :ordinary_param_defined],
7322
+ %q{m do || @1 end },
7323
+ %q{ ^^ location},
7324
+ SINCE_2_7)
7325
+
7326
+ assert_diagnoses(
7327
+ [:error, :ordinary_param_defined],
7328
+ %q{m do |a, b| @1 end },
7329
+ %q{ ^^ location},
7330
+ SINCE_2_7)
7331
+
7332
+ assert_diagnoses(
7333
+ [:error, :ordinary_param_defined],
7334
+ %q{m { |x = @1| }},
7335
+ %q{ ^^ location},
7336
+ SINCE_2_7)
7337
+
7338
+ assert_diagnoses(
7339
+ [:error, :ordinary_param_defined],
7340
+ %q{m { |x: @1| }},
7341
+ %q{ ^^ location},
7342
+ SINCE_2_7)
7343
+
7344
+ # Lambdas
7345
+
7346
+ assert_diagnoses(
7347
+ [:error, :ordinary_param_defined],
7348
+ %q{->() { @1 } },
7349
+ %q{ ^^ location},
7350
+ SINCE_2_7)
7351
+
7352
+ assert_diagnoses(
7353
+ [:error, :ordinary_param_defined],
7354
+ %q{->(a) { @1 } },
7355
+ %q{ ^^ location},
7356
+ SINCE_2_7)
7357
+
7358
+ assert_diagnoses(
7359
+ [:error, :ordinary_param_defined],
7360
+ %q{->() do @1 end },
7361
+ %q{ ^^ location},
7362
+ SINCE_2_7)
7363
+
7364
+ assert_diagnoses(
7365
+ [:error, :ordinary_param_defined],
7366
+ %q{->(a, b) do @1 end},
7367
+ %q{ ^^ location},
7368
+ SINCE_2_7)
7369
+
7370
+ assert_diagnoses(
7371
+ [:error, :ordinary_param_defined],
7372
+ %q{->(x=@1) {}},
7373
+ %q{ ^^ location},
7374
+ SINCE_2_7)
7375
+
7376
+ assert_diagnoses(
7377
+ [:error, :ordinary_param_defined],
7378
+ %q{->(x: @1) {}},
7379
+ %q{ ^^ location},
7380
+ SINCE_2_7)
7381
+
7382
+ assert_diagnoses(
7383
+ [:error, :ordinary_param_defined],
7384
+ %q{proc {|;a| @1}},
7385
+ %q{ ^^ location},
7386
+ SINCE_2_7)
7387
+
7388
+ assert_diagnoses(
7389
+ [:error, :ordinary_param_defined],
7390
+ "proc {|\n| @1}",
7391
+ %q{ ^^ location},
7392
+ SINCE_2_7)
7393
+ end
7394
+
7395
+ def test_numparam_outside_block
7396
+ assert_diagnoses(
7397
+ [:error, :numparam_outside_block],
7398
+ %q{class A; @1; end},
7399
+ %q{ ^^ location},
7400
+ SINCE_2_7)
7401
+
7402
+ assert_diagnoses(
7403
+ [:error, :numparam_outside_block],
7404
+ %q{module A; @1; end},
7405
+ %q{ ^^ location},
7406
+ SINCE_2_7)
7407
+
7408
+ assert_diagnoses(
7409
+ [:error, :numparam_outside_block],
7410
+ %q{class << foo; @1; end},
7411
+ %q{ ^^ location},
7412
+ SINCE_2_7)
7413
+
7414
+ assert_diagnoses(
7415
+ [:error, :numparam_outside_block],
7416
+ %q{def self.m; @1; end},
7417
+ %q{ ^^ location},
7418
+ SINCE_2_7)
7419
+ end
7420
+
7421
+ def test_ruby_bug_15789
7422
+ assert_parses(
7423
+ s(:send, nil, :m,
7424
+ s(:block,
7425
+ s(:lambda),
7426
+ s(:args,
7427
+ s(:optarg, :a,
7428
+ s(:numblock,
7429
+ s(:lambda), 1,
7430
+ s(:numparam, 1)))),
7431
+ s(:lvar, :a))),
7432
+ %q{m ->(a = ->{@1}) {a}},
7433
+ %q{},
7434
+ SINCE_2_7)
7435
+
7436
+ assert_parses(
7437
+ s(:send, nil, :m,
7438
+ s(:block,
7439
+ s(:lambda),
7440
+ s(:args,
7441
+ s(:kwoptarg, :a,
7442
+ s(:numblock,
7443
+ s(:lambda), 1,
7444
+ s(:numparam, 1)))),
7445
+ s(:lvar, :a))),
7446
+ %q{m ->(a: ->{@1}) {a}},
7447
+ %q{},
7448
+ SINCE_2_7)
7449
+ end
7450
+
7451
+ def test_ruby_bug_15839
7452
+ assert_diagnoses(
7453
+ [:error, :invalid_encoding],
7454
+ %q{# encoding: cp932
7455
+ <<-TEXT
7456
+ \xe9\x9d\u1234
7457
+ TEXT
7458
+ })
7459
+
7460
+ assert_diagnoses(
7461
+ [:error, :invalid_encoding],
7462
+ %q{
7463
+ # encoding: cp932
7464
+ <<-TEXT
7465
+ \xe9\x9d
7466
+ \u1234
7467
+ TEXT
7468
+ })
7469
+
7470
+ assert_diagnoses(
7471
+ [:error, :invalid_encoding],
7472
+ %q{
7473
+ # encoding: cp932
7474
+ <<-TEXT
7475
+ \u1234\xe9\x9d
7476
+ TEXT
7477
+ })
7478
+
7479
+ assert_diagnoses(
7480
+ [:error, :invalid_encoding],
7481
+ %q{
7482
+ # encoding: cp932
7483
+ <<-TEXT
7484
+ \u1234
7485
+ \xe9\x9d
7486
+ TEXT
7487
+ })
7488
+ end
7489
+
7490
+ def test_numparam_as_symbols
7491
+ assert_diagnoses(
7492
+ [:error, :ivar_name, { :name => '@' }],
7493
+ %q{:@},
7494
+ %q{ ^ location},
7495
+ SINCE_2_7)
7496
+
7497
+ assert_diagnoses(
7498
+ [:error, :ivar_name, { :name => '@1' }],
7499
+ %q{:@1},
7500
+ %q{ ^^ location},
7501
+ SINCE_2_7)
7502
+
7503
+ assert_diagnoses(
7504
+ [:error, :cvar_name, { :name => '@@' }],
7505
+ %q{:@@},
7506
+ %q{ ^^ location},
7507
+ SINCE_2_7)
7508
+
7509
+ assert_diagnoses(
7510
+ [:error, :cvar_name, { :name => '@@1' }],
7511
+ %q{:@@1},
7512
+ %q{ ^^^ location},
7513
+ SINCE_2_7)
7514
+ end
7515
+
7516
+ def test_csend_inside_lhs_of_masgn__since_27
7517
+ assert_diagnoses(
7518
+ [:error, :csend_in_lhs_of_masgn],
7519
+ %q{*a&.x = 0},
7520
+ %q{ ^^ location},
7521
+ SINCE_2_7)
7522
+
7523
+ assert_diagnoses(
7524
+ [:error, :csend_in_lhs_of_masgn],
7525
+ %q{a&.x, = 0},
7526
+ %q{ ^^ location},
7527
+ SINCE_2_7)
7528
+
7529
+ assert_diagnoses(
7530
+ [:error, :csend_in_lhs_of_masgn],
7531
+ %q{*a&.A = 0},
7532
+ %q{ ^^ location},
7533
+ SINCE_2_7)
7534
+ end
7156
7535
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+ require 'open3'
5
+
6
+ class TestRunnerParse < Minitest::Test
7
+ PATH_TO_RUBY_PARSE = File.expand_path('../bin/ruby-parse', __dir__).freeze
8
+
9
+ def assert_prints(argv, expected_output)
10
+ stdout, stderr, status = Open3.capture3(PATH_TO_RUBY_PARSE, *argv)
11
+
12
+ assert_equal 0, status.to_i
13
+ assert_includes(stdout, expected_output)
14
+ end
15
+
16
+ def test_emit_ruby
17
+ assert_prints ['--emit-ruby', '-e 123'],
18
+ 's(:int, 123)'
19
+ end
20
+
21
+ def test_emit_json
22
+ assert_prints ['--emit-json', '-e', '123'],
23
+ '["int",123]'
24
+ end
25
+
26
+ def test_emit_ruby_empty
27
+ assert_prints ['--emit-ruby', '-e', ''],
28
+ "\n"
29
+ end
30
+
31
+ def test_emit_json_empty
32
+ assert_prints ['--emit-json', '-e', ''],
33
+ "\n"
34
+ end
35
+ end