Skip to content

Enable parsing of nested parameters for @callback JSDoc tag. #54681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9150,7 +9150,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 @@ -9465,7 +9465,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" });
}