Skip to content

Commit

Permalink
Make Node#numeric_type? aware of rational and complex literals
Browse files Browse the repository at this point in the history
This PR makes `Node#numeric_type?` aware of complex and rational literals.
These literals could be treated as numeric.

```ruby
42r.is_a?(Numeric)  # => true
42i.is_a?(Numeric)  # => true
42ri.is_a?(Numeric) # => true
```
  • Loading branch information
koic authored and marcandre committed Nov 7, 2021
1 parent cdc39d3 commit 1446501
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
@@ -0,0 +1 @@
* [#213](https://github.com/rubocop/rubocop-ast/pull/213): Make `Node#numeric_type?` aware of rational and complex literals. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node.rb
Expand Up @@ -464,7 +464,7 @@ def boolean_type?
end

def numeric_type?
int_type? || float_type?
int_type? || float_type? || rational_type? || complex_type?
end

def range_type?
Expand Down
50 changes: 50 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Expand Up @@ -786,4 +786,54 @@ class << expr
it { is_expected.to eq nil }
end
end

describe '#nurimec_type?' do
context 'when integer literal' do
let(:src) { '42' }

it 'is true' do
expect(node).to be_numeric_type
end
end

context 'when float literal' do
let(:src) { '42.0' }

it 'is true' do
expect(node).to be_numeric_type
end
end

context 'when rational literal' do
let(:src) { '42r' }

it 'is true' do
expect(node).to be_numeric_type
end
end

context 'when complex literal' do
let(:src) { '42i' }

it 'is true' do
expect(node).to be_numeric_type
end
end

context 'when complex literal whose imaginary part is a rational' do
let(:src) { '42ri' }

it 'is true' do
expect(node).to be_numeric_type
end
end

context 'when string literal' do
let(:src) { '"42"' }

it 'is true' do
expect(node).not_to be_numeric_type
end
end
end
end

0 comments on commit 1446501

Please sign in to comment.