Skip to content

Conversation

@viralpraxis
Copy link
Contributor

In case of something like

class A
  class << self
    def foo = ??
  end
end

the prefixed name of the foo method is s(:self).foo, not self.foo.

Looks like ideally it should be A.foo (?), but this patch does not address that.

Here's how it looks in the report,

before:

"location": {
  "type": "method",
  "class": "SomeModule::SomeClass",
  "method": "s(:self).some_singleton_method"
}

after:

"location": {
  "type": "method",
  "class": "SomeModule::SomeClass",
  "method": "self.some_singleton_method"
}

name = :"#{src[1]}.#{name}"
# See https://github.com/presidentbeef/brakeman/pull/881 for context
prefix = sexp?(src[1]) ? src[1][0] : src[1]
name = :"#{prefix}.#{name}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't work if src[1] is not self.

E.g.

def A.foo
  # ...
end

src[1].value might work better (I haven't tested thoroughly).

Copy link
Contributor Author

@viralpraxis viralpraxis Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @presidentbeef,

I think this won't work if src[1] is not self.

Good point. I've added some tests for def Const.m, def @ivar.m and def lvar.m

src[1].value might work better (I haven't tested thoroughly).

It does not work with self (s(:self).value => nil), so I tried another approach. LKM what you think

@viralpraxis viralpraxis force-pushed the fix-singleton-method-prefixes branch 2 times, most recently from 1513225 to 6c0a252 Compare December 6, 2025 12:13
In case of something like

```ruby
class A
  class << self
    def foo = ??
  end
end
```

the prefixed name of the `foo` method is `s(:self).foo`, not `self.foo`.

Looks like ideally it should be `A.foo` (?), but this patch does not address that.

Here's how it looks in the report,

before:

```
"location": {
  "type": "method",
  "class": "SomeModule::SomeClass",
  "method": "s(:self).some_singleton_method"
}
```

after:

```
"location": {
  "type": "method",
  "class": "SomeModule::SomeClass",
  "method": "self.some_singleton_method"
}
```
@viralpraxis viralpraxis force-pushed the fix-singleton-method-prefixes branch from 6c0a252 to 6cb4075 Compare December 6, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants