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

Ensure lineWidth=0 is respected for block literal vs folded #232

Merged
merged 1 commit into from
Mar 1, 2021

Conversation

muff1nman
Copy link
Contributor

No description provided.

@eemeli
Copy link
Owner

eemeli commented Feb 26, 2021

Is this missing a description accidentally or on purpose? I can't immediately see what issue this is solving, and some discussion would be rather welcome.

@muff1nman
Copy link
Contributor Author

I believe the title adequately describes the issue at hand but let me put the comparison from the test inline here so we can discuss:

Previous behavior with lineWidth=0

lorem: >
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.
  
  Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti.

Desired behavior (and should match lineWidth > actual content i.e. 1000)

lorem: |
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.
  
  Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti.

Not how the content is nearly identical except the block literal is wrong in the first leading to missing newlines when actually parsed.

@eemeli
Copy link
Owner

eemeli commented Feb 26, 2021

So the folding is producing incorrect output in some circumstances? Could you show an example with JS code that I can execute somewhere that shows this invalid case producing broken output without the fixes you're looking to apply here?

@muff1nman
Copy link
Contributor Author

Okay after rereading some sections of the yaml spec, I misspoke when I said "wrong". Rather the behavior here is just not inline with the desired behavior of what should happen when lineWidth is 0 (or negative). As seen in the following, when lineWidth is zero, currently you still get a folded block, rather than the desired literal block:

var YAML = require("yaml");
var src = {
    lorem: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. \nCras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.\n\nDuis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti. \n'
};
YAML.scalarOptions.str.fold.lineWidth = 0;
var asYaml = YAML.stringify(src);
console.log(asYaml);

leads to:

lorem: >
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. 

  Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.


  Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti. 

@muff1nman muff1nman changed the title Ensures the actual block literal matches the folding behavior Ensure lineWidth=0 is respected for block literal vs folded Feb 26, 2021
@muff1nman
Copy link
Contributor Author

I've edited the title and will edit the commit message to match

@muff1nman muff1nman force-pushed the ensureFoldingBehaviorMatches branch from 6521db9 to 608781e Compare February 27, 2021 00:00
@eemeli
Copy link
Owner

eemeli commented Feb 27, 2021

Ah, now I get it. Yeah, okay, it does make sense to default to literal rather than folded block scalars when lineWidth has been set to 0.

But there shouldn't be a need to involve the folding code into this logic. Why not just add a condition here to the type === Type.BLOCK_LITERAL check? Would make sense to first extract lineWidth to a local variable, as it's then referred to more than once.

const literal =
type === Type.BLOCK_FOLDED
? false
: type === Type.BLOCK_LITERAL
? true
: !lineLengthOverLimit(value, strOptions.fold.lineWidth - indent.length)

@muff1nman
Copy link
Contributor Author

Sure I can see how the folding code should be separate, but I was trying to keep the lineWidth 0/negative logic in one place. I also thought that the lineLengthOverLimit function was a better spot to nest this logic as it is in line with the functions name/purpose and the double ternary already seemed complicated enough.

@muff1nman
Copy link
Contributor Author

I'd be fine with reverting the foldFlowLines changes and replicating the same logic to the lineLengthOverLimit function. Does that sound reasonable?

@eemeli
Copy link
Owner

eemeli commented Feb 27, 2021

Yeah, that sounds like a good plan. Also note that I finally got this code ported to TypeScript just now; hopefully that won't make it difficult for you to apply these changes to the .ts version of the file.

@muff1nman muff1nman force-pushed the ensureFoldingBehaviorMatches branch from 608781e to 327c67d Compare February 27, 2021 19:02
@eemeli eemeli merged commit d34e48c into eemeli:master Mar 1, 2021
@eemeli
Copy link
Owner

eemeli commented Mar 1, 2021

Thank you!

