Skip to content

Commit

Permalink
Deprecate varattributes in favor of storedvarattrs and computedvarattrs
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored and nicklockwood committed Nov 26, 2023
1 parent d6d2691 commit 0268bc5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2599,8 +2599,9 @@ Option | Description
--- | ---
`--funcattributes` | Function @attributes: "preserve", "prev-line", or "same-line"
`--typeattributes` | Type @attributes: "preserve", "prev-line", or "same-line"
`--varattributes` | Computed property @attributes: "preserve", "prev-line", or "same-line"
`--varattributes` | [Deprecated] Property @attributes: "preserve", "prev-line", or "same-line"
`--storedvarattrs` | Stored property @attributes: "preserve", "prev-line", or "same-line"
`--computedvarattrs` | Stored property @attributes: "preserve", "prev-line", or "same-line"

<details>
<summary>Examples</summary>
Expand Down
11 changes: 9 additions & 2 deletions Sources/OptionDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,22 @@ struct _Descriptors {
let varAttributes = OptionDescriptor(
argumentName: "varattributes",
displayName: "Var Attributes",
help: "Computed property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
help: "[Deprecated] Property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
deprecationMessage: "Replaced with --storedvarattrs and --computedvarattrs.",
keyPath: \.varAttributes
)
let storedVarAttributes = OptionDescriptor(
argumentName: "storedvarattrs",
displayName: "Stored Var Attributes",
displayName: "Stored Property Attributes",
help: "Stored property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
keyPath: \.storedVarAttributes
)
let computedVarAttributes = OptionDescriptor(
argumentName: "computedvarattrs",
displayName: "Computed Property Attributes",
help: "Stored property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
keyPath: \.computedVarAttributes
)
let yodaSwap = OptionDescriptor(
argumentName: "yodaswap",
displayName: "Yoda Swap",
Expand Down
3 changes: 3 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ public struct FormatOptions: CustomStringConvertible {
public var typeAttributes: AttributeMode
public var varAttributes: AttributeMode
public var storedVarAttributes: AttributeMode
public var computedVarAttributes: AttributeMode
public var markTypes: MarkMode
public var typeMarkComment: String
public var markExtensions: MarkMode
Expand Down Expand Up @@ -741,6 +742,7 @@ public struct FormatOptions: CustomStringConvertible {
typeAttributes: AttributeMode = .preserve,
varAttributes: AttributeMode = .preserve,
storedVarAttributes: AttributeMode = .preserve,
computedVarAttributes: AttributeMode = .preserve,
markTypes: MarkMode = .always,
typeMarkComment: String = "MARK: - %t",
markExtensions: MarkMode = .always,
Expand Down Expand Up @@ -840,6 +842,7 @@ public struct FormatOptions: CustomStringConvertible {
self.typeAttributes = typeAttributes
self.varAttributes = varAttributes
self.storedVarAttributes = storedVarAttributes
self.computedVarAttributes = computedVarAttributes
self.markTypes = markTypes
self.typeMarkComment = typeMarkComment
self.markExtensions = markExtensions
Expand Down
13 changes: 11 additions & 2 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5391,7 +5391,7 @@ public struct _FormatRules {

public let wrapAttributes = FormatRule(
help: "Wrap @attributes onto a separate line, or keep them on the same line.",
options: ["funcattributes", "typeattributes", "varattributes", "storedvarattrs"],
options: ["funcattributes", "typeattributes", "varattributes", "storedvarattrs", "computedvarattrs"],
sharedOptions: ["linebreaks", "maxwidth"]
) { formatter in
formatter.forEach(.attribute) { i, _ in
Expand Down Expand Up @@ -5424,10 +5424,19 @@ public struct _FormatRules {
case "class", "actor", "struct", "enum", "protocol", "extension":
attributeMode = formatter.options.typeAttributes
case "var", "let":
let storedOrComputedAttributeMode: AttributeMode
if formatter.isStoredProperty(atIntroducerIndex: keywordIndex) {
attributeMode = formatter.options.storedVarAttributes
storedOrComputedAttributeMode = formatter.options.storedVarAttributes
} else {
storedOrComputedAttributeMode = formatter.options.computedVarAttributes
}

// If the relevant `storedvarattrs` or `computedvarattrs` option hasn't been configured,
// fall back to the previous (now deprecated) `varattributes` option.
if storedOrComputedAttributeMode == .preserve {
attributeMode = formatter.options.varAttributes
} else {
attributeMode = storedOrComputedAttributeMode
}
default:
return
Expand Down
59 changes: 51 additions & 8 deletions Tests/RulesTests+Wrapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4475,6 +4475,18 @@ class WrappingTests: RulesTests {
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

func testWrapPrivateSetComputedVarAttributes() {
let input = """
@objc private(set) dynamic var foo = Foo()
"""
let output = """
@objc
private(set) dynamic var foo = Foo()
"""
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

func testWrapPrivateSetVarAttributes() {
let input = """
@objc private(set) dynamic var foo = Foo()
Expand All @@ -4483,7 +4495,7 @@ class WrappingTests: RulesTests {
@objc
private(set) dynamic var foo = Foo()
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(varAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand Down Expand Up @@ -4511,6 +4523,18 @@ class WrappingTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

func testWrapPropertyWrapperAttributeVarAttributes() {
let input = """
@OuterType.Wrapper var foo: Int
"""
let output = """
@OuterType.Wrapper
var foo: Int
"""
let options = FormatOptions(varAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

func testWrapPropertyWrapperAttribute() {
let input = """
@OuterType.Wrapper var foo: Int
Expand All @@ -4519,7 +4543,7 @@ class WrappingTests: RulesTests {
@OuterType.Wrapper
var foo: Int
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand All @@ -4543,7 +4567,7 @@ class WrappingTests: RulesTests {
@OuterType.Generic<WrappedType>
var foo: WrappedType
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand All @@ -4555,7 +4579,7 @@ class WrappingTests: RulesTests {
@OuterType.Generic<WrappedType>.Foo
var foo: WrappedType
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand Down Expand Up @@ -4601,7 +4625,7 @@ class WrappingTests: RulesTests {
var foo = Foo()
}
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand Down Expand Up @@ -4650,7 +4674,7 @@ class WrappingTests: RulesTests {
}
}
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .sameLine)
let options = FormatOptions(varAttributes: .sameLine, storedVarAttributes: .sameLine, computedVarAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

Expand All @@ -4670,7 +4694,26 @@ class WrappingTests: RulesTests {
}
"""

let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .sameLine)
let options = FormatOptions(varAttributes: .sameLine, storedVarAttributes: .sameLine, computedVarAttributes: .prevLine)
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

func testWrapAttributesInSwiftUIView() {
let input = """
struct MyView: View {
@State var textContent: String
var body: some View {
childView
}
@ViewBuilder var childView: some View {
Text(verbatim: textContent)
}
}
"""

let options = FormatOptions(varAttributes: .sameLine)
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

Expand All @@ -4679,7 +4722,7 @@ class WrappingTests: RulesTests {
var foo: @MainActor (Foo) -> Void
var bar: @MainActor (Bar) -> Void
"""
let options = FormatOptions(varAttributes: .prevLine, storedVarAttributes: .prevLine)
let options = FormatOptions(storedVarAttributes: .prevLine, computedVarAttributes: .prevLine)
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

Expand Down

0 comments on commit 0268bc5

Please sign in to comment.