mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Refactored —self insert logic and tests
This commit is contained in:
+13
-13
@@ -898,19 +898,19 @@ private struct Inference {
|
||||
}
|
||||
}
|
||||
let name = token.unescaped()
|
||||
if members.contains(name), !localNames.contains(name), !isAssignmentKeyword ||
|
||||
formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .operator("=", .infix) {
|
||||
if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index),
|
||||
lastToken.isOperator(".") {
|
||||
break
|
||||
}
|
||||
if isInit, formatter.next(.nonSpaceOrCommentOrLinebreak, after: index) == .operator("=", .infix) {
|
||||
initRemoved += 1
|
||||
} else {
|
||||
removed += 1
|
||||
}
|
||||
index += 1
|
||||
continue
|
||||
guard members.contains(name), !localNames.contains(name), !isAssignmentKeyword ||
|
||||
formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .operator("=", .infix),
|
||||
formatter.next(.nonSpaceOrComment, after: index) != .delimiter(":") else {
|
||||
break
|
||||
}
|
||||
if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index),
|
||||
lastToken.isOperator(".") {
|
||||
break
|
||||
}
|
||||
if isInit, formatter.next(.nonSpaceOrCommentOrLinebreak, after: index) == .operator("=", .infix) {
|
||||
initRemoved += 1
|
||||
} else {
|
||||
removed += 1
|
||||
}
|
||||
case .endOfScope("case"), .endOfScope("default"):
|
||||
return
|
||||
|
||||
+10
-10
@@ -2521,17 +2521,17 @@ public struct _FormatRules {
|
||||
}
|
||||
}
|
||||
let name = token.unescaped()
|
||||
if members.contains(name), !localNames.contains(name), !isAssignmentKeyword ||
|
||||
formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .operator("=", .infix) {
|
||||
if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index),
|
||||
lastToken.isOperator(".") {
|
||||
break
|
||||
}
|
||||
if formatter.nextToken(after: index) != .delimiter(":") {
|
||||
formatter.insertTokens([.identifier("self"), .operator(".", .infix)], at: index)
|
||||
index += 2
|
||||
}
|
||||
guard members.contains(name), !localNames.contains(name), !isAssignmentKeyword ||
|
||||
formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .operator("=", .infix),
|
||||
formatter.next(.nonSpaceOrComment, after: index) != .delimiter(":") else {
|
||||
break
|
||||
}
|
||||
if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index),
|
||||
lastToken.isOperator(".") {
|
||||
break
|
||||
}
|
||||
formatter.insertTokens([.identifier("self"), .operator(".", .infix)], at: index)
|
||||
index += 2
|
||||
case .endOfScope("case"), .endOfScope("default"):
|
||||
return
|
||||
case .endOfScope:
|
||||
|
||||
@@ -6554,21 +6554,19 @@ class RulesTests: XCTestCase {
|
||||
|
||||
func testSelfNotInsertedInParameterNames() {
|
||||
let input = """
|
||||
func foo(a _: String) {}
|
||||
class Bar {
|
||||
class Foo {
|
||||
let a: String
|
||||
|
||||
func hoge() {
|
||||
func bar() {
|
||||
foo(a: a)
|
||||
}
|
||||
}
|
||||
"""
|
||||
let output = """
|
||||
func foo(a _: String) {}
|
||||
class Bar {
|
||||
class Foo {
|
||||
let a: String
|
||||
|
||||
func hoge() {
|
||||
func bar() {
|
||||
foo(a: self.a)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user