Skip to content

Commit

Permalink
Allow inherited isolation parameter to be first in function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Nov 17, 2024
1 parent 9e61e81 commit 9ec1448
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
[jaredgrubb](https://github.com/jaredgrubb)
[#3750](https://github.com/realm/SwiftLint/issues/3750)

* Allow inherited isolation parameter to be first in function signatures
depending on the new option `ignore_first_isolation_inheritance_parameter`
which is `true` by default.
[SimplyDanny](https://github.com/SimplyDanny)
[#5793](https://github.com/realm/SwiftLint/issues/5793)

#### Bug Fixes

* Run command plugin in whole package if no targets are defined in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftSyntax

@SwiftSyntaxRule
struct FunctionDefaultParameterAtEndRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)
var configuration = FunctionDefaultParameterAtEndConfiguration()

static let description = RuleDescription(
identifier: "function_default_parameter_at_end",
Expand Down Expand Up @@ -44,12 +44,17 @@ struct FunctionDefaultParameterAtEndRule: OptInRule {
""", excludeFromDocumentation: true),
Example("func foo(bar: Int, baz: Int = 0, z: () -> Void) {}"),
Example("func foo(bar: Int, baz: Int = 0, z: () -> Void, x: Int = 0) {}"),
Example(
"func foo(isolation: isolated (any Actor)? = #isolation, bar: String) {}",
configuration: ["ignore_first_isolation_inheritance_parameter": true]
),
],
triggeringExamples: [
Example("func foo(↓bar: Int = 0, baz: String) {}"),
Example("private func foo(↓bar: Int = 0, baz: String) {}"),
Example("public init?(↓for date: Date = Date(), coordinate: CLLocationCoordinate2D) {}"),
Example("func foo(bar: Int, ↓baz: Int = 0, z: () -> Void, x: Int) {}"),
Example("func foo(isolation: isolated (any Actor)? = #isolation, bar: String) {}"),
]
)
}
Expand All @@ -69,16 +74,22 @@ private extension FunctionDefaultParameterAtEndRule {
}

private func collectViolations(for signature: FunctionSignatureSyntax) {
if signature.parameterClause.parameters.count < 2 {
let numberOfParameters = signature.parameterClause.parameters.count
if numberOfParameters < 2 {
return
}
var previousWithDefault = true
for param in signature.parameterClause.parameters.reversed() {
for (index, param) in signature.parameterClause.parameters.reversed().enumerated() {
if param.isClosure {
continue
}
let hasDefault = param.defaultValue != nil
if !previousWithDefault, hasDefault {
if index + 1 == numberOfParameters,
param.isInheritedIsolation,
configuration.ignoreFirstIsolationInheritanceParameter {
break // It's the last element anyway.
}
violations.append(param.positionAfterSkippingLeadingTrivia)
}
previousWithDefault = hasDefault
Expand All @@ -95,6 +106,10 @@ private extension FunctionParameterSyntax {
var isEscaping: Bool {
type.as(AttributedTypeSyntax.self)?.attributes.contains(attributeNamed: "escaping") == true
}

var isInheritedIsolation: Bool {
defaultValue?.value.as(MacroExpansionExprSyntax.self)?.macroName.text == "isolation"
}
}

private extension TypeSyntax {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import SwiftLintCore

@AutoConfigParser
struct FunctionDefaultParameterAtEndConfiguration: SeverityBasedRuleConfiguration {
// swiftlint:disable:previous type_name

typealias Parent = FunctionDefaultParameterAtEndRule

@ConfigurationElement(key: "severity")
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "ignore_first_isolation_inheritance_parameter")
private(set) var ignoreFirstIsolationInheritanceParameter = true
}
1 change: 1 addition & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function_body_length:
error: 100
function_default_parameter_at_end:
severity: warning
ignore_first_isolation_inheritance_parameter: true
function_parameter_count:
warning: 5
error: 8
Expand Down

0 comments on commit 9ec1448

Please sign in to comment.