mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Rename --preservesymbols to --preservedsymbols and add Package by default
This commit is contained in:
@@ -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
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -669,7 +669,7 @@ public struct FormatOptions: CustomStringConvertible {
|
||||
public var yodaSwap: YodaMode
|
||||
public var extensionACLPlacement: ExtensionACLPlacement
|
||||
public var propertyTypes: PropertyTypes
|
||||
public var preserveSymbols: Set<String>
|
||||
public var preservedSymbols: Set<String>
|
||||
public var inferredTypesInConditionalExpressions: Bool
|
||||
public var emptyBracesSpacing: EmptyBracesSpacing
|
||||
public var acronyms: Set<String>
|
||||
@@ -794,7 +794,7 @@ public struct FormatOptions: CustomStringConvertible {
|
||||
yodaSwap: YodaMode = .always,
|
||||
extensionACLPlacement: ExtensionACLPlacement = .onExtension,
|
||||
propertyTypes: PropertyTypes = .inferLocalsOnly,
|
||||
preserveSymbols: Set<String> = [],
|
||||
preservedSymbols: Set<String> = ["Package"],
|
||||
inferredTypesInConditionalExpressions: Bool = false,
|
||||
emptyBracesSpacing: EmptyBracesSpacing = .noSpace,
|
||||
acronyms: Set<String> = ["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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user