-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b45bb9f
commit c8294f7
Showing
7 changed files
with
323 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#333](https://github.com/rubocop/rubocop-ast/pull/333): Add `EnsureNode#rescue_node` method. ([@dvandersluis][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#333](https://github.com/rubocop/rubocop-ast/pull/333): Add `BeginNode` for `kwbegin` nodes. ([@dvandersluis][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `kwbegin` nodes. This will be used in place of a plain | ||
# node when the builder constructs the AST, making its methods available | ||
# to all `kwbegin` nodes within RuboCop. | ||
class KeywordBeginNode < Node | ||
# Returns the body of the `kwbegin` block. Returns `self` if the `kwbegin` contains | ||
# multiple nodes. | ||
# | ||
# @return [Node, nil] The body of the `kwbegin`. | ||
def body | ||
return unless node_parts.any? | ||
|
||
if rescue_node | ||
rescue_node.body | ||
elsif ensure_node | ||
ensure_node.node_parts[0] | ||
elsif node_parts.one? | ||
node_parts[0] | ||
else | ||
self | ||
end | ||
end | ||
|
||
# Returns the `rescue` node of the `kwbegin` block, if one is present. | ||
# | ||
# @return [Node, nil] The `rescue` node within `kwbegin`. | ||
def ensure_node | ||
node_parts[0] if node_parts[0]&.ensure_type? | ||
end | ||
|
||
# Returns the `rescue` node of the `kwbegin` block, if one is present. | ||
# | ||
# @return [Node, nil] The `rescue` node within `kwbegin`. | ||
def rescue_node | ||
return ensure_node&.rescue_node if ensure_node&.rescue_node | ||
|
||
node_parts[0] if node_parts[0]&.rescue_type? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::KeywordBeginNode do | ||
let(:parsed_source) { parse_source(source) } | ||
let(:kwbegin_node) { parsed_source.ast } | ||
|
||
describe '.new' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
it { expect(kwbegin_node).to be_a(described_class) } | ||
end | ||
|
||
describe '#body' do | ||
subject(:body) { kwbegin_node.body } | ||
|
||
let(:node) { parsed_source.node } | ||
|
||
context 'when the `kwbegin` node is empty' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when the `kwbegin` node only contains a single line' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
>> foo << | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to eq(node) } | ||
end | ||
|
||
context 'when the body has multiple lines' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it 'returns the entire `kwbegin` node' do | ||
expect(body).to eq(kwbegin_node) | ||
end | ||
end | ||
|
||
context 'when there is a `rescue` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
>>foo<< | ||
rescue | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to eq(node) } | ||
end | ||
|
||
context 'when there is an `ensure` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
>>foo<< | ||
ensure | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to eq(node) } | ||
end | ||
|
||
context 'when there is a `rescue` and `ensure` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
>>foo<< | ||
rescue | ||
bar | ||
ensure | ||
baz | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to eq(node) } | ||
end | ||
end | ||
|
||
describe '#ensure_node' do | ||
subject(:ensure_node) { kwbegin_node.ensure_node } | ||
|
||
context 'when the `kwbegin` node is empty' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when the `kwbegin` node only contains a single line' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when the body has multiple lines' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when there is a `rescue` node without `ensure`' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
rescue | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when there is an `ensure` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
ensure | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_a(RuboCop::AST::EnsureNode) } | ||
end | ||
|
||
context 'when there is a `rescue` and `ensure` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
rescue | ||
bar | ||
ensure | ||
baz | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_a(RuboCop::AST::EnsureNode) } | ||
end | ||
end | ||
|
||
describe '#rescue_node' do | ||
subject(:rescue_node) { kwbegin_node.rescue_node } | ||
|
||
context 'when the `kwbegin` node is empty' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when the `kwbegin` node only contains a single line' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when the body has multiple lines' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when there is a `rescue` node without `ensure`' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
rescue | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_a(RuboCop::AST::RescueNode) } | ||
end | ||
|
||
context 'when there is an `ensure` node without `rescue`' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
ensure | ||
bar | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'when there is a `rescue` and `ensure` node' do | ||
let(:source) do | ||
<<~RUBY | ||
begin | ||
foo | ||
rescue | ||
bar | ||
ensure | ||
baz | ||
end | ||
RUBY | ||
end | ||
|
||
it { is_expected.to be_a(RuboCop::AST::RescueNode) } | ||
end | ||
end | ||
end |