Skip to content

Commit

Permalink
src-transpiler/expandType: Implement JSDocFunctionType (#213)
Browse files Browse the repository at this point in the history
* src-transpiler/expandType: Implement `JSDocFunctionType`

* Add unit test
  • Loading branch information
kungfooman authored Nov 6, 2024
1 parent 2f9e342 commit 4f368cf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src-transpiler/expandType.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ export const requiredTypeofs = {};
* This function handles various TypeScript AST node types and converts them into a string
* or an object representing the type.
*
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName} node - The TypeScript AST node to convert.
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName|undefined} node - The TypeScript AST node to convert.
* @returns {string | number | boolean | {type: string, [key: string]: any} | undefined} The source string/number,
* or an object with type information based on the node, or `undefined` if the node kind is not handled.
*/
function toSourceTS(node) {
if (!node) {
return 'undefined';
}
const {typeArguments, typeName} = node;
const kind_ = ts.SyntaxKind[node.kind];
const {
AnyKeyword, // parseType('any' ).kind === ts.SyntaxKind.AnyKeyword && toSourceTS(parseType('any')) === 'any'
ArrayType, // parseType('number[]' ).kind === ts.SyntaxKind.ArrayType // todo toSourceTS(parseType('number[]')) === {type: 'array etc.
BooleanKeyword, // parseType("boolean" ).kind === ts.SyntaxKind.BooleanKeyword
FunctionType, // parseType("() => void" ).kind === ts.SyntaxKind.FunctionType
JSDocFunctionType, // parseType("function (number, number): number").kind === ts.SyntaxKind.JSDocFunctionType
Identifier, // parseType("{a: 1, b: 2}" ).members[0].name.kind === ts.SyntaxKind.Identifier
IntersectionType, // parseType("1 & 2" ).kind === ts.SyntaxKind.IntersectionType
JSDocAllType, // parseType("*" ).kind === ts.SyntaxKind.JSDocAllType
Expand Down Expand Up @@ -144,12 +148,20 @@ function toSourceTS(node) {
const ret = toSourceTS(node.type);
return {type: 'new', parameters, ret};
}
case FunctionType:
case FunctionType: {
if (!ts.isFunctionTypeNode(node)) {
throw Error("Impossible");
}
const parameters = node.parameters.map(toSourceTS);
return {type: 'function', parameters};
}
case JSDocFunctionType: {
if (!ts.isJSDocFunctionType(node)) {
throw Error("Impossible");
}
const parameters = node.parameters.map(toSourceTS);
return {type: 'function', parameters};
}
case IndexedAccessType:
if (!ts.isIndexedAccessTypeNode(node)) {
throw Error("Impossible");
Expand Down
4 changes: 4 additions & 0 deletions test/typechecking.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"input": "./test/typechecking/JSDoc-FunctionExpression-ExpressionStatement-input.mjs",
"output": "./test/typechecking/JSDoc-FunctionExpression-ExpressionStatement-output.mjs"
},
{
"input": "./test/typechecking/JSDocFunctionType-input.mjs",
"output": "./test/typechecking/JSDocFunctionType-output.mjs"
},
{
"input": "./test/typechecking/ParenthesizedExpression-multiline-input.mjs",
"output": "./test/typechecking/ParenthesizedExpression-multiline-output.mjs"
Expand Down
8 changes: 8 additions & 0 deletions test/typechecking/JSDocFunctionType-input.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @param {function (number, number): number} cb
*/
function op(cb, a, b) {
return cb(a, b);
}
const ret = op((a, b) => a + b, 10, 20);
console.log("ret", ret);
26 changes: 26 additions & 0 deletions test/typechecking/JSDocFunctionType-output.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {function (number, number): number} cb
*/
function op(cb, a, b) {
if (!inspectType(cb, {
"type": "function",
"parameters": [
{
"type": "number",
"name": "undefined"
},
{
"type": "number",
"name": "undefined"
}
],
"optional": false
}, 'op', 'cb')) {
youCanAddABreakpointHere();
}
return cb(a, b);
}
const ret = op((a, b) => {
return a + b;
}, 10, 20);
console.log("ret", ret);

0 comments on commit 4f368cf

Please sign in to comment.