Skip to content

Fix ups for AlwaysUseLowerCamelCase and OrderedImports. #650

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/SwiftFormat/Core/Pipelines+Generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 17 additions & 22 deletions Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/SwiftFormat/Rules/OrderedImports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
FindingSpec("5️⃣", message: "rename the variable 'PoorlyNamedVar' using lowerCamelCase"),
]
)

}

func testInvalidEnumCaseCasing() {
Expand All @@ -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() {
Expand Down