diff --git a/Sources/VACopyWithMacros/VACopyWithMacro+Support.swift b/Sources/VACopyWithMacros/VACopyWithMacro+Support.swift index 271716d..e18b6a1 100644 --- a/Sources/VACopyWithMacros/VACopyWithMacro+Support.swift +++ b/Sources/VACopyWithMacros/VACopyWithMacro+Support.swift @@ -7,21 +7,28 @@ import SwiftSyntax +private let staticKeyword: TokenKind = .keyword(.static) +private let classKeyword: TokenKind = .keyword(.class) +private let varKeyword: TokenKind = .keyword(.var) +private let letKeyword: TokenKind = .keyword(.let) +private let willSetKeyword: TokenKind = .keyword(.willSet) +private let didSetKeyword: TokenKind = .keyword(.didSet) + public extension VariableDeclSyntax { - var isLet: Bool { bindingSpecifier.tokenKind == .keyword(.let) } - var isVar: Bool { bindingSpecifier.tokenKind == .keyword(.var) } - var isStatic: Bool { modifiers.contains { $0.name.tokenKind == .keyword(.static) } } - var isClass: Bool { modifiers.contains { $0.name.tokenKind == .keyword(.class) } } - var isInstance: Bool { !isClass && !isStatic } + var isLet: Bool { bindingSpecifier.tokenKind == letKeyword } + var isVar: Bool { bindingSpecifier.tokenKind == varKeyword } + var isStatic: Bool { modifiers.contains { $0.name.tokenKind == staticKeyword } } + var isClass: Bool { modifiers.contains { $0.name.tokenKind == classKeyword } } + var isInstance: Bool { !(isClass || isStatic) } var isStored: Bool { get throws { guard isInstance else { return false } - guard bindings.count == 1, let binding = bindings.first, binding.pattern.as(TuplePatternSyntax.self) == nil else { + guard bindings.count == 1, let binding = bindings.first, !binding.pattern.is(TuplePatternSyntax.self) else { throw VACopyWithMacroError.multipleBindings } - guard isLet && binding.initializer == nil || isVar else { + guard isVar || isLet && binding.initializer == nil else { return false } @@ -29,7 +36,7 @@ public extension VariableDeclSyntax { case let .accessors(node): for accessor in node { switch accessor.accessorSpecifier.tokenKind { - case .keyword(.willSet), .keyword(.didSet): + case willSetKeyword, didSetKeyword: continue default: return false @@ -45,7 +52,8 @@ public extension VariableDeclSyntax { } } var nameWithType: (name: String, type: TypeSyntax)? { - guard let binding = bindings.first, bindings.count == 1, + guard bindings.count == 1, + let binding = bindings.first, let name = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text else { return nil } @@ -68,13 +76,13 @@ public extension VariableDeclSyntax { public extension ExprSyntax { var literalOrExprType: TypeSyntax? { - if self.as(StringLiteralExprSyntax.self) != nil { + if self.is(StringLiteralExprSyntax.self) { return TypeSyntax("String") - } else if self.as(IntegerLiteralExprSyntax.self) != nil { + } else if self.is(IntegerLiteralExprSyntax.self) { return TypeSyntax("Int") - } else if self.as(BooleanLiteralExprSyntax.self) != nil { + } else if self.is(BooleanLiteralExprSyntax.self) { return TypeSyntax("Bool") - } else if self.as(FloatLiteralExprSyntax.self) != nil { + } else if self.is(FloatLiteralExprSyntax.self) { return TypeSyntax("Double") } else if let member = self.as(MemberAccessExprSyntax.self)?.base?.description { return TypeSyntax("\(raw: member)") @@ -93,7 +101,7 @@ public extension ExprSyntax { } public extension TypeSyntax { - var isOptional: Bool { self.as(OptionalTypeSyntax.self) != nil } + var isOptional: Bool { self.is(OptionalTypeSyntax.self) } var nonOptional: TypeSyntax { self.as(OptionalTypeSyntax.self)?.wrappedType.trimmed ?? self.trimmed } } @@ -116,8 +124,8 @@ public extension DeclModifierListSyntax { switch declModifierSyntax.name.tokenKind { case let .keyword(keyword): switch keyword { - case .public, .open: DeclModifierSyntax(name: .keyword(.public)) - case .fileprivate, .internal: declModifierSyntax + case .open: DeclModifierSyntax(name: .keyword(.public)) + case .public, .fileprivate, .internal: declModifierSyntax default: nil } default: nil diff --git a/Sources/VACopyWithMacros/VACopyWithMacro.swift b/Sources/VACopyWithMacros/VACopyWithMacro.swift index 4b4c10a..be59962 100644 --- a/Sources/VACopyWithMacros/VACopyWithMacro.swift +++ b/Sources/VACopyWithMacros/VACopyWithMacro.swift @@ -12,23 +12,23 @@ public struct VACopyWithMacro: ExtensionMacro { conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext ) throws -> [ExtensionDeclSyntax] { - guard let decl = declaration.as(StructDeclSyntax.self) else { + guard declaration is StructDeclSyntax else { throw VACopyWithMacroError.notStruct } - let storedProperties = try decl.storedProperties() + let storedProperties = try declaration.storedProperties() let properties = storedProperties.compactMap(\.nameWithType) guard !properties.isEmpty else { return [] } - let isContainsOptional = properties.contains { $0.type.isOptional } - + let isContainsOptional = properties.contains(where: { $0.type.isOptional }) + return [ - ExtensionDeclSyntax(modifiers: decl.modifiers.accessModifier, extendedType: type) { + ExtensionDeclSyntax(modifiers: declaration.modifiers.accessModifier, extendedType: type) { if isContainsOptional { EnumDeclSyntax( - modifiers: decl.modifiers.accessModifier, + modifiers: declaration.modifiers.accessModifier, name: "OR", genericParameterClause: GenericParameterClauseSyntax(parameters: GenericParameterListSyntax(arrayLiteral: GenericParameterSyntax(name: "T"))), memberBlock: MemberBlockSyntax(members: MemberBlockItemListSyntax(itemsBuilder: { diff --git a/Sources/VACopyWithMacros/VAMutatedCopyMacro.swift b/Sources/VACopyWithMacros/VAMutatedCopyMacro.swift index 955aefe..c30e050 100644 --- a/Sources/VACopyWithMacros/VAMutatedCopyMacro.swift +++ b/Sources/VACopyWithMacros/VAMutatedCopyMacro.swift @@ -19,14 +19,14 @@ public struct VAMutatedCopyMacro: ExtensionMacro { conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext ) throws -> [ExtensionDeclSyntax] { - if let decl = declaration.as(StructDeclSyntax.self) { - let storedProperties = try decl.storedProperties() + if declaration is StructDeclSyntax { + let storedProperties = try declaration.storedProperties() guard storedProperties.contains(where: { $0.isVar }) else { return [] } return [ - ExtensionDeclSyntax(modifiers: decl.modifiers.accessModifier, extendedType: type) { + ExtensionDeclSyntax(modifiers: declaration.modifiers.accessModifier, extendedType: type) { """ func mutatedCopy(configuring: (_ it: inout \(type)) throws -> Void) rethrows -> \(type) { var mutableCopy = self @@ -37,9 +37,9 @@ public struct VAMutatedCopyMacro: ExtensionMacro { """ }, ] - } else if let decl = declaration.as(ProtocolDeclSyntax.self) { + } else if declaration is ProtocolDeclSyntax { return [ - ExtensionDeclSyntax(modifiers: decl.modifiers.accessModifier, extendedType: type) { + ExtensionDeclSyntax(modifiers: declaration.modifiers.accessModifier, extendedType: type) { """ func mutatedCopy(configuring: (_ it: inout Self) throws -> Void) rethrows -> Self { var mutableCopy = self diff --git a/Sources/VACopyWithMacros/VAMutatingMacro.swift b/Sources/VACopyWithMacros/VAMutatingMacro.swift index 855b802..34dced6 100644 --- a/Sources/VACopyWithMacros/VAMutatingMacro.swift +++ b/Sources/VACopyWithMacros/VAMutatingMacro.swift @@ -19,14 +19,14 @@ public struct VAMutatingMacro: ExtensionMacro { conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext ) throws -> [ExtensionDeclSyntax] { - if let decl = declaration.as(ClassDeclSyntax.self) { - let storedProperties = try decl.storedProperties() + if declaration is ClassDeclSyntax { + let storedProperties = try declaration.storedProperties() guard storedProperties.contains(where: { $0.isVar }) else { return [] } return [ - ExtensionDeclSyntax(modifiers: decl.modifiers.accessModifier, extendedType: type) { + ExtensionDeclSyntax(modifiers: declaration.modifiers.accessModifier, extendedType: type) { """ @discardableResult func mutating(configuring: (_ it: \(type)) throws -> Void) rethrows -> \(type) { @@ -37,10 +37,10 @@ public struct VAMutatingMacro: ExtensionMacro { """ }, ] - } else if let decl = declaration.as(ProtocolDeclSyntax.self) { - if decl.inheritanceClause?.inheritedTypes.contains(where: { $0.type.description.contains("AnyObject") }) == true { + } else if declaration is ProtocolDeclSyntax { + if declaration.inheritanceClause?.inheritedTypes.contains(where: { $0.type.description.contains("AnyObject") }) == true { return [ - ExtensionDeclSyntax(modifiers: decl.modifiers.accessModifier, extendedType: type) { + ExtensionDeclSyntax(modifiers: declaration.modifiers.accessModifier, extendedType: type) { """ @discardableResult func mutating(configuring: (_ it: Self) throws -> Void) rethrows -> Self {