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

Incorrect parse of parenthesized expression in second ternary operator operand #30635

Closed
ronnievdc opened this issue Mar 28, 2019 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript

Comments

@ronnievdc
Copy link

TypeScript Version: 3.4.0-dev.20190328

Search Terms:
expression expected, Ternary operation, shorthand

Code

let a;
let b;
const c = true ? (a = 1, b = 2) : function () { return true; };

Expected behavior:

var a;
var b;
var c = true ? (a = 1, b = 2) : function () { return true; };

Actual behavior:

var a;
var b;
var c = true ? function (a, b) {
    if (a === void 0) { a = 1; }
    if (b === void 0) { b = 2; }
    return true;
} : ;

Playground Link:
https://www.typescriptlang.org/play/#src=let%20a%3B%0D%0Alet%20b%3B%0D%0Aconst%20c%20%3D%20true%20%3F%20(a%20%3D%201%2C%20b%20%3D%202)%20%3A%20function%20()%20%7B%20return%20true%3B%20%7D%3B

Related Issues:
#16241
#11582

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 28, 2019
@RyanCavanaugh RyanCavanaugh changed the title Ternary operator Incorrect parse of parenthesized comma operator in second ternary operator operand Mar 28, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.5.0 milestone Mar 28, 2019
@dragomirtitian
Copy link
Contributor

A simpler repro is just to have () in the then of the ternary expression. This also causes weird parsing to occur:

 true ? (a): function () { return true; }

Transpiled as

true ? function (a) { return true; } : ;

@RyanCavanaugh RyanCavanaugh changed the title Incorrect parse of parenthesized comma operator in second ternary operator operand Incorrect parse of parenthesized expression in second ternary operator operand Mar 28, 2019
@weswigham
Copy link
Member

weswigham commented Mar 28, 2019

Looks like this stems from us parsing JSDoc function types function(), (or function(number, string) or function(number): number) outside of JSDoc. We should probably just disable that.

@andrewbranch
Copy link
Member

Yep, that's exactly what's happening @weswigham. But disabling it outright prevents this quick fix from working:

image

Any suggestions? 🤔

@weswigham
Copy link
Member

The quickfix is opportunistic in nature - we don't actually expect anyone to write let x: function(): void, since that's never been valid in anything. It's moreso for catching things like let x: string? (since postfix questionmark is a thing in flow/jsdoc but not TS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants