Skip to content

Commit 04a0c41

Browse files
committed
feat(evaluate): activate parse tracing without evaluation tracing
1 parent 0597540 commit 04a0c41

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/evaluate/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ const evaluate = (
2121
trace: parseTrace,
2222
} = parse(jsonPointer, { trace: !!trace });
2323

24-
const tracer = trace
25-
? new TraceBuilder(trace, {
26-
jsonPointer,
27-
referenceTokens,
28-
strictArrays,
29-
strictObjects,
30-
realm,
31-
value,
32-
})
33-
: null;
24+
const tracer =
25+
typeof trace === 'object' && trace !== null
26+
? new TraceBuilder(trace, {
27+
jsonPointer,
28+
referenceTokens,
29+
strictArrays,
30+
strictObjects,
31+
realm,
32+
value,
33+
})
34+
: null;
3435

3536
if (!parseResult.success) {
3637
let message = `Invalid JSON Pointer: "${jsonPointer}". Syntax error at position ${parseResult.maxMatched}`;

test/evaluate/trace.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert } from 'chai';
33
import { evaluate, JSONPointerEvaluateError } from '../../src/index.js';
44

55
describe('evaluate', function () {
6-
context('given trace option', function () {
6+
context('given trace option as object', function () {
77
specify('should trace successful evaluation', function () {
88
const data = { a: { b: 'c' } };
99
const trace = {};
@@ -88,19 +88,40 @@ describe('evaluate', function () {
8888
});
8989

9090
specify('should produce error message with tracing info', function () {
91+
const trace = {};
92+
9193
assert.throws(
92-
() => evaluate({ a: { b: 'c' } }, '1', { trace: {} }),
94+
() => evaluate({ a: { b: 'c' } }, '1', { trace }),
9395
JSONPointerEvaluateError,
9496
'Invalid JSON Pointer: "1". Syntax error at position 0, expected "/"',
9597
);
98+
assert.lengthOf(trace.steps, 1);
9699
});
100+
});
97101

98-
specify('should produce error message without tracking info', function () {
102+
context('given trace option as boolean', function () {
103+
specify('should produce error message with tracing info', function () {
99104
assert.throws(
100-
() => evaluate({ a: { b: 'c' } }, '1'),
105+
() => evaluate({ a: { b: 'c' } }, '1', { trace: true }),
101106
JSONPointerEvaluateError,
102-
'Invalid JSON Pointer: "1". Syntax error at position 0',
107+
'Invalid JSON Pointer: "1". Syntax error at position 0, expected "/"',
103108
);
104109
});
105110
});
111+
112+
specify('should produce error message without tracking info #1', function () {
113+
assert.throws(
114+
() => evaluate({ a: { b: 'c' } }, '1'),
115+
JSONPointerEvaluateError,
116+
'Invalid JSON Pointer: "1". Syntax error at position 0',
117+
);
118+
});
119+
120+
specify('should produce error message without tracking info #2', function () {
121+
assert.throws(
122+
() => evaluate({ a: { b: 'c' } }, '1', { trace: false }),
123+
JSONPointerEvaluateError,
124+
'Invalid JSON Pointer: "1". Syntax error at position 0',
125+
);
126+
});
106127
});

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface EvaluationOptions<R extends EvaluationRealm = JSONEvaluationRea
112112
strictArrays?: boolean;
113113
strictObjects?: boolean;
114114
realm?: R;
115-
trace?: false | Partial<EvaluationTrace>;
115+
trace?: boolean | Partial<EvaluationTrace>;
116116
}
117117

118118
export declare abstract class EvaluationRealm {

0 commit comments

Comments
 (0)