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

Refactor, optimize and fix call args expansion #13341

Merged
merged 14 commits into from Aug 29, 2022
Merged

Conversation

thorn0
Copy link
Member

@thorn0 thorn0 commented Aug 22, 2022

Description

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0 thorn0 marked this pull request as draft August 22, 2022 09:36
@thorn0 thorn0 linked an issue Aug 22, 2022 that may be closed by this pull request
@thorn0 thorn0 changed the title Refactor, optimize (and probably fix) call args expansion Refactor, optimize and fix call args expansion Aug 22, 2022
@thorn0 thorn0 linked an issue Aug 22, 2022 that may be closed by this pull request
// https://github.com/prettier/prettier/issues/2456
// https://github.com/prettier/prettier/issues/5172
// A proper (printWidth-aware) fix for those would require a complex change in the doc printer.
!secondArg?.left?.left &&
Copy link
Member

Choose a reason for hiding this comment

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

Because this check didn't pass?

Prettier pr-13341
Playground link

--parser babel

Input:

call(function () {
  return 1;
}, $var ||
  ($var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  "test"));

call(function () {
  return 1;
}, $var ??
  ($var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  $var ??
  "test"));

Output:

call(function () {
  return 1;
}, $var || ($var ?? $var ?? $var ?? $var ?? $var ?? $var ?? $var ?? $var ?? "test"));

call(
  function () {
    return 1;
  },
  $var ??
    $var ??
    $var ??
    $var ??
    $var ??
    $var ??
    $var ??
    $var ??
    $var ??
    "test",
);

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 this check didn't pass?

Yep. It's a hack, sure, but it does the trick for most cases.
What would you suggest? Should we add && !secondArg?.right?.right?
I decided not to touch #12892 and #4401 for now.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just check if it's LogicalExpression chain for now? Unless you mean to match other types with left.

Copy link
Member

Choose a reason for hiding this comment

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

secondArg?.right?.right will match a = b = c

Copy link
Member Author

Choose a reason for hiding this comment

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

secondArg?.right?.right will match a = b = c

That would be perfectly okay.

Unless you mean to match other types with left.

Yes, I mean to match binary expression that are more complex than this:

setTimeout(() => {
   // ...
}, 40 * rate);

Copy link
Member

@fisker fisker Aug 22, 2022

Choose a reason for hiding this comment

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

We have full list of those nodes with left, right, you can add more tests for them.

exports[`visitor keys estree 1`] = `

@thorn0
Copy link
Member Author

thorn0 commented Aug 24, 2022

An Interesting case fixed by this PR. I guess the body of the function was printed in MODE_FLAT.

Prettier pr-13341
Playground link

--parser babel

Input:

fs.readdirSync(suiteLoc).forEach(function (testName) {
  (skip ? it.skip : it)(
    testName,
    buildTest(binName, testName, opts),
    2_000_000,
  );
});

{
  (skip ? it.skip : it)(
    testName,
    buildTest(binName, testName, opts),
    2_000_000,
  );
}

Output:

fs.readdirSync(suiteLoc).forEach(function (testName) {
  (skip ? it.skip : it)(
    testName,
    buildTest(binName, testName, opts),
    2_000_000,
  );
});

{
  (skip ? it.skip : it)(
    testName,
    buildTest(binName, testName, opts),
    2_000_000,
  );
}

Prettier 2.7.1
Playground link

Output:

fs.readdirSync(suiteLoc).forEach(function (testName) {
  (skip
    ? it.skip
    : it)(testName, buildTest(binName, testName, opts), 2_000_000);
});

{
  (skip ? it.skip : it)(
    testName,
    buildTest(binName, testName, opts),
    2_000_000
  );
}

@thorn0 thorn0 marked this pull request as ready for review August 26, 2022 15:32
@thorn0 thorn0 requested a review from fisker August 29, 2022 09:26
Copy link
Member

@fisker fisker left a comment

Choose a reason for hiding this comment

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

The output looks good 👍 .

Code in this file is a nightmare to understand (not because of this PR, it already is) 😢 .

@thorn0
Copy link
Member Author

thorn0 commented Aug 29, 2022

In this PR, I tried to make it easier to follow what's going on. At least I untangled first arg expansion from last arg expansion. Now those are separate code paths.

@fisker
Copy link
Member

fisker commented Aug 29, 2022

A quick search we have 130+ places of CallExpression, and 100+ places used MemberExpression.
If someone want to implement #10244, he will get big headache.

@thorn0
Copy link
Member Author

thorn0 commented Aug 29, 2022

It's going to be very similar to adding support for TSNonNullExpression, which Prettier already supports (almost?) everywhere it handles member chains.

@thorn0 thorn0 merged commit 41ff44e into prettier:next Aug 29, 2022
@thorn0 thorn0 deleted the call-args branch August 29, 2022 11:37
@fisker fisker mentioned this pull request Mar 8, 2023
@kachkaev kachkaev added this to the 3.0 milestone Mar 8, 2023
@fisker fisker mentioned this pull request May 9, 2023
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 18, 2024
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 31, 2024
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 31, 2024
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants