Skip to content

Commit 67e75ff

Browse files
committed
Allow leading dot syntax for StaticParserFixIt
Following a similar pattern to StaticParserError, convert StaticParserFixIt into a struct with static members in a constrained protocol extension.
1 parent b112b47 commit 67e75ff

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Sources/SwiftParser/Diagnostics/DiagnosticExtensions.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,9 @@ import SwiftDiagnostics
1414
import SwiftSyntax
1515

1616
extension FixIt {
17-
init(message: StaticParserFixIt, changes: Changes) {
18-
self.init(message: message as FixItMessage, changes: changes)
19-
}
20-
2117
public init(message: FixItMessage, changes: [Changes]) {
2218
self.init(message: message, changes: FixIt.Changes(combining: changes))
2319
}
24-
25-
init(message: StaticParserFixIt, changes: [Changes]) {
26-
self.init(message: message as FixItMessage, changes: FixIt.Changes(combining: changes))
27-
}
2820
}
2921

3022
extension FixIt.Changes {

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,37 @@ public struct UnknownDirectiveError: ParserError {
254254

255255
// MARK: - Fix-Its (please sort alphabetically)
256256

257-
public enum StaticParserFixIt: String, FixItMessage {
258-
case insertSemicolon = "insert ';'"
259-
case insertAttributeArguments = "insert attribute argument"
260-
case removeOperatorBody = "remove operator body"
261-
case wrapKeywordInBackticks = "if this name is unavoidable, use backticks to escape it"
257+
/// A parser fix-it with a static message.
258+
public struct StaticParserFixIt: FixItMessage {
259+
public let message: String
260+
private let messageID: String
262261

263-
public var message: String { self.rawValue }
262+
/// This should only be called within a static var on FixItMessage, such
263+
/// as the examples below. This allows us to pick up the messageID from the
264+
/// var name.
265+
fileprivate init(_ message: String, messageID: String = #function) {
266+
self.message = message
267+
self.messageID = messageID
268+
}
264269

265270
public var fixItID: MessageID {
266-
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(self)")
271+
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(messageID)")
272+
}
273+
}
274+
275+
extension FixItMessage where Self == StaticParserFixIt {
276+
/// Please order alphabetically by property name.
277+
public static var insertSemicolon: Self {
278+
.init("insert ';'")
279+
}
280+
public static var insertAttributeArguments: Self {
281+
.init("insert attribute argument")
282+
}
283+
public static var removeOperatorBody: Self {
284+
.init("remove operator body")
285+
}
286+
public static var wrapKeywordInBackticks: Self {
287+
.init("if this name is unavoidable, use backticks to escape it")
267288
}
268289
}
269290

Tests/SwiftParserTest/DiagnosticInfrastructureTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ public class DiagnosticInfrastructureTests: XCTestCase {
1515

1616
XCTAssertEqual(StaticParserError.throwsInReturnPosition.diagnosticID, MessageID(domain: "SwiftParser", id: "StaticParserError.throwsInReturnPosition"))
1717
XCTAssertEqual(StaticParserError.throwsInReturnPosition.severity, .error)
18+
19+
XCTAssertEqual(
20+
StaticParserFixIt.insertSemicolon.fixItID,
21+
MessageID(domain: "SwiftParser", id: "StaticParserFixIt.insertSemicolon")
22+
)
1823
}
1924
}

0 commit comments

Comments
 (0)