Skip to content

Commit

Permalink
Deprecate --varattributes in favor of --storedvarattrs and `--com…
Browse files Browse the repository at this point in the history
…putedvarattrs`
  • Loading branch information
calda authored and nicklockwood committed Jun 9, 2024
1 parent 2915b97 commit f8622a9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
5 changes: 3 additions & 2 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2586,8 +2586,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"
`--storedvarattrs` | Stored property @attributes: "preserve", "prev-line", or "same-line"
`--varattributes` | Property @attributes: "preserve", "prev-line", or "same-line"
`--storedvarattrs` | Stored var @attribs: "preserve", "prev-line", or "same-line"
`--computedvarattrs` | Computed var @attribs: "preserve", "prev-line", "same-line"

<details>
<summary>Examples</summary>
Expand Down
23 changes: 15 additions & 8 deletions Sources/OptionDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,18 +859,18 @@ struct _Descriptors {
help: "Type @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
keyPath: \.typeAttributes
)
let varAttributes = OptionDescriptor(
argumentName: "varattributes",
displayName: "Var Attributes",
help: "Computed property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
keyPath: \.varAttributes
)
let storedVarAttributes = OptionDescriptor(
argumentName: "storedvarattrs",
displayName: "Stored Var Attributes",
help: "Stored property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
displayName: "Stored Property Attributes",
help: "Stored var @attribs: \"preserve\", \"prev-line\", or \"same-line\"",
keyPath: \.storedVarAttributes
)
let computedVarAttributes = OptionDescriptor(
argumentName: "computedvarattrs",
displayName: "Computed Property Attributes",
help: "Computed var @attribs: \"preserve\", \"prev-line\", \"same-line\"",
keyPath: \.computedVarAttributes
)
let yodaSwap = OptionDescriptor(
argumentName: "yodaswap",
displayName: "Yoda Swap",
Expand Down Expand Up @@ -1057,6 +1057,13 @@ struct _Descriptors {
trueValues: ["enabled", "true"],
falseValues: ["disabled", "false"]
)
let varAttributes = OptionDescriptor(
argumentName: "varattributes",
displayName: "Var Attributes",
help: "Property @attributes: \"preserve\", \"prev-line\", or \"same-line\"",
deprecationMessage: "Use with `--storedvarattrs` or `--computedvarattrs` instead.",
keyPath: \.varAttributes
)

// MARK: - RENAMED

Expand Down
3 changes: 3 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,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 @@ -712,6 +713,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 @@ -811,6 +813,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 @@ -5474,7 +5474,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 @@ -5503,10 +5503,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 @@ -4456,6 +4456,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 @@ -4464,7 +4476,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 @@ -4492,6 +4504,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 @@ -4500,7 +4524,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 @@ -4524,7 +4548,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 @@ -4536,7 +4560,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 @@ -4596,7 +4620,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 @@ -4645,7 +4669,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 @@ -4665,7 +4689,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 @@ -4674,7 +4717,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 f8622a9

Please sign in to comment.