-
Notifications
You must be signed in to change notification settings - Fork 470
Add ProtocolDecl test cases
#356
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ | |
| 'BinaryOperatorExpr', | ||
| 'DeclModifier', | ||
| 'IdentifierExpr' | ||
| ], | ||
| 'TypeBuildable': [ | ||
| 'ReturnClause' | ||
| ] | ||
| } | ||
|
|
||
This file contains hidden or 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,29 @@ | ||
| import XCTest | ||
| import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
|
|
||
| final class ProtocolDeclTests: XCTestCase { | ||
| func testProtocolDecl() { | ||
| let returnType = ArrayType(elementType: "DeclSyntax") | ||
| let input = ParameterClause(parameterListBuilder: { | ||
| FunctionParameter(firstName: .identifier("format"), colon: .colon, type: "Format", trailingComma: .comma, attributesBuilder: {}) | ||
| FunctionParameter(firstName: .identifier("leadingTrivia"), colon: .colon, type: OptionalType(wrappedType: "Trivia"), attributesBuilder: {}) | ||
| }) | ||
| let functionSignature = FunctionSignature(input: input, output: returnType) | ||
|
|
||
| // FIXME: We need to add the `modifiersBuilder` with a non-empty value, otherwise will the builder omit newline. | ||
| let functionDecl = FunctionDecl(identifier: .identifier("buildDeclList"), signature: functionSignature, modifiersBuilder: { TokenSyntax.public }) | ||
| let buildable = ProtocolDecl(modifiers: TokenSyntax.public, identifier: .identifier("DeclListBuildable"), members: functionDecl) | ||
|
|
||
| let syntax = buildable.buildSyntax(format: Format()) | ||
|
|
||
| var text = "" | ||
| syntax.write(to: &text) | ||
|
|
||
| XCTAssertEqual(text, """ | ||
| public protocol DeclListBuildable{ | ||
| public func buildDeclList(format: Format, leadingTrivia: Trivia?)-> [DeclSyntax] | ||
| } | ||
| """) | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
attributesBuilderargument necessary? Shouldn’t it be defaulted?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should yes.
But the problem is that we then get
Ambiguous use of 'init'.I think we should, somehow, solve this as there is many
initmethods that look like each other so we need to do things like this to avoid the error. It works now, but I think it makes some noice/cluttered code when using it.This is a long shot, and I'm not sure if it is technically possible, but I'll try to explain.
It would be nice if we could remove this type of
initmethods, where the convenienceinitmethod only have builders as different as the non-convenienceinit.Like the one here: https://github.com/apple/swift-syntax/blob/caa735a5a5c41f4fc1769e5c34bb2cde17157400/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift#L5676-L5721
This will mean, in this case, that our
AttributeListBuilderneed to conform toExpressibleAsAttributeList.And then we could things like
FunctionParameter(modifiers: { TokenSyntax.public }, firstName: .identifier("format"), colon: .colon, type: "Format", trailingComma: .comma)orFunctionParameter(modifiers: TokenSyntax.public, firstName: .identifier("format"), colon: .colon, type: "Format", trailingComma: .comma).I think we before have talked about that the
@resultBuildersshould conform to some of the protocols.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, makes sense 👍 I’m good with merging this as-is.
As to your other question:
If I understand you correctly, you are saying that you’d like to remove the second initializer listed in the code snippet and make
AttributeListBuilderconform toExpressibleAsAttributeList. In that case you expect to be able to use theAttributeListBuilderresult builder wherever anExpressibleAsAttributeListis expected, right?If this is what you’re suggesting, I’m pretty sure it won’t work. I think the result builder needs to be declared using
@AttributeListBuilderon the parameter or otherwise the type checker won’t try to apply it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay! Thanks, than it is this way :)
Did you see my question here too:
#356 (comment)