From 4826cfe294f65b4e9afff516f6acfa08175efbae Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 03:50:00 -0800 Subject: [PATCH 01/19] Add additional variable names to exclude from strongifiedSelf --- Sources/Rules.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 52e372e38..8e286a782 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3765,13 +3765,15 @@ public struct _FormatRules { guard formatter.options.swiftVersion >= "4.2" else { return } - formatter.forEach(.identifier("`self`")) { i, _ in - guard let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { - $0 == .operator("=", .infix) - }), formatter.next(.nonSpaceOrCommentOrLinebreak, after: equalIndex) == .identifier("self") else { - return + ["`self`", "ss", "sSelf", "strongSelf"].forEach { idName in + formatter.forEach(.identifier(idName)) { i, _ in + guard let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { + $0 == .operator("=", .infix) + }), formatter.next(.nonSpaceOrCommentOrLinebreak, after: equalIndex) == .identifier("self") else { + return + } + formatter.replaceToken(at: i, with: .identifier("self")) } - formatter.replaceToken(at: i, with: .identifier("self")) } } From a0c1360c235b7483d82d2c7e1dc6b7b8202916ca Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 03:50:55 -0800 Subject: [PATCH 02/19] Update rule info --- Sources/Rules.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 8e286a782..e56bf5cef 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3758,9 +3758,9 @@ public struct _FormatRules { } } - /// Removed backticks from `self` when strongifying + /// Standardize on using self when strongifying self public let strongifiedSelf = FormatRule( - help: "Remove backticks around `self` in Optional unwrap expressions." + help: "Standardize on using self when strongifying self." ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { return From 7e3cb264230de6ad4d9cdd3a24e20726eae2cf0a Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:10:10 -0800 Subject: [PATCH 03/19] Update strongifiedSelf description --- Rules.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules.md b/Rules.md index 111f03734..c034ebc8a 100644 --- a/Rules.md +++ b/Rules.md @@ -1300,7 +1300,7 @@ As per Apple's recommendation ## strongifiedSelf -Remove backticks around `self` in Optional unwrap expressions. +Standardize on using self when strongifying self.
Examples From e12614bc1f895cc1edff6250ca2b894928390a83 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:10:31 -0800 Subject: [PATCH 04/19] Add test for strongSelf --- Tests/RulesTests.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index 5db07710b..75d538e4b 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -8450,6 +8450,21 @@ class RulesTests: XCTestCase { testFormatting(for: input, rule: FormatRules.strongifiedSelf) } + func testStrongSelfConvertedToSelfInGuard() { + let input = """ + { [weak self] in + guard let strongSelf = self else { return } + } + """ + let output = """ + { [weak self] in + guard let self = self else { return } + } + """ + let options = FormatOptions(swiftVersion: "4.2") + testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) + } + // MARK: redundantObjc func testRedundantObjcRemovedFromBeforeOutlet() { From 28470e237d4e4059f19b31b0aa876828a75cec3e Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:21:57 -0800 Subject: [PATCH 05/19] Add strongifiedSelfIdentifiers option --- Sources/Options.swift | 3 +++ Sources/OptionsDescriptor.swift | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/Sources/Options.swift b/Sources/Options.swift index 4b6248fda..9ec490db1 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -263,6 +263,7 @@ public struct FormatOptions: CustomStringConvertible { public var tabWidth: Int public var maxWidth: Int public var noSpaceOperators: [String] + public var strongifiedSelfIdentifiers: [String] // Deprecated public var indentComments: Bool @@ -314,6 +315,7 @@ public struct FormatOptions: CustomStringConvertible { tabWidth: Int = 0, maxWidth: Int = 0, noSpaceOperators: [String] = [], + strongifiedSelfIdentifiers: [String] = [], // Doesn't really belong here, but hard to put elsewhere fragment: Bool = false, ignoreConflictMarkers: Bool = false, @@ -357,6 +359,7 @@ public struct FormatOptions: CustomStringConvertible { self.tabWidth = tabWidth self.maxWidth = maxWidth self.noSpaceOperators = noSpaceOperators + self.strongifiedSelfIdentifiers = strongifiedSelfIdentifiers // Doesn't really belong here, but hard to put elsewhere self.fragment = fragment self.ignoreConflictMarkers = ignoreConflictMarkers diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index 266fb8d9d..d164a242c 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -252,6 +252,7 @@ extension FormatOptions.Descriptor { tabWidth, maxWidth, noSpaceOperators, + strongifiedSelfIdentifiers, // Deprecated indentComments, @@ -577,6 +578,13 @@ extension FormatOptions.Descriptor { } } ) + static let strongifiedSelfIdentifiers = FormatOptions.Descriptor( + argumentName: "strongifiedselfidentifiers", + propertyName: "strongifiedSelfIdentifiers", + displayName: "Strongified Self Identifiers", + help: "Comma-delimited list of identifiers that should be replaced with self", + keyPath: \FormatOptions.strongifiedSelfIdentifiers + ) // MARK: - Internal From 576a461267a9027ec82798b5ae3a3f66c0ead283 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:29:50 -0800 Subject: [PATCH 06/19] Use formatter options instead of hardcoding list --- Sources/Rules.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index e56bf5cef..1c966ff3f 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3765,7 +3765,8 @@ public struct _FormatRules { guard formatter.options.swiftVersion >= "4.2" else { return } - ["`self`", "ss", "sSelf", "strongSelf"].forEach { idName in + let strongifiedSelfIdentifiers = formatter.options.strongifiedSelfIdentifiers + ["`self`"] + strongifiedSelfIdentifiers.forEach { idName in formatter.forEach(.identifier(idName)) { i, _ in guard let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { $0 == .operator("=", .infix) From a99cba1fedeb28b40cabb7ffaded4716011c14c2 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:37:32 -0800 Subject: [PATCH 07/19] Add argument tests for strongifiedSelfIdentifiers --- Tests/ArgumentsTests.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/ArgumentsTests.swift b/Tests/ArgumentsTests.swift index fe3448e05..47fb46cec 100644 --- a/Tests/ArgumentsTests.swift +++ b/Tests/ArgumentsTests.swift @@ -200,7 +200,7 @@ class ArgumentsTests: XCTestCase { } func testCommandLineArgumentsAreCorrect() { - let output = ["allman": "false", "wraparguments": "preserve", "stripunusedargs": "always", "self": "remove", "header": "ignore", "importgrouping": "alphabetized", "fractiongrouping": "disabled", "binarygrouping": "4,8", "octalgrouping": "4,8", "indentcase": "false", "trimwhitespace": "always", "decimalgrouping": "3,6", "exponentgrouping": "disabled", "patternlet": "hoist", "commas": "always", "wrapcollections": "preserve", "semicolons": "inline", "indent": "4", "exponentcase": "lowercase", "operatorfunc": "spaced", "symlinks": "ignore", "elseposition": "same-line", "empty": "void", "hexliteralcase": "uppercase", "linebreaks": "lf", "hexgrouping": "4,8", "ifdef": "indent", "closingparen": "balanced", "selfrequired": "", "trailingclosures": "", "xcodeindentation": "disabled", "fragment": "false", "conflictmarkers": "reject", "tabwidth": "unspecified", "maxwidth": "none", "nospaceoperators": ""] + let output = ["allman": "false", "wraparguments": "preserve", "stripunusedargs": "always", "self": "remove", "header": "ignore", "importgrouping": "alphabetized", "fractiongrouping": "disabled", "binarygrouping": "4,8", "octalgrouping": "4,8", "indentcase": "false", "trimwhitespace": "always", "decimalgrouping": "3,6", "exponentgrouping": "disabled", "patternlet": "hoist", "commas": "always", "wrapcollections": "preserve", "semicolons": "inline", "indent": "4", "exponentcase": "lowercase", "operatorfunc": "spaced", "symlinks": "ignore", "elseposition": "same-line", "empty": "void", "hexliteralcase": "uppercase", "linebreaks": "lf", "hexgrouping": "4,8", "ifdef": "indent", "closingparen": "balanced", "selfrequired": "", "trailingclosures": "", "xcodeindentation": "disabled", "fragment": "false", "conflictmarkers": "reject", "tabwidth": "unspecified", "maxwidth": "none", "nospaceoperators": "", "strongifiedselfidentifiers": ""] XCTAssertEqual(argumentsFor(.default), output) } @@ -557,4 +557,9 @@ class ArgumentsTests: XCTestCase { let options = try Options(["nospaceoperators": "...,..<"], in: "") XCTAssertEqual(options.formatOptions?.noSpaceOperators, ["...", "..<"]) } + + func testParseStrongifiedSelfIdentifiers() throws { + let options = try Options(["strongifiedselfidentifiers": "ss,sSelf,strongSelf"], in: "") + XCTAssertEqual(options.formatOptions?.strongifiedSelfIdentifiers, ["ss", "sSelf", "strongSelf"]) + } } From e3f5178f5fa2e9ea55c06cab2808dd03a078a285 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:38:22 -0800 Subject: [PATCH 08/19] Update rules tests for when option is provided or missing for strongifiedSelf --- Tests/RulesTests.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index 75d538e4b..8730feda0 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -8450,7 +8450,7 @@ class RulesTests: XCTestCase { testFormatting(for: input, rule: FormatRules.strongifiedSelf) } - func testStrongSelfConvertedToSelfInGuard() { + func testStrongSelfConvertedToSelfInOptions() { let input = """ { [weak self] in guard let strongSelf = self else { return } @@ -8461,6 +8461,21 @@ class RulesTests: XCTestCase { guard let self = self else { return } } """ + let options = FormatOptions(strongifiedSelfIdentifiers: ["strongSelf"], swiftVersion: "4.2") + testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) + } + + func testStrongSelfNotConvertedToSelfNotInOptions() { + let input = """ + { [weak self] in + guard let strongSelf = self else { return } + } + """ + let output = """ + { [weak self] in + guard let strongSelf = self else { return } + } + """ let options = FormatOptions(swiftVersion: "4.2") testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) } From 5364008f116c9af30efbe16250fd17eed671455a Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:38:41 -0800 Subject: [PATCH 09/19] Update test manifest --- Tests/XCTestManifests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/XCTestManifests.swift b/Tests/XCTestManifests.swift index 2f923e510..5a4e7b694 100644 --- a/Tests/XCTestManifests.swift +++ b/Tests/XCTestManifests.swift @@ -55,6 +55,7 @@ extension ArgumentsTests { ("testParseQuoteArguments", testParseQuoteArguments), ("testParseQuotedEscapedN", testParseQuotedEscapedN), ("testParseSimpleArguments", testParseSimpleArguments), + ("testParseStrongifiedSelfIdentifiers", testParseStrongifiedSelfIdentifiers), ("testParseUnexcludedURLsFileOption", testParseUnexcludedURLsFileOption), ("testParseUppercaseIgnoreFileHeader", testParseUppercaseIgnoreFileHeader), ("testPreprocessArguments", testPreprocessArguments), @@ -1470,6 +1471,8 @@ extension RulesTests { ("testStripHeader", testStripHeader), ("testStrippingSwiftNamespaceDoesNotStripPreviousSwiftNamespaceReferences", testStrippingSwiftNamespaceDoesNotStripPreviousSwiftNamespaceReferences), ("testStrippingSwiftNamespaceInOptionalTypeWhenConvertedToSugar", testStrippingSwiftNamespaceInOptionalTypeWhenConvertedToSugar), + ("testStrongSelfConvertedToSelfInOptions", testStrongSelfConvertedToSelfInOptions), + ("testStrongSelfNotConvertedToSelfNotInOptions", testStrongSelfNotConvertedToSelfNotInOptions), ("testsTupleNotUnwrapped", testsTupleNotUnwrapped), ("testsTupleOfClosuresNotUnwrapped", testsTupleOfClosuresNotUnwrapped), ("testSubscriptFunctionCallNotUnwrapped", testSubscriptFunctionCallNotUnwrapped), From d42fccad7b9910aafcdf4e272e3acebb17cfcc42 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:44:07 -0800 Subject: [PATCH 10/19] Make test names clearer --- Tests/RulesTests.swift | 4 ++-- Tests/XCTestManifests.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index 8730feda0..1873996b6 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -8450,7 +8450,7 @@ class RulesTests: XCTestCase { testFormatting(for: input, rule: FormatRules.strongifiedSelf) } - func testStrongSelfConvertedToSelfInOptions() { + func testStrongSelfConvertedToSelfIfDefinedInOptions() { let input = """ { [weak self] in guard let strongSelf = self else { return } @@ -8465,7 +8465,7 @@ class RulesTests: XCTestCase { testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) } - func testStrongSelfNotConvertedToSelfNotInOptions() { + func testStrongSelfNotConvertedToSelfIfNotDefinedInOptions() { let input = """ { [weak self] in guard let strongSelf = self else { return } diff --git a/Tests/XCTestManifests.swift b/Tests/XCTestManifests.swift index 5a4e7b694..741c7ee55 100644 --- a/Tests/XCTestManifests.swift +++ b/Tests/XCTestManifests.swift @@ -1471,8 +1471,8 @@ extension RulesTests { ("testStripHeader", testStripHeader), ("testStrippingSwiftNamespaceDoesNotStripPreviousSwiftNamespaceReferences", testStrippingSwiftNamespaceDoesNotStripPreviousSwiftNamespaceReferences), ("testStrippingSwiftNamespaceInOptionalTypeWhenConvertedToSugar", testStrippingSwiftNamespaceInOptionalTypeWhenConvertedToSugar), - ("testStrongSelfConvertedToSelfInOptions", testStrongSelfConvertedToSelfInOptions), - ("testStrongSelfNotConvertedToSelfNotInOptions", testStrongSelfNotConvertedToSelfNotInOptions), + ("testStrongSelfConvertedToSelfIfDefinedInOptions", testStrongSelfConvertedToSelfIfDefinedInOptions), + ("testStrongSelfNotConvertedToSelfIfNotDefinedInOptions", testStrongSelfNotConvertedToSelfIfNotDefinedInOptions), ("testsTupleNotUnwrapped", testsTupleNotUnwrapped), ("testsTupleOfClosuresNotUnwrapped", testsTupleOfClosuresNotUnwrapped), ("testSubscriptFunctionCallNotUnwrapped", testSubscriptFunctionCallNotUnwrapped), From 3fe0098dfb2c3a8f44c9c4c2abd316fe35540332 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 04:50:56 -0800 Subject: [PATCH 11/19] Update description for strongifiedSelf --- Rules.md | 7 ++++++- Sources/Rules.swift | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Rules.md b/Rules.md index c034ebc8a..f95c1ea62 100644 --- a/Rules.md +++ b/Rules.md @@ -1300,7 +1300,12 @@ As per Apple's recommendation ## strongifiedSelf -Standardize on using self when strongifying self. +Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. + +Option | Description +--- | --- +`--strongifiedselfidentifiers` | Comma-delimited list of identifiers that should be replaced with self +
Examples diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 1c966ff3f..9eff1fe00 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3758,9 +3758,9 @@ public struct _FormatRules { } } - /// Standardize on using self when strongifying self + /// Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. public let strongifiedSelf = FormatRule( - help: "Standardize on using self when strongifying self." + help: "Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions." ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { return From 225dc9686f8ad9e403cfd8ee9e2e5a74082647b0 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:02:14 -0800 Subject: [PATCH 12/19] List strongifiedSelfIdentifiers in strongifiedSelf rule option --- Sources/Rules.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 9eff1fe00..22976fbc8 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3760,7 +3760,8 @@ public struct _FormatRules { /// Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. public let strongifiedSelf = FormatRule( - help: "Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions." + help: "Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions.", + options: ["strongifiedSelfIdentifiers"] ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { return From 68131f1499af3be25b0bc766f3a3a7b26f20535e Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:07:03 -0800 Subject: [PATCH 13/19] Shorten strongifiedSelfIdentifiers to strongSelfIds --- Rules.md | 2 +- Sources/Options.swift | 6 +++--- Sources/OptionsDescriptor.swift | 10 +++++----- Sources/Rules.swift | 6 +++--- Tests/ArgumentsTests.swift | 8 ++++---- Tests/RulesTests.swift | 2 +- Tests/XCTestManifests.swift | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Rules.md b/Rules.md index f95c1ea62..63425e88e 100644 --- a/Rules.md +++ b/Rules.md @@ -1304,7 +1304,7 @@ Standardize on using self when strongifying self. Will also remove backticks aro Option | Description --- | --- -`--strongifiedselfidentifiers` | Comma-delimited list of identifiers that should be replaced with self +`--strongselfids` | Comma-delimited list of identifiers that should be replaced with self
diff --git a/Sources/Options.swift b/Sources/Options.swift index 9ec490db1..8932b4b17 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -263,7 +263,7 @@ public struct FormatOptions: CustomStringConvertible { public var tabWidth: Int public var maxWidth: Int public var noSpaceOperators: [String] - public var strongifiedSelfIdentifiers: [String] + public var strongSelfIds: [String] // Deprecated public var indentComments: Bool @@ -315,7 +315,7 @@ public struct FormatOptions: CustomStringConvertible { tabWidth: Int = 0, maxWidth: Int = 0, noSpaceOperators: [String] = [], - strongifiedSelfIdentifiers: [String] = [], + strongSelfIds: [String] = [], // Doesn't really belong here, but hard to put elsewhere fragment: Bool = false, ignoreConflictMarkers: Bool = false, @@ -359,7 +359,7 @@ public struct FormatOptions: CustomStringConvertible { self.tabWidth = tabWidth self.maxWidth = maxWidth self.noSpaceOperators = noSpaceOperators - self.strongifiedSelfIdentifiers = strongifiedSelfIdentifiers + self.strongSelfIds = strongSelfIds // Doesn't really belong here, but hard to put elsewhere self.fragment = fragment self.ignoreConflictMarkers = ignoreConflictMarkers diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index d164a242c..da6138b65 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -252,7 +252,7 @@ extension FormatOptions.Descriptor { tabWidth, maxWidth, noSpaceOperators, - strongifiedSelfIdentifiers, + strongSelfIds, // Deprecated indentComments, @@ -578,12 +578,12 @@ extension FormatOptions.Descriptor { } } ) - static let strongifiedSelfIdentifiers = FormatOptions.Descriptor( - argumentName: "strongifiedselfidentifiers", - propertyName: "strongifiedSelfIdentifiers", + static let strongSelfIds = FormatOptions.Descriptor( + argumentName: "strongselfids", + propertyName: "strongSelfIds", displayName: "Strongified Self Identifiers", help: "Comma-delimited list of identifiers that should be replaced with self", - keyPath: \FormatOptions.strongifiedSelfIdentifiers + keyPath: \FormatOptions.strongSelfIds ) // MARK: - Internal diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 22976fbc8..73c603927 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3761,13 +3761,13 @@ public struct _FormatRules { /// Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. public let strongifiedSelf = FormatRule( help: "Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions.", - options: ["strongifiedSelfIdentifiers"] + options: ["strongSelfIds"] ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { return } - let strongifiedSelfIdentifiers = formatter.options.strongifiedSelfIdentifiers + ["`self`"] - strongifiedSelfIdentifiers.forEach { idName in + let strongSelfIds = formatter.options.strongSelfIds + ["`self`"] + strongSelfIds.forEach { idName in formatter.forEach(.identifier(idName)) { i, _ in guard let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { $0 == .operator("=", .infix) diff --git a/Tests/ArgumentsTests.swift b/Tests/ArgumentsTests.swift index 47fb46cec..b87cf1163 100644 --- a/Tests/ArgumentsTests.swift +++ b/Tests/ArgumentsTests.swift @@ -200,7 +200,7 @@ class ArgumentsTests: XCTestCase { } func testCommandLineArgumentsAreCorrect() { - let output = ["allman": "false", "wraparguments": "preserve", "stripunusedargs": "always", "self": "remove", "header": "ignore", "importgrouping": "alphabetized", "fractiongrouping": "disabled", "binarygrouping": "4,8", "octalgrouping": "4,8", "indentcase": "false", "trimwhitespace": "always", "decimalgrouping": "3,6", "exponentgrouping": "disabled", "patternlet": "hoist", "commas": "always", "wrapcollections": "preserve", "semicolons": "inline", "indent": "4", "exponentcase": "lowercase", "operatorfunc": "spaced", "symlinks": "ignore", "elseposition": "same-line", "empty": "void", "hexliteralcase": "uppercase", "linebreaks": "lf", "hexgrouping": "4,8", "ifdef": "indent", "closingparen": "balanced", "selfrequired": "", "trailingclosures": "", "xcodeindentation": "disabled", "fragment": "false", "conflictmarkers": "reject", "tabwidth": "unspecified", "maxwidth": "none", "nospaceoperators": "", "strongifiedselfidentifiers": ""] + let output = ["allman": "false", "wraparguments": "preserve", "stripunusedargs": "always", "self": "remove", "header": "ignore", "importgrouping": "alphabetized", "fractiongrouping": "disabled", "binarygrouping": "4,8", "octalgrouping": "4,8", "indentcase": "false", "trimwhitespace": "always", "decimalgrouping": "3,6", "exponentgrouping": "disabled", "patternlet": "hoist", "commas": "always", "wrapcollections": "preserve", "semicolons": "inline", "indent": "4", "exponentcase": "lowercase", "operatorfunc": "spaced", "symlinks": "ignore", "elseposition": "same-line", "empty": "void", "hexliteralcase": "uppercase", "linebreaks": "lf", "hexgrouping": "4,8", "ifdef": "indent", "closingparen": "balanced", "selfrequired": "", "trailingclosures": "", "xcodeindentation": "disabled", "fragment": "false", "conflictmarkers": "reject", "tabwidth": "unspecified", "maxwidth": "none", "nospaceoperators": "", "strongselfids": ""] XCTAssertEqual(argumentsFor(.default), output) } @@ -558,8 +558,8 @@ class ArgumentsTests: XCTestCase { XCTAssertEqual(options.formatOptions?.noSpaceOperators, ["...", "..<"]) } - func testParseStrongifiedSelfIdentifiers() throws { - let options = try Options(["strongifiedselfidentifiers": "ss,sSelf,strongSelf"], in: "") - XCTAssertEqual(options.formatOptions?.strongifiedSelfIdentifiers, ["ss", "sSelf", "strongSelf"]) + func testParseStrongSelfIds() throws { + let options = try Options(["strongselfids": "ss,sSelf,strongSelf"], in: "") + XCTAssertEqual(options.formatOptions?.strongSelfIds, ["ss", "sSelf", "strongSelf"]) } } diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index 1873996b6..bd076dbb0 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -8461,7 +8461,7 @@ class RulesTests: XCTestCase { guard let self = self else { return } } """ - let options = FormatOptions(strongifiedSelfIdentifiers: ["strongSelf"], swiftVersion: "4.2") + let options = FormatOptions(strongSelfIds: ["strongSelf"], swiftVersion: "4.2") testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) } diff --git a/Tests/XCTestManifests.swift b/Tests/XCTestManifests.swift index 741c7ee55..d1c4cb8b6 100644 --- a/Tests/XCTestManifests.swift +++ b/Tests/XCTestManifests.swift @@ -55,7 +55,7 @@ extension ArgumentsTests { ("testParseQuoteArguments", testParseQuoteArguments), ("testParseQuotedEscapedN", testParseQuotedEscapedN), ("testParseSimpleArguments", testParseSimpleArguments), - ("testParseStrongifiedSelfIdentifiers", testParseStrongifiedSelfIdentifiers), + ("testParseStrongSelfIds", testParseStrongSelfIds), ("testParseUnexcludedURLsFileOption", testParseUnexcludedURLsFileOption), ("testParseUppercaseIgnoreFileHeader", testParseUppercaseIgnoreFileHeader), ("testPreprocessArguments", testPreprocessArguments), From ad8294aab3b95750588de34d4c55aac44178fb45 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:15:24 -0800 Subject: [PATCH 14/19] Update strongselfids description --- Rules.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules.md b/Rules.md index 63425e88e..a5c9984df 100644 --- a/Rules.md +++ b/Rules.md @@ -1304,7 +1304,7 @@ Standardize on using self when strongifying self. Will also remove backticks aro Option | Description --- | --- -`--strongselfids` | Comma-delimited list of identifiers that should be replaced with self +`--strongselfids` | Comma-delimited list of disallowed identifiers that should be replaced with self
From 54da3b2c38308ca051ae7dd48556e57f499aaaa6 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:20:49 -0800 Subject: [PATCH 15/19] Shorten description to max of 80 characters --- Rules.md | 5 ++--- Sources/Rules.swift | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Rules.md b/Rules.md index a5c9984df..b7b21194e 100644 --- a/Rules.md +++ b/Rules.md @@ -1300,12 +1300,11 @@ As per Apple's recommendation ## strongifiedSelf -Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. +Standardize on using self when strongifying self. Remove backticks around `self` Option | Description --- | --- -`--strongselfids` | Comma-delimited list of disallowed identifiers that should be replaced with self - +`--strongselfids` | Comma-delimited list of ids that should be replaced with self
Examples diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 73c603927..377c92f9a 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3758,9 +3758,9 @@ public struct _FormatRules { } } - /// Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions. + /// Standardize on using self when strongifying self. Remove backticks around `self` public let strongifiedSelf = FormatRule( - help: "Standardize on using self when strongifying self. Will also remove backticks around `self` in Optional unwrap expressions.", + help: "Standardize on using self when strongifying self. Remove backticks around `self`", options: ["strongSelfIds"] ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { From 099412d0fdaa3ee7220ee94f8dd86cc65dae6cfc Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:24:35 -0800 Subject: [PATCH 16/19] Fix option name --- Sources/Rules.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 377c92f9a..358967abd 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3761,7 +3761,7 @@ public struct _FormatRules { /// Standardize on using self when strongifying self. Remove backticks around `self` public let strongifiedSelf = FormatRule( help: "Standardize on using self when strongifying self. Remove backticks around `self`", - options: ["strongSelfIds"] + options: ["strongselfids"] ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { return From d6a6258cb9c4cd478b697cdcbd86c81dd0dd6d1d Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:27:09 -0800 Subject: [PATCH 17/19] Update help message for strongSelfIds to be the shortened version --- Sources/OptionsDescriptor.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index da6138b65..581f1aa58 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -582,7 +582,7 @@ extension FormatOptions.Descriptor { argumentName: "strongselfids", propertyName: "strongSelfIds", displayName: "Strongified Self Identifiers", - help: "Comma-delimited list of identifiers that should be replaced with self", + help: "Comma-delimited list of ids that should be replaced with self", keyPath: \FormatOptions.strongSelfIds ) From a53b48812e8bcbea2eea3b90a9843801be39c9a1 Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:33:53 -0800 Subject: [PATCH 18/19] Update description * Add period after help message * Shorten it a bit more --- Rules.md | 2 +- Sources/Rules.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Rules.md b/Rules.md index b7b21194e..108061cb2 100644 --- a/Rules.md +++ b/Rules.md @@ -1300,7 +1300,7 @@ As per Apple's recommendation ## strongifiedSelf -Standardize on using self when strongifying self. Remove backticks around `self` +Standardize to use self when strongifying self. Remove backticks around `self`. Option | Description --- | --- diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 358967abd..45be446b3 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -3758,9 +3758,9 @@ public struct _FormatRules { } } - /// Standardize on using self when strongifying self. Remove backticks around `self` + /// Standardize to use self when strongifying self public let strongifiedSelf = FormatRule( - help: "Standardize on using self when strongifying self. Remove backticks around `self`", + help: "Standardize to use self when strongifying self. Remove backticks around `self`.", options: ["strongselfids"] ) { formatter in guard formatter.options.swiftVersion >= "4.2" else { From 5c604dfa7af6f412eb92f571835909b4d28799fc Mon Sep 17 00:00:00 2001 From: Toland Hon Date: Fri, 3 Jan 2020 05:40:02 -0800 Subject: [PATCH 19/19] Don't define output when it's the same as input --- Tests/RulesTests.swift | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index bd076dbb0..58a146318 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -8471,13 +8471,8 @@ class RulesTests: XCTestCase { guard let strongSelf = self else { return } } """ - let output = """ - { [weak self] in - guard let strongSelf = self else { return } - } - """ let options = FormatOptions(swiftVersion: "4.2") - testFormatting(for: input, output, rule: FormatRules.strongifiedSelf, options: options) + testFormatting(for: input, rule: FormatRules.strongifiedSelf, options: options) } // MARK: redundantObjc