Skip to content

Commit 8c96c06

Browse files
committed
Export visit utility function as visit.js
This is likely to be useful to classes which extend OpenApiTransformerBase. Signed-off-by: Kevin Locke <[email protected]>
1 parent 4c6265a commit 8c96c06

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

index.js

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
'use strict';
88

9-
const assert = require('assert');
109
const { METHODS } = require('http');
10+
const visit = require('./visit.js');
1111

1212
const { isArray } = Array;
1313

@@ -18,44 +18,6 @@ const { isArray } = Array;
1818
*/
1919
const httpMethodSet = new Set(METHODS);
2020

21-
/** Visits a property by adding its name to transformPath while calling a
22-
* given method with a given value.
23-
*
24-
* @private
25-
* @template ArgsType, TransformedType
26-
* @param {!OpenApiTransformerBase} transformer Transformer on which
27-
* transformPath will be modified.
28-
* @param {function(this:!OpenApiTransformerBase, ...ArgsType):
29-
* TransformedType} method Method to be called.
30-
* @param {string} propName Name of property being visited.
31-
* @param {ArgsType} args Argument to method (usually property value).
32-
* @returns {TransformedType} Result of calling method on args.
33-
*/
34-
function visit(transformer, method, propName, ...args) {
35-
transformer.transformPath.push(propName);
36-
37-
let handlingException = false;
38-
try {
39-
return method.apply(transformer, args);
40-
} catch (err) {
41-
handlingException = true;
42-
if (err instanceof Error && !hasOwnProperty.call(err, 'transformPath')) {
43-
err.transformPath = [...transformer.transformPath];
44-
err.message +=
45-
` (while transforming /${err.transformPath.join('/')})`;
46-
}
47-
48-
throw err;
49-
} finally {
50-
const popProp = transformer.transformPath.pop();
51-
52-
// Avoid clobbering an exception which is already propagating
53-
if (!handlingException) {
54-
assert.strictEqual(popProp, propName);
55-
}
56-
}
57-
}
58-
5921
/** Transforms a value which has type object<string,ValueType> but is not
6022
* defined as Map[string,ValueType] in OpenAPI.
6123
*

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"main": "index.js",
2828
"exports": {
2929
".": "./index.js",
30-
"./package.json": "./package.json"
30+
"./package.json": "./package.json",
31+
"./visit.js": "./visit.js"
3132
},
3233
"//": "All scripts should run in POSIX sh and Windows cmd.exe",
3334
"scripts": {

visit.js

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

0 commit comments

Comments
 (0)