Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add rest types to parameter data and typescript types
  • Loading branch information
davepagurek authored and limzykenneth committed May 13, 2025
commit e77dde4611471c6c178e8fe335e6b395fe2a59e8
2 changes: 1 addition & 1 deletion docs/parameterData.json
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@
"createVector": {
"overloads": [
[
null
"...Number"
]
]
},
Expand Down
9 changes: 7 additions & 2 deletions utils/convert.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ function typeObject(node) {
return { type: signature };
} else if (node.type === 'ArrayType') {
return { type: `[${node.elements.map(e => typeObject(e).type).join(', ')}]` };
} else if (node.type === 'RestType') {
return { type: typeObject(node.expression).type, rest: true };
} else {
// TODO
// - handle record types
Expand Down Expand Up @@ -518,19 +520,22 @@ function cleanUpClassItems(data) {

const processOverload = overload => {
if (overload.params) {
return Object.values(overload.params).map(param => processOptionalParam(param));
return Object.values(overload.params).map(param => processParam(param));
}
return overload;
}

// To simplify `parameterData.json`, instead of having a separate field for
// optional parameters, we'll add a ? to the end of parameter type to
// indicate that it's optional.
const processOptionalParam = param => {
const processParam = param => {
let type = param.type;
if (param.optional) {
type += '?';
}
if (param.rest) {
type = `...${type}`;
}
return type;
}

Expand Down
9 changes: 8 additions & 1 deletion utils/helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ export function generateTypeFromTag(param) {
const innerTypeStrs = param.type.elements.map(e => generateTypeFromTag({ type: e }));
return `[${innerTypeStrs.join(', ')}]`;
}
case 'RestType':
return `${generateTypeFromTag({ type: param.type.expression })}[]`;
default:
return 'any';
}
Expand Down Expand Up @@ -420,6 +422,7 @@ export function generateTypeFromTag(param) {
if (!param) return 'any';

let type = param.type;
let prefix = '';
const isOptional = param.type?.type === 'OptionalType';
if (typeof type === 'string') {
type = normalizeTypeName(type);
Expand All @@ -429,7 +432,11 @@ export function generateTypeFromTag(param) {
type = 'any';
}

return `${param.name}${isOptional ? '?' : ''}: ${type}`;
if (param.type?.type === 'RestType') {
prefix = '...';
}

return `${prefix}${param.name}${isOptional ? '?' : ''}: ${type}`;
}

export function generateFunctionDeclaration(funcDoc) {
Expand Down