Skip to content

Commit

Permalink
build: docs incorrectly outputting function parameters with object li…
Browse files Browse the repository at this point in the history
…teral type (#29471)

We were extracting the type of a function parameter by splitting on `:` and taking the second item. The problem is that if the type is an object literal, it'll have more than one colon in its text.

These changes fix the issue by only looking for the first colon and extracting the name based on it.

(cherry picked from commit 315ebf1)
  • Loading branch information
crisbeto committed Jul 23, 2024
1 parent 1f9d800 commit 9af3494
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions tools/dgeni/common/normalize-function-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,53 @@ export type DefaultFunctionDoc = NormalizedFunctionParameters & ParameterContain
* an object.
*/
export function normalizeFunctionParameters(doc: DefaultFunctionDoc) {
if (doc.parameters) {
doc.parameters.forEach(parameter => {
let [parameterName, parameterType] = parameter.split(':');
if (!doc.parameters?.length) {
return;
}

// If the parameter is optional, the name here will contain a '?'. We store whether the
// parameter is optional and remove the '?' for comparison.
let isOptional = false;
if (parameterName.includes('?')) {
isOptional = true;
parameterName = parameterName.replace('?', '');
}
doc.parameters.forEach(parameter => {
const colonIndex = parameter.indexOf(':');
let parameterName: string;
let parameterType: string;

doc.params = doc.params || [];
if (colonIndex === -1) {
parameterName = parameter;
parameterType = '';
} else {
parameterName = parameter.slice(0, colonIndex);
parameterType = parameter.slice(colonIndex + 1).trim();
}

if (!parameterType) {
console.warn(
`Missing parameter type information (${parameterName}) in ` +
`${doc.fileInfo.relativePath}:${doc.startingLine}`,
);
return;
}
// If the parameter is optional, the name here will contain a '?'. We store whether the
// parameter is optional and remove the '?' for comparison.
let isOptional = false;
if (parameterName.includes('?')) {
isOptional = true;
parameterName = parameterName.replace('?', '');
}

const existingParameterInfo = doc.params.find(p => p.name == parameterName);
doc.params = doc.params || [];

if (!existingParameterInfo) {
doc.params.push({
name: parameterName,
type: parameterType.trim(),
isOptional: isOptional,
description: '',
});
} else {
existingParameterInfo.type = parameterType.trim();
existingParameterInfo.isOptional = isOptional;
}
});
}
if (!parameterType) {
console.warn(
`Missing parameter type information (${parameterName}) in ` +
`${doc.fileInfo.relativePath}:${doc.startingLine}`,
);
return;
}

const existingParameterInfo = doc.params.find(p => p.name == parameterName);

if (!existingParameterInfo) {
doc.params.push({
name: parameterName,
type: parameterType,
isOptional: isOptional,
description: '',
});
} else {
existingParameterInfo.type = parameterType;
existingParameterInfo.isOptional = isOptional;
}
});
}

0 comments on commit 9af3494

Please sign in to comment.