fix(types): Properly format nested test type docstrings (#36126)

This commit is contained in:
Adam Gastineau 2025-05-29 04:40:58 -07:00 committed by GitHub
parent 111f21ebac
commit 6626bba937
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 1832 additions and 1820 deletions

File diff suppressed because it is too large Load Diff

View File

@ -92,7 +92,7 @@ class TypesGenerator {
return '';
handledClasses.add(className);
return this.writeComment(docClass.comment, '') + '\n';
}, (className, methodName, overloadIndex) => {
}, (className, methodName, overloadIndex, indent) => {
if (methodName === '__call')
methodName = '(call)';
const docClass = this.docClassForName(className);
@ -110,7 +110,7 @@ class TypesGenerator {
return '';
throw new Error(`Unknown override method "${className}.${methodName}"`);
}
return this.memberJSDOC(method, ' ').trimLeft();
return this.memberJSDOC(method, indent).trimLeft();
}, (className) => {
const docClass = this.docClassForName(className);
if (!docClass || !this.shouldGenerate(docClass.name))

View File

@ -20,7 +20,7 @@ const ts = require('typescript');
/**
* @param {string} filePath
* @param {(className: string) => string} commentForClass
* @param {(className: string, methodName: string, overloadIndex: number) => string} commentForMethod
* @param {(className: string, methodName: string, overloadIndex: number, indent: string) => string} commentForMethod
* @param {(className: string) => string} extraForClass
*/
async function parseOverrides(filePath, commentForClass, commentForMethod, extraForClass) {
@ -42,6 +42,18 @@ async function parseOverrides(filePath, commentForClass, commentForMethod, extra
}
return src;
/**
* @param {number} pos
* @returns {string}
*/
function getIndentationAtPos(pos) {
const text = file.text;
const lineStart = text.lastIndexOf('\n', pos - 1) + 1;
const textBeforeNodeOnLine = text.substring(lineStart, pos);
const match = textBeforeNodeOnLine.match(/^(\s*)/);
return match ? match[1] : '';
}
/**
* @param {!ts.Node} node
*/
@ -83,7 +95,7 @@ async function parseOverrides(filePath, commentForClass, commentForMethod, extra
const pos = declaration.getStart(file, false);
replacers.push({
pos,
text: commentForMethod(className, name, index),
text: commentForMethod(className, name, index, getIndentationAtPos(pos)),
});
if (ts.isPropertySignature(declaration))
ts.forEachChild(declaration, child => visitProperties(className, name, child));
@ -108,14 +120,14 @@ async function parseOverrides(filePath, commentForClass, commentForMethod, extra
const pos = node.getStart(file, false);
replacers.push({
pos,
text: commentForMethod(className, `${prefix}.${name}`, 0),
text: commentForMethod(className, `${prefix}.${name}`, 0, getIndentationAtPos(pos)),
});
ts.forEachChild(node, child => visitProperties(className, `${prefix}.${name}`, child));
} else if (ts.isCallSignatureDeclaration(node)) {
const pos = node.getStart(file, false);
replacers.push({
pos,
text: commentForMethod(className, `${prefix}`, 0),
text: commentForMethod(className, `${prefix}`, 0, getIndentationAtPos(pos)),
});
} else if (ts.isIntersectionTypeNode(node) || ts.isTypeLiteralNode(node)) {
ts.forEachChild(node, child => visitProperties(className, prefix, child));