-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ArgNode for
arg
, kwarg
, optarg
, restarg
, kwoptarg
, `kwr…
…estarg`, `blockarg`, `forward_arg` and `shadowarg` types. - Add `ArgsNode#argument_list` to return all descendant argument type nodes. - Added Procarg0Node for modernized compatibility with ArgNode. - Expose `ArgsNode#argument_list` on `BlockNode`.
1 parent
6dad540
commit 67fdc91
Showing
14 changed files
with
507 additions
and
10 deletions.
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 @@ | ||
* [#154](https://github.com/rubocop-hq/rubocop-ast/pull/154): Add `BlockNode#argument_list` and `BlockNode#argument_names`. ([@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,2 @@ | ||
* [#154](https://github.com/rubocop-hq/rubocop-ast/pull/154): Add `ArgNode` and `Procarg0Node` ("modern" mode), and add `ArgsNode#argument_list` to get only argument type 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
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `arg`, `optarg`, `restarg`, `kwarg`, `kwoptarg`, | ||
# `kwrestarg`, `blockarg`, `shadowarg` and `forward_arg` nodes. | ||
# This will be used in place of a plain node when the builder constructs | ||
# the AST, making its methods available to all `arg` nodes within RuboCop. | ||
class ArgNode < Node | ||
# Returns the name of an argument. | ||
# | ||
# @return [Symbol, nil] the name of the argument | ||
def name | ||
node_parts[0] | ||
end | ||
|
||
# Returns the default value of the argument, if any. | ||
# | ||
# @return [Node, nil] the default value of the argument | ||
def default_value | ||
return unless default? | ||
|
||
node_parts[1] | ||
end | ||
|
||
# Checks whether the argument has a default value | ||
# | ||
# @return [Boolean] whether the argument has a default value | ||
def default? | ||
optarg_type? || kwoptarg_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
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A node extension for `procarg0` nodes. | ||
# This will be used in place of a plain node when the builder constructs | ||
# the AST, making its methods available to all `arg` nodes within RuboCop. | ||
class Procarg0Node < ArgNode | ||
# Returns the name of an argument. | ||
# | ||
# @return [Symbol, nil] the name of the argument | ||
def name | ||
node_parts[0].name | ||
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,312 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::ArgNode do | ||
let(:args_node) { parse_source(source).ast.arguments } | ||
let(:arg_node) { args_node.first } | ||
|
||
describe '.new' do | ||
context 'with a method definition' do | ||
let(:source) { 'def foo(x) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a block' do | ||
let(:source) { 'foo { |x| bar }' } | ||
|
||
if RuboCop::AST::Builder.emit_procarg0 | ||
it { expect(arg_node).to be_a(RuboCop::AST::Procarg0Node) } | ||
else | ||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
end | ||
|
||
context 'with a lambda literal' do | ||
let(:source) { '-> (x) { bar }' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a keyword argument' do | ||
let(:source) { 'def foo(x:) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with an optional argument' do | ||
let(:source) { 'def foo(x = 42) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with an optional keyword argument' do | ||
let(:source) { 'def foo(x: 42) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a splatted argument' do | ||
let(:source) { 'def foo(*x) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a double splatted argument' do | ||
let(:source) { 'def foo(**x) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a block argument' do | ||
let(:source) { 'def foo(&x) end' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with a shadow argument' do | ||
let(:source) { 'foo { |; x| }' } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
|
||
context 'with argument forwarding' do | ||
context 'with Ruby >= 2.7', :ruby27 do | ||
let(:source) { 'def foo(...); end' } | ||
|
||
if RuboCop::AST::Builder.emit_forward_arg | ||
it { expect(arg_node).to be_a(described_class) } | ||
else | ||
it { expect(arg_node).to be_forward_args_type } | ||
end | ||
end | ||
|
||
context 'with Ruby >= 3.0', :ruby30 do | ||
let(:source) { 'def foo(x, ...); end' } | ||
let(:arg_node) { args_node.last } | ||
|
||
it { expect(arg_node).to be_a(described_class) } | ||
end | ||
end | ||
end | ||
|
||
describe '#name' do | ||
subject { arg_node.name } | ||
|
||
context 'with a regular argument' do | ||
let(:source) { 'def foo(x) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a block' do | ||
let(:source) { 'foo { |x| x }' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a keyword argument' do | ||
let(:source) { 'def foo(x:) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with an optional argument' do | ||
let(:source) { 'def foo(x = 42) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with an optional keyword argument' do | ||
let(:source) { 'def foo(x: 42) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a splatted argument' do | ||
let(:source) { 'def foo(*x) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a nameless splatted argument' do | ||
let(:source) { 'def foo(*) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a double splatted argument' do | ||
let(:source) { 'def foo(**x) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a nameless double splatted argument' do | ||
let(:source) { 'def foo(**) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a block argument' do | ||
let(:source) { 'def foo(&x) end' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with a shadow argument' do | ||
let(:source) { 'foo { |; x| x = 5 }' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
|
||
context 'with argument forwarding' do | ||
context 'with Ruby >= 2.7', :ruby27 do | ||
let(:source) { 'def foo(...); end' } | ||
|
||
it { is_expected.to be_nil } if RuboCop::AST::Builder.emit_forward_arg | ||
end | ||
|
||
context 'with Ruby >= 3.0', :ruby30 do | ||
let(:source) { 'def foo(x, ...); end' } | ||
let(:arg_node) { args_node.last } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end | ||
|
||
describe '#default_value' do | ||
include AST::Sexp | ||
|
||
subject { arg_node.default_value } | ||
|
||
context 'with a regular argument' do | ||
let(:source) { 'def foo(x) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a block' do | ||
let(:source) { 'foo { |x| x }' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with an optional argument' do | ||
let(:source) { 'def foo(x = 42) end' } | ||
|
||
it { is_expected.to eq(s(:int, 42)) } | ||
end | ||
|
||
context 'with an optional keyword argument' do | ||
let(:source) { 'def foo(x: 42) end' } | ||
|
||
it { is_expected.to eq(s(:int, 42)) } | ||
end | ||
|
||
context 'with a splatted argument' do | ||
let(:source) { 'def foo(*x) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a double splatted argument' do | ||
let(:source) { 'def foo(**x) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a block argument' do | ||
let(:source) { 'def foo(&x) end' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with a shadow argument' do | ||
let(:source) { 'foo { |; x| x = 5 }' } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context 'with argument forwarding' do | ||
context 'with Ruby >= 2.7', :ruby27 do | ||
let(:source) { 'def foo(...); end' } | ||
|
||
it { is_expected.to be_nil } if RuboCop::AST::Builder.emit_forward_arg | ||
end | ||
|
||
context 'with Ruby >= 3.0', :ruby30 do | ||
let(:source) { 'def foo(x, ...); end' } | ||
let(:arg_node) { args_node.last } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end | ||
|
||
describe '#default?' do | ||
subject { arg_node.default? } | ||
|
||
context 'with a regular argument' do | ||
let(:source) { 'def foo(x) end' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with a block' do | ||
let(:source) { 'foo { |x| x }' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with an optional argument' do | ||
let(:source) { 'def foo(x = 42) end' } | ||
|
||
it { is_expected.to eq(true) } | ||
end | ||
|
||
context 'with an optional keyword argument' do | ||
let(:source) { 'def foo(x: 42) end' } | ||
|
||
it { is_expected.to eq(true) } | ||
end | ||
|
||
context 'with a splatted argument' do | ||
let(:source) { 'def foo(*x) end' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with a double splatted argument' do | ||
let(:source) { 'def foo(**x) end' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with a block argument' do | ||
let(:source) { 'def foo(&x) end' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with a shadow argument' do | ||
let(:source) { 'foo { |; x| x = 5 }' } | ||
|
||
it { is_expected.to eq(false) } | ||
end | ||
|
||
context 'with argument forwarding' do | ||
context 'with Ruby >= 2.7', :ruby27 do | ||
let(:source) { 'def foo(...); end' } | ||
|
||
it { is_expected.to eq(false) } if RuboCop::AST::Builder.emit_forward_arg | ||
end | ||
|
||
context 'with Ruby >= 3.0', :ruby30 do | ||
let(:source) { 'def foo(x, ...); end' } | ||
let(:arg_node) { args_node.last } | ||
|
||
it { is_expected.to eq(false) } | ||
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
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::Procarg0Node, :ruby27 do | ||
let(:procarg0_node) { parse_source(source).ast.arguments.first } | ||
|
||
describe '.new' do | ||
context 'with a block' do | ||
let(:source) { 'foo { |x| x }' } | ||
|
||
if RuboCop::AST::Builder.emit_procarg0 | ||
it { expect(procarg0_node).to be_a(described_class) } | ||
else | ||
it { expect(procarg0_node).to be_a(RuboCop::AST::ArgNode) } | ||
end | ||
end | ||
end | ||
|
||
describe '#name' do | ||
subject { procarg0_node.name } | ||
|
||
let(:source) { 'foo { |x| x }' } | ||
|
||
it { is_expected.to eq(:x) } | ||
end | ||
end |