-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[bug] Ternary + arrow function is not correctly parsed #16241
Comments
I have the same problem. Using playground, the code: var b = 0, c = 1, e = 2;
var a = b ? (c): d => e; Is transpiled to: var b = 0, c = 1, e = 2;
var a = b ? function (c) { return e; } : ; With the errors: As reference, using node console: var b = 0, c = 1, e = 2;
var a = b ? (c) : d => e;
console.log(a); outputs Changing to var b = 1, c = 1, e = 2;
var a = b ? (c) : d => e;
console.log(a); outputs |
Fixing this might introduce an ambiguity in the language: a ? (b) : c => (d) : e => f How should that expressions be parsed? a ? function (b) : c {
return (d)
} : function (e) {
return f
}
// or
a ? (b) : function (c) {
return function (d) : e {
return f
}
} |
@mhegazy, @DanielRosenwasser Another example: var b = 0, c = 1, e = 2;
var a = b ? (c + e) : d => c+e; is transpiled as: var b = 0, c = 1, e = 2;
var a = b ? function (c) {
if (c === void 0) { c = +e; }
return c + e;
} : ; I expect that the transpiled output be the same as input (with es6). Or like this (with es5): var b = 0, c = 1, e = 2;
var a = b ? (c + e) : function(d) { return c+e; }; |
We need to disambiguate this in the spec. We'll potentially
|
@DanielRosenwasser a problem is that the compiler show errors in javascript files with the sample of my previous response: var b = 0, c = 1, e = 2;
var a = b ? (c + e) : d => c+e;
This code has no errors at node and works as expected. In this case, removing the parenthesis solve the error. |
I have opened a PR to fix this same issue (but for Flow, not TypeScript) in Babylon (the Babel's parser). It basically tries to parse the middle part of a conditional expression up to three times, which should be enough to find the correct way of parsing. ref: babel/babylon#596 |
This is still a bug in v4.5.4, and occurs even when parsing JavaScript files. ts.transpileModule("x ? x => ({ x }) : x => ({ x })", {
fileName: "file.js",
compilerOptions: {
target: ts.ScriptTarget.ESNext,
allowJs: true,
},
}) outputs x ? x => ({ x }) => ({ x }) : ; This pattern occurs in real world files; for example, tsc currently outputs malformed js when run on monaco. |
The trouble with the current parser seems to be that when it parses out:
It tries to parse |
Right, we need a context flag that prevents us from parsing a type annotation on an arrow function immediately nested in one of the branches of a conditional expression. Sort of similar to the flag that prevents us from parsing an |
My fix in #47550 doesn't use a context flag, but a boolean that gets passed down via the few call sites where it matters. I'll have to look at how that |
TypeScript Version: Online repl
Code
https://www.typescriptlang.org/play/index.html#src=var%20a%20%3D%20b%20%3F%20(c)%3A%20d%20%3D%3E%20e%3B
Expected behavior:
It is valid javascript, thus it should be parsed correctly
Actual behavior:
It is parsed as
The text was updated successfully, but these errors were encountered: