Skip to content

Fixed up two issues that were discovered #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ struct DiagnosticsFormatter {
for diag in diags.dropLast(1) {
annotatedSource.append("\(preMessage)├─ \(colorizeIfRequested(diag.diagMessage))\n")
for fixIt in diag.fixIts {
annotatedSource.append("\(preMessage)│ ✏️ \(fixIt.message.message)")
annotatedSource.append("\(preMessage)│ ✏️ \(fixIt.message.message)\n")
}
}
annotatedSource.append("\(preMessage)╰─ \(colorizeIfRequested(diags.last!.diagMessage))\n")
for fixIt in diags.last!.fixIts {
annotatedSource.append("\(preMessage) ✏️ \(fixIt.message.message)")
annotatedSource.append("\(preMessage) ✏️ \(fixIt.message.message)\n")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import MacroTesting
import XCTest

final class DiagnosticsAndFixitsEmitterMacroTests: BaseTestCase {
override func invokeTest() {
withMacroTesting(macros: [DiagnosticsAndFixitsEmitterMacro.self]) {
super.invokeTest()
}
}

func testExpansionEmitsDiagnosticsAndFixits() {
assertMacro {
"""
@DiagnosticsAndFixitsEmitter
struct FooBar {
let foo: Foo
let bar: Bar
}
"""
} diagnostics: {
"""
@DiagnosticsAndFixitsEmitter
┬──────────────────────────
├─ ⚠️ This is the first diagnostic.
│ ✏️ This is the first fix-it.
│ ✏️ This is the second fix-it.
╰─ ℹ️ This is the second diagnostic, it's a note.
Comment on lines +23 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix in DiagnosticsFormatter.swift, this output took the following form:

       ┬──────────────────────────
       ├─ ⚠️ This is the first diagnostic.
         ✏️ This is the first fix-it.  ✏️ This is the second fix-it.╰─ ℹ️ This is the second diagnostic, it's a note.

struct FooBar {
let foo: Foo
let bar: Bar
}
"""
} expansion: {
"""
struct FooBar {
let foo: Foo
let bar: Bar
}
"""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftDiagnostics

/// Emits two diagnostics, the first of which is a warning and has two fix-its, and
/// the second is a note and has no fix-its.
public enum DiagnosticsAndFixitsEmitterMacro: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
let firstFixIt = FixIt(message: SimpleDiagnosticMessage(message: "This is the first fix-it.",
diagnosticID: MessageID(domain: "domain", id: "fixit1"),
severity: .error),
changes: [
.replace(oldNode: Syntax(node), newNode: Syntax(node)) // no-op
])
let secondFixIt = FixIt(message: SimpleDiagnosticMessage(message: "This is the second fix-it.",
diagnosticID: MessageID(domain: "domain", id: "fixit2"),
severity: .error),
changes: [
.replace(oldNode: Syntax(node), newNode: Syntax(node)) // no-op
])

context.diagnose(Diagnostic(node: node.attributeName,
message: SimpleDiagnosticMessage(message: "This is the first diagnostic.",
diagnosticID: MessageID(domain: "domain", id: "diagnostic2"),
severity: .warning),
fixIts: [firstFixIt, secondFixIt]))
context.diagnose(Diagnostic(node: node.attributeName,
message: SimpleDiagnosticMessage(message: "This is the second diagnostic, it's a note.",
diagnosticID: MessageID(domain: "domain", id: "diagnostic2"),
severity: .note)))

return []
}
}