Skip to content

Commit 6559329

Browse files
committed
chore: lint, update devDeps, fix TS; fixes JSONPath-Plus#114 ; closes JSONPath-Plus#113 ; closes JSONPath-Plus#115
1 parent 1dabe0b commit 6559329

47 files changed

Lines changed: 8952 additions & 5267 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.ncurc.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
reject: [
5+
// Until typedoc supports
6+
'typescript'
7+
]
8+
};

badges/tests-badge.svg

Lines changed: 1 addition & 1 deletion
Loading

bin/jsonpath-cli.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import {readFile} from 'fs/promises';
3-
import {JSONPath as jsonpath} from '../dist/index-node-esm.js';
3+
import {JSONPath as jsonpath} from '../src/jsonpath-node.js';
44

55
const file = process.argv[2];
66
const path = process.argv[3];
@@ -17,11 +17,16 @@ try {
1717
}
1818

1919
/**
20-
* @typedef {any} JSON
20+
* @typedef {JSONValue[]} JSONArray
2121
*/
2222

2323
/**
24-
* @param {JSON} json
24+
* @typedef {null|boolean|number|string|
25+
* {[key: string]: JSONValue}|JSONArray} JSONValue
26+
*/
27+
28+
/**
29+
* @param {JSONValue} json
2530
* @param {string} pth
2631
* @returns {void}
2732
*/

demo/index.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/// <reference path="./types.d.ts" />
1+
/* eslint-disable import-x/unambiguous -- Demo */
22
/* globals JSONPath, LZString -- Test UMD */
3-
/* eslint-disable import/unambiguous -- Demo */
3+
/// <reference path="./types.d.ts" />
44

55
// Todo: Extract testing example paths/contents and use for a
66
// pulldown that can populate examples
@@ -12,37 +12,51 @@
1212
// Todo: Could add JSON/JS syntax highlighting in sample and result,
1313
// ideally with a jsonpath-plus parser highlighter as well
1414

15-
const $ = (s) => document.querySelector(s);
15+
/**
16+
* @param {string} s
17+
* @returns {HTMLElement}
18+
*/
19+
const $ = (s) => /** @type {HTMLElement} */ (document.querySelector(s));
1620

17-
const jsonpathEl = $('#jsonpath');
18-
const jsonSample = $('#jsonSample');
21+
/**
22+
* @param {string} s
23+
* @returns {HTMLInputElement}
24+
*/
25+
const $i = (s) => /** @type {HTMLInputElement} */ (document.querySelector(s));
26+
27+
const jsonpathEl = $i('#jsonpath');
28+
const jsonSample = $i('#jsonSample');
1929

2030
const updateUrl = () => {
2131
const path = jsonpathEl.value;
2232
const jsonText = LZString.compressToEncodedURIComponent(jsonSample.value);
2333
const url = new URL(location.href);
2434
url.searchParams.set('path', path);
2535
url.searchParams.set('json', jsonText);
26-
url.searchParams.set('eval', $('#eval').value);
27-
url.searchParams.set('ignoreEvalErrors', $('#ignoreEvalErrors').value);
28-
history.replaceState(null, '', url.toString());
36+
url.searchParams.set('eval', $i('#eval').value);
37+
url.searchParams.set('ignoreEvalErrors', $i('#ignoreEvalErrors').value);
38+
history.replaceState(null, '', url.href);
2939
};
3040

3141
const loadUrl = () => {
3242
const url = new URL(location.href);
3343
if (url.searchParams.has('path')) {
34-
jsonpathEl.value = url.searchParams.get('path');
44+
jsonpathEl.value = /** @type {string} */ (url.searchParams.get('path'));
3545
}
3646
if (url.searchParams.has('json')) {
3747
jsonSample.value = LZString.decompressFromEncodedURIComponent(
38-
url.searchParams.get('json')
48+
/** @type {string} */ (url.searchParams.get('json'))
3949
);
4050
}
4151
if (url.searchParams.has('eval')) {
42-
$('#eval').value = url.searchParams.get('eval');
52+
$i('#eval').value = /** @type {string} */ (
53+
url.searchParams.get('eval')
54+
);
4355
}
4456
if (url.searchParams.has('ignoreEvalErrors')) {
45-
$('#ignoreEvalErrors').value = url.searchParams.get('ignoreEvalErrors');
57+
$i('#ignoreEvalErrors').value = /** @type {string} */ (
58+
url.searchParams.get('ignoreEvalErrors')
59+
);
4660
}
4761
};
4862

@@ -52,7 +66,7 @@ const updateResults = () => {
5266
setTimeout(() => {
5367
jsonSample.reportValidity();
5468
jsonpathEl.reportValidity();
55-
});
69+
}, 0);
5670
};
5771
let json;
5872
jsonSample.setCustomValidity('');
@@ -61,24 +75,29 @@ const updateResults = () => {
6175
try {
6276
json = JSON.parse(jsonSample.value);
6377
} catch (err) {
64-
jsonSample.setCustomValidity('Error parsing JSON: ' + err.toString());
78+
jsonSample.setCustomValidity(
79+
'Error parsing JSON: ' +
80+
/** @type {Error} */ (err).toString()
81+
);
6582
reportValidity();
6683
return;
6784
}
6885
try {
6986
const result = new JSONPath.JSONPath({
7087
path: jsonpathEl.value,
7188
json,
72-
eval: $('#eval').value === 'false' ? false : $('#eval').value,
73-
ignoreEvalErrors: $('#ignoreEvalErrors').value === 'true'
89+
eval: $i('#eval').value === 'false' ? false : $i('#eval').value,
90+
ignoreEvalErrors: $i('#ignoreEvalErrors').value === 'true'
7491
});
75-
$('#results').value = JSON.stringify(result, null, 2);
92+
$i('#results').value = JSON.stringify(result, null, 2);
7693
} catch (err) {
7794
jsonpathEl.setCustomValidity(
78-
'Error executing JSONPath: ' + err.toString()
95+
'Error executing JSONPath: ' +
96+
/** @type {Error} */
97+
(err).toString()
7998
);
8099
reportValidity();
81-
$('#results').value = '';
100+
$i('#results').value = '';
82101
}
83102
};
84103

demo/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"lib": ["es2024", "dom"]
5+
},
6+
"include": ["."]
7+
}

dist/Safe-Script.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type AssignmentExpression = import("@jsep-plugin/assignment").AssignmentExpression;
2+
export type Substitution = any;
3+
export type AnyParameter = any;
4+
export type Substitutions = Record<string, Substitution>;
5+
/**
6+
* A replacement for NodeJS' VM.Script which is also {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP | Content Security Policy} friendly.
7+
*/
8+
export class SafeScript {
9+
/**
10+
* @param {string} expr Expression to evaluate
11+
*/
12+
constructor(expr: string);
13+
code: string;
14+
ast: jsep.Expression;
15+
/**
16+
* @param {object} context Object whose items will be added
17+
* to evaluation
18+
* @returns {EvaluatedResult} Result of evaluated code
19+
*/
20+
runInNewContext(context: object): EvaluatedResult;
21+
}
22+
import jsep from 'jsep';
23+
import type { EvaluatedResult } from './jsonpath.js';

0 commit comments

Comments
 (0)