Skip to content

Commit de62cd7

Browse files
committed
Support RuboCop::AST::ComplexNode
This PR supports `RuboCop::AST::ComplexNode`. The `complex` type is categorized as a `numeric` type, so it should be handled transparently alongside `int`, `float`, and `rational`. ```ruby int: :numeric, float: :numeric, rational: :numeric, complex: :numeric, ``` https://github.com/rubocop/rubocop-ast/blob/v1.44.1/lib/rubocop/ast/node.rb#L106-L109 This eliminates the need for redundant branching, such as in rubocop/rubocop#14146.
1 parent c8a2a52 commit de62cd7

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#379](https://github.com/rubocop/rubocop-ast/pull/379): Support `RuboCop::AST::ComplexNode`. ([@koic][])

lib/rubocop/ast.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
require_relative 'ast/node/case_node'
4949
require_relative 'ast/node/casgn_node'
5050
require_relative 'ast/node/class_node'
51+
require_relative 'ast/node/complex_node'
5152
require_relative 'ast/node/const_node'
5253
require_relative 'ast/node/def_node'
5354
require_relative 'ast/node/defined_node'

lib/rubocop/ast/builder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def self.included(base)
3838
casgn: CasgnNode,
3939
case: CaseNode,
4040
class: ClassNode,
41+
complex: ComplexNode,
4142
const: ConstNode,
4243
def: DefNode,
4344
defined?: DefinedNode,

lib/rubocop/ast/node/complex_node.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module AST
5+
# A node extension for `complex` nodes. This will be used in place of a plain
6+
# node when the builder constructs the AST, making its methods available to
7+
# all `complex` nodes within RuboCop.
8+
class ComplexNode < Node
9+
include BasicLiteralNode
10+
include NumericNode
11+
end
12+
end
13+
end

lib/rubocop/ast/node/mixin/basic_literal_node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RuboCop
44
module AST
55
# Common functionality for primitive literal nodes: `sym`, `str`,
6-
# `int`, `float`, `rational`...
6+
# `int`, `float`, `rational`, `complex`...
77
module BasicLiteralNode
88
# Returns the value of the literal.
99
#

lib/rubocop/ast/node/mixin/numeric_node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module RuboCop
44
module AST
5-
# Common functionality for primitive numeric nodes: `int`, `float`, `rational`...
5+
# Common functionality for primitive numeric nodes: `int`, `float`, `rational`, `complex`...
66
module NumericNode
77
SIGN_REGEX = /\A[+-]/.freeze
88
private_constant :SIGN_REGEX

spec/rubocop/ast/complex_node_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::AST::ComplexNode do
4+
subject(:complex_node) { parse_source(source).ast }
5+
6+
describe '.new' do
7+
let(:source) { '+4.2i' }
8+
9+
it { is_expected.to be_a(described_class) }
10+
end
11+
12+
describe '#sign?' do
13+
subject { complex_node.sign? }
14+
15+
context 'when explicit positive complex' do
16+
let(:source) { '+4.2i' }
17+
18+
it { is_expected.to be(true) }
19+
end
20+
21+
context 'when explicit negative complex' do
22+
let(:source) { '-4.2i' }
23+
24+
it { is_expected.to be(true) }
25+
end
26+
end
27+
28+
describe '#value' do
29+
let(:source) { '+4.2i' }
30+
31+
it { expect(complex_node.value).to eq(+4.2i) }
32+
end
33+
end

0 commit comments

Comments
 (0)