diff --git a/Rules.md b/Rules.md index 13686e6bb..6eeaa25b3 100644 --- a/Rules.md +++ b/Rules.md @@ -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"
Examples diff --git a/Sources/OptionDescriptor.swift b/Sources/OptionDescriptor.swift index 85546c023..cd0cbe19e 100644 --- a/Sources/OptionDescriptor.swift +++ b/Sources/OptionDescriptor.swift @@ -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", diff --git a/Sources/Options.swift b/Sources/Options.swift index e64499075..ee72875e5 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -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 @@ -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, @@ -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 diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 1851922f0..fb13bf328 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -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 @@ -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 diff --git a/Tests/RulesTests+Wrapping.swift b/Tests/RulesTests+Wrapping.swift index e68dc0db2..372f6e155 100644 --- a/Tests/RulesTests+Wrapping.swift +++ b/Tests/RulesTests+Wrapping.swift @@ -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() @@ -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) } @@ -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 @@ -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) } @@ -4543,7 +4567,7 @@ class WrappingTests: RulesTests { @OuterType.Generic 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) } @@ -4555,7 +4579,7 @@ class WrappingTests: RulesTests { @OuterType.Generic.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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) }