Skip to content

Commit

Permalink
Allow self.Foo
Browse files Browse the repository at this point in the history
omitting `self` in `self.Foo` gives uninitialized constant NameError
  • Loading branch information
chulkilee committed Nov 22, 2013
1 parent e4b7f67 commit 5e9d697
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* All cops now support the `Include` param, which specifies the files on which they should operate. ([@bbatsov][])
* All cops now support the `Exclude` param, which specifies the files on which they should not operate. ([@bbatsov][])
* [#631](https://github.com/bbatsov/rubocop/issues/631): `IndentationWidth` cop now detects inconsistent indentation between lines that should have the same indentation. ([@jonas054][])
* Allow `self.Foo` in `RedundantSelf` cop. ([@chulkilee][])

### Changes

Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/redundant_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def on_send(node)
receiver, method_name, *_args = *node
if receiver && receiver.type == :self
unless operator?(method_name) || keyword?(method_name) ||
constant_name?(method_name) ||
@allowed_send_nodes.include?(node) ||
@local_variables.include?(method_name)
convention(node, :expression)
Expand Down Expand Up @@ -128,6 +129,10 @@ def keyword?(method_name)
:yield].include?(method_name)
end

def constant_name?(method_name)
method_name.match(/^[A-Z]/)
end

def allow_self(node)
if node.type == :send
receiver, _method_name, *_args = *node
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/redundant_self_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
expect(cop.offences).to be_empty
end

it 'accepts a self receiver used to distinguish from constant' do
src = ['self.Foo']
inspect_source(cop, src)
expect(cop.offences).to be_empty
end

it 'auto-corrects by removing redundant self' do
new_source = autocorrect_source(cop, ['self.x'])
expect(new_source).to eq('x')
Expand Down

0 comments on commit 5e9d697

Please sign in to comment.