isValidSwiftIdentifier(for:)
Checks whether name
can be used as an identifier in a certain context.
func isValidSwiftIdentifier(for context: IdentifierCheckContext) -> Bool
If the name cannot be used as an identifier in this context, it needs to be escaped.
For example, class
is not a valid identifier for a variable name and needs to be be wrapped in backticks to be valid Swift code, like the following.
let `class`: String
The context is important here – some names can be used as identifiers in some contexts but not others. For example, myStruct.class
is valid without adding backticks class
, but as mentioned above, backticks need to be added when class
is used as a variable name.