Skip to content

Commit

Permalink
Enable parsing of nested parameters for @callback JSDoc tag. (micro…
Browse files Browse the repository at this point in the history
…soft#54681)

Co-authored-by: Matej Sadovsky <[email protected]>
Co-authored-by: Nathan Shively-Sanders <[email protected]>
  • Loading branch information
3 people authored Jun 29, 2023
1 parent ed5008d commit 34f6e10
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9225,7 +9225,7 @@ namespace Parser {

const comment = parseTrailingTagComments(start, getNodePos(), indent, indentText);

const nestedTypeLiteral = target !== PropertyLikeParse.CallbackParameter && parseNestedTypeLiteral(typeExpression, name, target, indent);
const nestedTypeLiteral = parseNestedTypeLiteral(typeExpression, name, target, indent);
if (nestedTypeLiteral) {
typeExpression = nestedTypeLiteral;
isNameFirst = true;
Expand Down Expand Up @@ -9540,7 +9540,6 @@ namespace Parser {
if (canParseTag) {
const child = tryParseChildTag(target, indent);
if (child && (child.kind === SyntaxKind.JSDocParameterTag || child.kind === SyntaxKind.JSDocPropertyTag) &&
target !== PropertyLikeParse.CallbackParameter &&
name && (isIdentifierNode(child.name) || !escapedTextsEqual(name, child.name.left))) {
return false;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/callbackTagNestedParameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/conformance/jsdoc/callbackTagNestedParameter.ts] ////

//// [cb_nested.js]
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/

/**
* For each person, calls your callback.
* @param {WorksWithPeopleCallback} callback
* @returns {void}
*/
function eachPerson(callback) {
callback({ name: "Empty" });
}




//// [cb_nested.d.ts]
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/
/**
* For each person, calls your callback.
* @param {WorksWithPeopleCallback} callback
* @returns {void}
*/
declare function eachPerson(callback: WorksWithPeopleCallback): void;
type WorksWithPeopleCallback = (person: {
name: string;
age?: number;
}) => void;
25 changes: 25 additions & 0 deletions tests/baselines/reference/callbackTagNestedParameter.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/conformance/jsdoc/callbackTagNestedParameter.ts] ////

=== cb_nested.js ===
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/

/**
* For each person, calls your callback.
* @param {WorksWithPeopleCallback} callback
* @returns {void}
*/
function eachPerson(callback) {
>eachPerson : Symbol(eachPerson, Decl(cb_nested.js, 0, 0))
>callback : Symbol(callback, Decl(cb_nested.js, 13, 20))

callback({ name: "Empty" });
>callback : Symbol(callback, Decl(cb_nested.js, 13, 20))
>name : Symbol(name, Decl(cb_nested.js, 14, 14))
}

28 changes: 28 additions & 0 deletions tests/baselines/reference/callbackTagNestedParameter.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/conformance/jsdoc/callbackTagNestedParameter.ts] ////

=== cb_nested.js ===
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/

/**
* For each person, calls your callback.
* @param {WorksWithPeopleCallback} callback
* @returns {void}
*/
function eachPerson(callback) {
>eachPerson : (callback: WorksWithPeopleCallback) => void
>callback : WorksWithPeopleCallback

callback({ name: "Empty" });
>callback({ name: "Empty" }) : void
>callback : WorksWithPeopleCallback
>{ name: "Empty" } : { name: string; }
>name : string
>"Empty" : "Empty"
}

21 changes: 21 additions & 0 deletions tests/cases/conformance/jsdoc/callbackTagNestedParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @emitDeclarationOnly: true
// @declaration: true
// @allowJs: true
// @checkJs: true
// @Filename: cb_nested.js
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/

/**
* For each person, calls your callback.
* @param {WorksWithPeopleCallback} callback
* @returns {void}
*/
function eachPerson(callback) {
callback({ name: "Empty" });
}

0 comments on commit 34f6e10

Please sign in to comment.