From 2b1946a0ed3ff91dc53ffa4c18c065c2ddea35cb Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sat, 1 Aug 2020 18:36:19 +0300 Subject: [PATCH] Add `IntNode#value` and `FloatNode#value` --- CHANGELOG.md | 1 + lib/rubocop/ast/node/float_node.rb | 1 + lib/rubocop/ast/node/int_node.rb | 1 + spec/rubocop/ast/float_node_spec.rb | 16 ++++++++++++---- spec/rubocop/ast/int_node_spec.rb | 8 ++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8254d3c54..1f6e785d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#70](https://github.com/rubocop-hq/rubocop-ast/pull/70): Add `NextNode` ([@marcandre][]) +* [#85](https://github.com/rubocop-hq/rubocop-ast/pull/85): Add `IntNode#value` and `FloatNode#value`. ([@fatkodima][]) ### Bug fixes diff --git a/lib/rubocop/ast/node/float_node.rb b/lib/rubocop/ast/node/float_node.rb index 6f892c23f..61858f55e 100644 --- a/lib/rubocop/ast/node/float_node.rb +++ b/lib/rubocop/ast/node/float_node.rb @@ -6,6 +6,7 @@ module AST # node when the builder constructs the AST, making its methods available to # all `float` nodes within RuboCop. class FloatNode < Node + include BasicLiteralNode include NumericNode end end diff --git a/lib/rubocop/ast/node/int_node.rb b/lib/rubocop/ast/node/int_node.rb index 178270bbc..b6412da6d 100644 --- a/lib/rubocop/ast/node/int_node.rb +++ b/lib/rubocop/ast/node/int_node.rb @@ -6,6 +6,7 @@ module AST # node when the builder constructs the AST, making its methods available to # all `int` nodes within RuboCop. class IntNode < Node + include BasicLiteralNode include NumericNode end end diff --git a/spec/rubocop/ast/float_node_spec.rb b/spec/rubocop/ast/float_node_spec.rb index 126359383..5d9c0d855 100644 --- a/spec/rubocop/ast/float_node_spec.rb +++ b/spec/rubocop/ast/float_node_spec.rb @@ -1,25 +1,33 @@ # frozen_string_literal: true RSpec.describe RuboCop::AST::FloatNode do - let(:int_node) { parse_source(source).ast } + let(:float_node) { parse_source(source).ast } describe '.new' do let(:source) { '42.0' } - it { expect(int_node.is_a?(described_class)).to be_truthy } + it { expect(float_node.is_a?(described_class)).to be_truthy } end describe '#sign?' do context 'explicit positive float' do let(:source) { '+42.0' } - it { expect(int_node.sign?).to be_truthy } + it { expect(float_node.sign?).to be_truthy } end context 'explicit negative float' do let(:source) { '-42.0' } - it { expect(int_node.sign?).to be_truthy } + it { expect(float_node.sign?).to be_truthy } end end + + describe '#value' do + let(:source) do + '1.5' + end + + it { expect(float_node.value).to eq(1.5) } + end end diff --git a/spec/rubocop/ast/int_node_spec.rb b/spec/rubocop/ast/int_node_spec.rb index 699717605..b8262dad3 100644 --- a/spec/rubocop/ast/int_node_spec.rb +++ b/spec/rubocop/ast/int_node_spec.rb @@ -22,4 +22,12 @@ it { expect(int_node.sign?).to be_truthy } end end + + describe '#value' do + let(:source) do + '10' + end + + it { expect(int_node.value).to eq(10) } + end end