Skip to content
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

Allow inherited isolation parameter to be first in function signatures #5859

Merged
merged 1 commit into from
Nov 17, 2024
Merged
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
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) {}"),
],
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) {}",
configuration: ["ignore_first_isolation_inheritance_parameter": false]
),
]
)
}
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