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

Add a rule to discourage use of T::Struct#serialize #130

Open
glortho opened this issue Feb 2, 2023 · 2 comments
Open

Add a rule to discourage use of T::Struct#serialize #130

glortho opened this issue Feb 2, 2023 · 2 comments

Comments

@glortho
Copy link

glortho commented Feb 2, 2023

As the Sorbet docs themselves admit, T::Struct#serialize has some surprising behavior, such that it is often better to implement serialization by hand.

To avoid these issues, it would be nice to have a rule that disallows T::Struct#serialize. Ideally it would include these additional features:

  • It should link to the Sorbet docs that explain why we're make this recommendation
  • It should link to an example of the preferred pattern
  • It should respect an allowlist of exceptions
  • It should not discourage use of T::Enum#serialize (which is safer)

I'm happy to contribute an implementation for review, but I'd love to hear any feedback about this approach in the meantime!

@sambostock
Copy link
Contributor

I think this would be challenging, because RuboCop has no knowledge of what the type of an object is. Given

object.serialize

or even something more explicit like

MyStruct.new(key: value).serialize

there would be no way for it to know if the receiver is a T::Struct, T::Enum, or some other object.

@andyw8
Copy link
Contributor

andyw8 commented Feb 19, 2023

Maybe there is a way: #142

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

No branches or pull requests

3 participants