Skip to content

Commit 2b62c75

Browse files
committed
Adjustments to split FunctionParameterSyntax into multiple nodes for function parameters, closure parameters and enum parameters
Companion of swiftlang/swift-syntax#1455
1 parent 89a0b6f commit 2b62c75

8 files changed

+31
-22
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14131413
after(node.trailingComma, tokens: .break)
14141414

14151415
if let associatedValue = node.associatedValue {
1416-
arrangeParameterClause(associatedValue, forcesBreakBeforeRightParen: false)
1416+
arrangeEnumCaseParameterClause(associatedValue, forcesBreakBeforeRightParen: false)
14171417
}
14181418

14191419
return .visitChildren
@@ -2606,6 +2606,24 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
26062606
tokens: .break(.close(mustBreak: forcesBreakBeforeRightParen), size: 0), .close)
26072607
}
26082608

2609+
/// Applies formatting to a collection of enum case parameters for a decl.
2610+
///
2611+
/// - Parameters:
2612+
/// - parameters: A node that contains the parameters that can be passed to a decl when its
2613+
/// called.
2614+
/// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
2615+
/// when the right paren is on a different line than the corresponding left paren.
2616+
private func arrangeEnumCaseParameterClause(
2617+
_ parameters: EnumCaseParameterClauseSyntax, forcesBreakBeforeRightParen: Bool
2618+
) {
2619+
guard !parameters.parameterList.isEmpty else { return }
2620+
2621+
after(parameters.leftParen, tokens: .break(.open, size: 0), .open(argumentListConsistency()))
2622+
before(
2623+
parameters.rightParen,
2624+
tokens: .break(.close(mustBreak: forcesBreakBeforeRightParen), size: 0), .close)
2625+
}
2626+
26092627
/// Applies consistent formatting to the braces and contents of the given node.
26102628
///
26112629
/// - Parameters:

Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
7575
}
7676
} else if let parameterClause = input.as(ParameterClauseSyntax.self) {
7777
for param in parameterClause.parameterList {
78-
if let firstName = param.firstName {
79-
diagnoseLowerCamelCaseViolations(
80-
firstName, allowUnderscores: false, description: identifierDescription(for: node))
81-
}
78+
diagnoseLowerCamelCaseViolations(
79+
param.firstName, allowUnderscores: false, description: identifierDescription(for: node))
8280
if let secondName = param.secondName {
8381
diagnoseLowerCamelCaseViolations(
8482
secondName, allowUnderscores: false, description: identifierDescription(for: node))
@@ -106,10 +104,8 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
106104
for param in node.signature.input.parameterList {
107105
// These identifiers aren't described using `identifierDescription(for:)` because no single
108106
// node can disambiguate the argument label from the parameter name.
109-
if let label = param.firstName {
110-
diagnoseLowerCamelCaseViolations(
111-
label, allowUnderscores: false, description: "argument label")
112-
}
107+
diagnoseLowerCamelCaseViolations(
108+
param.firstName, allowUnderscores: false, description: "argument label")
113109
if let paramName = param.secondName {
114110
diagnoseLowerCamelCaseViolations(
115111
paramName, allowUnderscores: false, description: "function parameter")

Sources/SwiftFormatRules/AmbiguousTrailingClosureOverload.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class AmbiguousTrailingClosureOverload: SyntaxLintRule {
4141
for fn in functions {
4242
let params = fn.signature.input.parameterList
4343
guard let firstParam = params.firstAndOnly else { continue }
44-
guard let type = firstParam.type, type.is(FunctionTypeSyntax.self) else { continue }
44+
guard firstParam.type.is(FunctionTypeSyntax.self) else { continue }
4545
if let mods = fn.modifiers, mods.has(modifier: "static") || mods.has(modifier: "class") {
4646
staticOverloads[fn.identifier.text, default: []].append(fn)
4747
} else {

Sources/SwiftFormatRules/FunctionDeclSyntax+Convenience.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension FunctionDeclSyntax {
1616
/// Constructs a name for a function that includes parameter labels, i.e. `foo(_:bar:)`.
1717
var fullDeclName: String {
1818
let params = signature.input.parameterList.map { param in
19-
"\(param.firstName?.text ?? "_"):"
19+
"\(param.firstName.text):"
2020
}
2121
return "\(identifier.text)(\(params.joined()))"
2222
}

Sources/SwiftFormatRules/NoLeadingUnderscores.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public final class NoLeadingUnderscores: SyntaxLintRule {
6060
// If both names are provided, we want to check `secondName`, which will be the parameter name
6161
// (in that case, `firstName` is the label). If only one name is present, then it is recorded in
6262
// `firstName`, and it is both the label and the parameter name.
63-
if let variableIdentifier = node.secondName ?? node.firstName {
64-
diagnoseIfNameStartsWithUnderscore(variableIdentifier)
65-
}
63+
diagnoseIfNameStartsWithUnderscore(node.secondName ?? node.firstName)
6664
return .visitChildren
6765
}
6866

Sources/SwiftFormatRules/UseSynthesizedInitializer.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
106106
guard parameters.count == properties.count else { return false }
107107
for (idx, parameter) in parameters.enumerated() {
108108

109-
guard let paramId = parameter.firstName, parameter.secondName == nil else { return false }
110-
guard let paramType = parameter.type else { return false }
109+
guard parameter.secondName == nil else { return false }
111110

112111
let property = properties[idx]
113112
let propertyId = property.firstIdentifier
@@ -124,9 +123,9 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
124123
return false
125124
}
126125

127-
if propertyId.identifier.text != paramId.text
126+
if propertyId.identifier.text != parameter.firstName.text
128127
|| propertyType.description.trimmingCharacters(
129-
in: .whitespaces) != paramType.description.trimmingCharacters(in: .whitespacesAndNewlines)
128+
in: .whitespaces) != parameter.type.description.trimmingCharacters(in: .whitespacesAndNewlines)
130129
{ return false }
131130
}
132131
return true

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ fileprivate func funcParametersIdentifiers(in paramList: FunctionParameterListSy
139139
// If there is a label and an identifier, then the identifier (`secondName`) is the name that
140140
// should be documented. Otherwise, the label and identifier are the same, occupying
141141
// `firstName`.
142-
guard let parameterIdentifier = parameter.secondName ?? parameter.firstName else {
143-
continue
144-
}
142+
let parameterIdentifier = parameter.secondName ?? parameter.firstName
145143
funcParameters.append(parameterIdentifier.text)
146144
}
147145
return funcParameters

Sources/generate-pipeline/RuleCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ final class RuleCollector {
126126
guard let function = member.decl.as(FunctionDeclSyntax.self) else { continue }
127127
guard function.identifier.text == "visit" else { continue }
128128
let params = function.signature.input.parameterList
129-
guard let firstType = params.firstAndOnly?.type?.as(SimpleTypeIdentifierSyntax.self) else {
129+
guard let firstType = params.firstAndOnly?.type.as(SimpleTypeIdentifierSyntax.self) else {
130130
continue
131131
}
132132
visitedNodes.append(firstType.name.text)

0 commit comments

Comments
 (0)