Skip to content

Commit

Permalink
Cleaned up a few things in new isComputed func.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewmccormack committed Dec 19, 2024
1 parent 202cce9 commit 694e4dd
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions Sources/ForkedModelMacros/VariableDeclSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,39 @@ extension VariableDeclSyntax {

func isComputed() -> Bool {
// Each variable may have multiple declarations on the same line (ex. var foo: String, bar: String),
// but in this case, no accessor may be provided, so each declared var it may not be a computed var
guard bindings.count == 1 else { return false }
// but computed variables much always have 1.
guard bindings.count == 1 else {
return false
}

// If not accessors are presents, then the variable is in the form "var foo: Int = 3", which is by definition not a computed variable
// If no accessors are present then the variable is a
// stored variable
guard let accessorBlock = bindings.first!.accessorBlock else {
return false
}

// Get accessors list
let accessorsList: AccessorDeclListSyntax

switch accessorBlock.accessors {
case .getter:
// If the block is a "getter" block, then the var is computed
// ex. var foo: Int { 3 }
return true
case .accessors(let accessors):
accessorsList = accessors
}

// The variable is a "computed variable" only if a getter is present in the list
// of its accessors, with no setter present.
var containsGetter: Bool = false
var containsSetter: Bool = false

// The variable is a "computed variable" only if a getter is present in the list of its accessors without a setter.
// Check that the accessorsList may contains different accessors like "didSet" or "willSet", and no "set" and "get".
// In this case the var is not computed
// ex. var foo: Int = 4 { didSet { print("hello") } }
for accessor in accessorsList {
if accessor.accessorSpecifier.tokenKind == .keyword(.set) {
containsSetter = true
continue
}

if accessor.accessorSpecifier.tokenKind == .keyword(.get) {
} else if accessor.accessorSpecifier.tokenKind == .keyword(.get) {
containsGetter = true
continue
}
}

if containsGetter, !containsSetter {
return true
}

return false
return containsGetter && !containsSetter
}

func propertyMerge() throws -> PropertyMerge? {
Expand Down

0 comments on commit 694e4dd

Please sign in to comment.