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

Stabilize member chain with empty line #15522

Merged

Conversation

seiyab
Copy link
Sponsor Contributor

@seiyab seiyab commented Oct 19, 2023

Description

Fix #15435

Fortunately, this doesn't conflict with #15171.
I actually implemented this on the top of #15171 (99d389d) and rebased it into master without seeing any problems.

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

@seiyab

This comment has been minimized.

if (
groups.length <= cutoff &&
!nodeHasComment &&
!groups.some((g) => g.at(-1).hasTrailingEmptyLine)
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I'm not sure whether it should be something like group.some(willBreak) as

printedGroups.slice(0, -1).some(willBreak) ||
.
Probably it will affect code like following:

this.bar(a).baz(
  <ul>
    <li>a</li>
  </ul>,
);

Copy link
Member

Choose a reason for hiding this comment

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

Can you add test for it?

Copy link
Member

Choose a reason for hiding this comment

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

Should we name the element as printedNodes?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

Ah, I was wrong. Trying to add test for it, I found it isn't affected by groups.some((g) => g.at(-1).hasTrailingEmptyLine) vs willBreak(oneLine) (group.some(willBreak) that I suggested isn't correct).
Changing groups.some((g) => g.at(-1).hasTrailingEmptyLine) into willBreak(oneLine), some existing test are affected.

Example

stable and this PR

function f() {
  return this._getWorker(workerOptions)({
    filePath,
    hasteImplModulePath: this._options.hasteImplModulePath,
  }).then((metadata) => {
    // \`1\` for truthy values instead of \`true\` to save cache space.
    fileMetadata[H.VISITED] = 1;
    const metadataId = metadata.id;
    const metadataModule = metadata.module;
    if (metadataId && metadataModule) {
      fileMetadata[H.ID] = metadataId;
      setModule(metadataId, metadataModule);
    }
    fileMetadata[H.DEPENDENCIES] = metadata.dependencies || [];
  });
}

willBreak(oneLine)

function f() {
  return this._getWorker(workerOptions)({
    filePath,
    hasteImplModulePath: this._options.hasteImplModulePath,
  })
    .then((metadata) => {
      // \`1\` for truthy values instead of \`true\` to save cache space.
      fileMetadata[H.VISITED] = 1;
      const metadataId = metadata.id;
      const metadataModule = metadata.module;
      if (metadataId && metadataModule) {
        fileMetadata[H.ID] = metadataId;
        setModule(metadataId, metadataModule);
      }
      fileMetadata[H.DEPENDENCIES] = metadata.dependencies || [];
    });
}

I'd like to hear your opinion about

  • whether we should find and add test case specifically focusing on it.
    • Just for information: Zod looks good library to search for practical method chains.
  • which is better groups.some((g) => g.at(-1).hasTrailingEmptyLine) vs willBreak(oneLine)
    • IMO, groups.some((g) => g.at(-1).hasTrailingEmptyLine) looks slightly better because:

For naming, another variable is already named as printedNodes. I'm sorry but probably my mistake (group.some(willBreak), which is not correct) confused you.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

@fisker Could you respond this, please?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for been busy, will take a look.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I just worried you might have missed notification. There's no hurry. Thank you.

Copy link
Member

Choose a reason for hiding this comment

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

It's best to avoid adding more uses of willBreak. Eventually, the existing use should be somehow removed from the member chain printing code too because it causes #2803.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Oct 20, 2023

I found following inconsistency around here and this PR doesn't resolve it. Anyway, this PR resolves idempotency issue #15435. I don't matter closing this PR if member consider it needs discussion.

Prettier 3.0.3
Playground link

--parser typescript

Input:

new Set()
  
  .add(5)
  .add("some text");

new Set()  
  .add(5)

  .add("some text");

Output:

new Set().add(5).add("some text");

new Set()
  .add(5)

  .add("some text");

@seiyab seiyab marked this pull request as ready for review October 20, 2023 14:57
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.

LGTM from the result.

@thorn0
Copy link
Member

thorn0 commented Dec 10, 2023

These changes don't look good prettier/prettier-regression-testing#342 (comment)

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Dec 11, 2023

@thorn0
It's because this branch wasn't up-to-date. The difference comes from #15547.
I added test and merge latest main.

@fisker
Copy link
Member

fisker commented Dec 13, 2023

Maybe the empty line only makes sense when the chain is long enough.

@fisker fisker merged commit 224f70f into prettier:main Dec 13, 2023
28 checks passed
@seiyab seiyab deleted the 15435-stabilize-member-chain-with-empty-line branch December 13, 2023 08:14
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 17, 2024
Co-authored-by: fisker Cheung <lionkay@gmail.com>
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.

Unstable formatting for chained calls
3 participants