1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
import WIT
enum SwiftName {
static let keywordSet = Set(Keyword.allCases.map(\.rawValue))
/// Make a Swift-safe name from the given text.
static func makeName(_ text: String) -> String {
if keywordSet.contains(text) {
return "`\(text)`"
} else {
return text
}
}
static func makeName(_ identifier: Identifier) throws -> String {
try makeName(ConvertCase.camelCase(identifier))
}
static func makeName(kebab: String) -> String {
makeName(ConvertCase.camelCase(kebab: kebab))
}
}
/// List of Swift keywords.
/// Copied from https://github.com/apple/swift-syntax/blob/8c1977d8acb7b308082d2cc82f3e9b26674545b1/CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift#L77-L284
enum Keyword: String, CaseIterable {
case __consuming
case __owned
case __setter_access
case __shared
case _alignment
case _backDeploy
case _borrow
case _borrowing
case _cdecl
case _Class
case _compilerInitialized
case _const
case _consuming
case _documentation
case _dynamicReplacement
case _effects
case _expose
case _forward
case _implements
case _linear
case _local
case _modify
case _move
case _mutating
case _NativeClass
case _NativeRefCountedObject
case _noMetadata
case _nonSendable
case _objcImplementation
case _objcRuntimeName
case _opaqueReturnTypeOf
case _optimize
case _originallyDefinedIn
case _PackageDescription
case _private
case _projectedValueProperty
case _read
case _RefCountedObject
case _semantics
case _specialize
case _spi
case _spi_available
case _swift_native_objc_runtime_base
case _Trivial
case _TrivialAtMost
case _typeEraser
case _unavailableFromAsync
case _underlyingVersion
case _UnknownLayout
case _version
case accesses
case actor
case addressWithNativeOwner
case addressWithOwner
case any
case `Any`
case `as`
case assignment
case `associatedtype`
case associativity
case async
case attached
case autoclosure
case availability
case available
case await
case backDeployed
case before
case block
case borrowing
case `break`
case canImport
case `case`
case `catch`
case `class`
case compiler
case consume
case copy
case consuming
case `continue`
case convenience
case convention
case cType
case `default`
case `defer`
case `deinit`
case deprecated
case derivative
case didSet
case differentiable
case distributed
case `do`
case dynamic
case each
case `else`
case `enum`
case escaping
case exclusivity
case exported
case `extension`
case `fallthrough`
case `false`
case file
case `fileprivate`
case final
case `for`
case discard
case forward
case `func`
case get
case `guard`
case higherThan
case `if`
case `import`
case `in`
case indirect
case infix
case `init`
case initializes
case inline
case `inout`
case `internal`
case introduced
case `is`
case isolated
case kind
case lazy
case left
case `let`
case line
case linear
case lowerThan
case macro
case message
case metadata
case module
case mutableAddressWithNativeOwner
case mutableAddressWithOwner
case mutating
case `nil`
case noasync
case noDerivative
case noescape
case none
case nonisolated
case nonmutating
case objc
case obsoleted
case of
case open
case `operator`
case optional
case override
case package
case postfix
case `precedencegroup`
case prefix
case `private`
case `Protocol`
case `protocol`
case `public`
case reasync
case renamed
case `repeat`
case required
case `rethrows`
case `return`
case reverse
case right
case safe
case `self`
case `Self`
case Sendable
case set
case some
case sourceFile
case spi
case spiModule
case `static`
case `struct`
case `subscript`
case `super`
case swift
case `switch`
case target
case then
case `throw`
case `throws`
case transpose
case `true`
case `try`
case `Type`
case `typealias`
case unavailable
case unchecked
case unowned
case unsafe
case unsafeAddress
case unsafeMutableAddress
case `var`
case visibility
case weak
case `where`
case `while`
case willSet
case witness_method
case wrt
case yield
}
|