Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "Endless method definition" syntax for Ruby 2.8 (3.0) #49

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#49](https://github.com/rubocop-hq/rubocop-ast/pull/49): Support "Endless method definition" syntax for Ruby 2.8 (3.0). ([@koic][])

## 0.1.0 (2020-06-26)

### New features
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/ast/builder.rb
Expand Up @@ -26,6 +26,7 @@ class Builder < Parser::Builders::Default
case: CaseNode,
class: ClassNode,
def: DefNode,
def_e: DefNode,
defined?: DefinedNode,
defs: DefNode,
ensure: EnsureNode,
Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/ast/node/def_node.rb
Expand Up @@ -27,6 +27,15 @@ def argument_forwarding?
arguments.any?(&:forward_args_type?) || arguments.any?(&:forward_arg_type?)
end

# Checks whether this is an endless method definition node
# as per the feature added in Ruby 2.8.
#
# @note This is written in a way that may support method definition
# which are rumored to be added in a later version of Ruby.
#
# @return [Boolean] whether the `def_s` node
alias endless? def_e_type?

# The name of the defined method as a symbol.
#
# @return [Symbol] the name of the defined method
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/ast/def_node_spec.rb
Expand Up @@ -15,6 +15,12 @@

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

context 'with a def_e node', :ruby28 do
let(:source) { 'def foo() = 42' }

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

describe '#method_name' do
Expand All @@ -41,6 +47,12 @@

it { expect(def_node.method_name).to eq(:-@) }
end

context 'with endless method definition', :ruby28 do
let(:source) { 'def foo() = 42' }

it { expect(def_node.method_name).to eq(:foo) }
end
end

describe '#method?' do
Expand Down Expand Up @@ -109,6 +121,12 @@

it { expect(def_node.arguments.size).to eq(1) }
end

context 'with endless method definition', :ruby28 do
let(:source) { 'def foo(bar, baz) = 42' }

it { expect(def_node.arguments.size).to eq(2) }
end
end

describe '#first_argument' do
Expand Down Expand Up @@ -357,6 +375,20 @@
end
end

context 'when using Ruby 2.8 or newer', :ruby28 do
context 'with endless method definition', :ruby28 do
let(:source) { 'def foo() = 42' }

it { expect(def_node.endless?).to be(true) }
end

context 'with normal method definition' do
let(:source) { 'def foo; end' }

it { expect(def_node.endless?).to be(false) }
end
end

describe '#receiver' do
context 'with an instance method definition' do
let(:source) { 'def foo(bar); end' }
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -30,6 +30,10 @@
let(:ruby_version) { 2.7 }
end

RSpec.shared_context 'ruby 2.8', :ruby28 do
let(:ruby_version) { 2.8 }
end

module DefaultRubyVersion
extend RSpec::SharedContext

Expand Down