Skip to content

Add an initializer that accepts [ExprSyntax] for ArrayExprSyntax #1693

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
May 24, 2023

Conversation

gibachan
Copy link
Contributor

Resolve #1674

I have added initializers for ArrayExprSyntax and ArrayElementListSyntax that allow codes like below

let expressions: [ExprSyntax] = ...
let arrayExprSyntax = ArrayExprSyntax(expressions: expressions)

( I'm not sure if I need to write a test for the ArrayExprSyntax initializer or where to place it. )

@gibachan gibachan requested a review from ahoppen as a code owner May 23, 2023 09:11
@kimdv
Copy link
Contributor

kimdv commented May 23, 2023

Hi @gibachan 👋

The files you have changed are inside a generated folder.
This means that running the code generation they will be reverted. You can read about it here: https://github.com/apple/swift-syntax/blob/main/CodeGeneration/README.md

Also I think the code would fit better inside the SwiftSyntaxBuilder, as this is about building the syntax code.
I think you could add it to ConvenienceInitializers.swift.

You can then add test cases to ArrayExprTests.swift.

@gibachan
Copy link
Contributor Author

Hi @kimdv, thank you so much!

I moved the initializers to ConvenienceInitializers.swift!

@kimdv
Copy link
Contributor

kimdv commented May 23, 2023

@swift-ci please test

@kimdv
Copy link
Contributor

kimdv commented May 23, 2023

Would you mind to squash the two commits @gibachan ? 😁

@gibachan gibachan force-pushed the array-expr-syntax-initializer branch from 53fb44a to 8275367 Compare May 23, 2023 12:40
@gibachan
Copy link
Contributor Author

I did it 👍

@kimdv
Copy link
Contributor

kimdv commented May 23, 2023

@swift-ci please test

// MARK: - ArrayElementList

extension ArrayElementListSyntax {
public init(expressions: [ExprSyntaxProtocol]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should explicitly write any ExprSyntaxProtocol here.

Copy link
Member

Choose a reason for hiding this comment

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

We prefer to use ExprSyntax here. I’m adding some documentation about it in #1696

Copy link
Contributor

Choose a reason for hiding this comment

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

Or that.


extension ArrayElementListSyntax {
public init(expressions: [ExprSyntaxProtocol]) {
let elements = expressions.enumerated().map { index, expression in
Copy link
Contributor

Choose a reason for hiding this comment

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

It's valid for the last element to have a trailing comma in an array literal, so there's no need for enumerated() or index-checking here.

Copy link
Member

Choose a reason for hiding this comment

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

SwiftSyntaxBuilder doesn’t add a trailing comma for the last element, so I’d prefer to be consistent between the two initializers her and also not add a trailing comma here either. So: I’d prefer to keep it as you wrote it @gibachan.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thank you. I’ve got a couple of comments, but overall looks good

// MARK: - ArrayElementList

extension ArrayElementListSyntax {
public init(expressions: [ExprSyntaxProtocol]) {
Copy link
Member

Choose a reason for hiding this comment

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

We prefer to use ExprSyntax here. I’m adding some documentation about it in #1696


extension ArrayElementListSyntax {
public init(expressions: [ExprSyntaxProtocol]) {
let elements = expressions.enumerated().map { index, expression in
Copy link
Member

Choose a reason for hiding this comment

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

SwiftSyntaxBuilder doesn’t add a trailing comma for the last element, so I’d prefer to be consistent between the two initializers her and also not add a trailing comma here either. So: I’d prefer to keep it as you wrote it @gibachan.

public init(expressions: [ExprSyntaxProtocol]) {
let elements = expressions.enumerated().map { index, expression in
let element = ArrayElementSyntax(expression: expression)
return index != (expressions.count - 1) ? element.with(\.trailingComma, .commaToken()) : element
Copy link
Member

Choose a reason for hiding this comment

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

This will override any potentially already existing trailing comma (and trivia associated with it). You should be able to use element.ensuringTrailingComma() instead.

Also, I’d prefer an if to the ternary expression. I just think it reads a lot more easily.

@gibachan gibachan force-pushed the array-expr-syntax-initializer branch from 8275367 to f95fa76 Compare May 23, 2023 23:29
@gibachan
Copy link
Contributor Author

Thank you for all your comments 😄 I have made the fixes!

@@ -47,4 +47,10 @@ final class ArrayExprTests: XCTestCase {
"""
)
}

func testInitializerWithExpressions() {
let expressions: [ExprSyntax] = [.init(literal: 0), .init(literal: 1), .init(literal: 2)]
Copy link
Member

Choose a reason for hiding this comment

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

You could also make this even simpler as follows.

Suggested change
let expressions: [ExprSyntax] = [.init(literal: 0), .init(literal: 1), .init(literal: 2)]
let expressions: [ExprSyntax] = ["0", "1", "2"]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice!

@gibachan gibachan force-pushed the array-expr-syntax-initializer branch from f95fa76 to 50bc963 Compare May 24, 2023 02:02
@gibachan gibachan requested a review from ahoppen May 24, 2023 02:03
@kimdv
Copy link
Contributor

kimdv commented May 24, 2023

@swift-ci please test

Copy link
Contributor

@kimdv kimdv left a comment

Choose a reason for hiding this comment

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

LGTM

@ahoppen
Copy link
Member

ahoppen commented May 24, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge May 24, 2023 17:40
@ahoppen ahoppen merged commit bf18c71 into swiftlang:main May 24, 2023
@gibachan gibachan deleted the array-expr-syntax-initializer branch May 24, 2023 23:52
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.

ArrayExprSyntax initializer that takes an array of ExprSyntax
4 participants