Skip to content

Commit

Permalink
Fix miscategorized func with nested class init
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Sep 5, 2020
1 parent 6d2c301 commit f62c7a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/FormattingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private extension Formatter {

if Formatter.categoryOrdering.contains(.lifecycle) {
// `init` and `deinit` always go in Lifecycle if it's present
if tokens.contains(.keyword("init")) || tokens.contains(.keyword("deinit")) {
if ["init", "deinit"].contains(keyword) {
return .lifecycle
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/RulesTests+Organization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1109,4 +1109,28 @@ extension RulesTests {

testFormatting(for: input, rule: FormatRules.organizeDeclarations)
}

func testFuncWithNestedInitNotTreatedAsLifecycle() {
let input = """
struct Foo {
// MARK: Public
public func baz() {}
// MARK: Internal
func bar() {
class NestedClass {
init() {}
}
// ...
}
}
"""

testFormatting(for: input, rule: FormatRules.organizeDeclarations,
exclude: ["blankLinesAtStartOfScope"])
}
}
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ extension RulesTests {
("testFunctionThatReturnsAFunction", testFunctionThatReturnsAFunction),
("testFunctionThatReturnsAFunctionThatThrows", testFunctionThatReturnsAFunctionThatThrows),
("testFuncWithAnonymousVoidArgumentNotStripped", testFuncWithAnonymousVoidArgumentNotStripped),
("testFuncWithNestedInitNotTreatedAsLifecycle", testFuncWithNestedInitNotTreatedAsLifecycle),
("testGenericEnumCaseIndenting", testGenericEnumCaseIndenting),
("testGetNotRemovedInFunction", testGetNotRemovedInFunction),
("testGlobalAfterTypeNotTreatedAsMember", testGlobalAfterTypeNotTreatedAsMember),
Expand Down

0 comments on commit f62c7a5

Please sign in to comment.