Skip to content

Commit 3a3ddce

Browse files
authored
Improve JSDoc for Standard Material (#7532)
* Enhance JSDoc generation for standard material properties in rollup-types-fixup.mjs. Added a transformer function to dynamically generate property descriptions based on existing JSDoc comments, improving documentation accuracy. * Refactor JSDoc generation in rollup-types-fixup.mjs to improve readability and maintainability. Streamlined the mapping of standard material properties and enhanced the cleaning of type descriptions for better documentation output. * Remove commented-out console log in rollup-types-fixup.mjs to clean up code and enhance readability.
1 parent 74fa3b3 commit 3a3ddce

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

utils/plugins/rollup-types-fixup.mjs

+28-8
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,28 @@ const STANDARD_MAT_PROPS = [
191191
const REPLACEMENTS = [{
192192
path: `${TYPES_PATH}/scene/materials/standard-material.d.ts`,
193193
replacement: {
194-
from: 'reset(): void;',
195-
to: `reset(): void;
196-
${STANDARD_MAT_PROPS.map(prop => `
197-
set ${prop[0]}(arg: ${prop[1]});
198-
get ${prop[0]}(): ${prop[1]};
199-
`).join('')}`,
194+
transformer: (contents) => {
195+
196+
// Find the jsdoc block description using eg "@property {Type} {name}"
197+
return contents.replace('reset(): void;', `reset(): void;
198+
${STANDARD_MAT_PROPS.map((prop) => {
199+
const typeDefinition = `@property {${prop[1]}} ${prop[0]}`;
200+
const typeDescriptionIndex = contents.match(typeDefinition);
201+
const typeDescription = typeDescriptionIndex ?
202+
contents.slice(typeDescriptionIndex.index + typeDefinition.length, contents.indexOf('\n * @property', typeDescriptionIndex.index + typeDefinition.length)) :
203+
'';
204+
205+
// Strip newlines, asterisks, and tabs from the type description
206+
const cleanTypeDescription = typeDescription
207+
.trim()
208+
.replace(/[\n\t*]/g, ' ') // remove newlines, tabs, and asterisks
209+
.replace(/\s+/g, ' '); // collapse whitespace
210+
211+
const jsdoc = cleanTypeDescription ? `/** ${cleanTypeDescription} */` : '';
212+
return `\t${jsdoc}\n\tset ${prop[0]}(arg: ${prop[1]});\n\tget ${prop[0]}(): ${prop[1]};\n\n`;
213+
}).join('')}`
214+
);
215+
},
200216
footer: `
201217
import { Color } from '../../core/math/color.js';
202218
import { Vec2 } from '../../core/math/vec2.js';
@@ -243,9 +259,13 @@ export function typesFixup() {
243259
name: 'types-fixup',
244260
buildStart() {
245261
REPLACEMENTS.forEach((item) => {
246-
const { from, to, footer } = item.replacement;
262+
const { from, to, footer, transformer } = item.replacement;
247263
let contents = fs.readFileSync(item.path, 'utf-8');
248-
contents = contents.replace(from, to);
264+
if (transformer) {
265+
contents = transformer(contents);
266+
} else {
267+
contents = contents.replace(from, to);
268+
}
249269
contents += footer ?? '';
250270
fs.writeFileSync(item.path, contents, 'utf-8');
251271
console.log(`${GREEN_OUT}type fixed ${BOLD_OUT}${item.path}${REGULAR_OUT}`);

0 commit comments

Comments
 (0)