<commit_before>require_relative '../spec_helper'

ruby_version_is "2.7" do
  describe "Numbered parameters" do
    it "provides default parameters _1, _2, ... in a block" do
      -> { _1 }.call("a").should == "a"
      proc { _1 }.call("a").should == "a"
      lambda { _1 }.call("a").should == "a"
      ["a"].map { _1 }.should == ["a"]
    end

    it "assigns nil to not passed parameters" do
      proc { [_1, _2] }.call("a").should == ["a", nil]
      proc { [_1, _2] }.call("a", "b").should == ["a", "b"]
    end

    it "supports variables _1-_9 only for the first 9 passed parameters" do
      block = proc { [_1, _2, _3, _4, _5, _6, _7, _8, _9] }
      result = block.call(1, 2, 3, 4, 5, 6, 7, 8, 9)
      result.should == [1, 2, 3, 4, 5, 6, 7, 8, 9]
    end

    it "does not support more than 9 parameters" do
      -> {
        proc { [_10] }.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      }.should raise_error(NameError, /undefined local variable or method `_10'/)
    end

    it "can not be used in both outer and nested blocks at the same time" do
      -> {
        eval("-> { _1; -> { _2 } }")
      }.should raise_error(SyntaxError, /numbered parameter is already used in.+ outer block here/m)
    end

    it "can be overwritten with local variable" do
      suppress_warning do
        eval <<~CODE
          _1 = 0
          proc { _1 }.call("a").should == 0
        CODE
      end
    end

    it "warns when numbered parameter is overriten with local variable" do
      -> {
        eval("_1 = 0")
      }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/)
    end

    it "raises SyntaxError when block parameters are specified explicitly" do
      -> { eval("-> () { _1 }")         }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("-> (x) { _1 }")        }.should raise_error(SyntaxError, /ordinary parameter is defined/)

      -> { eval("proc { || _1 }")       }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("proc { |x| _1 }")      }.should raise_error(SyntaxError, /ordinary parameter is defined/)<commit_msg>Switch reserved for numbered parameter warning to SyntaxError<commit_after>require_relative '../spec_helper'

ruby_version_is "2.7" do
  describe "Numbered parameters" do
    it "provides default parameters _1, _2, ... in a block" do
      -> { _1 }.call("a").should == "a"
      proc { _1 }.call("a").should == "a"
      lambda { _1 }.call("a").should == "a"
      ["a"].map { _1 }.should == ["a"]
    end

    it "assigns nil to not passed parameters" do
      proc { [_1, _2] }.call("a").should == ["a", nil]
      proc { [_1, _2] }.call("a", "b").should == ["a", "b"]
    end

    it "supports variables _1-_9 only for the first 9 passed parameters" do
      block = proc { [_1, _2, _3, _4, _5, _6, _7, _8, _9] }
      result = block.call(1, 2, 3, 4, 5, 6, 7, 8, 9)
      result.should == [1, 2, 3, 4, 5, 6, 7, 8, 9]
    end

    it "does not support more than 9 parameters" do
      -> {
        proc { [_10] }.call(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      }.should raise_error(NameError, /undefined local variable or method `_10'/)
    end

    it "can not be used in both outer and nested blocks at the same time" do
      -> {
        eval("-> { _1; -> { _2 } }")
      }.should raise_error(SyntaxError, /numbered parameter is already used in.+ outer block here/m)
    end

    ruby_version_is '2.7'...'2.8' do
      it "can be overwritten with local variable" do
        suppress_warning do
          eval <<~CODE
            _1 = 0
            proc { _1 }.call("a").should == 0
          CODE
        end
      end

      it "warns when numbered parameter is overriten with local variable" do
        -> {
          eval("_1 = 0")
        }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/)
      end
    end

    ruby_version_is '2.8' do
      it "cannot be overwritten with local variable" do
        -> {
          eval <<~CODE
            _1 = 0
            proc { _1 }.call("a").should == 0
          CODE
        }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/)
      end

      it "errors when numbered parameter is overriten with local variable" do
        -> {
          eval("_1 = 0")
        }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/)
      end
    end

    it "raises SyntaxError when block parameters are specified explicitly" do
      -> { eval("-> () { _1 }")         }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("-> (x) { _1 }")        }.should raise_error(SyntaxError, /ordinary parameter is defined/)

      -> { eval("proc { || _1 }")       }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("proc { |x| _1 }")      }.should raise_error(SyntaxError, /ordinary parameter is defined/)
{'fraction_non_alphanumeric': 0.14410569105691057, 'fraction_numerical': 0.03170731707317073, 'mean_word_length': 2.207953063885267, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381704', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1555, 'n_words': 567}