Skip to content

Commit

Permalink
Support "Endless method definition" syntax for Ruby 2.8 (3.0)
Browse files Browse the repository at this point in the history
This PR supports "Endless method definition" syntax for Ruby 2.8 (3.0).
Parser gem supports this syntax by whitequark/parser#676.

Ref: https://bugs.ruby-lang.org/issues/16746
  • Loading branch information
koic committed Jun 26, 2020
1 parent 4377a64 commit b790142
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#41](https://github.com/rubocop-hq/rubocop-ast/pull/41): Add `delimiters` and related predicates for `RegexpNode`. ([@owst][])
* [#46](https://github.com/rubocop-hq/rubocop-ast/pull/46): Basic support for [non-legacy AST output from parser](https://github.com/whitequark/parser/#usage). Note that there is no support (yet) in main RuboCop gem. ([@marcandre][])
* [#48](https://github.com/rubocop-hq/rubocop-ast/pull/48): Support `Parser::Ruby28` for Ruby 2.8 (3.0) parser. ([@koic][])
* [#49](https://github.com/rubocop-hq/rubocop-ast/pull/49): Support "Endless method definition" syntax for Ruby 2.8 (3.0). ([@koic][])

## 0.0.3 (2020-05-15)

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

0 comments on commit b790142

Please sign in to comment.