Skip to content

Commit c7c19eb

Browse files
authored
Better detection if we have a typedef or an inlined JSDoc object (#180)
* Better detection if we have a typedef or a inlined JSDoc object * Add unit test for syntax
1 parent b7d3ef3 commit c7c19eb

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src-transpiler/Asserter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ class Asserter extends Stringifier {
504504
}
505505
continue;
506506
} else if (param.left.type === 'ObjectPattern') {
507-
if (type.type === 'object') {
507+
// If we have properties from JSDoc, we can create individual type inspections.
508+
if (type?.properties && Object.keys(type.properties).length) {
508509
// Add a type assertion for each property of the ObjectPattern
509510
for (const property of param.left.properties) {
510511
if (property.type !== 'ObjectProperty') {
@@ -540,7 +541,7 @@ class Asserter extends Stringifier {
540541
out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
541542
}
542543
continue;
543-
} else if (typeof type === 'string') {
544+
} else {
544545
// The case when we have an ObjectPattern with a @typedef
545546
name = `arguments[${paramIndex}]`;
546547
}

test/typechecking.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
"input": "./test/typechecking/simple-ObjectPattern-rest-input.mjs",
128128
"output": "./test/typechecking/simple-ObjectPattern-rest-output.mjs"
129129
},
130+
{
131+
"input": "./test/typechecking/simple-ObjectPattern-typedef-2-input.mjs",
132+
"output": "./test/typechecking/simple-ObjectPattern-typedef-2-output.mjs"
133+
},
130134
{
131135
"input": "./test/typechecking/simple-ObjectPattern-typedef-input.mjs",
132136
"output": "./test/typechecking/simple-ObjectPattern-typedef-output.mjs"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {PretrainedOptions} [something] Optional parameters.
3+
*/
4+
function test(
5+
{
6+
revision = 'main',
7+
} = {}
8+
) {
9+
return revision;
10+
}
11+
const ret = test();
12+
console.log('ret', ret);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {PretrainedOptions} [something] Optional parameters.
3+
*/
4+
function test({
5+
revision = 'main',
6+
} = {}) {
7+
if (!inspectType(arguments[0], {
8+
"type": "PretrainedOptions",
9+
"optional": true
10+
}, 'test', 'something')) {
11+
youCanAddABreakpointHere();
12+
}
13+
return revision;
14+
}
15+
const ret = test();
16+
console.log('ret', ret);

0 commit comments

Comments
 (0)