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

fix: ActiveSupportConcern recursively checks for ClassMethod #1933

Conversation

spencewenski
Copy link
Contributor

@spencewenski spencewenski commented Jun 18, 2024

Motivation

ActiveSupportConcern uses const_defined? to check if ClassMethods exists on a module. By default, const_defined? checks the module and its ancestors for the existence of the constant. This means that const_defined? will return true when a module does not have ClassMethods defined, but one of its ancestors does. This can result in ActiveSupportConcern generating mixes_in_class_methods.*ClassMethods lines for invalid .*ClassMethods modules.

Implementation

const_defined? takes a second parameter that controls whether to include ancestors in the check. Setting this parameter to false to exclude ancestors.

Tests

The first commit in this PR adds a negative test case that should fail with the existing code and pass with the fix from the second commit.

Problem
-------
`ActiveSupportConcern` uses
[const_defined?](https://apidock.com/ruby/Module/const_defined%3f) to
check if `ClassMethods` exists on a module. By default, `const_defined?`
checks the module and its ancestors for the existence of the constant.
This means that `const_defined?` will return `true` when a module does
not have `ClassMethods` defined, but one of its ancestors does. This can
result in `ActiveSupportConcern` generating
`mixes_in_class_methods.*ClassMethods` lines for invalid
`.*ClassMethods` modules.

The first commit in this PR adds a negative test case that should fail
with the existing code.

Solution
--------
`const_defined?` takes a second parameter that controls whether to
include ancestors in the check. Setting this parameter to `false` to
exclude ancestors.
Set the second parameter of
[const_defined?](https://apidock.com/ruby/Module/const_defined%3f) to
false in order to exclude ancestors of the module when checking for the
existence of the `ClassMethods` constant.
@spencewenski spencewenski marked this pull request as ready for review June 18, 2024 21:32
@spencewenski spencewenski requested a review from a team as a code owner June 18, 2024 21:32
Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

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

Thanks. I'll merge after the linting fix.

@spencewenski
Copy link
Contributor Author

Thanks. I'll merge after the linting fix.

This should be fixed now. Thanks!

@egiurleo egiurleo merged commit e1086ca into Shopify:main Jun 20, 2024
16 checks passed
@spencewenski spencewenski deleted the ActiveSupportConcern-generates-invalid-classmethod branch June 20, 2024 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants