Skip to content
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

Bad JSDoc comments leads to generate invalid declaration file #37703

Closed
Jack-Works opened this issue Mar 31, 2020 · 3 comments · Fixed by #38155
Closed

Bad JSDoc comments leads to generate invalid declaration file #37703

Jack-Works opened this issue Mar 31, 2020 · 3 comments · Fixed by #38155
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Help Wanted You can do this
Milestone

Comments

@Jack-Works
Copy link
Contributor

TypeScript Version: 3.9.0-dev.20200328

Search Terms: JSDoc A required parameter cannot follow an optional parameter.

Code

/**
 * @param {string} password Passphrase
 * @param {string} [targetRoomId] Room ID to target a specific room.
 * Restores all rooms if omitted.
 * @param {object} opts Optional params such as callbacks
 */
function x(password, targetRoomId, opts) {}
x()

Expected behavior:
Generate declaration file:

function x(password: string, targetRoomId?: string, opts?: object): void

Actual behavior:
Generate declaration file:

function x(password: string, targetRoomId?: string, opts: object): void
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ Notice the question mark `?` has gone
// error TS1016: A required parameter cannot follow an optional parameter.

Playground Link: https://www.typescriptlang.org/v2/en/play?ts=3.9.0-dev.20200328&useJavaScript=true#code/PQKhFgCgAIWgBADgQwE7ILbQN4GcAuqAlgHYDmAvtCrrgO4D2qAJtAArK2IAW6uAplFgIU6LHkKlK0ANr40ZfvgBKDBhgCSzALrRV66BoAi0fA1MKl0ZNFyJ+AYyIAzIg+io1GAHRC4y-gImQOsAG1CPL1xoF2h1Inx8fmZfGDgkNEwcBgAjACtHfCoGRHxogHlSogYSZAjRTGjcAFcHbmtohzrQnOQHAGtcP2AoZ2aSB3xqkmgADwAKGnomZgAaC1RFFS8tdZKygEocCigoBYOoIA

Hover the mouse on the function x, you can see it's signature is inferred as function x(password: string, targetRoomId?: string, opts: object): void which is wrong.

@Jack-Works
Copy link
Contributor Author

Possible fix:

Add it in checker.ts, function isOptionalParameter(...)

            const parameterIndex = node.parent.parameters.indexOf(node);
            if (isInJSFile(node) && parameterIndex >= 1) {
                // Check from (n-1)th parameter to 0th parameter
                let descentIndex = parameterIndex - 1;
                while (descentIndex >= 0) {
                    // if the parameter before this parameter is optional, this parameter must be optional too (only for JSDoc in JS files).
                    if (isOptionalParameter(node.parent.parameters[descentIndex])) {
                        return true;
                    }
                    descentIndex -= 1;
                }
            }

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 31, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 31, 2020
@RyanCavanaugh RyanCavanaugh added the Domain: Declaration Emit The issue relates to the emission of d.ts files label Mar 31, 2020
@Qiyu8
Copy link

Qiyu8 commented Apr 8, 2020

@Jack-Works Are you working on this? your solution looks fine. Is there any way to fix this Bug without recursion?

@Jack-Works
Copy link
Contributor Author

I'm not working on this because I didn't find a way to do this without recursion or big changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants