Skip to content

Commit

Permalink
Fix node destructuring for DefinedNode extension
Browse files Browse the repository at this point in the history
To make this polymorphic with other method dispatch nodes, we need
to do some custom destructuring.

Before:

```
defined_node.node_parts
```

After:

```
defined_node.node_parts
```
  • Loading branch information
Drenmi committed Feb 17, 2019
1 parent 9d94d10 commit d9c607f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* [#6763](https://github.com/rubocop-hq/rubocop/pull/6763): Fix false positives in range literals for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov]][])
* [#6748](https://github.com/rubocop-hq/rubocop/issues/6748): Fix `Style/RaiseArgs` auto-correction breaking in contexts that require parentheses. ([@drenmi][])
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` keyword. ([@drenmi][])
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` and `break` keywords. ([@drenmi][])

### Changes

Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node/defined_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module AST
class DefinedNode < Node
include ParameterizedNode
include MethodDispatchNode

def node_parts
[nil, :defined?, *to_a]
end
end
end
end
32 changes: 32 additions & 0 deletions spec/rubocop/ast/defined_node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::DefinedNode do
let(:defined_node) { parse_source(source).ast }

describe '.new' do
context 'with a defined? node' do
let(:source) { 'defined? :foo' }

it { expect(defined_node.is_a?(described_class)).to be(true) }
end
end

describe '#receiver' do
let(:source) { 'defined? :foo' }

it { expect(defined_node.receiver).to eq(nil) }
end

describe '#method_name' do
let(:source) { 'defined? :foo' }

it { expect(defined_node.method_name).to eq(:defined?) }
end

describe '#arguments' do
let(:source) { 'defined? :foo' }

it { expect(defined_node.arguments.size).to eq(1) }
it { expect(defined_node.arguments).to all(be_sym_type) }
end
end

0 comments on commit d9c607f

Please sign in to comment.