Skip to content

Commit

Permalink
chore(dotnet): improve name generation for objects (#5860)
Browse files Browse the repository at this point in the history
  • Loading branch information
avodovnik committed Mar 23, 2021
1 parent 9f1b2f6 commit 3a27bdd
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions utils/doclint/generateDotnetApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,37 @@ function generateNameDefault(member, name, t, parent) {
let enumName = generateEnumNameIfApplicable(member, name, t, parent);
if (!enumName && member) {
if (member.kind === 'method' || member.kind === 'property') {
// this should be easy to name... let's call it the same as the argument (eternal optimist)
let probableName = `${parent.name}${translateMemberName(``, name, null)}`;
let probableType = additionalTypes.get(probableName);
if (probableType) {
// compare it with what?
if (probableType.expression != t.expression) {
throw new Error(`Non-matching types with the same name. Panic.`);
let names = [
parent.alias || parent.name,
translateMemberName(``, member.alias || member.name, null),
translateMemberName(``, name, null),
];
if (names[2] === names[1])
names.pop(); // get rid of duplicates, cheaply
let attemptedName = names.pop();
let typesDiffer = function (left, right) {
if (left.expression && right.expression)
return left.expression !== right.expression;
return JSON.stringify(right.properties) !== JSON.stringify(left.properties);
}
while (true) {
// crude attempt at removing plurality
if (attemptedName.endsWith('s')
&& !["properties", "httpcredentials"].includes(attemptedName.toLowerCase()))
attemptedName = attemptedName.substring(0, attemptedName.length - 1);
let probableType = additionalTypes.get(attemptedName);
if ((probableType && typesDiffer(t, probableType))
|| (["Value"].includes(attemptedName))) {
if (!names.length)
throw new Error(`Ran out of possible names: ${attemptedName}`);
attemptedName = `${names.pop()}${attemptedName}`;
continue;
} else {
additionalTypes.set(attemptedName, t);
}
} else {
additionalTypes.set(probableName, t);
break;
}

return probableName;
return attemptedName;
}

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

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

output(XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth));
Expand Down

0 comments on commit 3a27bdd

Please sign in to comment.