-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed up two issues that were discovered (#12)
* Fixed up two issues that were discovered 1. When a diagnostic has multiple fix its the annotated string didn't insert newlines between fix-its 2. When a syntax has the same node offset as one of its children, replacing the child with a fix-it would *always* replace the outermost node * Fixed up formatting * Updates per code review suggestions * Additional updates per code review suggestions
- Loading branch information
1 parent
35acd94
commit d0a8c51
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
Tests/MacroTestingTests/DiagnosticsAndFixitsEmitterMacroTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
struct FooBar { | ||
let foo: Foo | ||
let bar: Bar | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct FooBar { | ||
let foo: Foo | ||
let bar: Bar | ||
} | ||
""" | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Tests/MacroTestingTests/MacroExamples/DiagnosticsAndFixitsEmitterMacro.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] | ||
} | ||
} |