Skip to content

Commit 14a014b

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 c84557f commit 14a014b

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Sources/SwiftParser/Diagnostics/DiagnosticExtensions.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ 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
}
2420

21+
// These overloads shouldn't be needed, but are currently required for the
22+
// Swift 5.5 compiler to handle non-trivial FixIt initializations using
23+
// leading-dot syntax.
24+
// TODO: These can be dropped once we require a minimum of Swift 5.6 to
25+
// compile the library.
26+
init(message: StaticParserFixIt, changes: Changes) {
27+
self.init(message: message as FixItMessage, changes: changes)
28+
}
2529
init(message: StaticParserFixIt, changes: [Changes]) {
2630
self.init(message: message as FixItMessage, changes: FixIt.Changes(combining: changes))
2731
}

Sources/SwiftParser/Diagnostics/ParserDiagnosticMessages.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,43 @@ public struct UnknownDirectiveError: ParserError {
272272

273273
// MARK: - Fix-Its (please sort alphabetically)
274274

275-
public enum StaticParserFixIt: String, FixItMessage {
276-
case insertSemicolon = "insert ';'"
277-
case insertAttributeArguments = "insert attribute argument"
278-
case joinIdentifiers = "join the identifiers together"
279-
case joinIdentifiersWithCamelCase = "join the identifiers together with camel-case"
280-
case removeOperatorBody = "remove operator body"
281-
case wrapKeywordInBackticks = "if this name is unavoidable, use backticks to escape it"
275+
/// A parser fix-it with a static message.
276+
public struct StaticParserFixIt: FixItMessage {
277+
public let message: String
278+
private let messageID: String
282279

283-
public var message: String { self.rawValue }
280+
/// This should only be called within a static var on FixItMessage, such
281+
/// as the examples below. This allows us to pick up the messageID from the
282+
/// var name.
283+
fileprivate init(_ message: String, messageID: String = #function) {
284+
self.message = message
285+
self.messageID = messageID
286+
}
284287

285288
public var fixItID: MessageID {
286-
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(self)")
289+
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(messageID)")
290+
}
291+
}
292+
293+
extension FixItMessage where Self == StaticParserFixIt {
294+
/// Please order alphabetically by property name.
295+
public static var insertSemicolon: Self {
296+
.init("insert ';'")
297+
}
298+
public static var insertAttributeArguments: Self {
299+
.init("insert attribute argument")
300+
}
301+
public static var joinIdentifiers: Self {
302+
.init("join the identifiers together")
303+
}
304+
public static var joinIdentifiersWithCamelCase: Self {
305+
.init("join the identifiers together with camel-case")
306+
}
307+
public static var removeOperatorBody: Self {
308+
.init("remove operator body")
309+
}
310+
public static var wrapKeywordInBackticks: Self {
311+
.init("if this name is unavoidable, use backticks to escape it")
287312
}
288313
}
289314

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)