Skip to content

Commit

Permalink
fix: fixed IgnoreCoding failing with didSet accessors (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyamahunt committed Jan 10, 2024
1 parent 9d20fbf commit bd881c9
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .github/config/spellcheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ matrix:
- code
- pre
sources:
- '**/*.md'
- '**/*.md|!CHANGELOG.md'
default_encoding: utf-8
14 changes: 14 additions & 0 deletions Plugins/MetaProtocolCodable/Config.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@_implementationOnly import Foundation

/// The configuration data for plugin.
///
/// Depending on the configuration data, source file check and
Expand Down Expand Up @@ -43,4 +45,16 @@ extension Config: Codable {
ScanMode.self, forKey: .scan
) ?? .target
}

static func url(forFilePath filePath: String) -> URL {
#if canImport(Darwin)
if #available(macOS 13, iOS 16, macCatalyst 16, tvOS 16, watchOS 9, *) {
return URL(filePath: filePath)
} else {
return URL(fileURLWithPath: filePath)
}
#else
return URL(fileURLWithPath: filePath)
#endif
}
}
15 changes: 3 additions & 12 deletions Plugins/MetaProtocolCodable/Plugin.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import PackagePlugin
@_implementationOnly import Foundation
@_implementationOnly import PackagePlugin

/// Provides `protocol` decoding/encoding syntax generation.
///
Expand Down Expand Up @@ -31,16 +31,7 @@ struct MetaProtocolCodable: BuildToolPlugin {
}
guard let file else { return .init(scan: .target) }
let pathStr = target.directory.appending([file]).string
#if canImport(Darwin)
let path =
if #available(macOS 13, *) {
URL(filePath: pathStr)
} else {
URL(fileURLWithPath: pathStr)
}
#else
let path = URL(fileURLWithPath: pathStr)
#endif
let path = Config.url(forFilePath: pathStr)
let conf = try Data(contentsOf: path)
let pConf = try? PropertyListDecoder().decode(Config.self, from: conf)
let config = try pConf ?? JSONDecoder().decode(Config.self, from: conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ struct UninitializedVariableDecl<Attr: PropertyAttribute>: DiagnosticProducer {
// && !accessors.contains { decl in
// decl.accessorKind.tokenKind == .keyword(.`init`)
// }
guard !computed else { continue }
guard computed else { fallthrough }
continue
default:
guard binding.initializer == nil else { continue }
}
Expand Down
46 changes: 0 additions & 46 deletions Sources/ProtocolGen/Config.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/ProtocolGen/Config.swift
11 changes: 1 addition & 10 deletions Sources/ProtocolGen/Fetch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,7 @@ extension ProtocolGen {
/// Performs asynchronous config data fetch and
/// prints to console in `JSON` format.
func run() async throws {
#if canImport(Darwin)
let path =
if #available(macOS 13, *) {
URL(filePath: path)
} else {
URL(fileURLWithPath: path)
}
#else
let path = URL(fileURLWithPath: path)
#endif
let path = Config.url(forFilePath: path)
let data = try Data(contentsOf: path)
let config =
if let config = try? JSONDecoder().decode(
Expand Down
11 changes: 1 addition & 10 deletions Sources/ProtocolGen/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,7 @@ extension ProtocolGen {
/// Generates `HelperCoder` implementations
/// for stored protocol datas.
func run() async throws {
#if canImport(Darwin)
let inputs =
if #available(macOS 13, *) {
inputs.map { URL(filePath: $0) }
} else {
inputs.map { URL(fileURLWithPath: $0) }
}
#else
let inputs = inputs.map { URL(fileURLWithPath: $0) }
#endif
let inputs = inputs.map { Config.url(forFilePath: $0) }

let data = try await fetchInputData(input: inputs)
let context = BasicMacroExpansionContext()
Expand Down
19 changes: 2 additions & 17 deletions Sources/ProtocolGen/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,8 @@ extension ProtocolGen {
/// Performs parsing of swift source files and storing
/// `SourceData` in JSON format in `output` file path.
func run() async throws {
#if canImport(Darwin)
let input =
if #available(macOS 13, *) {
URL(filePath: input)
} else {
URL(fileURLWithPath: input)
}
let output =
if #available(macOS 13, *) {
URL(filePath: output)
} else {
URL(fileURLWithPath: output)
}
#else
let input = URL(fileURLWithPath: input)
let output = URL(fileURLWithPath: output)
#endif
let input = Config.url(forFilePath: input)
let output = Config.url(forFilePath: output)
let sourceData = try Data(contentsOf: input)
let sourceText = String(data: sourceData, encoding: .utf8)!
let sourceFile = Parser.parse(source: sourceText)
Expand Down
54 changes: 37 additions & 17 deletions Tests/MetaCodableTests/CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import SwiftSyntaxMacrosTestSupport
import XCTest

@testable import PluginCore
@testable import MacroPlugin

final class CodableTests: XCTestCase {

Expand Down Expand Up @@ -310,6 +309,42 @@ final class CodableTests: XCTestCase {
}
}

#if canImport(MacroPlugin)
@testable import MacroPlugin

let allMacros: [String: Macro.Type] = [
"CodedAt": MacroPlugin.CodedAt.self,
"CodedIn": MacroPlugin.CodedIn.self,
"Default": MacroPlugin.Default.self,
"CodedBy": MacroPlugin.CodedBy.self,
"CodedAs": MacroPlugin.CodedAs.self,
"ContentAt": MacroPlugin.ContentAt.self,
"IgnoreCoding": MacroPlugin.IgnoreCoding.self,
"IgnoreDecoding": MacroPlugin.IgnoreDecoding.self,
"IgnoreEncoding": MacroPlugin.IgnoreEncoding.self,
"Codable": MacroPlugin.Codable.self,
"MemberInit": MacroPlugin.MemberInit.self,
"CodingKeys": MacroPlugin.CodingKeys.self,
"IgnoreCodingInitialized": MacroPlugin.IgnoreCodingInitialized.self,
]
#else
let allMacros: [String: Macro.Type] = [
"CodedAt": CodedAt.self,
"CodedIn": CodedIn.self,
"Default": Default.self,
"CodedBy": CodedBy.self,
"CodedAs": CodedAs.self,
"ContentAt": ContentAt.self,
"IgnoreCoding": IgnoreCoding.self,
"IgnoreDecoding": IgnoreDecoding.self,
"IgnoreEncoding": IgnoreEncoding.self,
"Codable": Codable.self,
"MemberInit": MemberInit.self,
"CodingKeys": CodingKeys.self,
"IgnoreCodingInitialized": IgnoreCodingInitialized.self,
]
#endif

func assertMacroExpansion(
_ originalSource: String,
expandedSource: String,
Expand All @@ -321,25 +356,10 @@ func assertMacroExpansion(
file: StaticString = #file,
line: UInt = #line
) {
let macros: [String: Macro.Type] = [
"CodedAt": MacroPlugin.CodedAt.self,
"CodedIn": MacroPlugin.CodedIn.self,
"Default": MacroPlugin.Default.self,
"CodedBy": MacroPlugin.CodedBy.self,
"CodedAs": MacroPlugin.CodedAs.self,
"ContentAt": MacroPlugin.ContentAt.self,
"IgnoreCoding": MacroPlugin.IgnoreCoding.self,
"IgnoreDecoding": MacroPlugin.IgnoreDecoding.self,
"IgnoreEncoding": MacroPlugin.IgnoreEncoding.self,
"Codable": MacroPlugin.Codable.self,
"MemberInit": MacroPlugin.MemberInit.self,
"CodingKeys": MacroPlugin.CodingKeys.self,
"IgnoreCodingInitialized": MacroPlugin.IgnoreCodingInitialized.self,
]
assertMacroExpansion(
originalSource, expandedSource: expandedSource,
diagnostics: diagnostics,
macroSpecs: macros.mapValues { value in
macroSpecs: allMacros.mapValues { value in
return MacroSpec(type: value, conformances: conformances)
},
testModuleName: testModuleName, testFileName: testFileName,
Expand Down
11 changes: 11 additions & 0 deletions Tests/MetaCodableTests/IgnoreCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ final class IgnoreCodingTests: XCTestCase {
var three: String { "some" }
@IgnoreDecoding
var four: String { get { "some" } }
@IgnoreCoding
var five: String = "some" {
didSet {
print(five)
}
}
}
""",
expandedSource:
Expand All @@ -27,6 +33,11 @@ final class IgnoreCodingTests: XCTestCase {
var two: String
var three: String { "some" }
var four: String { get { "some" } }
var five: String = "some" {
didSet {
print(five)
}
}
}
extension SomeCodable: Decodable {
Expand Down

0 comments on commit bd881c9

Please sign in to comment.