Skip to content

Support construction of DeclSyntax subtypes from string interpolation #2032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 8, 2023

Allow the creation of e.g. an ExtensionDeclSyntax as

let extension = try ExtensionDeclSyntax("extension Foo: Proto {}")

The initializer throws if the string describes a declaration other than an 'extension'. If the string can get parsed as an extension but contains syntax errors, we return a node that contains syntax errors. This matches the behavior of the HasTrailingCodeBlock intializers that take a header + result builder.

Allow the creation of e.g. an `ExtensionDeclSyntax` as

```swift
let extension = try ExtensionDeclSyntax("extension Foo: Proto {}")
```

The initializer throws if the string describes a declaration other than an 'extension'. If the string can get parsed as an extension but contains syntax errors, we return a node that contains syntax errors. This matches the behavior of the `HasTrailingCodeBlock` intializers that take a header + result builder.
@ahoppen ahoppen requested review from rintaro and bnbarham August 8, 2023 20:33
@ahoppen
Copy link
Member Author

ahoppen commented Aug 8, 2023

@swift-ci Please test

Comment on lines +20 to +21
public protocol DeclSyntaxParseable: DeclSyntaxProtocol {}
public extension DeclSyntaxParseable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason init(_ stringInterpolation: SyntaxNodeString) can't be just in extension DeclSyntaxProtocol?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because, for example, AccessorDeclSyntax does not go through parseDecl, it has its own parser entry function. And thus, trying to call parser.parseDecl and casting it to AccessorDeclSyntax wouldn’t work.

@ahoppen ahoppen merged commit 1e8b158 into swiftlang:main Aug 9, 2023
@ahoppen ahoppen deleted the ahoppen/extensiondecl-from-string branch August 9, 2023 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants