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/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]) } } 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() {