Commit d3b9518
committed
Add declarations to make construction of diagnostics in macros easier
Essentially, this makes three improvement:
1. Add types to allow the creation of error/warning/Fix-It messages from string literals. These types have a static diagnostic ID but I think that should be sufficient for most macros.
2. Add a convenience initializer to `Diagnostic` to create it with a single Fix-It, eliminating the need to create an array literal containing all Fix-Its
3. Add a static method on `FixIt` to create a Fix-It with a single `replace` change.
This simplifies the creation of a simple diagnostic from
```swift
let diagnosticDomain = "AddCompletionHandlerMacro"
struct CanOnlyBeAppliedDoAsyncFunctionErrorMessage: DiagnosticMessage {
let message: String
var diagnosticID: SwiftDiagnostics.MessageID { .init(domain: diagnosticDomain, id: "\(Self.self)") }
var severity: SwiftDiagnostics.DiagnosticSeverity { .error }
init(_ funcDecl: FunctionDeclSyntax) {
message = "can only add a \(funcDecl) completion-handler variant to an 'async' function"
}
}
enum AddCompletionHandlerFixItMessage: FixItMessage {
case addAsync
var fixItID: SwiftDiagnostics.MessageID { .init(domain: diagnosticDomain, id: "\(Self.self)") }
var message: String {
switch self {
case .addAsync: return "add 'async'"
}
}
}
let diagnostic = Diagnostic(
node: Syntax(funcDecl.funcKeyword),
message: CanOnlyBeAppliedDoAsyncFunctionErrorMessage(funcDecl),
fixIt: FixIt(
message: AddCompletionHandlerFixItMessage.addAsync,
changes: [
.replace(oldNode: Syntax(funcDecl.signature), newNode: Syntax(newSignature))
]
)
)
```
to
```swift
let diagnostic = Diagnostic(
node: funcDecl.funcKeyword,
message: MacroExpansionErrorMessage("can only add a \(node) completion-handler variant to an 'async' function"),
fixIt: FixIt.replace(
message: MacroExpansionFixItMessage("add 'async'")
oldNode: funcDecl.signature,
newNode: newSignature
)
)
```1 parent e340af4 commit d3b9518
File tree
7 files changed
+134
-39
lines changed- Sources
- SwiftDiagnostics
- SwiftSyntaxMacroExpansion
- Tests/SwiftSyntaxMacroExpansionTest
7 files changed
+134
-39
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
Lines changed: 68 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
420 | 420 | | |
421 | 421 | | |
422 | 422 | | |
423 | | - | |
| 423 | + | |
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
| |||
Lines changed: 10 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | 23 | | |
35 | 24 | | |
36 | 25 | | |
37 | 26 | | |
38 | 27 | | |
39 | 28 | | |
40 | 29 | | |
41 | | - | |
| 30 | + | |
42 | 31 | | |
43 | 32 | | |
44 | 33 | | |
| |||
110 | 99 | | |
111 | 100 | | |
112 | 101 | | |
113 | | - | |
| 102 | + | |
114 | 103 | | |
115 | 104 | | |
116 | 105 | | |
| |||
123 | 112 | | |
124 | 113 | | |
125 | 114 | | |
126 | | - | |
| 115 | + | |
127 | 116 | | |
128 | 117 | | |
129 | 118 | | |
| |||
147 | 136 | | |
148 | 137 | | |
149 | 138 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | 139 | | |
161 | 140 | | |
162 | 141 | | |
| |||
168 | 147 | | |
169 | 148 | | |
170 | 149 | | |
171 | | - | |
| 150 | + | |
172 | 151 | | |
173 | 152 | | |
174 | 153 | | |
175 | 154 | | |
176 | 155 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
| 156 | + | |
182 | 157 | | |
183 | 158 | | |
184 | 159 | | |
| |||
197 | 172 | | |
198 | 173 | | |
199 | 174 | | |
200 | | - | |
| 175 | + | |
201 | 176 | | |
202 | 177 | | |
203 | 178 | | |
| |||
304 | 279 | | |
305 | 280 | | |
306 | 281 | | |
307 | | - | |
| 282 | + | |
308 | 283 | | |
309 | 284 | | |
310 | 285 | | |
311 | 286 | | |
312 | | - | |
| 287 | + | |
313 | 288 | | |
314 | 289 | | |
315 | 290 | | |
| |||
559 | 534 | | |
560 | 535 | | |
561 | 536 | | |
562 | | - | |
| 537 | + | |
563 | 538 | | |
564 | 539 | | |
565 | 540 | | |
566 | 541 | | |
567 | 542 | | |
568 | 543 | | |
569 | | - | |
| 544 | + | |
570 | 545 | | |
571 | 546 | | |
572 | 547 | | |
| |||
0 commit comments