|
| 1 | +import SwiftDiagnostics |
| 2 | +import SwiftSyntax |
| 3 | +import SwiftSyntaxMacros |
| 4 | + |
| 5 | +enum ExtractCaseValueMacroDiagnostic { |
| 6 | + case requiresEnum |
| 7 | + case requiresArgs |
| 8 | + case requiresPropertyNameArg |
| 9 | + case requiresPropertyNameStringLiteral |
| 10 | + case requiresGenericType |
| 11 | + case noValue(case: String, index: Int) |
| 12 | + case noMatchingType(type: String, case: String) |
| 13 | + case noAssociatedValues(case: String) |
| 14 | + case noAssociatedValueForName(name: String, case: String) |
| 15 | + case typeMismatch(case: String, index: Int) |
| 16 | + case typeMismatchNamed(name: String, case: String) |
| 17 | +} |
| 18 | + |
| 19 | +extension ExtractCaseValueMacroDiagnostic: DiagnosticMessage { |
| 20 | + func diagnose(at node: some SyntaxProtocol, fixIts: [FixIt] = []) -> Diagnostic { |
| 21 | + Diagnostic(node: Syntax(node), message: self, fixIts: fixIts) |
| 22 | + } |
| 23 | + |
| 24 | + var message: String { |
| 25 | + switch self { |
| 26 | + case .requiresEnum: |
| 27 | + return "'ExtractCaseValue' macro can only be applied to an enum" |
| 28 | + |
| 29 | + case .requiresArgs: |
| 30 | + return "'ExtractCaseValue' macro requires arguments" |
| 31 | + |
| 32 | + case .requiresPropertyNameArg: |
| 33 | + return "'ExtractCaseValue' macro requires `\(caseParamExtractionPropertyNameArgumentLabel)` argument" |
| 34 | + |
| 35 | + case .requiresPropertyNameStringLiteral: |
| 36 | + return "'ExtractCaseValue' macro argument `\(caseParamExtractionPropertyNameArgumentLabel)` must be a string literal" |
| 37 | + |
| 38 | + case .requiresGenericType: |
| 39 | + return "'ExtractCaseValue' macro requires a generic type for the computed property" |
| 40 | + |
| 41 | + case let .noValue(caseName, index): |
| 42 | + return "'ExtractCaseValue' macro could not find an associated value for `\(caseName)` at index \(index). Consider using a default value." |
| 43 | + |
| 44 | + case let .noMatchingType(type, caseName): |
| 45 | + return "'ExtractCaseValue' macro found no associated value of type \(type) in `\(caseName)`. Consider using a default value." |
| 46 | + |
| 47 | + case let .noAssociatedValues(caseName): |
| 48 | + return "'ExtractCaseValue' macro could not find associated values for `\(caseName)`. Consider using a default value." |
| 49 | + |
| 50 | + case let .noAssociatedValueForName(name, caseName): |
| 51 | + return "'ExtractCaseValue' macro found no associated value named \(name) in `\(caseName)`. Consider using a default value." |
| 52 | + |
| 53 | + case let .typeMismatch(caseName, index): |
| 54 | + return "'ExtractCaseValue' macro found a mismatching type for `\(caseName)` at index \(index)" |
| 55 | + |
| 56 | + case let .typeMismatchNamed(paramName, caseName): |
| 57 | + return "'ExtractCaseValue' macro found a mismatching type for \(paramName) in the `\(caseName)` case" |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + var severity: DiagnosticSeverity { .error } |
| 62 | + |
| 63 | + var diagnosticID: MessageID { |
| 64 | + MessageID(domain: "Swift", id: "ExtractCaseValue.\(self)") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +struct InsertDefaultValueItMessage: FixItMessage { |
| 69 | + var message: String { |
| 70 | + "Insert default value" |
| 71 | + } |
| 72 | + |
| 73 | + var fixItID: MessageID { |
| 74 | + MessageID(domain: "Swift", id: "ExtractCaseValue.\(self)") |
| 75 | + } |
| 76 | +} |
0 commit comments