Skip to content

Commit de92a86

Browse files
authored
Merge pull request swiftlang#39 from nkcsgexi/isunknown
Add an API for unknown checking.
2 parents 2d032c5 + 6a295a9 commit de92a86

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Sources/SwiftSyntax/Syntax.swift

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ extension Syntax {
105105
return raw.kind.isPattern
106106
}
107107

108+
/// Whether or not this node represents an unknown node.
109+
public var isUnknown: Bool {
110+
return raw.kind.isUnknown
111+
}
112+
108113
/// The parent of this syntax node, or `nil` if this node is the root.
109114
public var parent: Syntax? {
110115
guard let parentData = data.parent else { return nil }

Sources/SwiftSyntax/SyntaxKind.swift.gyb

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public enum SyntaxKind: String, Codable {
4545
% end
4646
% end
4747

48+
public var isUnknown: Bool {
49+
switch self {
50+
% for name, nodes in grouped_nodes.items():
51+
% if name not in ["Syntax", "SyntaxCollection"]:
52+
case .unknown${name}: return true
53+
% end
54+
% end
55+
case .unknown: return true
56+
default: return false
57+
}
58+
}
59+
4860
public init(from decoder: Decoder) throws {
4961
let container = try decoder.singleValueContainer()
5062
let kind = try container.decode(String.self)

Tests/SwiftSyntaxTest/SyntaxFactory.swift

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
2222
("testGenerated", testGenerated),
2323
("testTokenSyntax", testTokenSyntax),
2424
("testFunctionCallSyntaxBuilder", testFunctionCallSyntaxBuilder),
25+
("testUnknownSyntax", testUnknownSyntax),
2526
]
2627

2728
public func testGenerated() {
@@ -115,4 +116,11 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
115116
XCTAssertEqual("\(callWithTerminator)",
116117
"print(\"Hello, world!\", terminator: \" \")")
117118
}
119+
120+
public func testUnknownSyntax() {
121+
let expr = SyntaxFactory.makeStringLiteralExpr("Hello, world!")
122+
XCTAssertFalse(expr.isUnknown)
123+
XCTAssertTrue(SyntaxFactory.makeUnknownSyntax(
124+
tokens: [SyntaxFactory.makeLeftBraceToken()]).isUnknown)
125+
}
118126
}

0 commit comments

Comments
 (0)