-
Notifications
You must be signed in to change notification settings - Fork 0
/
visit.js
49 lines (42 loc) · 1.56 KB
/
visit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @copyright Copyright 2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformer-base/visit.js"
*/
'use strict';
const assert = require('node:assert');
const toJsonPointer = require('./lib/to-json-pointer.js');
/** Visits a property being transformed by an OpenApiTransformerBase by adding
* its name to transformPath while calling a given method with a given value.
*
* @template ArgsType, TransformedType
* @param {!module:openapi-transformer-base} transformer Transformer on which
* transformPath will be modified.
* @param {function(this:!module:openapi-transformer-base, ...ArgsType):
* TransformedType} method Method to be called.
* @param {string} propName Name of property being visited.
* @param {ArgsType} args Argument to method (usually property value).
* @returns {TransformedType} Result of calling method on args.
*/
module.exports =
function visit(transformer, method, propName, ...args) {
transformer.transformPath.push(propName);
let handlingException = false;
try {
return method.apply(transformer, args);
} catch (err) {
handlingException = true;
if (err instanceof Error && !Object.hasOwn(err, 'transformPath')) {
err.transformPath = [...transformer.transformPath];
err.message +=
` (while transforming ${toJsonPointer(err.transformPath)})`;
}
throw err;
} finally {
const popProp = transformer.transformPath.pop();
// Avoid clobbering an exception which is already propagating
if (!handlingException) {
assert.strictEqual(popProp, propName);
}
}
};