From 6e1349d6b370775f3f3da07ea90bdd52549475ac Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Sat, 16 Nov 2024 11:06:08 +0000 Subject: [PATCH] Rename `--preservesymbols` to `--preservedsymbols` and add Package by default --- Rules.md | 2 +- Sources/OptionDescriptor.swift | 12 ++++++------ Sources/Options.swift | 6 +++--- Sources/Rules/PropertyTypes.swift | 14 +++++++------- Tests/Rules/PropertyTypesTests.swift | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Rules.md b/Rules.md index cb43401b..befbb770 100644 --- a/Rules.md +++ b/Rules.md @@ -1644,7 +1644,7 @@ Option | Description --- | --- `--propertytypes` | "inferred", "explicit", or "infer-locals-only" (default) `--inferredtypes` | "exclude-cond-exprs" (default) or "always" -`--preservesymbols` | Comma-delimited list of symbol names to preserve +`--preservedsymbols` | Comma-delimited list of symbols to be ignored by the rule
Examples diff --git a/Sources/OptionDescriptor.swift b/Sources/OptionDescriptor.swift index 2daf2183..eb98d6ef 100644 --- a/Sources/OptionDescriptor.swift +++ b/Sources/OptionDescriptor.swift @@ -1235,6 +1235,12 @@ struct _Descriptors { help: "Comma separated list of declaration names to exclude", keyPath: \.preservedPrivateDeclarations ) + let preservedSymbols = OptionDescriptor( + argumentName: "preservedsymbols", + displayName: "Preserved Symbols", + help: "Comma-delimited list of symbols to be ignored by the rule", + keyPath: \.preservedSymbols + ) // MARK: - Internal @@ -1266,12 +1272,6 @@ struct _Descriptors { help: "The Swift language mode used in the files being formatted", keyPath: \.languageMode ) - let preserveSymbols = OptionDescriptor( - argumentName: "preservesymbols", - displayName: "Preserve Symbols", - help: "Comma-delimited list of symbol names to preserve", - keyPath: \.preserveSymbols - ) // MARK: - DEPRECATED diff --git a/Sources/Options.swift b/Sources/Options.swift index 9c082264..c56900d7 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -669,7 +669,7 @@ public struct FormatOptions: CustomStringConvertible { public var yodaSwap: YodaMode public var extensionACLPlacement: ExtensionACLPlacement public var propertyTypes: PropertyTypes - public var preserveSymbols: Set + public var preservedSymbols: Set public var inferredTypesInConditionalExpressions: Bool public var emptyBracesSpacing: EmptyBracesSpacing public var acronyms: Set @@ -794,7 +794,7 @@ public struct FormatOptions: CustomStringConvertible { yodaSwap: YodaMode = .always, extensionACLPlacement: ExtensionACLPlacement = .onExtension, propertyTypes: PropertyTypes = .inferLocalsOnly, - preserveSymbols: Set = [], + preservedSymbols: Set = ["Package"], inferredTypesInConditionalExpressions: Bool = false, emptyBracesSpacing: EmptyBracesSpacing = .noSpace, acronyms: Set = ["ID", "URL", "UUID"], @@ -909,7 +909,7 @@ public struct FormatOptions: CustomStringConvertible { self.yodaSwap = yodaSwap self.extensionACLPlacement = extensionACLPlacement self.propertyTypes = propertyTypes - self.preserveSymbols = preserveSymbols + self.preservedSymbols = preservedSymbols self.inferredTypesInConditionalExpressions = inferredTypesInConditionalExpressions self.emptyBracesSpacing = emptyBracesSpacing self.acronyms = acronyms diff --git a/Sources/Rules/PropertyTypes.swift b/Sources/Rules/PropertyTypes.swift index 013505bb..b2cd9e58 100644 --- a/Sources/Rules/PropertyTypes.swift +++ b/Sources/Rules/PropertyTypes.swift @@ -13,7 +13,7 @@ public extension FormatRule { help: "Convert property declarations to use inferred types (`let foo = Foo()`) or explicit types (`let foo: Foo = .init()`).", disabledByDefault: true, orderAfter: [.redundantType], - options: ["propertytypes", "inferredtypes", "preservesymbols"] + options: ["propertytypes", "inferredtypes", "preservedsymbols"] ) { formatter in formatter.forEach(.operator("=", .infix)) { equalsIndex, _ in // Preserve all properties in conditional statements like `if let foo = Bar() { ... }` @@ -73,7 +73,7 @@ public extension FormatRule { } // Preserve the formatting as-is if the type is manually excluded - if formatter.options.preserveSymbols.contains(type.name) { + if formatter.options.preservedSymbols.contains(type.name) { return } @@ -81,7 +81,7 @@ public extension FormatRule { if formatter.tokens[rhsStartIndex].isOperator(".") { // Preserve the formatting as-is if the identifier is manually excluded if let identifierAfterDot = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: rhsStartIndex), - formatter.options.preserveSymbols.contains(formatter.tokens[identifierAfterDot].string) + formatter.options.preservedSymbols.contains(formatter.tokens[identifierAfterDot].string) { return } // Update the . token from a prefix operator to an infix operator. @@ -109,7 +109,7 @@ public extension FormatRule { // Preserve the formatting as-is if the identifier is manually excluded if let identifierAfterDot = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: rhsStartIndex), - formatter.options.preserveSymbols.contains(formatter.tokens[identifierAfterDot].string) + formatter.options.preservedSymbols.contains(formatter.tokens[identifierAfterDot].string) { hasInvalidConditionalBranch = true } @@ -163,7 +163,7 @@ public extension FormatRule { // Preserve any types that have been manually excluded. // Preserve any `Void` types and tuples, since they're special and don't support things like `.init` - guard !(formatter.options.preserveSymbols + ["Void"]).contains(rhsType.name), + guard !(formatter.options.preservedSymbols + ["Void"]).contains(rhsType.name), !rhsType.name.hasPrefix("(") else { return } @@ -171,7 +171,7 @@ public extension FormatRule { // so that the init call stays valid after we move the type to the LHS. if formatter.tokens[indexAfterType] == .startOfScope("(") { // Preserve the existing format if `init` is manually excluded - if formatter.options.preserveSymbols.contains("init") { + if formatter.options.preservedSymbols.contains("init") { return } @@ -189,7 +189,7 @@ public extension FormatRule { // Preserve the formatting as-is if the identifier is manually excluded. // Don't convert `let foo = Foo.self` to `let foo: Foo = .self`, since `.self` returns the metatype - let symbolsToExclude = formatter.options.preserveSymbols + ["self"] + let symbolsToExclude = formatter.options.preservedSymbols + ["self"] if let indexAfterDot = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: indexAfterType), symbolsToExclude.contains(formatter.tokens[indexAfterDot].string) { return } diff --git a/Tests/Rules/PropertyTypesTests.swift b/Tests/Rules/PropertyTypesTests.swift index b0e8e190..9e7f027c 100644 --- a/Tests/Rules/PropertyTypesTests.swift +++ b/Tests/Rules/PropertyTypesTests.swift @@ -431,7 +431,7 @@ class PropertyTypesTests: XCTestCase { } """ - let options = FormatOptions(propertyTypes: .inferLocalsOnly, preserveSymbols: ["Foo", "Baaz", "quux"]) + let options = FormatOptions(propertyTypes: .inferLocalsOnly, preservedSymbols: ["Foo", "Baaz", "quux"]) testFormatting(for: input, output, rule: .propertyTypes, options: options) } @@ -464,7 +464,7 @@ class PropertyTypesTests: XCTestCase { } """ - let options = FormatOptions(propertyTypes: .inferLocalsOnly, preserveSymbols: ["init"]) + let options = FormatOptions(propertyTypes: .inferLocalsOnly, preservedSymbols: ["init"]) testFormatting(for: input, output, rule: .propertyTypes, options: options, exclude: [.redundantInit]) }