diff --git a/changelog/new_add_regexp_dot_p_to_token.md b/changelog/new_add_regexp_dot_p_to_token.md new file mode 100644 index 000000000..d96804c80 --- /dev/null +++ b/changelog/new_add_regexp_dot_p_to_token.md @@ -0,0 +1 @@ +* [#235](https://github.com/rubocop-hq/rubocop-ast/pull/235): Add `regexp_dot?` method to `RuboCop::AST::Token`. ([@koic][]) diff --git a/lib/rubocop/ast/token.rb b/lib/rubocop/ast/token.rb index ecd8ef87a..69119cc80 100644 --- a/lib/rubocop/ast/token.rb +++ b/lib/rubocop/ast/token.rb @@ -100,6 +100,10 @@ def comma? type == :tCOMMA end + def regexp_dot? + %i[tDOT2 tDOT3].include?(type) + end + def rescue_modifier? type == :kRESCUE_MOD end diff --git a/spec/rubocop/ast/token_spec.rb b/spec/rubocop/ast/token_spec.rb index d2699dc6b..03e8d487f 100644 --- a/spec/rubocop/ast/token_spec.rb +++ b/spec/rubocop/ast/token_spec.rb @@ -8,6 +8,8 @@ def some_method [ 1, 2 ]; foo[0] = 3 + 1..42 + 1...42 end RUBY @@ -22,6 +24,8 @@ def some_method processed_source.find_token { |t| t.text == '[' && t.line == 3 } end let(:comma_token) { processed_source.find_token { |t| t.text == ',' } } + let(:irange_token) { processed_source.find_token { |t| t.text == '..' } } + let(:erange_token) { processed_source.find_token { |t| t.text == '...' } } let(:right_array_bracket_token) do processed_source.find_token { |t| t.text == ']' && t.line == 3 } end @@ -69,7 +73,7 @@ def some_method it 'returns line of token' do expect(first_token.line).to eq 1 expect(zero_token.line).to eq 4 - expect(end_token.line).to eq 5 + expect(end_token.line).to eq 7 end end @@ -85,7 +89,7 @@ def some_method it 'returns index of first char in token range of entire source' do expect(first_token.begin_pos).to eq 0 expect(zero_token.begin_pos).to eq 44 - expect(end_token.begin_pos).to eq 51 + expect(end_token.begin_pos).to eq 68 end end @@ -93,7 +97,7 @@ def some_method it 'returns index of last char in token range of entire source' do expect(first_token.end_pos).to eq 9 expect(zero_token.end_pos).to eq 45 - expect(end_token.end_pos).to eq 54 + expect(end_token.end_pos).to eq 71 end end @@ -232,6 +236,18 @@ def some_method end end + describe '#regexp_dot?' do + it 'returns true for regexp tokens' do + expect(irange_token).to be_regexp_dot + expect(erange_token).to be_regexp_dot + end + + it 'returns false for non comma tokens' do + expect(semicolon_token).not_to be_regexp_dot + expect(right_ref_bracket_token).not_to be_regexp_dot + end + end + describe '#rescue_modifier?' do let(:source) { <<~RUBY } def foo