|
1 | 1 | import { toValue } from '@swagger-api/apidom-core';
|
2 | 2 |
|
3 |
| -const ParameterMacroVisitor = ({ parameterMacro }) => { |
4 |
| - let macroOperation = null; |
| 3 | +import compose from '../utils/compose.js'; |
| 4 | +import toPath from '../utils/to-path.js'; |
| 5 | + |
| 6 | +const ParameterMacroVisitor = compose({ |
| 7 | + init({ parameterMacro, options }) { |
| 8 | + this.parameterMacro = parameterMacro; |
| 9 | + this.options = options; |
| 10 | + }, |
| 11 | + props: { |
| 12 | + parameterMacro: null, |
| 13 | + options: null, |
| 14 | + macroOperation: null, |
5 | 15 |
|
6 |
| - return { |
7 | 16 | OperationElement: {
|
8 | 17 | enter(operationElement) {
|
9 |
| - macroOperation = operationElement; |
| 18 | + this.macroOperation = operationElement; |
10 | 19 | },
|
11 | 20 | leave() {
|
12 |
| - macroOperation = null; |
| 21 | + this.macroOperation = null; |
13 | 22 | },
|
14 | 23 | },
|
15 | 24 | ParameterElement: {
|
16 |
| - leave(parameterElement) { |
17 |
| - const pojoOperation = macroOperation === null ? null : toValue(macroOperation); |
| 25 | + leave(parameterElement, key, parent, path, ancestors) { |
| 26 | + const pojoOperation = this.macroOperation === null ? null : toValue(this.macroOperation); |
18 | 27 | const pojoParameter = toValue(parameterElement);
|
19 |
| - const defaultValue = parameterMacro(pojoOperation, pojoParameter); |
20 | 28 |
|
21 |
| - parameterElement.set('default', defaultValue); |
| 29 | + try { |
| 30 | + const macroValue = this.parameterMacro(pojoOperation, pojoParameter); |
| 31 | + parameterElement.set('default', macroValue); |
| 32 | + } catch (error) { |
| 33 | + const macroError = new Error(error, { cause: error }); |
| 34 | + macroError.fullPath = toPath([...ancestors, parent]); |
| 35 | + this.options.dereference.dereferenceOpts?.errors?.push?.(macroError); |
| 36 | + } |
22 | 37 | },
|
23 | 38 | },
|
24 |
| - }; |
25 |
| -}; |
| 39 | + }, |
| 40 | +}); |
26 | 41 |
|
27 | 42 | export default ParameterMacroVisitor;
|
0 commit comments