Skip to content

Commit 38e4196

Browse files
committed
fix: make JavaScript types compatible with TypeScript 7
- preserve prototype-based VM members without runtime shadowing - update test globals and cross-version diagnostics - keep TypeScript 6 and TypeDoc compatibility (thus, use of ts-ignore instead of ts-expect-error)
1 parent 127d9cd commit 38e4196

8 files changed

Lines changed: 52 additions & 29 deletions

File tree

src/jsonpath-browser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ class Script {
157157
}
158158
}
159159

160-
JSONPathClass.prototype.vm = {
160+
/** @type {{vm: ScriptType}} */
161+
(/** @type {unknown} */ (JSONPathClass.prototype)).vm = {
161162
Script
162163
};
163164

src/jsonpath-node.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ import {JSONPath, JSONPathClass} from './jsonpath.js';
7474
// Node's vm module shape is wider than ScriptType, but is compatible for
7575
// the properties actually used (Script) -- kept Node-specific here so
7676
// `node:vm` types don't leak into the shared/browser declarations.
77-
JSONPathClass.prototype.vm = /** @type {ScriptType} */ (
78-
/** @type {unknown} */ (vm)
79-
);
77+
/** @type {{vm: ScriptType}} */
78+
(/** @type {unknown} */ (JSONPathClass.prototype)).vm =
79+
/** @type {ScriptType} */ (
80+
/** @type {unknown} */ (vm)
81+
);
8082

8183
export {
8284
JSONPath, JSONPathClass

src/jsonpath.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function unshift (item, arr) {
9797
* @param {ExpressionArray} path
9898
* @param {ParentValue} parent
9999
* @param {string|null} parentPropName
100-
* @returns {boolean}
100+
* @returns {boolean|null}
101101
*/
102102

103103
/**
@@ -264,7 +264,6 @@ class JSONPathClass {
264264
* "other" type or not (or it may handle transformations and return
265265
* `false`).
266266
* @param {undefined} [otherTypeCallback]
267-
* @returns {JSONPath|JSONPathClass}
268267
*/
269268
/**
270269
* @overload
@@ -310,16 +309,6 @@ class JSONPathClass {
310309
/** @type {OtherTypeCallback|undefined} */
311310
this.currOtherTypeCallback = undefined;
312311

313-
/** @type {SafeScriptType} */
314-
// eslint-disable-next-line @stylistic/max-len -- Long
315-
// eslint-disable-next-line unicorn/no-undeclared-class-members, no-unused-expressions -- On prototype
316-
this.safeVm;
317-
318-
/** @type {ScriptType} */
319-
// eslint-disable-next-line @stylistic/max-len -- Long
320-
// eslint-disable-next-line unicorn/no-undeclared-class-members, no-unused-expressions -- On prototype
321-
this.vm;
322-
323312
/** @type {SandboxType|undefined} */
324313
this.currSandbox = undefined;
325314

@@ -1021,18 +1010,32 @@ class JSONPathClass {
10211010
);
10221011
if (['safe', true, undefined].includes(evalType)) {
10231012
const {cache} = JSONPath;
1013+
// eslint-disable-next-line @stylistic/max-len -- Long
1014+
/* eslint-disable unicorn/no-undeclared-class-members -- Prototype members */
10241015
cache[scriptCacheKey] = new (
1025-
// eslint-disable-next-line @stylistic/max-len -- Long
1026-
// eslint-disable-next-line unicorn/no-undeclared-class-members -- Prototype
1027-
this.safeVm
1028-
).Script(script);
1016+
/**
1017+
* @type {JSONPathClass & {
1018+
* safeVm: SafeScriptType,
1019+
* vm: ScriptType
1020+
* }}
1021+
*/ (/** @type {unknown} */ (this))
1022+
).safeVm.Script(script);
1023+
// eslint-disable-next-line @stylistic/max-len -- Long
1024+
/* eslint-enable unicorn/no-undeclared-class-members -- End prototype member scope */
10291025
} else if (this.currEval === 'native') {
10301026
const {cache} = JSONPath;
1027+
// eslint-disable-next-line @stylistic/max-len -- Long
1028+
/* eslint-disable unicorn/no-undeclared-class-members -- Prototype members */
10311029
cache[scriptCacheKey] = new (
1032-
// eslint-disable-next-line @stylistic/max-len -- Long
1033-
// eslint-disable-next-line unicorn/no-undeclared-class-members -- Prototype
1034-
this.vm
1035-
).Script(script);
1030+
/**
1031+
* @type {JSONPathClass & {
1032+
* safeVm: SafeScriptType,
1033+
* vm: ScriptType
1034+
* }}
1035+
*/ (/** @type {unknown} */ (this))
1036+
).vm.Script(script);
1037+
// eslint-disable-next-line @stylistic/max-len -- Long
1038+
/* eslint-enable unicorn/no-undeclared-class-members -- End prototype member scope */
10361039
} else if (
10371040
typeof this.currEval === 'function' &&
10381041
this.currEval.prototype &&
@@ -1088,7 +1091,8 @@ class JSONPathClass {
10881091
}
10891092
}
10901093

1091-
JSONPathClass.prototype.safeVm = {
1094+
/** @type {{safeVm: SafeScriptType}} */
1095+
(/** @type {unknown} */ (JSONPathClass.prototype)).safeVm = {
10921096
Script: SafeScript
10931097
};
10941098

test-helpers/globals.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare global {
2+
var assert: typeof import('chai').assert;
3+
var expect: typeof import('chai').expect;
4+
var jsonpath: typeof import('../src/jsonpath.js').JSONPath;
5+
var jsonpathNodeVM: typeof import('../src/jsonpath-node.js').JSONPath;
6+
var jsonpathBrowser: typeof import('../src/jsonpath-browser.js').JSONPath;
7+
var JSONPath: typeof import('../src/jsonpath.js').JSONPath;
8+
var JSONPathClass: typeof import('../src/jsonpath.js').JSONPathClass;
9+
}
10+
11+
export {};

test/test.api.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ describe('JSONPath - API', function () {
249249
values: [1, 'text', true, null, {obj: 'value'}]
250250
};
251251

252-
// @ts-expect-error Testing
253252
const result = jsonpath({
254253
json: testJson,
255254
path: '$.values[*]@other()',

test/test.errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
2525

2626
it('should throw with a bad result type', () => {
2727
expect(() => {
28-
// @ts-expect-error Bad argument
28+
// @ts-ignore -- Deliberately invalid result type.
2929
jsonpath({
3030
json: {children: [5]},
3131
path: '$..children',
32+
// @ts-ignore -- Deliberately invalid result type.
3233
resultType: 'badType'
3334
});
3435
}).to.throw(TypeError, 'Unknown result type');

test/test.eval.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
185185
before(setBuiltInState);
186186
it('preserves the prototype compatibility surface', () => {
187187
assert.strictEqual(JSONPath.prototype.evaluate, JSONPathClass.prototype.evaluate);
188+
// @ts-ignore -- Prototype compatibility surface.
188189
assert.strictEqual(JSONPath.prototype.safeVm, JSONPathClass.prototype.safeVm);
190+
// @ts-ignore -- Prototype compatibility surface.
189191
assert.strictEqual(JSONPath.prototype.vm, JSONPathClass.prototype.vm);
190192
});
191193
const json = {
@@ -213,6 +215,7 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
213215
it('eval as callback function', () => {
214216
/** @type {EvalValue} */
215217
const evalCb = (code, ctxt) => {
218+
// @ts-ignore -- Prototype compatibility surface.
216219
const script = new JSONPathClass.prototype.safeVm.Script(code);
217220
return script.runInNewContext(ctxt);
218221
};
@@ -229,6 +232,7 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
229232
const result = jsonpath({
230233
json,
231234
path: '$..[?(@.category === "reference")]',
235+
// @ts-ignore -- Prototype compatibility surface.
232236
eval: JSONPathClass.prototype.safeVm.Script
233237
});
234238
assert.deepEqual(result, expected);
@@ -239,6 +243,7 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
239243
const result = jsonpath({
240244
json,
241245
path: '$..[?(@.category.toLowerCase() === "reference")]',
246+
// @ts-ignore -- Prototype compatibility surface.
242247
eval: JSONPathClass.prototype.safeVm.Script,
243248
ignoreEvalErrors: true
244249
});

test/test.type-operators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('JSONPath - Type Operators', function () {
7474
*
7575
* @type {OtherTypeCallback}
7676
*/
77-
function endsIn99 (/** @type {number} */ val /* , path, parent, parentPropName */) {
78-
return (/\.99/u).test(val.toString());
77+
function endsIn99 (val /* , path, parent, parentPropName */) {
78+
return (/\.99/u).test(String(val));
7979
}
8080
const result = jsonpath({json, path: '$.store.book..*@other()', flatten: true, otherTypeCallback: endsIn99});
8181
assert.deepEqual(result, expected);

0 commit comments

Comments
 (0)