From b8d171e138b40e5e8980ff178eeb33f091e594e2 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 9 Jul 2022 19:42:38 +0900 Subject: [PATCH 1/2] Add `ragexp_dot?` method to `RuboCop::AST::Token`. Follow up #https://github.com/rubocop/rubocop/pull/10798. This PR adds `ragexp_dot?` method to `RuboCop::AST::Token`. --- changelog/new_add_regexp_dot_p_to_token.md | 1 + lib/rubocop/ast/token.rb | 4 ++++ spec/rubocop/ast/token_spec.rb | 22 +++++++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 changelog/new_add_regexp_dot_p_to_token.md 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..244ceca05 --- /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 `ragexp_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 From a15bdb827e12321c39f15a3ace4ecd4d818aedc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Sat, 9 Jul 2022 11:47:18 -0400 Subject: [PATCH 2/2] Update changelog/new_add_regexp_dot_p_to_token.md --- changelog/new_add_regexp_dot_p_to_token.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/new_add_regexp_dot_p_to_token.md b/changelog/new_add_regexp_dot_p_to_token.md index 244ceca05..d96804c80 100644 --- a/changelog/new_add_regexp_dot_p_to_token.md +++ b/changelog/new_add_regexp_dot_p_to_token.md @@ -1 +1 @@ -* [#235](https://github.com/rubocop-hq/rubocop-ast/pull/235): Add `ragexp_dot?` method to `RuboCop::AST::Token`. ([@koic][]) +* [#235](https://github.com/rubocop-hq/rubocop-ast/pull/235): Add `regexp_dot?` method to `RuboCop::AST::Token`. ([@koic][])