From dd235ed193a4bd51f62d3c426010b641283ca3fe Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Sun, 4 May 2025 21:15:14 +0300 Subject: [PATCH] Fix `RuboCop::AST::NumericNode#sign?` to return boolean The documentation states that `sign?` returns boolean: ```ruby def sign? source.match(SIGN_REGEX) end ``` but it does not. Looks like the only usage [1] of `sign?` is this [2] module, so nothing should break. [1] https://github.com/search?q=org%3Arubocop%20sign%3F&type=code [2] https://github.com/rubocop/rubocop/blob/ddbb2a1bb65a29ac2d2d963196f7b00821779fd6/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb#L226 Co-authored-by: Koichi ITO --- changelog/fix_numeric_node_sign_to_return_boolean.md | 1 + lib/rubocop/ast/node/mixin/numeric_node.rb | 2 +- spec/rubocop/ast/float_node_spec.rb | 6 ++++-- spec/rubocop/ast/int_node_spec.rb | 6 ++++-- spec/rubocop/ast/rational_node_spec.rb | 6 ++++-- 5 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 changelog/fix_numeric_node_sign_to_return_boolean.md diff --git a/changelog/fix_numeric_node_sign_to_return_boolean.md b/changelog/fix_numeric_node_sign_to_return_boolean.md new file mode 100644 index 000000000..7f6ee39b2 --- /dev/null +++ b/changelog/fix_numeric_node_sign_to_return_boolean.md @@ -0,0 +1 @@ +* [#380](https://github.com/rubocop/rubocop-ast/pull/380): Fix `RuboCop::AST::NumericNode#sign?` to return boolean. ([@viralpraxis][]) diff --git a/lib/rubocop/ast/node/mixin/numeric_node.rb b/lib/rubocop/ast/node/mixin/numeric_node.rb index 69e7ab146..66e0dd51a 100644 --- a/lib/rubocop/ast/node/mixin/numeric_node.rb +++ b/lib/rubocop/ast/node/mixin/numeric_node.rb @@ -15,7 +15,7 @@ module NumericNode # # @return [Boolean] whether this literal has a sign. def sign? - source.match(SIGN_REGEX) + source.match?(SIGN_REGEX) end end end diff --git a/spec/rubocop/ast/float_node_spec.rb b/spec/rubocop/ast/float_node_spec.rb index 8f2803313..b4fe5491f 100644 --- a/spec/rubocop/ast/float_node_spec.rb +++ b/spec/rubocop/ast/float_node_spec.rb @@ -10,16 +10,18 @@ end describe '#sign?' do + subject { float_node.sign? } + context 'explicit positive float' do let(:source) { '+42.0' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end context 'explicit negative float' do let(:source) { '-42.0' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end end diff --git a/spec/rubocop/ast/int_node_spec.rb b/spec/rubocop/ast/int_node_spec.rb index 677e7109b..47d1bfd10 100644 --- a/spec/rubocop/ast/int_node_spec.rb +++ b/spec/rubocop/ast/int_node_spec.rb @@ -10,16 +10,18 @@ end describe '#sign?' do + subject { int_node.sign? } + context 'explicit positive int' do let(:source) { '+42' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end context 'explicit negative int' do let(:source) { '-42' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end end diff --git a/spec/rubocop/ast/rational_node_spec.rb b/spec/rubocop/ast/rational_node_spec.rb index 8e27da8e8..a253ce395 100644 --- a/spec/rubocop/ast/rational_node_spec.rb +++ b/spec/rubocop/ast/rational_node_spec.rb @@ -10,16 +10,18 @@ end describe '#sign?' do + subject { rational_node.sign? } + context 'when explicit positive rational' do let(:source) { '+0.2r' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end context 'when explicit negative rational' do let(:source) { '-0.2r' } - it { is_expected.to be_sign } + it { is_expected.to be(true) } end end