Skip to content

Commit bd881c9

Browse files
authored
fix: fixed IgnoreCoding failing with didSet accessors (#54)
1 parent 9d20fbf commit bd881c9

File tree

10 files changed

+73
-114
lines changed

10 files changed

+73
-114
lines changed

.github/config/spellcheck.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ matrix:
1616
- code
1717
- pre
1818
sources:
19-
- '**/*.md'
19+
- '**/*.md|!CHANGELOG.md'
2020
default_encoding: utf-8

Plugins/MetaProtocolCodable/Config.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@_implementationOnly import Foundation
2+
13
/// The configuration data for plugin.
24
///
35
/// Depending on the configuration data, source file check and
@@ -43,4 +45,16 @@ extension Config: Codable {
4345
ScanMode.self, forKey: .scan
4446
) ?? .target
4547
}
48+
49+
static func url(forFilePath filePath: String) -> URL {
50+
#if canImport(Darwin)
51+
if #available(macOS 13, iOS 16, macCatalyst 16, tvOS 16, watchOS 9, *) {
52+
return URL(filePath: filePath)
53+
} else {
54+
return URL(fileURLWithPath: filePath)
55+
}
56+
#else
57+
return URL(fileURLWithPath: filePath)
58+
#endif
59+
}
4660
}

Plugins/MetaProtocolCodable/Plugin.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Foundation
2-
import PackagePlugin
1+
@_implementationOnly import Foundation
2+
@_implementationOnly import PackagePlugin
33

44
/// Provides `protocol` decoding/encoding syntax generation.
55
///
@@ -31,16 +31,7 @@ struct MetaProtocolCodable: BuildToolPlugin {
3131
}
3232
guard let file else { return .init(scan: .target) }
3333
let pathStr = target.directory.appending([file]).string
34-
#if canImport(Darwin)
35-
let path =
36-
if #available(macOS 13, *) {
37-
URL(filePath: pathStr)
38-
} else {
39-
URL(fileURLWithPath: pathStr)
40-
}
41-
#else
42-
let path = URL(fileURLWithPath: pathStr)
43-
#endif
34+
let path = Config.url(forFilePath: pathStr)
4435
let conf = try Data(contentsOf: path)
4536
let pConf = try? PropertyListDecoder().decode(Config.self, from: conf)
4637
let config = try pConf ?? JSONDecoder().decode(Config.self, from: conf)

Sources/PluginCore/Diagnostics/UninitializedVariableDecl.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ struct UninitializedVariableDecl<Attr: PropertyAttribute>: DiagnosticProducer {
7575
// && !accessors.contains { decl in
7676
// decl.accessorKind.tokenKind == .keyword(.`init`)
7777
// }
78-
guard !computed else { continue }
78+
guard computed else { fallthrough }
79+
continue
7980
default:
8081
guard binding.initializer == nil else { continue }
8182
}

Sources/ProtocolGen/Config.swift

Lines changed: 0 additions & 46 deletions
This file was deleted.

Sources/ProtocolGen/Config.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Plugins/MetaProtocolCodable/Config.swift

