Skip to content
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

Make long parenthesized expressions not always break. #1115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

belav
Copy link
Owner

@belav belav commented Jan 7, 2024

closes #921

The goal of this ticket was to prefer breaking trailing members on a parenthesized expression before breaking the expression.
For example.

// input
(someObject as SomeLongType__________________________________________).CallMethod();

// expected
(someObject as SomeLongType__________________________________________)
    .CallMethod();

// actual
(
    someObject as SomeLongType__________________________________________
).CallMethod();

The way it is currently implemented though results in the following

var asyncMethodsWithToken = (
    from method in asyncMethods
    where method.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken))
    select method
)
    .ToList();

// previously
var asyncMethodsWithToken = (
    from method in asyncMethods
    where method.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken))
    select method
).ToList();

This does mean things are more consistent with longer chains

var asyncMethodsWithToken = (
    from method in asyncMethods
    where method.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken))
    select method
)
    .ToList________________________()
    .ToList________________________()
    .ToList________________________();

Another example where I think the old version was probably better

Assert.Equal(
    CoreStrings.FindValueTypeMismatch(0, "IntKey", "string", "int"),
    (
        await Assert.ThrowsAsync<ArgumentException>(
            () =>
                Finder
                    .FindAsync<IntKey>(cancellationType, context, new object[] { "77" })
                    .AsTask()
        )
    )
        .Message
);

// previously
Assert.Equal(
    CoreStrings.FindValueTypeMismatch(0, "IntKey", "string", "int"),
    (
        await Assert.ThrowsAsync<ArgumentException>(
            () =>
                Finder
                    .FindAsync<IntKey>(cancellationType, context, new object[] { "77" })
                    .AsTask()
        )
    ).Message
);

I'm not sure how much work it would be to keep the tailing call next to the ) if the contents of the parenthsized expression break.

FWIW prettier does not have any logic around this.

@shocklateboy92
Copy link
Collaborator

Do we have a group type that breaks if its children are longer than its siblings' children?

@belav
Copy link
Owner Author

belav commented Jan 12, 2024

Do we have a group type that breaks if its children are longer than its siblings' children?

No and the way things work I don't know if it would be possibly.

A ConditionalGroup may accomplish something like that. If the first possible group breaks the children and does not break the siblings and is considered to fit, then it gets printed.

@shocklateboy92 shocklateboy92 enabled auto-merge (squash) January 24, 2024 05:14
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.

ParenthesizedExpression breaking inconsistently
2 participants