Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 3c0427b

Browse files
committed
refactor(oas3): move source map filtering into main parser
This logic does not belong in parseOpenAPIObject as it isn't directly related to parsing the OpenAPI Object and instead a general parsing behaviour added after parsing components.
1 parent d566e23 commit 3c0427b

File tree

2 files changed

+73
-75
lines changed

2 files changed

+73
-75
lines changed

packages/openapi3-parser/lib/parser.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-underscore-dangle */
2+
13
const R = require('ramda');
24
const parseYAML = require('./parser/parseYAML');
35

@@ -9,12 +11,81 @@ const parseOpenAPIObject = require('./parser/oas/parseOpenAPIObject');
911

1012
const isObjectOrAnnotation = R.either(isObject, isAnnotation);
1113

14+
const recurseSkippingAnnotations = R.curry((visitor, e) => {
15+
if (isAnnotation(e)) {
16+
return e;
17+
}
18+
if (e) {
19+
if (!e.element) {
20+
return e;
21+
}
22+
23+
visitor(e);
24+
25+
if (e._attributes) {
26+
e.attributes.forEach((value, key, member) => {
27+
recurseSkippingAnnotations(visitor, member);
28+
});
29+
}
30+
if (e._meta) {
31+
e.meta.forEach((value, key, member) => {
32+
recurseSkippingAnnotations(visitor, member);
33+
});
34+
}
35+
if (e.content) {
36+
if (Array.isArray(e.content)) {
37+
e.content.forEach((value) => {
38+
recurseSkippingAnnotations(visitor, value);
39+
});
40+
}
41+
if (e.content.key) {
42+
recurseSkippingAnnotations(visitor, e.content.key);
43+
if (e.content.value) {
44+
recurseSkippingAnnotations(visitor, e.content.value);
45+
}
46+
}
47+
if (e.content.element) {
48+
recurseSkippingAnnotations(visitor, e.content);
49+
}
50+
}
51+
}
52+
return e;
53+
});
54+
55+
function removeSourceMap(e) {
56+
if (e._attributes) {
57+
e.attributes.remove('sourceMap');
58+
}
59+
}
60+
61+
function removeColumnLine(result) {
62+
if (result._attributes) {
63+
const sourceMaps = result.attributes.get('sourceMap');
64+
if (sourceMaps) {
65+
sourceMaps.content.forEach((sourceMap) => {
66+
sourceMap.content.forEach((sourcePoint) => {
67+
sourcePoint.content.forEach((sourceCoordinate) => {
68+
if (sourceCoordinate._attributes) {
69+
sourceCoordinate.attributes.remove('line');
70+
sourceCoordinate.attributes.remove('column');
71+
}
72+
});
73+
});
74+
});
75+
}
76+
}
77+
}
78+
79+
const filterColumnLine = recurseSkippingAnnotations(removeColumnLine);
80+
const filterSourceMaps = recurseSkippingAnnotations(removeSourceMap);
81+
1282
function parse(source, context) {
1383
const document = parseYAML(source, context);
1484

1585
const parseDocument = pipeParseResult(context.namespace,
1686
R.unless(isObjectOrAnnotation, createError(context.namespace, 'Source document is not an object')),
17-
R.unless(isAnnotation, parseOpenAPIObject(context)));
87+
R.unless(isAnnotation, parseOpenAPIObject(context)),
88+
context.options.generateSourceMap ? filterColumnLine : filterSourceMaps);
1889

1990
return R.chain(parseDocument, document);
2091
}

packages/openapi3-parser/lib/parser/oas/parseOpenAPIObject.js

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable no-underscore-dangle */
2-
31
const R = require('ramda');
42

53
const {
@@ -31,74 +29,6 @@ const unsupportedKeys = ['tags', 'externalDocs'];
3129
*/
3230
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));
3331

34-
const recurseSkippingAnnotations = R.curry((visitor, e) => {
35-
if (isAnnotation(e)) {
36-
return e;
37-
}
38-
if (e) {
39-
if (!e.element) {
40-
return e;
41-
}
42-
43-
visitor(e);
44-
45-
if (e._attributes) {
46-
e.attributes.forEach((value, key, member) => {
47-
recurseSkippingAnnotations(visitor, member);
48-
});
49-
}
50-
if (e._meta) {
51-
e.meta.forEach((value, key, member) => {
52-
recurseSkippingAnnotations(visitor, member);
53-
});
54-
}
55-
if (e.content) {
56-
if (Array.isArray(e.content)) {
57-
e.content.forEach((value) => {
58-
recurseSkippingAnnotations(visitor, value);
59-
});
60-
}
61-
if (e.content.key) {
62-
recurseSkippingAnnotations(visitor, e.content.key);
63-
if (e.content.value) {
64-
recurseSkippingAnnotations(visitor, e.content.value);
65-
}
66-
}
67-
if (e.content.element) {
68-
recurseSkippingAnnotations(visitor, e.content);
69-
}
70-
}
71-
}
72-
return e;
73-
});
74-
75-
function removeSourceMap(e) {
76-
if (e._attributes) {
77-
e.attributes.remove('sourceMap');
78-
}
79-
}
80-
81-
function removeColumnLine(result) {
82-
if (result._attributes) {
83-
const sourceMaps = result.attributes.get('sourceMap');
84-
if (sourceMaps) {
85-
sourceMaps.content.forEach((sourceMap) => {
86-
sourceMap.content.forEach((sourcePoint) => {
87-
sourcePoint.content.forEach((sourceCoordinate) => {
88-
if (sourceCoordinate._attributes) {
89-
sourceCoordinate.attributes.remove('line');
90-
sourceCoordinate.attributes.remove('column');
91-
}
92-
});
93-
});
94-
});
95-
}
96-
}
97-
}
98-
99-
const filterColumnLine = recurseSkippingAnnotations(removeColumnLine);
100-
const filterSourceMaps = recurseSkippingAnnotations(removeSourceMap);
101-
10232
function parseOASObject(context, object) {
10333
const { namespace } = context;
10434

@@ -183,10 +113,7 @@ function parseOASObject(context, object) {
183113
return api;
184114
});
185115

186-
if (context.options.generateSourceMap) {
187-
return filterColumnLine(parseOASObject(object));
188-
}
189-
return filterSourceMaps(parseOASObject(object));
116+
return parseOASObject(object);
190117
}
191118

192119
module.exports = R.curry(parseOASObject);

0 commit comments

Comments
 (0)