diff --git a/Rules.md b/Rules.md index 173e2fed5..b549f46e5 100644 --- a/Rules.md +++ b/Rules.md @@ -2383,7 +2383,7 @@ Prefer shorthand syntax for Arrays, Dictionaries and Optionals. Option | Description --- | --- -`--shortoptionals` | Use ? for optionals "always" (default) or "except-properties" +`--shortoptionals` | Use ? for optionals "always" or "except-properties" (default)
Examples diff --git a/Sources/OptionDescriptor.swift b/Sources/OptionDescriptor.swift index 81c2e156d..69bcd9930 100644 --- a/Sources/OptionDescriptor.swift +++ b/Sources/OptionDescriptor.swift @@ -756,7 +756,7 @@ struct _Descriptors { let shortOptionals = OptionDescriptor( argumentName: "shortoptionals", displayName: "Short Optional Syntax", - help: "Use ? for optionals \"always\" (default) or \"except-properties\"", + help: "Use ? for optionals \"always\" or \"except-properties\" (default)", keyPath: \.shortOptionals ) let markTypes = OptionDescriptor( diff --git a/Sources/Options.swift b/Sources/Options.swift index 3574c5a90..dc49d04df 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -735,7 +735,7 @@ public struct FormatOptions: CustomStringConvertible { noSpaceOperators: Set = [], noWrapOperators: Set = [], modifierOrder: [String] = [], - shortOptionals: OptionalsMode = .always, + shortOptionals: OptionalsMode = .exceptProperties, funcAttributes: AttributeMode = .preserve, typeAttributes: AttributeMode = .preserve, varAttributes: AttributeMode = .preserve, diff --git a/Tests/RulesTests+Syntax.swift b/Tests/RulesTests+Syntax.swift index 11143f28e..925c1d298 100644 --- a/Tests/RulesTests+Syntax.swift +++ b/Tests/RulesTests+Syntax.swift @@ -1668,40 +1668,51 @@ class SyntaxTests: RulesTests { // optionals + func testOptionalPropertyTypeNotConvertedToSugarByDefault() { + let input = "var bar: Optional" + testFormatting(for: input, rule: FormatRules.typeSugar) + } + func testOptionalTypeConvertedToSugar() { let input = "var foo: Optional" let output = "var foo: String?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testSwiftOptionalTypeConvertedToSugar() { let input = "var foo: Swift.Optional" let output = "var foo: String?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testOptionalClosureParenthesizedConvertedToSugar() { let input = "var foo: Optional<(Int) -> String>" let output = "var foo: ((Int) -> String)?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testOptionalTupleWrappedInParensConvertedToSugar() { let input = "let foo: Optional<(foo: Int, bar: String)>" let output = "let foo: (foo: Int, bar: String)?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testOptionalComposedProtocolWrappedInParensConvertedToSugar() { let input = "let foo: Optional" let output = "let foo: (UIView & Foo)?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testSwiftOptionalClosureParenthesizedConvertedToSugar() { let input = "var foo: Swift.Optional<(Int) -> String>" let output = "var foo: ((Int) -> String)?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testStrippingSwiftNamespaceInOptionalTypeWhenConvertedToSugar() { @@ -1713,7 +1724,8 @@ class SyntaxTests: RulesTests { func testStrippingSwiftNamespaceDoesNotStripPreviousSwiftNamespaceReferences() { let input = "let a: Swift.String = Optional" let output = "let a: Swift.String = String?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testOptionalTypeInsideCaseConvertedToSugar() { @@ -1742,65 +1754,63 @@ class SyntaxTests: RulesTests { testFormatting(for: input, output, rule: FormatRules.typeSugar) } - // shortOptionals = exceptProperties - - func testPropertyTypeNotConvertedToSugar() { - let input = "var foo: Optional" - let options = FormatOptions(shortOptionals: .exceptProperties) - testFormatting(for: input, rule: FormatRules.typeSugar, options: options) - } - // swift parser bug func testAvoidSwiftParserBugWithClosuresInsideArrays() { let input = "var foo = Array<(_ image: Data?) -> Void>()" - testFormatting(for: input, rule: FormatRules.typeSugar) + testFormatting(for: input, rule: FormatRules.typeSugar, options: FormatOptions(shortOptionals: .always)) } func testAvoidSwiftParserBugWithClosuresInsideDictionaries() { let input = "var foo = Dictionary Void>()" - testFormatting(for: input, rule: FormatRules.typeSugar) + testFormatting(for: input, rule: FormatRules.typeSugar, options: FormatOptions(shortOptionals: .always)) } func testAvoidSwiftParserBugWithClosuresInsideOptionals() { let input = "var foo = Optional<(_ image: Data?) -> Void>()" - testFormatting(for: input, rule: FormatRules.typeSugar) + testFormatting(for: input, rule: FormatRules.typeSugar, options: FormatOptions(shortOptionals: .always)) } func testDontOverApplyBugWorkaround() { let input = "var foo: Array<(_ image: Data?) -> Void>" let output = "var foo: [(_ image: Data?) -> Void]" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testDontOverApplyBugWorkaround2() { let input = "var foo: Dictionary Void>" let output = "var foo: [String: (_ image: Data?) -> Void]" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testDontOverApplyBugWorkaround3() { let input = "var foo: Optional<(_ image: Data?) -> Void>" let output = "var foo: ((_ image: Data?) -> Void)?" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testDontOverApplyBugWorkaround4() { let input = "var foo = Array<(image: Data?) -> Void>()" let output = "var foo = [(image: Data?) -> Void]()" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testDontOverApplyBugWorkaround5() { let input = "var foo = Array<(Data?) -> Void>()" let output = "var foo = [(Data?) -> Void]()" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } func testDontOverApplyBugWorkaround6() { let input = "var foo = Dictionary Void>>()" let output = "var foo = [Int: Array<(_ image: Data?) -> Void>]()" - testFormatting(for: input, output, rule: FormatRules.typeSugar) + let options = FormatOptions(shortOptionals: .always) + testFormatting(for: input, output, rule: FormatRules.typeSugar, options: options) } // MARK: - preferKeyPath