From ab39dc366cb43ca69d80ef01945c4588c1aabd81 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 9 Jul 2026 12:19:57 +0100 Subject: [PATCH] fix(filter): allow bare @ adjacent to an operator without whitespace The current-node token @ was only substituted for _$_v when followed by '.', whitespace, ')' or '['. A bare @ directly before a comparison or arithmetic operator (e.g. $[?(@>1)] or $[?(@===2)]) was left untouched, so the compiled filter script started with a literal @ and failed to parse under both the safe and native evaluators. Extend the delimiter set with the operator characters so these expressions evaluate; @ inside identifiers and quoted keys (e.g. @['@tag']) stays protected. --- src/jsonpath.js | 2 +- test/test.at_and_dollar.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/jsonpath.js b/src/jsonpath.js index e2bdbb5..068254e 100644 --- a/src/jsonpath.js +++ b/src/jsonpath.js @@ -649,7 +649,7 @@ JSONPath.prototype._eval = function ( .replaceAll('@parent', '_$_parent') .replaceAll('@property', '_$_property') .replaceAll('@root', '_$_root') - .replaceAll(/@([.\s)[])/gu, '_$_v$1'); + .replaceAll(/@([-.\s)[<>=!%*/+&|])/gu, '_$_v$1'); if (containsPath) { script = script.replaceAll('@path', '_$_path'); } diff --git a/test/test.at_and_dollar.js b/test/test.at_and_dollar.js index c69d871..d5d3ae4 100644 --- a/test/test.at_and_dollar.js +++ b/test/test.at_and_dollar.js @@ -52,4 +52,12 @@ describe('JSONPath - At and Dollar sign', function () { const result = jsonpath({json, path: "$.a[?(@property === 'b' && @ < 1)]", wrap: false}); assert.deepEqual(result, expected); }); + + it('bare @ adjacent to an operator (no whitespace)', () => { + const json = [0, 1, 2, 3]; + assert.deepEqual(jsonpath({json, path: '$[?(@>1)]'}), [2, 3]); + assert.deepEqual(jsonpath({json, path: '$[?(@<2)]'}), [0, 1]); + assert.deepEqual(jsonpath({json, path: '$[?(@===2)]'}), [2]); + assert.deepEqual(jsonpath({json, path: '$[?(@!==2)]'}), [0, 1, 3]); + }); });