-
Notifications
You must be signed in to change notification settings - Fork 533
YAML: prevent block-scalar value mutations from corrupting siblings #8154
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
Merged
+629
−12
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9becb98
YAML: prevent block-scalar value mutations from corrupting siblings
steve-aom-elliott bc78df8
YAML: keep block-scalar line endings on CRLF files
steve-aom-elliott fd1bf85
YAML: trim block-scalar fix comments and normalize test indentation
steve-aom-elliott 4ed2190
YAML: trim Yaml.Scalar.value javadoc per PR feedback
steve-aom-elliott 92c7333
YAML: drop BlockScalarUtils.withBody trailing-newline fallback
steve-aom-elliott 9b36514
YAML: promote BlockScalarUtils helpers onto Yaml.Scalar
steve-aom-elliott 234e4a8
YAML: trim javadoc on Yaml.Scalar body helpers
steve-aom-elliott 5ce3f24
YAML: drop unhelpful javadoc on Yaml.Scalar.withBody overloads
steve-aom-elliott f896b98
YAML: move getBody/withBody logic to a BlockScalar trait
steve-aom-elliott b808576
YAML: refactor BlockScalar to @Value + Matcher, drop passthroughs
steve-aom-elliott 1362677
YAML: condense ChangeValue.visitMappingEntry block-scalar check
steve-aom-elliott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
rewrite-yaml/src/main/java/org/openrewrite/yaml/trait/BlockScalar.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright 2026 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.yaml.trait; | ||
|
|
||
| import lombok.Value; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.Cursor; | ||
| import org.openrewrite.internal.StringUtils; | ||
| import org.openrewrite.trait.SimpleTraitMatcher; | ||
| import org.openrewrite.trait.Trait; | ||
| import org.openrewrite.yaml.tree.Yaml; | ||
|
|
||
| /** | ||
| * Body-aware access to a FOLDED / LITERAL {@link Yaml.Scalar}: the raw {@link Yaml.Scalar#value} | ||
| * carries the block envelope (chomp indicator, header newline, indented body, trailing whitespace | ||
| * bounding the next sibling), so rewriting it directly via the Lombok-generated {@code withValue} | ||
| * clobbers the envelope. | ||
| */ | ||
| @Value | ||
| public class BlockScalar implements Trait<Yaml.Scalar> { | ||
| Cursor cursor; | ||
|
|
||
| public String getBody() { | ||
| String value = getTree().getValue(); | ||
| int headerEnd = value.indexOf('\n'); | ||
| if (headerEnd < 0) { | ||
| return ""; | ||
| } | ||
| int bodyEnd = value.length(); | ||
| while (bodyEnd > headerEnd + 1 && Character.isWhitespace(value.charAt(bodyEnd - 1))) { | ||
| bodyEnd--; | ||
| } | ||
| if (bodyEnd <= headerEnd + 1) { | ||
| return ""; | ||
| } | ||
| String bodyRegion = value.substring(headerEnd + 1, bodyEnd); | ||
| int indent = 0; | ||
| while (indent < bodyRegion.length() && bodyRegion.charAt(indent) == ' ') { | ||
| indent++; | ||
| } | ||
| String indentStr = bodyRegion.substring(0, indent); | ||
| String[] lines = bodyRegion.split("\r\n|\r|\n", -1); | ||
| StringBuilder out = new StringBuilder(bodyRegion.length()); | ||
| for (int i = 0; i < lines.length; i++) { | ||
| String line = lines[i]; | ||
| if (indent > 0 && line.startsWith(indentStr)) { | ||
| line = line.substring(indent); | ||
| } | ||
| if (i > 0) { | ||
| out.append('\n'); | ||
| } | ||
| out.append(line); | ||
| } | ||
| return out.toString(); | ||
| } | ||
|
|
||
| public Yaml.Scalar withBody(String newBody) { | ||
| return withBody(newBody, 2); | ||
| } | ||
|
|
||
| public Yaml.Scalar withBody(String newBody, int defaultIndentSpaces) { | ||
| Yaml.Scalar scalar = getTree(); | ||
| String value = scalar.getValue(); | ||
| int headerEnd = value.indexOf('\n'); | ||
| String header = headerEnd < 0 ? value : value.substring(0, headerEnd + 1); | ||
| String newLine = (headerEnd > 0 && value.charAt(headerEnd - 1) == '\r') ? "\r\n" : "\n"; | ||
| int bodyEnd = value.length(); | ||
| while (bodyEnd > 0 && Character.isWhitespace(value.charAt(bodyEnd - 1))) { | ||
| bodyEnd--; | ||
| } | ||
| String indent; | ||
| if (headerEnd >= 0 && headerEnd + 1 < bodyEnd) { | ||
| int indentEnd = headerEnd + 1; | ||
| while (indentEnd < bodyEnd && value.charAt(indentEnd) == ' ') { | ||
| indentEnd++; | ||
| } | ||
| indent = value.substring(headerEnd + 1, indentEnd); | ||
| if (indent.isEmpty()) { | ||
| indent = StringUtils.repeat(" ", defaultIndentSpaces); | ||
| } | ||
| } else { | ||
| indent = StringUtils.repeat(" ", defaultIndentSpaces); | ||
| } | ||
| String trailing = value.substring(bodyEnd); | ||
| String[] lines = newBody.split("\r\n|\r|\n", -1); | ||
| StringBuilder body = new StringBuilder(); | ||
| for (int i = 0; i < lines.length; i++) { | ||
| if (i > 0) { | ||
| body.append(newLine); | ||
| } | ||
| if (!lines[i].isEmpty()) { | ||
| body.append(indent).append(lines[i]); | ||
| } | ||
| } | ||
| return scalar.withValue(header + body + trailing); | ||
| } | ||
|
|
||
| public static class Matcher extends SimpleTraitMatcher<BlockScalar> { | ||
| @Override | ||
| protected @Nullable BlockScalar test(Cursor cursor) { | ||
| if (cursor.getValue() instanceof Yaml.Scalar) { | ||
| Yaml.Scalar.Style style = ((Yaml.Scalar) cursor.getValue()).getStyle(); | ||
| if (style == Yaml.Scalar.Style.FOLDED || style == Yaml.Scalar.Style.LITERAL) { | ||
| return new BlockScalar(cursor); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.