-
Notifications
You must be signed in to change notification settings - Fork 223
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
allevato
merged 3 commits into
swiftlang:main
from
xedin:upstream-lint-type-capitalization-rule
Aug 14, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Sources/SwiftFormatRules/TypeNamesShouldBeCapitalized.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftFormatCore | ||
import SwiftSyntax | ||
|
||
/// `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 { | ||
public override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: AssociatedTypeDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
public override func visit(_ node: TypeAliasDeclSyntax) -> SyntaxVisitorContinueKind { | ||
diagnoseNameConventionMismatch(node, name: node.name) | ||
return .visitChildren | ||
} | ||
|
||
private func diagnoseNameConventionMismatch<T: DeclSyntaxProtocol>(_ type: T, name: TokenSyntax) { | ||
let leadingUnderscores = name.text.prefix { $0 == "_" } | ||
if let firstChar = name.text[leadingUnderscores.endIndex...].first, | ||
firstChar.uppercased() != String(firstChar) { | ||
diagnose(.capitalizeTypeName(name: name.text), on: type, severity: .convention) | ||
} | ||
} | ||
} | ||
|
||
extension Finding.Message { | ||
public static func capitalizeTypeName(name: String) -> Finding.Message { | ||
var capitalized = name | ||
let leadingUnderscores = capitalized.prefix { $0 == "_" } | ||
let charAt = leadingUnderscores.endIndex | ||
capitalized.replaceSubrange(charAt...charAt, with: capitalized[charAt].uppercased()) | ||
return "type names should be capitalized: \(name) -> \(capitalized)" | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
Tests/SwiftFormatRulesTests/TypeNamesShouldBeCapitalizedTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import SwiftFormatRules | ||
|
||
final class TypeNamesShouldBeCapitalizedTests: LintOrFormatRuleTestCase { | ||
func testConstruction() { | ||
let input = | ||
""" | ||
struct a {} | ||
class klassName { | ||
struct subType {} | ||
} | ||
protocol myProtocol {} | ||
|
||
extension myType { | ||
struct innerType {} | ||
} | ||
""" | ||
|
||
performLint(TypeNamesShouldBeCapitalized.self, input: input) | ||
|
||
XCTAssertDiagnosed(.capitalizeTypeName(name: "a")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "klassName")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "subType")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "myProtocol")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "myType")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "innerType")) | ||
} | ||
|
||
func testActors() { | ||
let input = | ||
""" | ||
actor myActor {} | ||
actor OtherActor {} | ||
distributed actor greeter {} | ||
distributed actor DistGreeter {} | ||
""" | ||
|
||
performLint(TypeNamesShouldBeCapitalized.self, input: input) | ||
|
||
XCTAssertDiagnosed(.capitalizeTypeName(name: "myActor")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "OtherActor")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "greeter")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "DistGreeter")) | ||
} | ||
|
||
func testAssociatedTypeandTypeAlias() { | ||
let input = | ||
""" | ||
protocol P { | ||
associatedtype kind | ||
associatedtype OtherKind | ||
} | ||
|
||
typealias x = Int | ||
typealias Y = String | ||
|
||
struct MyType { | ||
typealias data<T> = Y | ||
|
||
func test() { | ||
typealias Value<T> = Y | ||
} | ||
} | ||
""" | ||
|
||
performLint(TypeNamesShouldBeCapitalized.self, input: input) | ||
|
||
XCTAssertDiagnosed(.capitalizeTypeName(name: "kind")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "OtherKind")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "x")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "Y")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "data")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "Value")) | ||
} | ||
|
||
func testThatUnderscoredNamesAreDiagnosed() { | ||
let input = | ||
""" | ||
protocol _p { | ||
associatedtype _value | ||
associatedtype __Value | ||
} | ||
|
||
protocol ___Q { | ||
} | ||
|
||
struct _data { | ||
typealias _x = Int | ||
} | ||
|
||
struct _Data {} | ||
|
||
actor _internalActor {} | ||
|
||
enum __e { | ||
} | ||
|
||
enum _OtherE { | ||
} | ||
|
||
func test() { | ||
class _myClass {} | ||
do { | ||
class _MyClass {} | ||
} | ||
} | ||
|
||
distributed actor __greeter {} | ||
distributed actor __InternalGreeter {} | ||
""" | ||
|
||
performLint(TypeNamesShouldBeCapitalized.self, input: input) | ||
|
||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_p")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "___Q")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_value")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "__Value")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_data")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "_Data")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_x")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_internalActor")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "__e")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "__OtherE")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "_myClass")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "_MyClass")) | ||
XCTAssertDiagnosed(.capitalizeTypeName(name: "__greeter")) | ||
XCTAssertNotDiagnosed(.capitalizeTypeName(name: "__InternalGreeter")) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
, andTypeAliasDeclSyntax
? 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 :)