Skip to content

Commit 0c4be0e

Browse files
authored
fix: minimized warnings generated by macro expanded code (#103)
1 parent 7246a7f commit 0c4be0e

32 files changed

+686
-296
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Package.resolved
5858
.build/
5959
*.o
6060
*.d
61-
*.swiftdeps
61+
*.swiftdeps*
6262

6363
# CocoaPods
6464
#

Examples/Podfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ IPHONEOS_DEPLOYMENT_TARGET = '14.0'
33

44
GITHUB_REPOSITORY = ENV['GITHUB_REPOSITORY'] || 'SwiftyLab/MetaCodable'
55
GITHUB_SERVER_URL = ENV['GITHUB_SERVER_URL'] || 'https://github.com'
6-
METACODABLE_DEV_BRANCH = ENV['GITHUB_HEAD_REF'] || ENV['GITHUB_REF_NAME']
6+
GITHUB_HEAD_REF = ENV['GITHUB_HEAD_REF']
7+
METACODABLE_DEV_BRANCH = GITHUB_HEAD_REF&.empty? ? ENV['GITHUB_REF_NAME'] : GITHUB_HEAD_REF
78
DEV_SOURCE = METACODABLE_DEV_BRANCH ? { :git => "#{GITHUB_SERVER_URL}/#{GITHUB_REPOSITORY}.git", :branch => METACODABLE_DEV_BRANCH } : { :path => "../" }
89

910
def add_metacodable

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ let package = Package(
3131
name: "PluginCore",
3232
dependencies: [
3333
.product(name: "SwiftSyntax", package: "swift-syntax"),
34+
.product(name: "SwiftOperators", package: "swift-syntax"),
3435
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
3536
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
3637
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),

Package@swift-5.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ let package = Package(
3030
name: "PluginCore",
3131
dependencies: [
3232
.product(name: "SwiftSyntax", package: "swift-syntax"),
33+
.product(name: "SwiftOperators", package: "swift-syntax"),
3334
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
3435
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
3536
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),

Sources/PluginCore/Diagnostics/MetaCodableMessage.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ struct MetaCodableMessage: Error, DiagnosticMessage, FixItMessage {
9898
message: String, id: MessageID
9999
) -> MetaCodableMessage {
100100
return .init(
101-
macro: macro,
102-
message: message,
103-
messageID: id,
104-
severity: .warning
101+
macro: macro, message: message, messageID: id, severity: .warning
105102
)
106103
}
107104
}
105+
106+
#if !canImport(SwiftSyntax600)
107+
extension MetaCodableMessage: @unchecked Sendable {}
108+
#endif

Sources/PluginCore/Expansion/AttributeExpander.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,22 @@ struct AttributeExpander {
6464
let decodable = variable.protocol(named: dProtocol, in: protocols)
6565
let encodable = variable.protocol(named: eProtocol, in: protocols)
6666

67-
return [
67+
var extensions = [
6868
decoding(type: type, conformingTo: decodable, in: context),
6969
encoding(type: type, conformingTo: encodable, in: context),
7070
codingKeys(for: type, confirmingTo: protocols, in: context),
7171
].compactMap { $0 }
72+
for index in extensions.indices {
73+
// attach available attributes from original declaration
74+
// to generated expanded declaration
75+
extensions[index].attributes = AttributeListSyntax {
76+
for attr in options.availableAttributes {
77+
.attribute(attr)
78+
}
79+
extensions[index].attributes
80+
}
81+
}
82+
return extensions
7283
}
7384

7485
/// Provides the `Decodable` extension declaration.

Sources/PluginCore/Expansion/Options/Options.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ extension AttributeExpander {
1111
/// The list of modifiers generator for
1212
/// conformance implementation declarations.
1313
let modifiersGenerator: DeclModifiersGenerator
14+
/// The list of `@available` attributes attached to
15+
/// original expanded declaration.
16+
let availableAttributes: [AttributeSyntax]
1417

1518
/// Memberwise initialization generator with provided options.
1619
///
@@ -29,6 +32,17 @@ extension AttributeExpander {
2932
/// - Returns: The newly created options.
3033
init(for decl: some DeclGroupSyntax) {
3134
self.modifiersGenerator = .init(decl: decl)
35+
self.availableAttributes = decl.attributes.compactMap { attribute in
36+
switch attribute {
37+
case .attribute(let attr):
38+
guard
39+
attr.attributeName.trimmed.description == "available"
40+
else { fallthrough }
41+
return attr
42+
default:
43+
return nil
44+
}
45+
}
3246
}
3347
}
3448
}

Sources/PluginCore/Variables/Enum/Case/BasicEnumCaseVariable.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SwiftOperators
12
import SwiftSyntax
23
import SwiftSyntaxMacros
34

@@ -117,7 +118,17 @@ struct BasicEnumCaseVariable: EnumCaseVariable {
117118
let label = SwitchCaseLabelSyntax(
118119
caseItems: .init {
119120
for value in location.values {
120-
let pattern = ExpressionPatternSyntax(expression: value)
121+
let expr =
122+
OperatorTable.standardOperators
123+
.foldAll(value) { _ in }.as(ExprSyntax.self) ?? value
124+
let pattern =
125+
if let asExpr = expr.as(AsExprSyntax.self) {
126+
ExpressionPatternSyntax(
127+
expression: asExpr.expression.trimmed
128+
)
129+
} else {
130+
ExpressionPatternSyntax(expression: expr)
131+
}
121132
SwitchCaseItemSyntax(pattern: pattern)
122133
}
123134
}

Sources/PluginCore/Variables/Enum/Switcher/InternallyTaggedEnumSwitcher.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ where Variable: PropertyVariable {
114114
let containerVariable = ContainerVariable(
115115
encodeContainer: encodeContainer, base: output.variable
116116
)
117-
node.register(variable: containerVariable, keyPath: keys)
117+
node.register(
118+
variable: containerVariable, keyPath: keys,
119+
immutableEncodeContainer: true
120+
)
118121
self.node = node
119122
}
120123

Sources/PluginCore/Variables/Enum/Switcher/TaggedEnumSwitcherVariable.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ extension EnumSwitcherVariable {
101101
let label = SwitchCaseLabelSyntax {
102102
.init(pattern: pattern, whereClause: whereClause)
103103
}
104-
let caseSyntax = CodeBlockItemListSyntax {
105-
preSyntax("\(value.encodeExprs.first!)")
106-
generated.code.combined()
107-
}
104+
105+
let generatedCode = generated.code.combined()
108106
SwitchCaseSyntax(label: .case(label)) {
109-
!caseSyntax.isEmpty ? caseSyntax : "break"
107+
if !generatedCode.isEmpty {
108+
CodeBlockItemListSyntax {
109+
preSyntax("\(value.encodeExprs.first!)")
110+
generatedCode
111+
}
112+
} else {
113+
"break"
114+
}
110115
}
111116
}
112117
if `default` || !allEncodable || anyEncodeCondition {

0 commit comments

Comments
 (0)