|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +@_spi(RawSyntax) import SwiftSyntax |
| 14 | + |
| 15 | +/// Context in which to check if a name can be used as an identifier. |
| 16 | +/// |
| 17 | +/// - SeeAlso: `Swift.isValidSwiftIdentifier(for:)` extension added by SwiftParser. |
| 18 | +public enum IdentifierCheckContext { |
| 19 | + /// Check if a name can be used to declare a variable, ie. if it can be used after a `let` or `var` keyword. |
| 20 | + /// |
| 21 | + /// ### Examples |
| 22 | + /// - `test` is a valid variable name and `let test: Int` is valid Swift code |
| 23 | + /// - `class` is not a valid variable and `let class: Int` is invalid Swift code |
| 24 | + case variableName |
| 25 | + |
| 26 | + /// Check if a name can be used as a member access, ie. if it can be used after a `.`. |
| 27 | + /// |
| 28 | + /// ### Examples |
| 29 | + /// - `test` is a valid identifier for member access because `myStruct.test` is valid |
| 30 | + /// - `class` is a valid identifier for member access because `myStruct.class` is valid, even though `class` |
| 31 | + /// needs to be wrapped in backticks when used to declare a variable. |
| 32 | + /// - `self` is not a valid identifier for member access because `myStruct.self` does not access a member named |
| 33 | + /// `self` on `myStruct` and instead returns `myStruct` itself. |
| 34 | + case memberAccess |
| 35 | +} |
| 36 | + |
| 37 | +extension String { |
| 38 | + /// Checks whether `name` can be used as an identifier in a certain context. |
| 39 | + /// |
| 40 | + /// If the name cannot be used as an identifier in this context, it needs to be escaped. |
| 41 | + /// |
| 42 | + /// For example, `class` is not a valid identifier for a variable name and needs to be be wrapped in backticks |
| 43 | + /// to be valid Swift code, like the following. |
| 44 | + /// |
| 45 | + /// ```swift |
| 46 | + /// let `class`: String |
| 47 | + /// ``` |
| 48 | + /// |
| 49 | + /// The context is important here – some names can be used as identifiers in some contexts but not others. |
| 50 | + /// For example, `myStruct.class` is valid without adding backticks `class`, but as mentioned above, |
| 51 | + /// backticks need to be added when `class` is used as a variable name. |
| 52 | + /// |
| 53 | + /// - SeeAlso: ``SwiftParser/IdentifierCheckContext`` |
| 54 | + public func isValidSwiftIdentifier(for context: IdentifierCheckContext) -> Bool { |
| 55 | + switch context { |
| 56 | + case .variableName: |
| 57 | + return isValidVariableName(self) |
| 58 | + case .memberAccess: |
| 59 | + return isValidMemberAccess(self) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +private func isValidVariableName(_ name: String) -> Bool { |
| 65 | + var parser = Parser("var \(name)") |
| 66 | + let decl = DeclSyntax.parse(from: &parser) |
| 67 | + guard parser.at(.endOfFile) else { |
| 68 | + // We didn't parse the entire name. Probably some garbage left in the name, so not an identifier. |
| 69 | + return false |
| 70 | + } |
| 71 | + guard !decl.hasError && !decl.hasWarning else { |
| 72 | + // There were syntax errors in the source code. So not valid. |
| 73 | + return false |
| 74 | + } |
| 75 | + guard let variable = decl.as(VariableDeclSyntax.self) else { |
| 76 | + return false |
| 77 | + } |
| 78 | + guard let identifier = variable.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier else { |
| 79 | + return false |
| 80 | + } |
| 81 | + guard identifier.rawTokenKind == .identifier else { |
| 82 | + // We parsed the name as a keyword, eg. `self`, so not a valid identifier. |
| 83 | + return false |
| 84 | + } |
| 85 | + guard identifier.rawText.count == name.utf8.count else { |
| 86 | + // The identifier doesn't cover all the characters in `name`, so we parsed |
| 87 | + // some of these characters into trivia or another token. |
| 88 | + // Thus, `name` is not a valid identifier. |
| 89 | + return false |
| 90 | + } |
| 91 | + return true |
| 92 | +} |
| 93 | + |
| 94 | +private func isValidMemberAccess(_ name: String) -> Bool { |
| 95 | + var parser = Parser("t.\(name)") |
| 96 | + let expr = ExprSyntax.parse(from: &parser) |
| 97 | + guard parser.at(.endOfFile) else { |
| 98 | + // We didn't parse the entire name. Probably some garbage left in the name, so not an identifier. |
| 99 | + return false |
| 100 | + } |
| 101 | + guard !expr.hasError && !expr.hasWarning else { |
| 102 | + // There were syntax errors in the source code. So not valid. |
| 103 | + return false |
| 104 | + } |
| 105 | + guard let memberAccess = expr.as(MemberAccessExprSyntax.self) else { |
| 106 | + return false |
| 107 | + } |
| 108 | + let identifier = memberAccess.declName.baseName |
| 109 | + guard identifier.rawTokenKind == .identifier else { |
| 110 | + // We parsed the name as a keyword, eg. `self`, so not a valid identifier. |
| 111 | + return false |
| 112 | + } |
| 113 | + guard identifier.rawText.count == name.utf8.count else { |
| 114 | + // The identifier doesn't cover all the characters in `name`, so we parsed |
| 115 | + // some of these characters into trivia or another token. |
| 116 | + // Thus, `name` is not a valid identifier. |
| 117 | + return false |
| 118 | + } |
| 119 | + return true |
| 120 | +} |
0 commit comments