Skip to content

Commit

Permalink
Syntax improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenghaiWang committed Dec 21, 2023
1 parent dc0db4c commit fcb9202
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Sources/Macros/Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ public struct Access: AccessorMacro {
Declaration: DeclSyntaxProtocol>(of node: AttributeSyntax,
providingAccessorsOf declaration: Declaration,
in context: Context) throws -> [AccessorDeclSyntax] {
guard let firstArg = node.argument?.as(TupleExprElementListSyntax.self)?.first,
guard let firstArg = node.arguments?.as(LabeledExprListSyntax.self)?.first,
let type = firstArg.type else {
throw MacroDiagnostics.errorMacroUsage(message: "Must specify a content type")
}
if type == "userDefaults",
let dataType = node.attributeName.as(SimpleTypeIdentifierSyntax.self)?.type {
let dataType = node.attributeName.as(IdentifierTypeSyntax.self)?.type {
return processUserDefaults(for: declaration,
userDefaults: firstArg.userDefaults,
type: "\(dataType)")
} else if ["nsCache", "nsMapTable"].contains(type),
let object = firstArg.object,
let dataType = node.attributeName.as(SimpleTypeIdentifierSyntax.self)?.type {
let isOptionalType = node.attributeName.as(SimpleTypeIdentifierSyntax.self)?.genericArgumentClause?.arguments
.first?.as(GenericArgumentSyntax.self)?.argumentType.is(OptionalTypeSyntax.self) ?? false
let dataType = node.attributeName.as(IdentifierTypeSyntax.self)?.type {
let isOptionalType = node.attributeName.as(IdentifierTypeSyntax.self)?.genericArgumentClause?.arguments
.first?.as(GenericArgumentSyntax.self)?.argument.is(OptionalTypeSyntax.self) ?? false
return processNSCacheAndNSMapTable(for: declaration,
object: object,
type: "\(dataType)",
Expand All @@ -36,7 +36,7 @@ public struct Access: AccessorMacro {
private static func processKeychain(for declaration: DeclSyntaxProtocol) -> [AccessorDeclSyntax] {
guard let binding = declaration.as(VariableDeclSyntax.self)?.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text,
binding.accessor == nil else { return [] }
binding.accessorBlock == nil else { return [] }
let getAccessor: AccessorDeclSyntax =
"""
get {
Expand All @@ -63,7 +63,7 @@ public struct Access: AccessorMacro {
type: String) -> [AccessorDeclSyntax] {
guard let binding = declaration.as(VariableDeclSyntax.self)?.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text,
binding.accessor == nil else { return [] }
binding.accessorBlock == nil else { return [] }
var defaultValue = ""
if let value = binding.initializer?.value {
defaultValue = " ?? \(value)"
Expand All @@ -90,7 +90,7 @@ public struct Access: AccessorMacro {
isOptionalType: Bool) -> [AccessorDeclSyntax] {
guard let binding = declaration.as(VariableDeclSyntax.self)?.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text,
binding.accessor == nil else { return [] }
binding.accessorBlock == nil else { return [] }
var defaultValue = ""
if let value = binding.initializer?.value {
defaultValue = " ?? \(value)"
Expand Down
1 change: 1 addition & 0 deletions Tests/MacroTests/ConformToHashableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class ConformToHashableTests: XCTestCase {
extension AClass: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(a)
hasher.combine(b)
}
}
""",
Expand Down

0 comments on commit fcb9202

Please sign in to comment.