|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | +import SwiftSyntaxBuilder |
| 15 | +import SwiftSyntaxMacros |
| 16 | +import SwiftDiagnostics |
| 17 | + |
| 18 | +/// Emits two diagnostics, the first of which is a warning and has two fix-its, and |
| 19 | +/// the second is a note and has no fix-its. |
| 20 | +public enum DiagnosticsAndFixitsEmitterMacro: MemberMacro { |
| 21 | + public static func expansion( |
| 22 | + of node: AttributeSyntax, |
| 23 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 24 | + in context: some MacroExpansionContext |
| 25 | + ) throws -> [DeclSyntax] { |
| 26 | + let firstFixIt = FixIt(message: SimpleDiagnosticMessage(message: "This is the first fix-it.", |
| 27 | + diagnosticID: MessageID(domain: "domain", id: "fixit1"), |
| 28 | + severity: .error), |
| 29 | + changes: [ |
| 30 | + .replace(oldNode: Syntax(node), newNode: Syntax(node)) // no-op |
| 31 | + ]) |
| 32 | + let secondFixIt = FixIt(message: SimpleDiagnosticMessage(message: "This is the second fix-it.", |
| 33 | + diagnosticID: MessageID(domain: "domain", id: "fixit2"), |
| 34 | + severity: .error), |
| 35 | + changes: [ |
| 36 | + .replace(oldNode: Syntax(node), newNode: Syntax(node)) // no-op |
| 37 | + ]) |
| 38 | + |
| 39 | + context.diagnose(Diagnostic(node: node.attributeName, |
| 40 | + message: SimpleDiagnosticMessage(message: "This is the first diagnostic.", |
| 41 | + diagnosticID: MessageID(domain: "domain", id: "diagnostic2"), |
| 42 | + severity: .warning), |
| 43 | + fixIts: [firstFixIt, secondFixIt])) |
| 44 | + context.diagnose(Diagnostic(node: node.attributeName, |
| 45 | + message: SimpleDiagnosticMessage(message: "This is the second diagnostic, it's a note.", |
| 46 | + diagnosticID: MessageID(domain: "domain", id: "diagnostic2"), |
| 47 | + severity: .note))) |
| 48 | + |
| 49 | + return [] |
| 50 | + } |
| 51 | +} |
0 commit comments