Skip to content

Commit 3a27bdd

Browse files
authored
chore(dotnet): improve name generation for objects (#5860)
1 parent 9f1b2f6 commit 3a27bdd

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

utils/doclint/generateDotnetApi.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,37 @@ function generateNameDefault(member, name, t, parent) {
320320
let enumName = generateEnumNameIfApplicable(member, name, t, parent);
321321
if (!enumName && member) {
322322
if (member.kind === 'method' || member.kind === 'property') {
323-
// this should be easy to name... let's call it the same as the argument (eternal optimist)
324-
let probableName = `${parent.name}${translateMemberName(``, name, null)}`;
325-
let probableType = additionalTypes.get(probableName);
326-
if (probableType) {
327-
// compare it with what?
328-
if (probableType.expression != t.expression) {
329-
throw new Error(`Non-matching types with the same name. Panic.`);
323+
let names = [
324+
parent.alias || parent.name,
325+
translateMemberName(``, member.alias || member.name, null),
326+
translateMemberName(``, name, null),
327+
];
328+
if (names[2] === names[1])
329+
names.pop(); // get rid of duplicates, cheaply
330+
let attemptedName = names.pop();
331+
let typesDiffer = function (left, right) {
332+
if (left.expression && right.expression)
333+
return left.expression !== right.expression;
334+
return JSON.stringify(right.properties) !== JSON.stringify(left.properties);
335+
}
336+
while (true) {
337+
// crude attempt at removing plurality
338+
if (attemptedName.endsWith('s')
339+
&& !["properties", "httpcredentials"].includes(attemptedName.toLowerCase()))
340+
attemptedName = attemptedName.substring(0, attemptedName.length - 1);
341+
let probableType = additionalTypes.get(attemptedName);
342+
if ((probableType && typesDiffer(t, probableType))
343+
|| (["Value"].includes(attemptedName))) {
344+
if (!names.length)
345+
throw new Error(`Ran out of possible names: ${attemptedName}`);
346+
attemptedName = `${names.pop()}${attemptedName}`;
347+
continue;
348+
} else {
349+
additionalTypes.set(attemptedName, t);
330350
}
331-
} else {
332-
additionalTypes.set(probableName, t);
351+
break;
333352
}
334-
335-
return probableName;
353+
return attemptedName;
336354
}
337355

338356
if (member.kind === 'event') {
@@ -528,7 +546,7 @@ function renderMethod(member, parent, output, name) {
528546
};
529547

530548
member.argsArray
531-
.sort((a, b) => b.alias === 'options' ? -1 : 0) //move options to the back to the arguments list
549+
.sort((a, b) => b.alias === 'options' ? -1 : 0) //move options to the back to the arguments list
532550
.forEach(parseArg);
533551

534552
output(XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth));

0 commit comments

Comments
 (0)