Skip to content

Commit

Permalink
Add --condassignment after-property option
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored and nicklockwood committed Jun 9, 2024
1 parent 1995910 commit b135d17
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ Option | Description

Assign properties using if / switch expressions.

Option | Description
--- | ---
`--condassignment` | Use cond. assignment: "after-property" (default) or "always".

<details>
<summary>Examples</summary>

Expand Down Expand Up @@ -533,6 +537,7 @@ Assign properties using if / switch expressions.
}
```

// With --condassignment always (disabled by default)
- switch condition {
+ foo.bar = switch condition {
case true:
Expand Down
1 change: 1 addition & 0 deletions Sources/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,7 @@ private struct Examples {
}
```
// With --condassignment always (disabled by default)
- switch condition {
+ foo.bar = switch condition {
case true:
Expand Down
8 changes: 8 additions & 0 deletions Sources/OptionDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,14 @@ struct _Descriptors {
trueValues: ["preserve"],
falseValues: ["before-declarations", "declarations"]
)
let conditionalAssignmentOnlyAfterNewProperties = OptionDescriptor(
argumentName: "condassignment",
displayName: "Apply conditionalAssignment rule",
help: "Use cond. assignment: \"after-property\" (default) or \"always\".",
keyPath: \.preserveSingleLineForEach,
trueValues: ["after-property"],
falseValues: ["always"]
)
let initCoderNil = OptionDescriptor(
argumentName: "initcodernil",
displayName: "Return nil in init?(coder)",
Expand Down
3 changes: 3 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ public struct FormatOptions: CustomStringConvertible {
public var preserveAnonymousForEach: Bool
public var preserveSingleLineForEach: Bool
public var preserveDocComments: Bool
public var conditionalAssignmentOnlyAfterNewProperties: Bool
public var typeDelimiterSpacing: DelimiterSpacing
public var initCoderNil: Bool
public var dateFormat: DateFormat
Expand Down Expand Up @@ -747,6 +748,7 @@ public struct FormatOptions: CustomStringConvertible {
preserveAnonymousForEach: Bool = false,
preserveSingleLineForEach: Bool = true,
preserveDocComments: Bool = false,
conditionalAssignmentOnlyAfterNewProperties: Bool = true,
typeDelimiterSpacing: DelimiterSpacing = .spaceAfter,
initCoderNil: Bool = false,
dateFormat: DateFormat = .system,
Expand Down Expand Up @@ -849,6 +851,7 @@ public struct FormatOptions: CustomStringConvertible {
self.preserveAnonymousForEach = preserveAnonymousForEach
self.preserveSingleLineForEach = preserveSingleLineForEach
self.preserveDocComments = preserveDocComments
self.conditionalAssignmentOnlyAfterNewProperties = conditionalAssignmentOnlyAfterNewProperties
self.typeDelimiterSpacing = typeDelimiterSpacing
self.initCoderNil = initCoderNil
self.dateFormat = dateFormat
Expand Down
5 changes: 3 additions & 2 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7131,7 +7131,8 @@ public struct _FormatRules {

public let conditionalAssignment = FormatRule(
help: "Assign properties using if / switch expressions.",
orderAfter: ["redundantReturn"]
orderAfter: ["redundantReturn"],
options: ["condassignment"]
) { formatter in
// If / switch expressions were added in Swift 5.9 (SE-0380)
guard formatter.options.swiftVersion >= "5.9" else {
Expand Down Expand Up @@ -7328,7 +7329,7 @@ public struct _FormatRules {
}

// Otherwise we insert an `identifier =` before the if/switch expression
else {
else if !formatter.options.conditionalAssignmentOnlyAfterNewProperties {
// In this case we should only apply the conversion if this is a top-level condition,
// and not nested in some parent condition. In large complex if/switch conditions
// with multiple layers of nesting, for example, this prevents us from making any
Expand Down
16 changes: 8 additions & 8 deletions Tests/RulesTests+Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4328,7 +4328,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, [output], rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4350,7 +4350,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4367,7 +4367,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4391,7 +4391,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, [output], rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4415,7 +4415,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, [output], rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4441,7 +4441,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, [output], rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand Down Expand Up @@ -4483,7 +4483,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, [output], rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand All @@ -4509,7 +4509,7 @@ class SyntaxTests: RulesTests {
}
"""

let options = FormatOptions(swiftVersion: "5.9")
let options = FormatOptions(conditionalAssignmentOnlyAfterNewProperties: false, swiftVersion: "5.9")
testFormatting(for: input, rules: [FormatRules.conditionalAssignment, FormatRules.wrapMultilineConditionalAssignment, FormatRules.indent], options: options)
}

Expand Down

0 comments on commit b135d17

Please sign in to comment.