|
| 1 | +import SwiftSyntax |
| 2 | + |
| 3 | +/// A property of a declaration group such as a `struct`. |
| 4 | +public struct Property { |
| 5 | + public var _syntax: TokenSyntax |
| 6 | + public var identifier: String |
| 7 | + public var type: Type? |
| 8 | + public var initialValue: Expr? |
| 9 | + public var accessors: [AccessorDeclSyntax] |
| 10 | + |
| 11 | + public var getter: AccessorDeclSyntax? { |
| 12 | + accessors.first { $0.accessorSpecifier.tokenKind == .keyword(.get) } |
| 13 | + } |
| 14 | + |
| 15 | + public var setter: AccessorDeclSyntax? { |
| 16 | + accessors.first { $0.accessorSpecifier.tokenKind == .keyword(.set) } |
| 17 | + } |
| 18 | + |
| 19 | + public var isStored: Bool { |
| 20 | + getter == nil |
| 21 | + } |
| 22 | + |
| 23 | + static func properties(from binding: PatternBindingSyntax) -> [Property] { |
| 24 | + let accessors: [AccessorDeclSyntax] = switch binding.accessorBlock?.accessors { |
| 25 | + case .accessors(let block): |
| 26 | + Array(block) |
| 27 | + case .getter(let getter): |
| 28 | + [AccessorDeclSyntax(accessorSpecifier: .keyword(.get)) { getter }] |
| 29 | + case .none: |
| 30 | + [] |
| 31 | + } |
| 32 | + return properties( |
| 33 | + pattern: binding.pattern, |
| 34 | + initialValue: (binding.initializer?.value).map(Expr.init), |
| 35 | + type: (binding.typeAnnotation?.type).map(Type.init), |
| 36 | + accessors: accessors |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + private static func properties( |
| 41 | + pattern: PatternSyntax, |
| 42 | + initialValue: Expr?, |
| 43 | + type: Type?, |
| 44 | + accessors: [AccessorDeclSyntax] |
| 45 | + ) -> [Property] { |
| 46 | + switch pattern.asProtocol(PatternSyntaxProtocol.self) { |
| 47 | + case let pattern as IdentifierPatternSyntax: |
| 48 | + let type: Type? = if let type { |
| 49 | + type |
| 50 | + } else { |
| 51 | + if initialValue?.asIntegerLiteral != nil { |
| 52 | + Type("Int") |
| 53 | + } else if initialValue?.asFloatLiteral != nil { |
| 54 | + Type("Double") |
| 55 | + } else if initialValue?.asStringLiteral != nil { |
| 56 | + Type("String") |
| 57 | + } else if initialValue?.asBooleanLiteral != nil { |
| 58 | + Type("Bool") |
| 59 | + } else if initialValue?.asRegexLiteral != nil { |
| 60 | + Type("Regex") |
| 61 | + } else if let array = initialValue?._syntax.as(ArrayExprSyntax.self) { |
| 62 | + inferArrayLiteralType(array) |
| 63 | + } else { |
| 64 | + nil |
| 65 | + } |
| 66 | + } |
| 67 | + return [ |
| 68 | + Property( |
| 69 | + _syntax: pattern.identifier, |
| 70 | + identifier: pattern.identifier.text, |
| 71 | + type: type, |
| 72 | + initialValue: initialValue, |
| 73 | + accessors: accessors |
| 74 | + ) |
| 75 | + ] |
| 76 | + case let pattern as TuplePatternSyntax: |
| 77 | + let tupleInitialValue: TupleExprSyntax? = |
| 78 | + if let initialValue, let tuple = initialValue._syntax.as(TupleExprSyntax.self), |
| 79 | + tuple.elements.count == pattern.elements.count |
| 80 | + { |
| 81 | + tuple |
| 82 | + } else { |
| 83 | + nil |
| 84 | + } |
| 85 | + let tupleType: TupleType? = |
| 86 | + if let type, |
| 87 | + let tuple = TupleType(type), |
| 88 | + tuple.elements.count == pattern.elements.count |
| 89 | + { |
| 90 | + tuple |
| 91 | + } else { |
| 92 | + nil |
| 93 | + } |
| 94 | + return pattern.elements.enumerated().flatMap { (index, element) in |
| 95 | + let initialValue = if let tupleInitialValue { |
| 96 | + Expr(Array(tupleInitialValue.elements)[index].expression) |
| 97 | + } else { |
| 98 | + initialValue.map { expr in |
| 99 | + Expr( |
| 100 | + MemberAccessExprSyntax( |
| 101 | + leadingTrivia: nil, base: expr._syntax.parenthesized, |
| 102 | + period: .periodToken(), |
| 103 | + name: .identifier(String(index)), trailingTrivia: nil |
| 104 | + ) |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // If in a tuple initial value expression, an empty array literal is inferred to have |
| 110 | + // type `Array<Any>`, unlike with regular initial value expressions. |
| 111 | + let type = |
| 112 | + if let arrayLiteral = initialValue?._syntax.as(ArrayExprSyntax.self), |
| 113 | + arrayLiteral.elements.isEmpty |
| 114 | + { |
| 115 | + Type("Array<Any>") |
| 116 | + } else { |
| 117 | + tupleType?.elements[index] |
| 118 | + } |
| 119 | + |
| 120 | + // Tuple bindings can't have accessors |
| 121 | + return properties( |
| 122 | + pattern: element.pattern, initialValue: initialValue, type: type, |
| 123 | + accessors: [] |
| 124 | + ) |
| 125 | + } |
| 126 | + case _ as WildcardPatternSyntax: |
| 127 | + return [] |
| 128 | + default: |
| 129 | + // TODO: Handle all patterns |
| 130 | + return [] |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static func inferArrayLiteralType(_ arrayLiteral: ArrayExprSyntax) -> Type? { |
| 135 | + var elementType: String? |
| 136 | + for element in arrayLiteral.elements { |
| 137 | + if element.expression.is(IntegerLiteralExprSyntax.self) { |
| 138 | + if elementType == nil { |
| 139 | + elementType = "Int" |
| 140 | + } else if elementType == "Double" { |
| 141 | + continue |
| 142 | + } else { |
| 143 | + return nil |
| 144 | + } |
| 145 | + } else if element.expression.is(FloatLiteralExprSyntax.self) { |
| 146 | + if elementType == nil || elementType == "Int" { |
| 147 | + elementType = "Double" |
| 148 | + } else { |
| 149 | + return nil |
| 150 | + } |
| 151 | + } else if element.expression.is(BooleanLiteralExprSyntax.self) { |
| 152 | + if elementType == nil { |
| 153 | + elementType = "Bool" |
| 154 | + } else { |
| 155 | + return nil |
| 156 | + } |
| 157 | + } else if element.expression.is(StringLiteralExprSyntax.self) { |
| 158 | + if elementType == nil { |
| 159 | + elementType = "String" |
| 160 | + } else { |
| 161 | + return nil |
| 162 | + } |
| 163 | + } else if element.expression.is(RegexLiteralExprSyntax.self) { |
| 164 | + if elementType == nil { |
| 165 | + elementType = "Regex" |
| 166 | + } else { |
| 167 | + return nil |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return if let elementType { |
| 173 | + Type("Array<\(raw: elementType)>") |
| 174 | + } else { |
| 175 | + nil |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments