Skip to content

Commit bf63c4c

Browse files
committed
Fix incorrect --condassignment keyPath and add test
1 parent e852a07 commit bf63c4c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Sources/OptionDescriptor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ struct _Descriptors {
10011001
argumentName: "condassignment",
10021002
displayName: "Apply conditionalAssignment rule",
10031003
help: "Use cond. assignment: \"after-property\" (default) or \"always\"",
1004-
keyPath: \.preserveSingleLineForEach,
1004+
keyPath: \.conditionalAssignmentOnlyAfterNewProperties,
10051005
trueValues: ["after-property"],
10061006
falseValues: ["always"]
10071007
)

Tests/OptionDescriptorTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import XCTest
1010
@testable import SwiftFormat
1111

12+
private let projectDirectory = URL(fileURLWithPath: #file)
13+
.deletingLastPathComponent().deletingLastPathComponent()
14+
1215
class OptionDescriptorTests: XCTestCase {
1316
private typealias OptionArgumentMapping<T> = (optionValue: T, argumentValue: String)
1417

@@ -114,6 +117,31 @@ class OptionDescriptorTests: XCTestCase {
114117

115118
// MARK: All options
116119

120+
func testAllDescriptorsHaveCorrectKeypath() throws {
121+
let rulesFile = projectDirectory.appendingPathComponent("Sources/OptionDescriptor.swift")
122+
let rulesSource = try String(contentsOf: rulesFile, encoding: .utf8)
123+
let tokens = tokenize(rulesSource)
124+
let formatter = Formatter(tokens)
125+
formatter.forEach(.identifier("OptionDescriptor")) { i, _ in
126+
guard formatter.token(at: i + 1) == .startOfScope("("),
127+
let endOfScope = formatter.endOfScope(at: i + 1),
128+
!formatter.tokens[i ..< endOfScope].contains(.stringBody("deprecated")),
129+
let nameIndex = formatter.index(of: .identifier, before: i),
130+
case let .identifier(name) = formatter.tokens[nameIndex]
131+
else {
132+
return
133+
}
134+
guard let keypathIndex = formatter.tokens[i ..< endOfScope].firstIndex(of: .identifier(name)),
135+
formatter.tokens[keypathIndex - 1].isOperator("."),
136+
let prevToken = formatter.token(at: keypathIndex - 2),
137+
[.operator("\\", .prefix), .identifier("FormatOptions")].contains(prevToken)
138+
else {
139+
XCTFail("Descriptor for \(name) has incorrect keyPath (must match descriptor name)")
140+
return
141+
}
142+
}
143+
}
144+
117145
func testAllDescriptorsHaveProperty() {
118146
let allProperties = Set(FormatOptions.default.allOptions.keys)
119147
for descriptor in Descriptors.all where !descriptor.isDeprecated {

0 commit comments

Comments
 (0)