fix: Array slicing for [:X] and [X:]#11
Open
nightscape wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts slice parsing/execution semantics in jq.js, primarily to treat omitted slice endpoints as open-ended (defaulting to start 0 / end length) instead of using -1, and adds parse result metadata (length) to detect empty bracket expressions.
Changes:
- Represent an omitted slice end as
nulland default it at evaluation-time (l.length/input.length) forSliceNodeandGenericSlice. - Extend
parse()return objects with alengthfield to enable empty-expression detection inparseDotSquare. - Update
toString()formatting for slice nodes to render omitted endpoints as empty strings.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+741
to
+743
| return {node: new CommaNode(commaAccum), i, length: ret.length + commaAccum.length} | ||
| } | ||
| return {node: makeFilterNode(ret), i} | ||
| return {node: makeFilterNode(ret), i, length: ret.length} |
| if (commaAccum.length) { | ||
| commaAccum.push(makeFilterNode(ret)) | ||
| return {node: new CommaNode(commaAccum), i} | ||
| return {node: new CommaNode(commaAccum), i, length: ret.length + commaAccum.length} |
Comment on lines
+1130
to
1135
| for (let l of this.lhs.paths(input, conf)) { | ||
| let fromIter = this.from ? this.from.apply(input, conf) : [0] | ||
| for (let a of fromIter) { | ||
| let toIter = this.to ? this.to.apply(input, conf) : [l.length] | ||
| for (let b of toIter) | ||
| yield l.concat([{start:a, end:b}]) |
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.
Slicing arrays using
[:X]and[X:]returned an empty array in all cases I tried.This PR fixes it in my own manual testing.
If one wanted proper testing, one could e.g. add a submodule for jq itself and try to run the
.testfiles against this implementation:https://github.com/jqlang/jq/blob/master/tests/manonig.test
For me, this is good enough for now.