Skip to content

Commit 919eca8

Browse files
committed
[INTERNAL] lib/processors/jsdoc: Simplify base type handling
Improved handling of compound types, including union types (e.g., string | boolean) and array notations (e.g., string[]). This update ensures accurate and efficient identification of built-in types, reducing the likelihood of incorrect links in documentation due to unrecognized types. Cherry-picked from UI5/openui5@2dc53cf86.
1 parent 40fe736 commit 919eca8

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

lib/processors/jsdoc/lib/transformApiJson.js

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Node script to preprocess api.json files for use in the UI5 SDKs.
33
*
44
* (c) Copyright 2009-2024 SAP SE or an SAP affiliate company.
@@ -43,7 +43,7 @@ function fnCreateTypesArr(sTypes) {
4343
});
4444
}
4545

46-
/*
46+
/**
4747
* Transforms the api.json as created by the JSDoc build into a pre-processed api.json file suitable for the SDK.
4848
*
4949
* The pre-processing includes formatting of type references, rewriting of links and other time consuming calculations.
@@ -121,8 +121,66 @@ function transformer(sInputFile, sOutputFile, sLibraryFile, vDependencyAPIFiles,
121121
* @param {object} oChainObject chain object
122122
*/
123123
let transformApiJson = function (oChainObject) {
124+
/**
125+
* Check if a type is a built-in type, handling both simple
126+
* and compound types gracefully.
127+
*
128+
* @param {string} type A type to check
129+
* @returns {boolean} true if the type is built-in, otherwise false
130+
*/
124131
function isBuiltInType(type) {
125-
return formatters._baseTypes.indexOf(type) >= 0;
132+
const builtInTypes = formatters._baseTypes;
133+
134+
// Early return if the type is directly in baseTypes
135+
if (builtInTypes.includes(type)) {
136+
return true;
137+
}
138+
139+
// Handle array notation directly
140+
if (type.endsWith("[]")) {
141+
return builtInTypes.includes(type.slice(0, -2));
142+
}
143+
144+
// Check if the type is a union type
145+
if (type.includes("|")) {
146+
const unionParts = type.split("|").map(part => part.trim());
147+
return unionParts.every(part => isBuiltInType(part));
148+
}
149+
150+
// Predefined regex patterns for reuse
151+
const arrayRegex = /Array<(.+)>/;
152+
const arrayDotRegex = /Array\.<(.+)>/;
153+
const objectRegex = /Object<([^,]+),([^>]+)>/;
154+
const objectDotRegex = /Object\.<([^,]+),([^>]+)>/;
155+
156+
// Check if the type is a generic Array type
157+
const arrayMatch = arrayRegex.exec(type);
158+
if (arrayMatch) {
159+
const innerType = arrayMatch[1];
160+
return isBuiltInType(innerType);
161+
}
162+
163+
const arrayDotMatch = arrayDotRegex.exec(type);
164+
if (arrayDotMatch) {
165+
const innerType = arrayDotMatch[1];
166+
return isBuiltInType(innerType);
167+
}
168+
169+
// Check if the type is a generic Object type
170+
const objectMatch = objectRegex.exec(type);
171+
if (objectMatch) {
172+
const innerTypes = [objectMatch[1], objectMatch[2]].map(t => t.trim());
173+
return innerTypes.every(innerType => isBuiltInType(innerType));
174+
}
175+
176+
const objectDotMatch = objectDotRegex.exec(type);
177+
if (objectDotMatch) {
178+
const innerTypes = [objectDotMatch[1], objectDotMatch[2]].map(t => t.trim());
179+
return innerTypes.every(innerType => isBuiltInType(innerType));
180+
}
181+
182+
// Fallback case: if none of the above matched, return false
183+
return false;
126184
}
127185

128186
/**
@@ -1106,32 +1164,28 @@ function transformer(sInputFile, sOutputFile, sLibraryFile, vDependencyAPIFiles,
11061164
});
11071165
}
11081166

1109-
/*
1167+
/**
11101168
* =====================================================================================================================
11111169
* IMPORTANT NOTE: Formatter code is a copy from APIDetail.controller.js with a very little modification and mocking and
11121170
* code can be significantly improved
11131171
* =====================================================================================================================
11141172
*/
11151173
let formatters = {
1116-
11171174
_sTopicId: "",
11181175
_oTopicData: {},
11191176
_baseTypes: [
1120-
// TODO this list URGENTLY needs to be replaced by the Type parser and a much smaller list
11211177
"sap.ui.core.any",
11221178
"sap.ui.core.object",
11231179
"sap.ui.core.function",
1124-
"sap.ui.core.number", // TODO discuss with Thomas, type does not exist
1180+
"sap.ui.core.number",
11251181
"sap.ui.core.float",
11261182
"sap.ui.core.int",
11271183
"sap.ui.core.boolean",
11281184
"sap.ui.core.string",
11291185
"sap.ui.core.void",
11301186
"null",
11311187
"any",
1132-
"any[]",
11331188
"Error",
1134-
"Error[]",
11351189
"array",
11361190
"element",
11371191
"Element",
@@ -1141,19 +1195,11 @@ function transformer(sInputFile, sOutputFile, sLibraryFile, vDependencyAPIFiles,
11411195
"QUnit.Assert",
11421196
"object",
11431197
"Object",
1144-
"object[]",
1145-
"object|object[]",
1146-
"[object Object][]",
1147-
"Array<[object Object]>",
1148-
"Array.<[object Object]>",
1149-
"Object<string,string>",
1150-
"Object.<string,string>",
11511198
"function",
11521199
"float",
11531200
"int",
11541201
"boolean",
11551202
"string",
1156-
"string[]",
11571203
"number",
11581204
"map",
11591205
"promise",
@@ -1163,7 +1209,8 @@ function transformer(sInputFile, sOutputFile, sLibraryFile, vDependencyAPIFiles,
11631209
"Touch",
11641210
"TouchList",
11651211
"undefined",
1166-
"this"
1212+
"this",
1213+
"[object Object]"
11671214
],
11681215
ANNOTATIONS_LINK: 'http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part3-csdl.html',
11691216
ANNOTATIONS_NAMESPACE_LINK: 'http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/vocabularies/',

0 commit comments

Comments
 (0)