Skip to content

Commit

Permalink
Add token#new_line?
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeo committed Oct 8, 2022
1 parent 241cd04 commit 6bf122d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_tokennew_line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#240](https://github.com/rubocop/rubocop-ast/pull/240): Add a type predicate `new_line?` to Token. ([@tdeo][])
4 changes: 4 additions & 0 deletions lib/rubocop/ast/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def end?
def equal_sign?
%i[tEQL tOP_ASGN].include?(type)
end

def new_line?
type == :tNL
end
end
end
end
12 changes: 12 additions & 0 deletions spec/rubocop/ast/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def some_method
let(:equals_token) { processed_source.find_token { |t| t.text == '=' } }

let(:end_token) { processed_source.find_token { |t| t.text == 'end' } }
let(:new_line_token) { processed_source.find_token { |t| t.line == 7 && t.column == 3 } }

describe '.from_parser_token' do
subject(:token) { described_class.from_parser_token(parser_token) }
Expand Down Expand Up @@ -303,6 +304,17 @@ def foo
end
end

describe '#new_line?' do
it 'returns true for new line tokens' do
expect(new_line_token).to be_a_new_line
end

it 'returns false for non new line tokens' do
expect(end_token).not_to be_a_new_line
expect(semicolon_token).not_to be_a_new_line
end
end

context 'with braces & parens' do
let(:source) { <<~RUBY }
{ a: 1 }
Expand Down

0 comments on commit 6bf122d

Please sign in to comment.