Skip to content

Commit

Permalink
[core] Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
VAndrJ committed Jul 4, 2024
1 parent 2554ac7 commit f296063
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
40 changes: 24 additions & 16 deletions Sources/VACopyWithMacros/VACopyWithMacro+Support.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@

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
}

switch binding.accessorBlock?.accessors {
case let .accessors(node):
for accessor in node {
switch accessor.accessorSpecifier.tokenKind {
case .keyword(.willSet), .keyword(.didSet):
case willSetKeyword, didSetKeyword:
continue
default:
return false
Expand All @@ -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
}
Expand All @@ -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)")
Expand All @@ -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 }
}

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Sources/VACopyWithMacros/VACopyWithMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 5 additions & 5 deletions Sources/VACopyWithMacros/VAMutatedCopyMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Sources/VACopyWithMacros/VAMutatingMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit f296063

Please sign in to comment.