Skip to content

Commit 60e72f0

Browse files
committed
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.
1 parent 74fa3b3 commit 60e72f0

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

utils/plugins/rollup-types-fixup.mjs

+29-8
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,29 @@ 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+
// console.log(prop[0], contents.match(`@property {${prop[1]}} ${prop[0]}`), cleanTypeDescription);
213+
return `\t${jsdoc}\n\tset ${prop[0]}(arg: ${prop[1]});\n\tget ${prop[0]}(): ${prop[1]};\n\n`;
214+
}).join('')}`,
215+
)
216+
},
200217
footer: `
201218
import { Color } from '../../core/math/color.js';
202219
import { Vec2 } from '../../core/math/vec2.js';
@@ -243,9 +260,13 @@ export function typesFixup() {
243260
name: 'types-fixup',
244261
buildStart() {
245262
REPLACEMENTS.forEach((item) => {
246-
const { from, to, footer } = item.replacement;
263+
const { from, to, footer, transformer } = item.replacement;
247264
let contents = fs.readFileSync(item.path, 'utf-8');
248-
contents = contents.replace(from, to);
265+
if (transformer) {
266+
contents = transformer(contents);
267+
} else {
268+
contents = contents.replace(from, to);
269+
}
249270
contents += footer ?? '';
250271
fs.writeFileSync(item.path, contents, 'utf-8');
251272
console.log(`${GREEN_OUT}type fixed ${BOLD_OUT}${item.path}${REGULAR_OUT}`);

0 commit comments

Comments
 (0)