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

Fix rule of fill violations in utils and tests #16656

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

Conversation

seiyab
Copy link
Sponsor Contributor

@seiyab seiyab commented Sep 11, 2024

Description

#16582

Do we need changelog for change in stripTrailingHardline()?

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

Comment on lines -75 to -86
/*
This is not really `cleanDoc` from `src/document/utils.js`
But if we pass array to it, it will flat array
*/
const cleanDoc = (parts) => parts.flat();
expect(
await formatDoc(
// The argument of fill must not be passed to cleanDoc because it's not a doc
fill(cleanDoc(["foo", literalline, "bar"])), // invalid fill
),
).toBe('fill(["foo", literallineWithoutBreakParent, breakParent, "bar"])');

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 considering this test case doesn't make sense. I'll add some tests if we need alternative tests.

@fisker fisker self-requested a review September 11, 2024 23:45
@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Sep 12, 2024

Oops, a test case I recently added might be somewhat fragile. I'll fix it (increase relativeMargin) if the problem repeats.

FAIL tests/unit/doc-printer.js
`printDocToString` has linear time complexity at most to print fill() > numWords=20000
    Error: expect(received).toBeLessThan(expected)
    
    Expected: < 41.39885533368215
    Received:   41.98062500008382

@fisker
Copy link
Member

fisker commented Sep 12, 2024

Though we expect "fill" to have odd length, but we didn't throw for violations, we even did wrong things in core, so plugins should already made mistakes, we can't force them to follow the strict rule, at least not a good time.

I know I said add assert for "fill", but it should only run in dev mode.

For "stripTrailingHardline", instead of remove "hardline" from parts directly, maybe we can replace it with empty string. Do you think it will work?

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Sep 12, 2024

so plugins should already made mistakes

Yes, I agree with you, they shuold.

However, maybe the comment misses an important point. We don't (won't) expect fill() to have odd elements. The rule we are working on is that even-th (1-indexed) elements have to be line-like doc.
What I'm worrying about stripTrailingHardline() is, change in behavior.
It's not easy to demonstrate the change simply. New stripTrailingHardline() always output valid fill() when input is valid fill(). The one in main can output invalid fill() for valid fill(). The behaviour will change, anyway.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Sep 12, 2024

And I found that new logic might be able to get simpler 😅 . I was somewhat confused and it might confused you. I'll see it later.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Sep 13, 2024

I reverted some commits to get this PR to be conservative.
We can discuss whether we let current stripTrailingHardline() go with seiyab@8565e91 or fix stripTrailingHardline(), later.

@fisker
Copy link
Member

fisker commented Sep 14, 2024

seiyab/prettier@8565e91 seemsgood, but it should not need call cleanDoc since the parts already "clean"ed.

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.

2 participants