Skip to content

Commit d117cd1

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 4d8e061 commit d117cd1

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
@@ -281,18 +281,43 @@ public struct UnknownDirectiveError: ParserError {
281281

282282
// MARK: - Fix-Its (please sort alphabetically)
283283

284-
public enum StaticParserFixIt: String, FixItMessage {
285-
case insertSemicolon = "insert ';'"
286-
case insertAttributeArguments = "insert attribute argument"
287-
case joinIdentifiers = "join the identifiers together"
288-
case joinIdentifiersWithCamelCase = "join the identifiers together with camel-case"
289-
case removeOperatorBody = "remove operator body"
290-
case wrapKeywordInBackticks = "if this name is unavoidable, use backticks to escape it"
284+
/// A parser fix-it with a static message.
285+
public struct StaticParserFixIt: FixItMessage {
286+
public let message: String
287+
private let messageID: String
291288

292-
public var message: String { self.rawValue }
289+
/// This should only be called within a static var on FixItMessage, such
290+
/// as the examples below. This allows us to pick up the messageID from the
291+
/// var name.
292+
fileprivate init(_ message: String, messageID: String = #function) {
293+
self.message = message
294+
self.messageID = messageID
295+
}
293296

294297
public var fixItID: MessageID {
295-
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(self)")
298+
MessageID(domain: diagnosticDomain, id: "\(type(of: self)).\(messageID)")
299+
}
300+
}
301+
302+
extension FixItMessage where Self == StaticParserFixIt {
303+
/// Please order alphabetically by property name.
304+
public static var insertSemicolon: Self {
305+
.init("insert ';'")
306+
}
307+
public static var insertAttributeArguments: Self {
308+
.init("insert attribute argument")
309+
}
310+
public static var joinIdentifiers: Self {
311+
.init("join the identifiers together")
312+
}
313+
public static var joinIdentifiersWithCamelCase: Self {
314+
.init("join the identifiers together with camel-case")
315+
}
316+
public static var removeOperatorBody: Self {
317+
.init("remove operator body")
318+
}
319+
public static var wrapKeywordInBackticks: Self {
320+
.init("if this name is unavoidable, use backticks to escape it")
296321
}
297322
}
298323

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)