-
-
Notifications
You must be signed in to change notification settings - Fork 131
preserve empty block literals #634
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks decent, though your PR description is very LLM-ish in its verbosity.
Please add tests to cover your change, and see the inline comment.
src/stringify/stringifyString.ts
Outdated
@@ -201,7 +201,7 @@ function blockString( | |||
: type === Scalar.BLOCK_LITERAL | |||
? true | |||
: !lineLengthOverLimit(value, lineWidth, indent.length) | |||
if (!value) return literal ? '|\n' : '>\n' | |||
if (!value) return literal ? '|-\n' : '>\n' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? Given how both |
and |-
with only whitespace parse as the empty string, there's no need for the -
indicator.
Yes, from a parsing perspective both | and |- parse to the same empty string. Would you suggest a different approach for handling this type of empty block literal case? |
When does And doesn't your proposed fix make it so that an empty |
Thanks for the follow-up and for confirming the current behavior. What we're trying to fixToday: Both Goal: Round-tripping (parse → stringify) should preserve the user's exact syntax, including trailing Your question: "When does Our initial approaches1. Removing the empty-value test// original
if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value))
// attempt
if (!blockQuote || /\n[\t ]+$/.test(value)) keeping: if (!value) return literal ? '|\n' : '>\n' Result: 2. Always defaulting to
|
I continue to have a really strong sense that I'm interacting here with an LLM here, and I'd really rather not. In fact, this PR inspired me to add this to the contribution guide:
With that in mind, please clarify if you're using an LLM to write your comments. And going forward, I'd be much more willing to consider your ideas if you represent them directly (even if it's in bad English, I honestly don't mind) rather than if I have to presume they've been generated by an LLM. |
We're kind of block on this issue and not very versed with the library, so we have been quite detailed in explaining our requirements in the PR, which might come across as Llmish. We're looking for suggestions on how to move forward. Going forward, we'll keep our explanations more concise. |
We have spent the last three days trying to understand the library to provide a fix and validated test case scenarios for this PR. If this PR is being rejected mainly due to the tone or what seems like LLM-ish verbosity, please do let us know. It seems that the style of the explanation is getting more attention than the actual problem, especially when it is blocking us. We are blocked due to an issue in the library which we had already explained and want to unblock ourselves with the provided fix. If the fix itself needs work, we are happy to discuss and improve it. If it's mainly about the writing and the tone, let us know so we can revise and resubmit the PR with a more natural tone to avoid rejection over verbosity. |
I'm supportive of keeping a block literal as a block literal, even if it's empty. I'm not convinced that there's sufficient value in keeping track of what block chomping indicator was used (esp. as we're also not keeping track of the block indentation indicator). On these, the spec specifically mentions:
And so since the The Document level API of this library pretty explicitly does not retain all of the details of the source, and so needs to leave out some things. The chomping indicator used on an empty block literal is such a detail. Note though that the CST API does support such details, and is provided for users with a need to control things at such a fine-grained level. Would that provide a solution for you? |
Thanks for the insights and clarification on the Document level API limitations. We will revise our code to remove the - indicator from the condition while keeping the block literal format. We will also explore the CST API for finer control. |
we have removed the '-' indicator from empty block literal as discussed. The block literal format is still retained. Please review. |
Yeah, that should work. Please add tests to verify that the behaviour is as expected. |
Hi @eemeli, I have added test cases to verify the behavior with my changes. |
Changes have been incorporated. Please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine. Not 100% happy about the trailing \n\n
for top-level block scalars, but that's not really that significant.
Fix: Preserve empty block literals (
|-
) instead of converting to empty stringsDescription
This PR fixes the issue where empty block literals using the
|-
chomping indicator are incorrectly stringified as quoted empty strings (''
) instead of retaining their original block literal formatting.Currently, this input:
is stringified as:
Related Issue
Fixes #630
What’s the Fix?
The following condition in
blockString()
was:This logic caused empty or whitespace-only block scalars to fallback to quoted strings (
''
), even when the user clearly wrote a|-
block scalar.Updated To:
Key Change:
^\s*$
which incorrectly matched valid empty block literals.|-
instead of|
to match typical YAML idioms and avoid unexpected trailing newlines.Result
With this fix, the following roundtrip works as expected:
After parse → stringify:
✅ Block literal preserved
Additional Notes