mergify bot referenced this pull request in projen/projen Mar 15, 2021
Bumps [yaml](https://github.com/eemeli/yaml) from 1.10.0 to 1.10.2.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/eemeli/yaml/releases">yaml's releases</a>.</em></p>
<blockquote>
<h2>v1.10.2</h2>
<ul>
<li>Allow for unindented comment after node props (prettier/prettier#10510)</li>
</ul>
<h2>v1.10.1</h2>
<p>This release backports the following non-breaking fixes made during the work on <code>yaml@2</code> on top of <code>[email protected]</code>:</p>
<ul>
<li>Support for<code> __proto__</code> as mapping key &amp; anchor identifier (<a href="https://github.com/eemeli/yaml/issues/192">#192</a>)</li>
<li>Fix broken TS type for BigInt toggle</li>
<li>Dump long keys properly (<a href="https://github.com/eemeli/yaml/issues/195">#195</a>)</li>
<li>When folding highly indented lines, require at least <code>minContentWidth</code> chars on the first line (<a href="https://github.com/eemeli/yaml/issues/196">#196</a>)</li>
<li>Fix <code>YAML.stringify()</code> for certain null values (<a href="https://github.com/eemeli/yaml/issues/197">#197</a>)</li>
<li>Do not break escaped chars with escaped newlines (<a href="https://github.com/eemeli/yaml/issues/237">#237</a>, <a href="https://github.com/awslabs/cdk8s/issues/8">awslabs/cdk8s8</a>)</li>
<li>Set <code>type: &quot;module&quot;</code> within browser/dist/ (<a href="https://github.com/eemeli/yaml/issues/208">#208</a>)</li>
<li>Use CommonJS for the browser endpoints <code>yaml/types</code> &amp; <code>yaml/util</code> (<a href="https://github.com/eemeli/yaml/issues/208">#208</a>)</li>
<li>Always stringify non-Node object keys using explicit notation (<a href="https://github.com/eemeli/yaml/issues/218">#218</a>)</li>
<li>Specify node type of <code>Document.Parsed.contents</code> (<a href="https://github.com/eemeli/yaml/issues/221">#221</a>)</li>
<li>Add missing type for CST <code>Node.rangeAsLinePos</code> (<a href="https://github.com/eemeli/yaml/issues/222">#222</a>)</li>
<li>Prefer literal over folded block scalar when <code>lineWidth=0</code> is set (<a href="https://github.com/eemeli/yaml/issues/232">#232</a>)</li>
<li>Allow for empty lines after node props (<a href="https://github.com/eemeli/yaml/issues/242">#242</a>)</li>
<li>Update dev dependencies</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/eemeli/yaml/commit/4cdcde632ece71155f3108ec0120c1a0329a6914"><code>4cdcde6</code></a> 1.10.2</li>
<li><a href="https://github.com/eemeli/yaml/commit/7c0e08316d82f167ac0a054428627f6e1f20ac6e"><code>7c0e083</code></a> Allow for unindented comment after node props (<a href="https://github.com/eemeli/yaml/issues/242">#242</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/8ef015788b219a4b249736f4bb8968dafe68dcc4"><code>8ef0157</code></a> 1.10.1</li>
<li><a href="https://github.com/eemeli/yaml/commit/6296dae2e5f61a4aa4605b4a374bd94ec5713c3a"><code>6296dae</code></a> Update links in docs</li>
<li><a href="https://github.com/eemeli/yaml/commit/b1d2b287e80caeb262c4dc81459f52b982a5e741"><code>b1d2b28</code></a> Allow for empty lines after node props (Fixes <a href="https://github.com/eemeli/yaml/issues/242">#242</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/3e5a64098791ea7a7c01a5465a0794049b511367"><code>3e5a640</code></a> Satisfy Prettier</li>
<li><a href="https://github.com/eemeli/yaml/commit/bd031cb67f4411826bd61cc2b3bbe21b1b398755"><code>bd031cb</code></a> Update dev dependencies + switch to lockfileVersion 2</li>
<li><a href="https://github.com/eemeli/yaml/commit/9c6e7d0ed367b2af439cfc52936e65b2fc3e5ecc"><code>9c6e7d0</code></a> Use CommonJS for browser endpoints yaml/types &amp; yaml/util (<a href="https://github.com/eemeli/yaml/issues/208">#208</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/7ddb18b4e18d4d4625b94af8f64738a4725cbbb5"><code>7ddb18b</code></a> Prefer literal over folded block scalar when lineWidth=0 is set (<a href="https://github.com/eemeli/yaml/issues/232">#232</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/fd817be1774145aec9354a30c4b48cc08fe98e41"><code>fd817be</code></a> Update dev dependencies</li>
<li>Additional commits viewable in <a href="https://github.com/eemeli/yaml/compare/v1.10.0...v1.10.2">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=yaml&package-manager=npm_and_yarn&previous-version=1.10.0&new-version=1.10.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually


</details>
This was referenced Mar 15, 2021
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