From de46e3dca7960539d5320a092d9beaaff0f2fcd4 Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Fri, 13 Oct 2023 09:02:29 -0400 Subject: [PATCH 1/2] AlwaysUseLowerCamelCase: Include enum case parameters in the checks. Also remove some warnings that arose from dead code. --- .../Core/Pipelines+Generated.swift | 1 + .../Rules/AlwaysUseLowerCamelCase.swift | 39 ++++++++----------- .../Rules/AlwaysUseLowerCamelCaseTests.swift | 18 ++++++++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Sources/SwiftFormat/Core/Pipelines+Generated.swift b/Sources/SwiftFormat/Core/Pipelines+Generated.swift index 6e9615823..1053bcd9e 100644 --- a/Sources/SwiftFormat/Core/Pipelines+Generated.swift +++ b/Sources/SwiftFormat/Core/Pipelines+Generated.swift @@ -109,6 +109,7 @@ class LintPipeline: SyntaxVisitor { } override func visit(_ node: EnumCaseParameterSyntax) -> SyntaxVisitorContinueKind { + visitIfEnabled(AlwaysUseLowerCamelCase.visit, for: node) visitIfEnabled(NoLeadingUnderscores.visit, for: node) return .visitChildren } diff --git a/Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift b/Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift index aad4fdb44..22c5432e5 100644 --- a/Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift +++ b/Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift @@ -71,32 +71,13 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule { public override func visit(_ node: ClosureSignatureSyntax) -> SyntaxVisitorContinueKind { if let input = node.parameterClause { - if let closureParamList = input.as(ClosureShorthandParameterListSyntax.self) { + switch input { + case .simpleInput(let closureParamList): for param in closureParamList { diagnoseLowerCamelCaseViolations( param.name, allowUnderscores: false, description: identifierDescription(for: node)) } - } else if let parameterClause = input.as(ClosureParameterClauseSyntax.self) { - for param in parameterClause.parameters { - diagnoseLowerCamelCaseViolations( - param.firstName, allowUnderscores: false, description: identifierDescription(for: node)) - if let secondName = param.secondName { - diagnoseLowerCamelCaseViolations( - secondName, allowUnderscores: false, description: identifierDescription(for: node)) - } - } - } else if let parameterClause = input.as(EnumCaseParameterClauseSyntax.self) { - for param in parameterClause.parameters { - if let firstName = param.firstName { - diagnoseLowerCamelCaseViolations( - firstName, allowUnderscores: false, description: identifierDescription(for: node)) - } - if let secondName = param.secondName { - diagnoseLowerCamelCaseViolations( - secondName, allowUnderscores: false, description: identifierDescription(for: node)) - } - } - } else if let parameterClause = input.as(FunctionParameterClauseSyntax.self) { + case .parameterClause(let parameterClause): for param in parameterClause.parameters { diagnoseLowerCamelCaseViolations( param.firstName, allowUnderscores: false, description: identifierDescription(for: node)) @@ -140,6 +121,20 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule { public override func visit(_ node: EnumCaseElementSyntax) -> SyntaxVisitorContinueKind { diagnoseLowerCamelCaseViolations( node.name, allowUnderscores: false, description: identifierDescription(for: node)) + return .visitChildren + } + + public override func visit(_ node: EnumCaseParameterSyntax) -> SyntaxVisitorContinueKind { + if let firstName = node.firstName { + diagnoseLowerCamelCaseViolations( + firstName, + allowUnderscores: false, + description: node.secondName != nil ? "enum case argument label" : "enum case parameter") + } + if let secondName = node.secondName { + diagnoseLowerCamelCaseViolations( + secondName, allowUnderscores: false, description: "enum case parameter") + } return .skipChildren } diff --git a/Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift b/Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift index 59201e42e..3e8c75a99 100644 --- a/Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift +++ b/Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift @@ -44,7 +44,6 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase { FindingSpec("5️⃣", message: "rename the variable 'PoorlyNamedVar' using lowerCamelCase"), ] ) - } func testInvalidEnumCaseCasing() { @@ -60,7 +59,24 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase { FindingSpec("1️⃣", message: "rename the enum case 'UpperCamelCase' using lowerCamelCase"), ] ) + } + func testInvalidEnumPayloadCasing() { + assertLint( + AlwaysUseLowerCamelCase.self, + """ + enum FooBarCases { + case payload1(1️⃣UpperCamelCase: Int) + case payload2(lowerCamelCase: Int, 2️⃣Label param: String) + case payload3(lowerCamelCase: Int, _ 3️⃣Param: String) + } + """, + findings: [ + FindingSpec("1️⃣", message: "rename the enum case parameter 'UpperCamelCase' using lowerCamelCase"), + FindingSpec("2️⃣", message: "rename the enum case argument label 'Label' using lowerCamelCase"), + FindingSpec("3️⃣", message: "rename the enum case parameter 'Param' using lowerCamelCase"), + ] + ) } func testInvalidClosureCasing() { From 089a56f5c0af126fb28fc1475078c0f2b06b9015 Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Fri, 13 Oct 2023 09:22:47 -0400 Subject: [PATCH 2/2] OrderedImports: Fix some other places where trailing comments were dropped. --- .../SwiftFormat/Rules/OrderedImports.swift | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/SwiftFormat/Rules/OrderedImports.swift b/Sources/SwiftFormat/Rules/OrderedImports.swift index 1a1463197..292bbd472 100644 --- a/Sources/SwiftFormat/Rules/OrderedImports.swift +++ b/Sources/SwiftFormat/Rules/OrderedImports.swift @@ -315,17 +315,17 @@ fileprivate func generateLines(codeBlockItemList: CodeBlockItemListSyntax, conte blockWithoutTrailingTrivia.trailingTrivia = [] currentLine.syntaxNode = .importCodeBlock(blockWithoutTrailingTrivia, sortable: sortable) } else { - guard let syntaxNode = currentLine.syntaxNode else { - currentLine.syntaxNode = .nonImportCodeBlocks([block]) - continue - } - // Multiple code blocks can be merged, as long as there isn't an import statement. - switch syntaxNode { - case .importCodeBlock: - appendNewLine() + if let syntaxNode = currentLine.syntaxNode { + // Multiple code blocks can be merged, as long as there isn't an import statement. + switch syntaxNode { + case .importCodeBlock: + appendNewLine() + currentLine.syntaxNode = .nonImportCodeBlocks([block]) + case .nonImportCodeBlocks(let existingCodeBlocks): + currentLine.syntaxNode = .nonImportCodeBlocks(existingCodeBlocks + [block]) + } + } else { currentLine.syntaxNode = .nonImportCodeBlocks([block]) - case .nonImportCodeBlocks(let existingCodeBlocks): - currentLine.syntaxNode = .nonImportCodeBlocks(existingCodeBlocks + [block]) } }