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

YAML: Support other cases of lists of lists #4187

Merged
merged 1 commit into from
May 11, 2024

Conversation

pdulich
Copy link
Contributor

@pdulich pdulich commented May 11, 2024

The commit 264e8e8 introduced support for list of lists of scalars. But this didn't fix lists of lists of other blocks (like mappings). This commit will also fix these cases.

The underlying problem was that some tokens were interpreted twice. This was caused by YamlParser.parseFromInput. This method calculates the prefix of a token by using the substring yamlSource[lastEnd:tokenIdx]. Normally, the characters that were newly interpreted should be marked as used by incrementing the index lastEnd. But for sequences this index wasn't updated in every case. It was only updated if there was an anchor or an openingBracketIndex. Because of this, the prefix was used for multiple consecutive sequences in other cases.
To fix this, update lastEnd in every case for sequences.

The yaml-parser has inconsistent behaviour when parsing sequences with dashes. If a dash-sequence has indentation the start- and the event-mark point to the same character: the dash. If a dash-sequence has no indentation the event-mark points to the character AFTER the dash. Because of this, the lastEnd would get an offset of one which means the YamlParser would skip one character.
Introduce a workaround to correct the lastEnd if this faulty case.

With these changes, every dash is only interpreted once. That means that in the SequenceBuilder.push method the rawPrefix will contain at most one dash. Because of this, the old fix isn't needed anymore. Remove it.

Fixes #4176

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

The commit 264e8e8 introduced support for list of lists of scalars.
But this didn't fix lists of lists of other blocks (like mappings).
This commit will also fix these cases.

The underlying problem was that some tokens were interpreted twice.
This was caused by YamlParser.parseFromInput. This method calculates the
prefix of a token by using the substring yamlSource[lastEnd:tokenIdx].
Normally, the characters that were newly interpreted should be marked as
used by incrementing the index `lastEnd`. But for sequences this index
wasn't updated in every case. It was only updated if there was an anchor
or an openingBracketIndex. Because of this, the prefix was used for
multiple consecutive sequences in other cases.
To fix this, update `lastEnd` in every case for sequences.

The yaml-parser has inconsistent behaviour when parsing sequences with
dashes. If a dash-sequence has indentation the start- and the event-mark
point to the same character: the dash. If a dash-sequence has no
indentation the event-mark points to the character AFTER the dash.
Because of this, the `lastEnd` would get an offset and one which means
the YamlParser would skip one character.
Introduce a workaround to correct the `lastEnd` if this faulty case.

With these changes, every dash is only interpreted once. That means
that in the SequenceBuilder.push method the rawPrefix will contain at
most one dash. Because of this, the old fix isn't needed anymore.
Remove it.

Fixes openrewrite#4176
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks a lot for diving deep for the fix here, and the thorough testing of various odd test cases. Should help to have this properly covered now.

@timtebeek timtebeek merged commit 2122964 into openrewrite:main May 11, 2024
1 check passed
@pdulich
Copy link
Contributor Author

pdulich commented May 12, 2024

You're very welcome 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

YAML: Unable to parse lists of lists
2 participants