@swaggerexpert/jsonpath 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,7 +32,8 @@ The development of this library contributed to the identification and formal sub
32
32
  - [Parsing](#parsing)
33
33
  - [Concrete Syntax Tree (CST)](#concrete-syntax-tree-cst)
34
34
  - [Interpreting Parse result as XML](#interpreting-parse-result-as-xml)
35
- - [Parse statistics](#parse-statistics)
35
+ - [Statistics](#statistics)
36
+ - [Tracing](#tracing)
36
37
  - [Errors](#errors)
37
38
  - [Grammar](#grammar)
38
39
  - [More about JSONPath](#more-about-jsonpath)
@@ -288,7 +289,7 @@ const parseResult = parse('$.store.book[0].title');
288
289
  const xml = parseResult.ast.toXml();
289
290
  ```
290
291
 
291
- ###### Parse statistics
292
+ ###### Statistics
292
293
 
293
294
  `parse` function returns additional statistical information about the parsing process.
294
295
  Collection of the statistics can be enabled by setting `stats` option to `true`.
@@ -302,6 +303,25 @@ stats.displayStats(); // returns operator stats
302
303
  stats.displayHits(); // returns rules grouped by hit count
303
304
  ```
304
305
 
306
+ ###### Tracing
307
+
308
+ `parse` function returns additional tracing information about the parsing process.
309
+ Tracing can be enabled by setting `trace` option to `true`. Tracing is essential
310
+ for debugging failed matches or analyzing rule execution flow.
311
+
312
+ ```js
313
+ import { parse } from '@swaggerexpert/jsonpath';
314
+
315
+ const { result, trace } = parse('$fdfadfd', { trace: true });
316
+
317
+ result.success; // returns false
318
+ trace.displayTrace(); // returns trace information
319
+ ```
320
+
321
+ By combining information from `result` and `trace`, it is possible to analyze the parsing process in detail
322
+ and generate a messages like this: `'Syntax error at position 1, expected "[", ".", ".."'`. Please see this
323
+ [test file](https://github.com/swaggerexpert/jsonpath/blob/main/test/parse/trace.js) for more information how to achieve that.
324
+
305
325
  #### Errors
306
326
 
307
327
  `@swaggerexpert/jsonpath` provides a structured error class hierarchy,
@@ -12,6 +12,7 @@ const grammar = new _grammar.default();
12
12
  const parse = (jsonPath, {
13
13
  ast = new _JSONPathQueryCST.default(),
14
14
  stats = false,
15
+ trace = false,
15
16
  evaluator = _translate.default
16
17
  } = {}) => {
17
18
  if (typeof jsonPath !== 'string') {
@@ -21,6 +22,7 @@ const parse = (jsonPath, {
21
22
  const parser = new _apgLite.Parser();
22
23
  parser.ast = ast;
23
24
  if (stats) parser.stats = new _apgLite.Stats();
25
+ if (trace) parser.trace = new _apgLite.Trace();
24
26
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
25
27
  const computed = evaluator(ast, {
26
28
  result
@@ -29,6 +31,7 @@ const parse = (jsonPath, {
29
31
  result,
30
32
  ast,
31
33
  stats: parser.stats,
34
+ trace: parser.trace,
32
35
  computed
33
36
  };
34
37
  } catch (error) {
@@ -1,4 +1,4 @@
1
- import { Parser, Stats } from 'apg-lite';
1
+ import { Parser, Stats, Trace } from 'apg-lite';
2
2
  import Grammar from "../grammar.mjs";
3
3
  import translateEvaluator from "./evaluators/translate.mjs";
4
4
  import JSONPathQueryCST from "./ast/JSONPathQueryCST.mjs";
@@ -7,6 +7,7 @@ const grammar = new Grammar();
7
7
  const parse = (jsonPath, {
8
8
  ast = new JSONPathQueryCST(),
9
9
  stats = false,
10
+ trace = false,
10
11
  evaluator = translateEvaluator
11
12
  } = {}) => {
12
13
  if (typeof jsonPath !== 'string') {
@@ -16,6 +17,7 @@ const parse = (jsonPath, {
16
17
  const parser = new Parser();
17
18
  parser.ast = ast;
18
19
  if (stats) parser.stats = new Stats();
20
+ if (trace) parser.trace = new Trace();
19
21
  const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
20
22
  const computed = evaluator(ast, {
21
23
  result
@@ -24,6 +26,7 @@ const parse = (jsonPath, {
24
26
  result,
25
27
  ast,
26
28
  stats: parser.stats,
29
+ trace: parser.trace,
27
30
  computed
28
31
  };
29
32
  } catch (error) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.1.0",
6
+ "version": "1.2.0",
7
7
  "description": "RCF 9535 implementation of JSONPath",
8
8
  "main": "./cjs/index.cjs",
9
9
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export function parse(jsonpath: string, options?: ParseOptions): ParseResult;
6
6
  export interface ParseOptions {
7
7
  readonly ast?: AST;
8
8
  readonly stats?: boolean;
9
+ readonly trace?: boolean;
9
10
  }
10
11
 
11
12
  export interface AST {
@@ -20,6 +21,7 @@ export interface ParseResult {
20
21
  readonly ast: AST;
21
22
  readonly computed: Record<string, CSTNode>;
22
23
  readonly stats?: Stats;
24
+ readonly trace?: Trace;
23
25
  }
24
26
 
25
27
  export interface CSTNode {
@@ -35,6 +37,10 @@ export interface Stats {
35
37
  displayHits(): string;
36
38
  }
37
39
 
40
+ export interface Trace {
41
+ displayTrace(): string;
42
+ }
43
+
38
44
  /**
39
45
  * Grammar
40
46
  */