Skip to content

fix: Array slicing for [:X] and [X:]#11

Open
nightscape wants to merge 1 commit into
mwh:masterfrom
nightscape:fix-array-slicing
Open

fix: Array slicing for [:X] and [X:]#11
nightscape wants to merge 1 commit into
mwh:masterfrom
nightscape:fix-array-slicing

Conversation

@nightscape

Copy link
Copy Markdown

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 .test files against this implementation:
https://github.com/jqlang/jq/blob/master/tests/manonig.test
For me, this is good enough for now.

Copilot AI review requested due to automatic review settings March 18, 2026 14:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 null and default it at evaluation-time (l.length / input.length) for SliceNode and GenericSlice.
  • Extend parse() return objects with a length field to enable empty-expression detection in parseDotSquare.
  • 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 thread jq.js
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}
Comment thread jq.js
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 thread jq.js
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}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants