Skip to content

Commit 74e35b0

Browse files
committed
fix: restore TypeScript compatibility for exported JSONPath types
1 parent b41ce03 commit 74e35b0

14 files changed

Lines changed: 100 additions & 64 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ evaluate method (as the first argument) include:
195195
to be returned within results.
196196
- ***parentProperty*** (**default: null**) - In the event that a query
197197
could be made to return the root node, this allows the `parentProperty`
198-
of that root node to be returned within results.
198+
of that root node to be returned within results. This may be a string
199+
property name or a numeric array index.
199200
- ***callback*** (**default: (none)**) - If supplied, a callback will be
200201
called immediately upon retrieval of an end point value. The three arguments
201202
supplied will be the value of the payload (according to `resultType`),

badges/tests-badge.svg

Lines changed: 1 addition & 1 deletion
Loading

demo/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const updateResults = () => {
8585
const result = new JSONPath.JSONPath({
8686
path: jsonpathEl.value,
8787
json,
88-
eval: $i('#eval').value === 'false' ? false : $i('#eval').value,
88+
eval: /** @type {'safe'|'native'|boolean} */ (
89+
$i('#eval').value === 'false' ? false : $i('#eval').value
90+
),
8991
ignoreEvalErrors: $i('#ignoreEvalErrors').value === 'true'
9092
});
9193
$i('#results').value = JSON.stringify(result, null, 2);

demo/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { JSONPathType } from 'jsonpath-plus';
1+
import type {JSONPathClass} from 'jsonpath-plus';
22

33
declare global {
44
var LZString: {
55
decompressFromEncodedURIComponent: (value: string) => string;
66
compressToEncodedURIComponent: (value: string) => string;
77
};
88
var JSONPath: {
9-
JSONPath: JSONPathType
9+
JSONPath: typeof JSONPathClass
1010
}
1111
}

dist/Safe-Script.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type AssignmentExpression = import("@jsep-plugin/assignment").AssignmentExpression;
1+
export type AssignmentExpression = any;
22
export type Substitution = any;
33
export type AnyParameter = any;
44
export type Substitutions = Record<string, Substitution>;
@@ -11,13 +11,12 @@ export class SafeScript {
1111
*/
1212
constructor(expr: string);
1313
code: string;
14-
ast: jsep.Expression;
14+
ast: unknown;
1515
/**
1616
* @param {object} context Object whose items will be added
1717
* to evaluation
1818
* @returns {EvaluatedResult} Result of evaluated code
1919
*/
2020
runInNewContext(context: object): EvaluatedResult;
2121
}
22-
import jsep from 'jsep';
2322
import type { EvaluatedResult } from './jsonpath.js';

dist/index-browser-esm.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,7 @@ const plugin = {
12031203
*/
12041204

12051205
/**
1206-
* @typedef {import('@jsep-plugin/assignment').
1207-
* AssignmentExpression} AssignmentExpression
1206+
* @typedef {any} AssignmentExpression
12081207
*/
12091208

12101209
/**
@@ -1447,7 +1446,7 @@ class SafeScript {
14471446
*/
14481447
constructor(expr) {
14491448
this.code = expr;
1450-
this.ast = jsep(this.code);
1449+
this.ast = /** @type {unknown} */jsep(this.code);
14511450
}
14521451

14531452
/**
@@ -1458,7 +1457,7 @@ class SafeScript {
14581457
runInNewContext(context) {
14591458
// `Object.create(null)` creates a prototypeless object
14601459
const keyMap = Object.assign(Object.create(null), context);
1461-
return SafeEval.evalAst(this.ast, keyMap);
1460+
return SafeEval.evalAst(/** @type {jsep.Expression} */this.ast, keyMap);
14621461
}
14631462
}
14641463

@@ -1506,7 +1505,8 @@ class SafeScript {
15061505
*/
15071506

15081507
/**
1509-
* @typedef {unknown|ParentValue|string|ReturnObject} PreferredOutput
1508+
* @typedef {ReturnObject|string|number|boolean|null|unknown[]|
1509+
* Record<string, unknown>} PreferredOutput
15101510
*/
15111511

15121512
/**
@@ -1595,11 +1595,17 @@ function unshift(item, arr) {
15951595
*/
15961596

15971597
/**
1598-
* @typedef {{Script: typeof SafeScript}} SafeScriptType
1598+
* @typedef {new (expr: string) => {
1599+
* runInNewContext: (context: object) => EvaluatedResult
1600+
* }} ScriptConstructor
15991601
*/
16001602

16011603
/**
1602-
* @typedef {{Script: typeof Script}} ScriptType
1604+
* @typedef {{Script: ScriptConstructor}} SafeScriptType
1605+
*/
1606+
1607+
/**
1608+
* @typedef {{Script: ScriptConstructor}} ScriptType
16031609
*/
16041610

16051611
/**
@@ -1624,7 +1630,7 @@ function unshift(item, arr) {
16241630
* @property {SandboxType} [sandbox={}]
16251631
* @property {EvalValue} [eval='safe']
16261632
* @property {any|null} [parent=null]
1627-
* @property {string|null} [parentProperty=null]
1633+
* @property {ParentProperty} [parentProperty=null]
16281634
* @property {JSONPathCallback} [callback]
16291635
* @property {OtherTypeCallback} [otherTypeCallback] Defaults to
16301636
* function which throws on encountering `@other`
@@ -1932,7 +1938,7 @@ class JSONPathClass {
19321938
case 'value':
19331939
case 'parent':
19341940
case 'parentProperty':
1935-
return ea[resultType];
1941+
return /** @type {PreferredOutput} */ea[resultType];
19361942
case 'path':
19371943
if (typeof ea.path === 'string') {
19381944
return ea.path;

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: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,7 @@
12091209
*/
12101210

12111211
/**
1212-
* @typedef {import('@jsep-plugin/assignment').
1213-
* AssignmentExpression} AssignmentExpression
1212+
* @typedef {any} AssignmentExpression
12141213
*/
12151214

12161215
/**
@@ -1453,7 +1452,7 @@
14531452
*/
14541453
constructor(expr) {
14551454
this.code = expr;
1456-
this.ast = jsep(this.code);
1455+
this.ast = /** @type {unknown} */jsep(this.code);
14571456
}
14581457

14591458
/**
@@ -1464,7 +1463,7 @@
14641463
runInNewContext(context) {
14651464
// `Object.create(null)` creates a prototypeless object
14661465
const keyMap = Object.assign(Object.create(null), context);
1467-
return SafeEval.evalAst(this.ast, keyMap);
1466+
return SafeEval.evalAst(/** @type {jsep.Expression} */this.ast, keyMap);
14681467
}
14691468
}
14701469

@@ -1512,7 +1511,8 @@
15121511
*/
15131512

15141513
/**
1515-
* @typedef {unknown|ParentValue|string|ReturnObject} PreferredOutput
1514+
* @typedef {ReturnObject|string|number|boolean|null|unknown[]|
1515+
* Record<string, unknown>} PreferredOutput
15161516
*/
15171517

15181518
/**
@@ -1601,11 +1601,17 @@
16011601
*/
16021602

16031603
/**
1604-
* @typedef {{Script: typeof SafeScript}} SafeScriptType
1604+
* @typedef {new (expr: string) => {
1605+
* runInNewContext: (context: object) => EvaluatedResult
1606+
* }} ScriptConstructor
16051607
*/
16061608

16071609
/**
1608-
* @typedef {{Script: typeof Script}} ScriptType
1610+
* @typedef {{Script: ScriptConstructor}} SafeScriptType
1611+
*/
1612+
1613+
/**
1614+
* @typedef {{Script: ScriptConstructor}} ScriptType
16091615
*/
16101616

