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

Stop monkey-patching core classes #481

Merged
merged 2 commits into from
Sep 7, 2021
Merged

Conversation

paracycle
Copy link
Member

@paracycle paracycle commented Sep 3, 2021

Motivation

Monkey patching Class to add descendants causes a confusion with the Class#descendants method that Active Support adds and the signature on this method leaks out into the Tapioca generated RBI which causes type-checking problems in Core.

Monkey patching String to add underscore causes a confusion with the String#underscore method that Active Support adds and causes problems like https://discourse.shopify.io/t/file-naming-inflection-issues-with-tapioca-dsl-verify-in-ci/19663 (since the method later on gets replaced by the AS version and starts using application inflectors, etc, which we don't want, we just want a simple deterministic CamelCase to under_score implementation)

Implementation

descendants is arguably a reflection method, so I moved it to Tapioca::Reflection module, which is already included in the Base DSL class and SymbolGenerator class, as descendants_of(klass). The added benefit is that through type parameters, we get better typing on this method, thus we need less casts.

I moved the underscore method onto the Generator class and also duplicated it into the DSL test base class.

Tests

No added tests.

This causes a confusion with the `Class#descendants` method that Active
Support adds and the signature on this method leaks out into the Tapioca
generated RBI and causes type-checking problems in Core because of it.

This is arguably a reflection method, so I moved it to
`Tapioca::Reflection` module which is already included in the Base DSL
class and SymbolGenerator class.
@paracycle paracycle requested a review from a team September 3, 2021 19:25
This causes a confusion with the `String#underscore` method that Active
Support adds and causes problems like https://discourse.shopify.io/t/file-naming-inflection-issues-with-tapioca-dsl-verify-in-ci/19663

I moved the method onto the `Generator` class and also duplicated it
into the DSL test base class.
underscore(target_class_name)
end

sig { params(class_name: String).returns(String) }
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be replication of the previous declaration of this method in lib/tapioca/generator.rb. Would it be better to make this a helper module somewhere we can inject instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed it is, but since this is a test base class, I wasn't too worried about refactoring to a helper class/module. The real usage is in generator.rb only.

@paracycle paracycle merged commit b8e60c6 into main Sep 7, 2021
@paracycle paracycle deleted the uk-fix-patching-problems branch September 7, 2021 14:20
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.

4 participants