Shopify / rubocop-sorbet

A collection of RuboCop rules for Sorbet
MIT License
182 stars 27 forks source link

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

Open glortho opened 1 year ago

glortho commented 1 year ago

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:

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

sambostock commented 1 year ago

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 commented 1 year ago

Maybe there is a way: https://github.com/Shopify/rubocop-sorbet/issues/142