chore(dotnet): improve name generation for objects (#5860)

This commit is contained in:
Anže Vodovnik 2021-03-23 10:44:50 +01:00 committed by GitHub
parent 9f1b2f68bf
commit 3a27bdd3e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -320,19 +320,37 @@ function generateNameDefault(member, name, t, parent) {
let enumName = generateEnumNameIfApplicable(member, name, t, parent); let enumName = generateEnumNameIfApplicable(member, name, t, parent);
if (!enumName && member) { if (!enumName && member) {
if (member.kind === 'method' || member.kind === 'property') { 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 names = [
let probableName = `${parent.name}${translateMemberName(``, name, null)}`; parent.alias || parent.name,
let probableType = additionalTypes.get(probableName); translateMemberName(``, member.alias || member.name, null),
if (probableType) { translateMemberName(``, name, null),
// compare it with what? ];
if (probableType.expression != t.expression) { if (names[2] === names[1])
throw new Error(`Non-matching types with the same name. Panic.`); names.pop(); // get rid of duplicates, cheaply
} let attemptedName = names.pop();
} else { let typesDiffer = function (left, right) {
additionalTypes.set(probableName, t); if (left.expression && right.expression)
return left.expression !== right.expression;
return JSON.stringify(right.properties) !== JSON.stringify(left.properties);
} }
while (true) {
return probableName; // 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);
}
break;
}
return attemptedName;
} }
if (member.kind === 'event') { if (member.kind === 'event') {
@ -528,7 +546,7 @@ function renderMethod(member, parent, output, name) {
}; };
member.argsArray 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); .forEach(parseArg);
output(XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth)); output(XmlDoc.renderXmlDoc(member.spec, maxDocumentationColumnWidth));