Skip to content

Commit 3d1d241

Browse files
committed
fix: Fix parse when the last value of an implied AQF object/array is falsey
Closes #556.
1 parent 7fc3768 commit 3d1d241

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

src/JsonURL.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,8 @@ class ValueStack extends Array {
11091109
* Pop an object key/value off the stack and assign the value in a target.
11101110
* @returns the target
11111111
*/
1112-
popObjectValue(options, value) {
1113-
value = value || this.pop();
1112+
popObjectValue(options) {
1113+
let value = this.pop();
11141114
let key = this.pop();
11151115
let target = this[this.length - 1];
11161116

@@ -1128,8 +1128,8 @@ class ValueStack extends Array {
11281128
* Pop a value off the stack and append it to a target.
11291129
* @returns the target
11301130
*/
1131-
popArrayValue(options, value) {
1132-
value = value || this.pop();
1131+
popArrayValue(options) {
1132+
let value = this.pop();
11331133
let target = this[this.length - 1];
11341134

11351135
if (value !== null || !options.isPresentAndTrue("ignoreNullArrayMembers")) {
@@ -1455,7 +1455,8 @@ function parse(text, offsetOrOpt, endOrOpt, options, limits) {
14551455
}
14561456
if (chars.done()) {
14571457
if (stateStack.depth() === 0 && options.impliedArray) {
1458-
return valueStack.popArrayValue(options, lv);
1458+
valueStack.push(lv);
1459+
return valueStack.popArrayValue(options);
14591460
}
14601461
throw new SyntaxError(Err.fmt(Err.MSG_STILLOPEN, end));
14611462
}
@@ -1530,7 +1531,8 @@ function parse(text, offsetOrOpt, endOrOpt, options, limits) {
15301531
}
15311532
if (chars.done()) {
15321533
if (stateStack.depth() === 0 && options.impliedObject) {
1533-
return valueStack.popObjectValue(options, lv);
1534+
valueStack.push(lv);
1535+
return valueStack.popObjectValue(options);
15341536
}
15351537
throw new SyntaxError(Err.fmt(Err.MSG_STILLOPEN, end));
15361538
}

test/issue556.test.js

+39-28
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
1-
import { JsonURL } from "../src/JsonURL.js";
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2022 Leonard Crestez, David MacCormack
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
212
3-
test("aqf implied array e empty", function () {
4-
expect(JsonURL.parse("e,!e", { AQF: true, impliedArray: [] })).toStrictEqual(
5-
["e", ""])
6-
})
7-
test("aqf implied array empty e", function () {
8-
expect(JsonURL.parse("!e,e", { AQF: true, impliedArray: [] })).toStrictEqual(["", "e"]);
9-
})
10-
test("aqf explicit array e empty", function () {
11-
expect(JsonURL.parse("(e,!e)", { AQF: true })).toStrictEqual(
12-
["e", ""]);
13-
})
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
import { JsonURL } from "../src/JsonURL.js";
1426

15-
test("aqf implied dict e:empty", function () {
16-
expect(JsonURL.parse("e:!e", { AQF: true, impliedObject: {} })).toStrictEqual(
17-
{ "e": "" });
18-
})
19-
test("aqf explicit dict e:empty", function () {
20-
expect(JsonURL.parse("(e:!e)", { AQF: true })).toStrictEqual(
21-
{ "e": "" });
22-
})
23-
test("aqf implied dict empty:e", function () {
24-
expect(JsonURL.parse("!e:e", { AQF: true, impliedObject: {} })).toStrictEqual(
25-
{ "": "e" })
26-
})
27-
test("aqf implied dict e:empty a:b", function () {
28-
expect(JsonURL.parse("e:!e,a:b", { AQF: true, impliedObject: {} })).toStrictEqual(
29-
{ "e": "", "a": "b" });
30-
})
27+
test.each([
28+
["e,!e", { AQF: true, impliedArray: [] }, ["e", ""]],
29+
["e,false", { AQF: true, impliedArray: [] }, ["e", false]],
30+
["!e,e", { AQF: true, impliedArray: [] }, ["", "e"]],
31+
["e:!e", { AQF: true, impliedObject: {} }, { e: "" }],
32+
["e:false", { AQF: true, impliedObject: {} }, { e: false }],
33+
["!e:e", { AQF: true, impliedObject: {} }, { "": "e" }],
34+
["e:!e,a:b", { AQF: true, impliedObject: {} }, { e: "", a: "b" }],
35+
["(e,!e)", { AQF: true }, ["e", ""]],
36+
["(e,false)", { AQF: true }, ["e", false]],
37+
["(e:!e)", { AQF: true }, { e: "" }],
38+
["(e:false)", { AQF: true }, { e: false }],
39+
])("JsonURL.parse(%p, %p)", (text, options, expected) => {
40+
expect(JsonURL.parse(text, options)).toStrictEqual(expected);
41+
});

0 commit comments

Comments
 (0)