Skip to content

Commit 7df0fe9

Browse files
youdie006brettz9
authored andcommitted
fix: leave @root, @parent, @Property, @parentProperty and @path inside literals intact
The bare @ replacement already skips quoted strings and regex literals, but the named tokens were replaced with plain replaceAll calls first, so a filter like $[?(@.a === 'jo@rootmail.com')] compared against 'jo_$_rootmail.com' and matched nothing. Replace them in the same literal-aware pass.
1 parent f010455 commit 7df0fe9

11 files changed

Lines changed: 47 additions & 45 deletions

‎badges/tests-badge.svg‎

Lines changed: 1 addition & 1 deletion
Loading

‎dist/index-browser-esm.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,15 +2394,12 @@ class JSONPathClass {
23942394
}
23952395
const scriptCacheKey = this.currEval + 'Script:' + code;
23962396
if (!scriptCache.has(scriptCacheKey)) {
2397-
let script = code.replaceAll('@parentProperty', '_$_parentProperty').replaceAll('@parent', '_$_parent').replaceAll('@property', '_$_property').replaceAll('@root', '_$_root')
2398-
// Replace a bare `@` (not followed by an identifier
2397+
// Replace `@parentProperty`, `@parent`, `@property`, `@root`,
2398+
// `@path` and a bare `@` (not followed by an identifier
23992399
// character) while leaving quoted string literals and
24002400
// regex literals (a `/` where an operand is expected,
24012401
// as opposed to division) intact
2402-
.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?![\w$])/gv, (_, literal) => literal ?? '_$_v');
2403-
if (containsPath) {
2404-
script = script.replaceAll('@path', '_$_path');
2405-
}
2402+
const script = code.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?:(parentProperty|parent|property|root|path)|(?![\w$]))/gv, (_, literal, name) => literal ?? '_$_' + (name ?? 'v'));
24062403
const evalType = /** @type {string|boolean|undefined} */
24072404
this.currEval;
24082405
if (['safe', true, undefined].includes(evalType)) {

‎dist/index-browser-esm.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index-browser-esm.min.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index-browser-umd.cjs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,15 +2400,12 @@
24002400
}
24012401
const scriptCacheKey = this.currEval + 'Script:' + code;
24022402
if (!scriptCache.has(scriptCacheKey)) {
2403-
let script = code.replaceAll('@parentProperty', '_$_parentProperty').replaceAll('@parent', '_$_parent').replaceAll('@property', '_$_property').replaceAll('@root', '_$_root')
2404-
// Replace a bare `@` (not followed by an identifier
2403+
// Replace `@parentProperty`, `@parent`, `@property`, `@root`,
2404+
// `@path` and a bare `@` (not followed by an identifier
24052405
// character) while leaving quoted string literals and
24062406
// regex literals (a `/` where an operand is expected,
24072407
// as opposed to division) intact
2408-
.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?![\w$])/gv, (_, literal) => literal ?? '_$_v');
2409-
if (containsPath) {
2410-
script = script.replaceAll('@path', '_$_path');
2411-
}
2408+
const script = code.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?:(parentProperty|parent|property|root|path)|(?![\w$]))/gv, (_, literal, name) => literal ?? '_$_' + (name ?? 'v'));
24122409
const evalType = /** @type {string|boolean|undefined} */
24132410
this.currEval;
24142411
if (['safe', true, undefined].includes(evalType)) {

‎dist/index-browser-umd.min.cjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/index-browser-umd.min.cjs.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index-node-cjs.cjs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,15 +2398,12 @@ class JSONPathClass {
23982398
}
23992399
const scriptCacheKey = this.currEval + 'Script:' + code;
24002400
if (!scriptCache.has(scriptCacheKey)) {
2401-
let script = code.replaceAll('@parentProperty', '_$_parentProperty').replaceAll('@parent', '_$_parent').replaceAll('@property', '_$_property').replaceAll('@root', '_$_root')
2402-
// Replace a bare `@` (not followed by an identifier
2401+
// Replace `@parentProperty`, `@parent`, `@property`, `@root`,
2402+
// `@path` and a bare `@` (not followed by an identifier
24032403
// character) while leaving quoted string literals and
24042404
// regex literals (a `/` where an operand is expected,
24052405
// as opposed to division) intact
2406-
.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?![\w$])/gv, (_, literal) => literal ?? '_$_v');
2407-
if (containsPath) {
2408-
script = script.replaceAll('@path', '_$_path');
2409-
}
2406+
const script = code.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?:(parentProperty|parent|property|root|path)|(?![\w$]))/gv, (_, literal, name) => literal ?? '_$_' + (name ?? 'v'));
24102407
const evalType = /** @type {string|boolean|undefined} */
24112408
this.currEval;
24122409
if (['safe', true, undefined].includes(evalType)) {

‎dist/index-node-esm.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,15 +2396,12 @@ class JSONPathClass {
23962396
}
23972397
const scriptCacheKey = this.currEval + 'Script:' + code;
23982398
if (!scriptCache.has(scriptCacheKey)) {
2399-
let script = code.replaceAll('@parentProperty', '_$_parentProperty').replaceAll('@parent', '_$_parent').replaceAll('@property', '_$_property').replaceAll('@root', '_$_root')
2400-
// Replace a bare `@` (not followed by an identifier
2399+
// Replace `@parentProperty`, `@parent`, `@property`, `@root`,
2400+
// `@path` and a bare `@` (not followed by an identifier
24012401
// character) while leaving quoted string literals and
24022402
// regex literals (a `/` where an operand is expected,
24032403
// as opposed to division) intact
2404-
.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?![\w$])/gv, (_, literal) => literal ?? '_$_v');
2405-
if (containsPath) {
2406-
script = script.replaceAll('@path', '_$_path');
2407-
}
2404+
const script = code.replaceAll(/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?:(parentProperty|parent|property|root|path)|(?![\w$]))/gv, (_, literal, name) => literal ?? '_$_' + (name ?? 'v'));
24082405
const evalType = /** @type {string|boolean|undefined} */
24092406
this.currEval;
24102407
if (['safe', true, undefined].includes(evalType)) {

‎src/jsonpath.js‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,22 +1048,15 @@ class JSONPathClass {
10481048

10491049
const scriptCacheKey = this.currEval + 'Script:' + code;
10501050
if (!scriptCache.has(scriptCacheKey)) {
1051-
let script = code
1052-
.replaceAll('@parentProperty', '_$_parentProperty')
1053-
.replaceAll('@parent', '_$_parent')
1054-
.replaceAll('@property', '_$_property')
1055-
.replaceAll('@root', '_$_root')
1056-
// Replace a bare `@` (not followed by an identifier
1057-
// character) while leaving quoted string literals and
1058-
// regex literals (a `/` where an operand is expected,
1059-
// as opposed to division) intact
1060-
.replaceAll(
1061-
/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?![\w$])/gv,
1062-
(_, literal) => literal ?? '_$_v'
1063-
);
1064-
if (containsPath) {
1065-
script = script.replaceAll('@path', '_$_path');
1066-
}
1051+
// Replace `@parentProperty`, `@parent`, `@property`, `@root`,
1052+
// `@path` and a bare `@` (not followed by an identifier
1053+
// character) while leaving quoted string literals and
1054+
// regex literals (a `/` where an operand is expected,
1055+
// as opposed to division) intact
1056+
const script = code.replaceAll(
1057+
/('(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|(?<=(?:^|[\(,=:\[!\|?\{\};+\-*%<>~^]|&)\s*)\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\/\\\[])+\/[dgimsuvy]*)|@(?:(parentProperty|parent|property|root|path)|(?![\w$]))/gv,
1058+
(_, literal, name) => literal ?? '_$_' + (name ?? 'v')
1059+
);
10671060
const evalType = /** @type {string|boolean|undefined} */ (
10681061
this.currEval
10691062
);

0 commit comments

Comments
 (0)