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

Regression: origStart is not accurate in some cases #126

Closed
ikatyang opened this issue Oct 1, 2019 · 4 comments
Closed

Regression: origStart is not accurate in some cases #126

ikatyang opened this issue Oct 1, 2019 · 4 comments
Labels
bug Something isn't working

Comments

@ikatyang
Copy link
Contributor

ikatyang commented Oct 1, 2019

version: 1.7.0 (minimal reproducible version: 1.2.0)

const YAML = require("yaml");

const text = `
a: #b

`.replace(/\n/g, "\r\n");

const cst = YAML.parseCST(text);

cst.setOrigRanges();

console.log(
  JSON.stringify(text[cst[0].contents[1].items[2].props[0].origStart])
); // expected "#" but got "\r"
@ikatyang ikatyang changed the title origStart is not accurate in some cases Regression: origStart is not accurate in some cases Oct 1, 2019
@eemeli eemeli added the bug Something isn't working label Oct 2, 2019
@eemeli
Copy link
Owner

eemeli commented Oct 2, 2019

Something weird is happening with the BlankLine node there; not sure if the issues are related. But I find it surprising that it's before rather than after the CollectionItem. Will investigate.

@eemeli
Copy link
Owner

eemeli commented Oct 3, 2019

Bother, the issues are indeed related, and they do get a bit complicated. There are three things going on here that are, when put together, a bit wrong:

  • CollectionItem is too eager to generate BlankLine nodes, in particular for empty map values. Sometimes, this may currently lead to one blank line being expressed twice in the CST.
  • Comments may exist in the CST both as independent nodes and as node props; comments after the : but before the value currently end up as props of the CollectionItem. Blank lines on the other hand are never props, and if they're after a CollectionItem comment but before its value or another comment, they have no better place to go int the CST than before the node's entry.
  • When setOrigRanges() is run, it doesn't handle well non-monotonously increasing ranges.

So something's got to give here. I can fix most of the bugginess without any deeper changes, but that'll still leave some problematic cases where it's possible to end up with BlankLine nodes that have ranges that start after the start location of subsequent nodes. I'm pretty sure that this is isolated to map values, but that's because blank lines between most other prop comments & values are currently dropped.

Now, there are three possible ways to fix this, and it's not obvious to me which one to go with:

  1. Stop detecting blank lines between map value comments or its comment & value.
  2. Also allow blank lines to exist as CST node props, and put them there for map values.
  3. For map value comments, add them to the collection as independent nodes rather than props.

I'm leaning towards the third option right now, but I'll at least sleep on this before picking a way forward.

@eemeli eemeli closed this as completed in b8709e2 Oct 4, 2019
@eemeli
Copy link
Owner

eemeli commented Oct 4, 2019

I went with the first option, at least for now. Blank lines that could appear between comments or a value and a preceding comment are in any case ignored by the AST level, and this should fix the actual bugs that were identified.

Strictly speaking option 2 might've required a major version bump, as it would add a new prop type for nodes. Option 3 might work, but there's some special handling for map value comments at the AST level that breaks if the comments are nodes rather than props, as well as issues that come up with comments in compact in-line notation cases, where the reference to the surrounding item array is more complicated. All that added complexity just isn't worth it.

@eemeli
Copy link
Owner

eemeli commented Oct 7, 2019

Fix included in patch release 1.7.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants