Skip to content

Commit

Permalink
[Fix #2898] Allow method definitions inside Class.new(SuperClass)
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Feb 29, 2016
1 parent 4678771 commit 116a4a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@
* [#2877](https://github.com/bbatsov/rubocop/issues/2877): `Style/SpaceAroundKeyword` doesn't flag `!super.method`, `!yield.method`, and so on. ([@alexdowad][])
* [#2631](https://github.com/bbatsov/rubocop/issues/2631): `Style/Encoding` can remove unneeded encoding comment when autocorrecting with `when_needed` style. ([@alexdowad][])
* [#2860](https://github.com/bbatsov/rubocop/issues/2860): Fix false positive in `Rails/Date` when `to_time` is chained with safe method. ([@palkan][])
* [#2898](https://github.com/bbatsov/rubocop/issues/2898): `Lint/NestedMethodDefinition` allows methods defined inside `Class.new(S)` blocks. ([@segiddins][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/nested_method_definition.rb
Expand Up @@ -27,7 +27,7 @@ class NestedMethodDefinition < Cop
PATTERN

def_node_matcher :class_or_module_new_call?, <<-PATTERN
(block (send (const nil {:Class :Module}) :new) ...)
(block (send (const nil {:Class :Module}) :new ...) ...)
PATTERN

def on_method_def(node, _method_name, _args, _body)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/nested_method_definition_spec.rb
Expand Up @@ -105,6 +105,18 @@
expect(cop.offenses.size).to eq(0)
end

it 'does not register offense for nested definition inside Class.new(S)' do
inspect_source(cop, ['class Foo',
' def self.define',
' Class.new(S) do',
' def y',
' end',
' end',
' end',
'end'])
expect(cop.offenses.size).to eq(0)
end

it 'does not register offense for nested definition inside Module.new' do
inspect_source(cop, ['class Foo',
' def self.define',
Expand Down

0 comments on commit 116a4a7

Please sign in to comment.