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

[Lint] Add a rule to detect that type declarations are not capitalized #587

Merged
merged 3 commits into from
Aug 14, 2023

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Aug 10, 2023

This is a first "convention" rule that checks that type names are capitalized.

@xedin xedin requested a review from allevato August 10, 2023 17:50
Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

Thanks!

/// `struct`, `class`, `enum` and `protocol` declarations should have a capitalized name.
///
/// Lint: Types with un-capitalized names will yield a lint error.
public final class TypeNamesShouldBeCapitalized : SyntaxLintRule {
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add ActorDeclSyntax, AssociatedTypeDeclSyntax, and TypeAliasDeclSyntax? I think that covers everything we'd want to apply this to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Will do! I went conservative initially to not over do it :)

}

private func diagnoseNameConventionMismatch<T: DeclSyntaxProtocol>(_ type: T, name: TokenSyntax) {
if let firstChar = name.text.first, !firstChar.isUppercase {
Copy link
Member

Choose a reason for hiding this comment

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

Another thought occurred to me later; this will also flag type names that start with underscores, like _PrivateTypeButPublicForReasons. Do we want this rule to flag those too? Since those situations still come up in real-world code I don't know if we want to burden users with having to write // swift-format-ignore: TypeNamesShouldBeCapitalized above those.

Maybe strip off any sequence of leading underscores before checking the "first" character?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to instead check that the first character does not change when uppercased to make it independent of the IdentifiersMustBeASCII rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed this is in 2fe98cf

@xedin xedin requested a review from allevato August 11, 2023 22:39

extension Finding.Message {
public static func capitalizeTypeName(name: String) -> Finding.Message {
let capitalized = name.prefix(1).uppercased() + name.dropFirst()
Copy link
Member

Choose a reason for hiding this comment

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

This won't produce the right result if there were leading underscores; if someone used _foo or __foo, I think we'd want to recommend _Foo or __Foo to them.

Unfortunately the tests don't catch this because they assert the diagnostic values rather than the generated message. (Something I'd like to fix one day....)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I forgot to change that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

Looks great now, thank you!

@allevato allevato merged commit a27f05e into swiftlang:main Aug 14, 2023
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.

None yet

3 participants