16111617
/**
@@ -1630,7 +1636,7 @@
16301636
* @property {SandboxType} [sandbox={}]
16311637
* @property {EvalValue} [eval='safe']
16321638
* @property {any|null} [parent=null]
1633-
* @property {string|null} [parentProperty=null]
1639+
* @property {ParentProperty} [parentProperty=null]
16341640
* @property {JSONPathCallback} [callback]
16351641
* @property {OtherTypeCallback} [otherTypeCallback] Defaults to
16361642
* function which throws on encountering `@other`
@@ -1938,7 +1944,7 @@
19381944
case 'value':
19391945
case 'parent':
19401946
case 'parentProperty':
1941-
return ea[resultType];
1947+
return /** @type {PreferredOutput} */ea[resultType];
19421948
case 'path':
19431949
if (typeof ea.path === 'string') {
19441950
return ea.path;

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: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,7 @@ const plugin = {
12071207
*/
12081208

12091209
/**
1210-
* @typedef {import('@jsep-plugin/assignment').
1211-
* AssignmentExpression} AssignmentExpression
1210+
* @typedef {any} AssignmentExpression
12121211
*/
12131212

12141213
/**
@@ -1451,7 +1450,7 @@ class SafeScript {
14511450
*/
14521451
constructor(expr) {
14531452
this.code = expr;
1454-
this.ast = jsep(this.code);
1453+
this.ast = /** @type {unknown} */jsep(this.code);
14551454
}
14561455

14571456
/**
@@ -1462,7 +1461,7 @@ class SafeScript {
14621461
runInNewContext(context) {
14631462
// `Object.create(null)` creates a prototypeless object
14641463
const keyMap = Object.assign(Object.create(null), context);
1465-
return SafeEval.evalAst(this.ast, keyMap);
1464+
return SafeEval.evalAst(/** @type {jsep.Expression} */this.ast, keyMap);
14661465
}
14671466
}
14681467

@@ -1510,7 +1509,8 @@ class SafeScript {
15101509
*/
15111510

15121511
/**
1513-
* @typedef {unknown|ParentValue|string|ReturnObject} PreferredOutput
1512+
* @typedef {ReturnObject|string|number|boolean|null|unknown[]|
1513+
* Record<string, unknown>} PreferredOutput
15141514
*/
15151515

15161516
/**
@@ -1599,11 +1599,17 @@ function unshift(item, arr) {
15991599
*/
16001600

16011601
/**
1602-
* @typedef {{Script: typeof SafeScript}} SafeScriptType
1602+
* @typedef {new (expr: string) => {
1603+
* runInNewContext: (context: object) => EvaluatedResult
1604+
* }} ScriptConstructor
16031605
*/
16041606

16051607
/**
1606-
* @typedef {{Script: typeof Script}} ScriptType
1608+
* @typedef {{Script: ScriptConstructor}} SafeScriptType
1609+
*/
1610+
1611+
/**
1612+
* @typedef {{Script: ScriptConstructor}} ScriptType
16071613
*/
16081614

16091615
/**
@@ -1628,7 +1634,7 @@ function unshift(item, arr) {
16281634
* @property {SandboxType} [sandbox={}]
16291635
* @property {EvalValue} [eval='safe']
16301636
* @property {any|null} [parent=null]
1631-
* @property {string|null} [parentProperty=null]
1637+
* @property {ParentProperty} [parentProperty=null]
16321638
* @property {JSONPathCallback} [callback]
16331639
* @property {OtherTypeCallback} [otherTypeCallback] Defaults to
16341640
* function which throws on encountering `@other`
@@ -1936,7 +1942,7 @@ class JSONPathClass {
19361942
case 'value':
19371943
case 'parent':
19381944
case 'parentProperty':
1939-
return ea[resultType];
1945+
return /** @type {PreferredOutput} */ea[resultType];
19401946
case 'path':
19411947
if (typeof ea.path === 'string') {
19421948
return ea.path;

0 commit comments

Comments
 (0)