Skip to content

Commit 5d6201f

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 3ab2c98 commit 5d6201f

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

270270
// MARK: - Fix-Its (please sort alphabetically)
271271

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

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

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

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)