Sources/ProtocolGen/Fetch.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ extension ProtocolGen {
2525
/// Performs asynchronous config data fetch and
2626
/// prints to console in `JSON` format.
2727
func run() async throws {
28-
#if canImport(Darwin)
29-
let path =
30-
if #available(macOS 13, *) {
31-
URL(filePath: path)
32-
} else {
33-
URL(fileURLWithPath: path)
34-
}
35-
#else
36-
let path = URL(fileURLWithPath: path)
37-
#endif
28+
let path = Config.url(forFilePath: path)
3829
let data = try Data(contentsOf: path)
3930
let config =
4031
if let config = try? JSONDecoder().decode(

Sources/ProtocolGen/Generate.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,7 @@ extension ProtocolGen {
285285
/// Generates `HelperCoder` implementations
286286
/// for stored protocol datas.
287287
func run() async throws {
288-
#if canImport(Darwin)
289-
let inputs =
290-
if #available(macOS 13, *) {
291-
inputs.map { URL(filePath: $0) }
292-
} else {
293-
inputs.map { URL(fileURLWithPath: $0) }
294-
}
295-
#else
296-
let inputs = inputs.map { URL(fileURLWithPath: $0) }
297-
#endif
288+
let inputs = inputs.map { Config.url(forFilePath: $0) }
298289

299290
let data = try await fetchInputData(input: inputs)
300291
let context = BasicMacroExpansionContext()

Sources/ProtocolGen/Parse.swift

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,8 @@ extension ProtocolGen {
120120
/// Performs parsing of swift source files and storing
121121
/// `SourceData` in JSON format in `output` file path.
122122
func run() async throws {
123-
#if canImport(Darwin)
124-
let input =
125-
if #available(macOS 13, *) {
126-
URL(filePath: input)
127-
} else {
128-
URL(fileURLWithPath: input)
129-
}
130-
let output =
131-
if #available(macOS 13, *) {
132-
URL(filePath: output)
133-
} else {
134-
URL(fileURLWithPath: output)
135-
}
136-
#else
137-
let input = URL(fileURLWithPath: input)
138-
let output = URL(fileURLWithPath: output)
139-
#endif
123+
let input = Config.url(forFilePath: input)
124+
let output = Config.url(forFilePath: output)
140125
let sourceData = try Data(contentsOf: input)
141126
let sourceText = String(data: sourceData, encoding: .utf8)!
142127
let sourceFile = Parser.parse(source: sourceText)

Tests/MetaCodableTests/CodableTests.swift

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import SwiftSyntaxMacrosTestSupport
77
import XCTest
88

99
@testable import PluginCore
10-
@testable import MacroPlugin
1110

1211
final class CodableTests: XCTestCase {
1312

@@ -310,6 +309,42 @@ final class CodableTests: XCTestCase {
310309
}
311310
}
312311

312+
#if canImport(MacroPlugin)
313+
@testable import MacroPlugin
314+
315+
let allMacros: [String: Macro.Type] = [
316+
"CodedAt": MacroPlugin.CodedAt.self,
317+
"CodedIn": MacroPlugin.CodedIn.self,
318+
"Default": MacroPlugin.Default.self,
319+
"CodedBy": MacroPlugin.CodedBy.self,
320+
"CodedAs": MacroPlugin.CodedAs.self,
321+
"ContentAt": MacroPlugin.ContentAt.self,
322+
"IgnoreCoding": MacroPlugin.IgnoreCoding.self,
323+
"IgnoreDecoding": MacroPlugin.IgnoreDecoding.self,
324+
"IgnoreEncoding": MacroPlugin.IgnoreEncoding.self,
325+
"Codable": MacroPlugin.Codable.self,
326+
"MemberInit": MacroPlugin.MemberInit.self,
327+
"CodingKeys": MacroPlugin.CodingKeys.self,
328+
"IgnoreCodingInitialized": MacroPlugin.IgnoreCodingInitialized.self,
329+
]
330+
#else
331+
let allMacros: [String: Macro.Type] = [
332+
"CodedAt": CodedAt.self,
333+
"CodedIn": CodedIn.self,
334+
"Default": Default.self,
335+
"CodedBy": CodedBy.self,
336+
"CodedAs": CodedAs.self,
337+
"ContentAt": ContentAt.self,
338+
"IgnoreCoding": IgnoreCoding.self,
339+
"IgnoreDecoding": IgnoreDecoding.self,
340+
"IgnoreEncoding": IgnoreEncoding.self,
341+
"Codable": Codable.self,
342+
"MemberInit": MemberInit.self,
343+
"CodingKeys": CodingKeys.self,
344+
"IgnoreCodingInitialized": IgnoreCodingInitialized.self,
345+
]
346+
#endif
347+
313348
func assertMacroExpansion(
314349
_ originalSource: String,
315350
expandedSource: String,
@@ -321,25 +356,10 @@ func assertMacroExpansion(
321356
file: StaticString = #file,
322357
line: UInt = #line
323358
) {
324-
let macros: [String: Macro.Type] = [
325-
"CodedAt": MacroPlugin.CodedAt.self,
326-
"CodedIn": MacroPlugin.CodedIn.self,
327-
"Default": MacroPlugin.Default.self,
328-
"CodedBy": MacroPlugin.CodedBy.self,
329-
"CodedAs": MacroPlugin.CodedAs.self,
330-
"ContentAt": MacroPlugin.ContentAt.self,
331-
"IgnoreCoding": MacroPlugin.IgnoreCoding.self,
332-
"IgnoreDecoding": MacroPlugin.IgnoreDecoding.self,
333-
"IgnoreEncoding": MacroPlugin.IgnoreEncoding.self,
334-
"Codable": MacroPlugin.Codable.self,
335-
"MemberInit": MacroPlugin.MemberInit.self,
336-
"CodingKeys": MacroPlugin.CodingKeys.self,
337-
"IgnoreCodingInitialized": MacroPlugin.IgnoreCodingInitialized.self,
338-
]
339359
assertMacroExpansion(
340360
originalSource, expandedSource: expandedSource,
341361
diagnostics: diagnostics,
342-
macroSpecs: macros.mapValues { value in
362+
macroSpecs: allMacros.mapValues { value in
343363
return MacroSpec(type: value, conformances: conformances)
344364
},
345365
testModuleName: testModuleName, testFileName: testFileName,

0 commit comments

Comments
 (0)