Skip to content

Commit 4f368cf

Browse files
authored
src-transpiler/expandType: Implement JSDocFunctionType (#213)
* src-transpiler/expandType: Implement `JSDocFunctionType` * Add unit test
1 parent 2f9e342 commit 4f368cf

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src-transpiler/expandType.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,22 @@ export const requiredTypeofs = {};
6161
* This function handles various TypeScript AST node types and converts them into a string
6262
* or an object representing the type.
6363
*
64-
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName} node - The TypeScript AST node to convert.
64+
* @param {ts.TypeNode|ts.Identifier|ts.QualifiedName|undefined} node - The TypeScript AST node to convert.
6565
* @returns {string | number | boolean | {type: string, [key: string]: any} | undefined} The source string/number,
6666
* or an object with type information based on the node, or `undefined` if the node kind is not handled.
6767
*/
6868
function toSourceTS(node) {
69+
if (!node) {
70+
return 'undefined';
71+
}
6972
const {typeArguments, typeName} = node;
7073
const kind_ = ts.SyntaxKind[node.kind];
7174
const {
7275
AnyKeyword, // parseType('any' ).kind === ts.SyntaxKind.AnyKeyword && toSourceTS(parseType('any')) === 'any'
7376
ArrayType, // parseType('number[]' ).kind === ts.SyntaxKind.ArrayType // todo toSourceTS(parseType('number[]')) === {type: 'array etc.
7477
BooleanKeyword, // parseType("boolean" ).kind === ts.SyntaxKind.BooleanKeyword
7578
FunctionType, // parseType("() => void" ).kind === ts.SyntaxKind.FunctionType
79+
JSDocFunctionType, // parseType("function (number, number): number").kind === ts.SyntaxKind.JSDocFunctionType
7680
Identifier, // parseType("{a: 1, b: 2}" ).members[0].name.kind === ts.SyntaxKind.Identifier
7781
IntersectionType, // parseType("1 & 2" ).kind === ts.SyntaxKind.IntersectionType
7882
JSDocAllType, // parseType("*" ).kind === ts.SyntaxKind.JSDocAllType
@@ -144,12 +148,20 @@ function toSourceTS(node) {
144148
const ret = toSourceTS(node.type);
145149
return {type: 'new', parameters, ret};
146150
}
147-
case FunctionType:
151+
case FunctionType: {
148152
if (!ts.isFunctionTypeNode(node)) {
149153
throw Error("Impossible");
150154
}
151155
const parameters = node.parameters.map(toSourceTS);
152156
return {type: 'function', parameters};
157+
}
158+
case JSDocFunctionType: {
159+
if (!ts.isJSDocFunctionType(node)) {
160+
throw Error("Impossible");
161+
}
162+
const parameters = node.parameters.map(toSourceTS);
163+
return {type: 'function', parameters};
164+
}
153165
case IndexedAccessType:
154166
if (!ts.isIndexedAccessTypeNode(node)) {
155167
throw Error("Impossible");

test/typechecking.json

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"input": "./test/typechecking/JSDoc-FunctionExpression-ExpressionStatement-input.mjs",
3636
"output": "./test/typechecking/JSDoc-FunctionExpression-ExpressionStatement-output.mjs"
3737
},
38+
{
39+
"input": "./test/typechecking/JSDocFunctionType-input.mjs",
40+
"output": "./test/typechecking/JSDocFunctionType-output.mjs"
41+
},
3842
{
3943
"input": "./test/typechecking/ParenthesizedExpression-multiline-input.mjs",
4044
"output": "./test/typechecking/ParenthesizedExpression-multiline-output.mjs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {function (number, number): number} cb
3+
*/
4+
function op(cb, a, b) {
5+
return cb(a, b);
6+
}
7+
const ret = op((a, b) => a + b, 10, 20);
8+
console.log("ret", ret);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {function (number, number): number} cb
3+
*/
4+
function op(cb, a, b) {
5+
if (!inspectType(cb, {
6+
"type": "function",
7+
"parameters": [
8+
{
9+
"type": "number",
10+
"name": "undefined"
11+
},
12+
{
13+
"type": "number",
14+
"name": "undefined"
15+
}
16+
],
17+
"optional": false
18+
}, 'op', 'cb')) {
19+
youCanAddABreakpointHere();
20+
}
21+
return cb(a, b);
22+
}
23+
const ret = op((a, b) => {
24+
return a + b;
25+
}, 10, 20);
26+
console.log("ret", ret);

0 commit comments

Comments
 (0)