fix: make Remove patch op sound for flow collections and empty parents#2121
Open
mostafa wants to merge 1 commit into
Open
fix: make Remove patch op sound for flow collections and empty parents#2121mostafa wants to merge 1 commit into
mostafa wants to merge 1 commit into
Conversation
The Remove op previously deleted whole lines, which corrupted flow
mappings and sequences (e.g. removing `b` from `{a: 1, b: 2}` deleted the
entire line) and left dangling null parents (e.g. removing the only key
under `env:` left `env:` with no value).
Resolve removal spans in yamlpath via the parse tree instead:
- Dispatch on the value's actual container kind (block vs flow), so
nested mixtures like `[{a: 1}, {b: 2}]` and `{x: [1, 2]}` resolve
correctly rather than by sniffing for `{`/`[` tokens.
- Remove flow members with a single adjacent comma and surrounding
whitespace, collapsing a sole member cleanly to `{}`/`[]`.
- Resolve the element at its own lexical site so a terminal alias is not
followed (removing `b: *x` no longer deletes the anchor `a: &x 1`).
- Collapse dangling empty block parents upward to the first ancestor that
retains other content; flow containers stay as valid `{}`/`[]`.
yamlpatch's Op::Remove now delegates to Document::removal_span.
4 tasks
Member
|
Thanks @mostafa! I'm hoping to give this a review in the next day or so. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pre-submission checks
Please check these boxes:
Mandatory: This PR corresponds to an issue (if not, please create
one first).
Having read the AI policy, I hereby disclose the use of an LLM or other
AI coding assistant in the creation of this PR. PRs will not be rejected
for using AI tools, but will be rejected for undisclosed use or
use that violates the policy.
If a checkbox is not applicable, you can leave it unchecked.
Summary
Closes #2122. Reworks the
Removepatch op so it is sound for flow collections and nested structures, addressing the soundness concern raised in #1020 and following the "re-think withinyamlpath" direction of #1000. Relates to META #876.Previously
Removedeleted whole lines, which:bfrom{a: 1, b: 2}deleted the whole line), andenv:leftenv:with no value).The earlier flow handling in #1020 detected the container by sniffing for
{/[tokens, which is brittle because it cannot distinguish nesting like{[ ... ]}from[{ ... }].Removal span computation now lives in
yamlpathasDocument::removal_span, which uses the parse tree rather than token heuristics:[{a: 1}, {b: 2}]and{x: [1, 2]}resolve correctly.{}or[].b: *xdeletesb: *xrather than the anchor definitiona: &x 1it points to.{}/[], and block containers only nest inside block containers, so collapse never crosses styles.yamlpatch'sOp::Removeis now a thin call intoDocument::removal_span.Verified outputs match the (draft) expectations in #1000:
foo: { a: a, b: b }with/foo/bremoved givesfoo: { a: a }; an empty keyfoo/bazremoval preserves the rest; andfoo: [1, 2, 3: {abc: def}]with/foo/2removed givesfoo: [1, 2].Design note: #1000 leaned toward extending
QueryMode::Prettyinquery_nodeand deleting that span. I added a dedicatedremoval_spaninstead, because removal has needs that pretty extraction does not (separator handling, sole-member collapse, parent collapse) andquery_prettyis shared withOp::Replace. Happy to fold this intoquery_prettyif you prefer.Test Plan
cargo test -p yamlpath -p yamlpatch(added snapshot coverage for block/flow mappings and sequences, nested flow collections, multiline flow, alias safety, and block parent collapse)cargo test -p zizmor --bins insecure_commands(the only currentOp::Removeconsumer)cargo fmt --checkcargo clippy -- --deny warnings