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 TypeAliasName to ensure all constants used as T.type_alias are in CamelCase #92

Merged
merged 1 commit into from
Feb 9, 2022

Conversation

Morriar
Copy link
Contributor

@Morriar Morriar commented Feb 8, 2022

This cop ensures all constants used as T.type_alias are using CamelCase:

# bad
FOO_OR_BAR = T.type_alias { T.any(Foo, Bar) }

# good
FooOrBar = T.type_alias { T.any(Foo, Bar) }

@Morriar Morriar requested a review from a team as a code owner February 8, 2022 23:55
@Morriar Morriar self-assigned this Feb 8, 2022
@Morriar Morriar added enhancement New feature or request feature Add a new feature labels Feb 8, 2022
@Morriar
Copy link
Contributor Author

Morriar commented Feb 9, 2022

Please skip the first commit as it belong to #93 🙏

name = node.children[1]

# From https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/naming/class_and_module_camel_case.rb
return unless /_/.match?(name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will pass happily if name is something like "HELLO"

Should we also check for name == name.capitalize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we want to allow such things no? Like for class names: SSH, HTTP?

Copy link
Member

Choose a reason for hiding this comment

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

I think we'll have to compromise here. Typically, we'd expect only acronyms to be all upcase, like SSH and HTTP, but Hello should be camelcase. Rails uses an inflector initializer to define which words in your app are acronyms and can be all upcase and if they're not on the list it's always considered as camelcase.

For rubocop-sorbet that feels like too much complexity for type aliases - especially since it's not tied to Rails. I think we'll need to decide whether we prefer acronyms to be forced to be camelcase or allow folks to attach type aliases to all upcase words like HELLO. My vote goes to force all to be camelcase.

Copy link
Contributor

Choose a reason for hiding this comment

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

I hadn't thought about the acronym case, good shout @Morriar
FWIW: There's at least one type_alias in Core called ID which would be affected by this decision. Supporting rails-style acronym convention would be consistent with Rubocop, as Alex pointed out.
My vote goes to upcase. My 0.02 dollars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are a few ones that are legit, yet would be rejected by a stricter rule:

  • ID
  • GraphQLVariablesType
  • ARObject
  • StatsDTag

I'm not sure I want to force renaming those as they look good. What I really want to avoid is things like VALUE_TYPE. But I agree that in the current state this cop is too permissive and would allow TYPE.

Not sure what's the best, I'm happy to let the votes decide.

Copy link
Member

Choose a reason for hiding this comment

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

That's fair, I agree with only marking names with underscores as offenses.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should lean towards giving more flexibility to the devs since we can't perfectly know what's allowed. Underscore offence seems good enough.

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.

lgtm after typo

spec/rubocop/cop/sorbet/type_alias_name_spec.rb Outdated Show resolved Hide resolved
@Morriar Morriar merged commit df539b3 into main Feb 9, 2022
@Morriar Morriar deleted the at-alias-cop branch February 9, 2022 16:42
@shopify-shipit shopify-shipit bot temporarily deployed to production February 9, 2022 17:01 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature Add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants