-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent Unrelated Casts on Child Choice Node
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SyntaxSupport | ||
|
||
@MemberBlockItemListBuilder | ||
func choiceNodeCastingMethods(for syntaxNodeKind: SyntaxNodeKind) -> MemberBlockItemListSyntax { | ||
if syntaxNodeKind.isBase { | ||
DeclSyntax( | ||
""" | ||
public func `is`<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> Bool { | ||
return self.as(syntaxType) != nil | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public func `as`<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> S? { | ||
return S.init(self) | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public func cast<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> S { | ||
return self.as(S.self)! | ||
} | ||
""" | ||
) | ||
} else { | ||
DeclSyntax( | ||
""" | ||
public func `is`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> Bool { | ||
return self.as(syntaxType) != nil | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public func `as`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType)? { | ||
return \(syntaxNodeKind.syntaxType).init(self) | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public func cast(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType) { | ||
return self.as(\(syntaxNodeKind.syntaxType).self)! | ||
} | ||
""" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters