-
Notifications
You must be signed in to change notification settings - Fork 221
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
[Lint] Add a rule to detect that type declarations are not capitalized #587
Conversation
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
…pes and typealias
|
||
extension Finding.Message { | ||
public static func capitalizeTypeName(name: String) -> Finding.Message { | ||
let capitalized = name.prefix(1).uppercased() + name.dropFirst() |
There was a problem hiding this comment.
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....)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
2fe98cf
to
be4493c
Compare
There was a problem hiding this 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!
This is a first "convention" rule that checks that type names are capitalized.