Print trailing comma after a single TS generic in arrow fns#14113
Print trailing comma after a single TS generic in arrow fns#14113nicolo-ribaudo merged 5 commits intobabel:mainfrom ozanhonamlioglu:tsx-generic-function-comma-separator
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/50639/ |
| ): void { | ||
| this.token("<"); | ||
| this.printList(node.params, node, {}); | ||
| if(node.extra?.trailingComma) this.token(","); |
There was a problem hiding this comment.
Thanks for that PR!
I think it would be safer to always print the comma if node.params.length === 1, so that it also works when the node has been modified/generated by a plugin. For example:
https://astexplorer.net/#/gist/ef9b1bdad45421b25b826a06f3a707e1/latest
There was a problem hiding this comment.
If I do it like that, now generator starts adding comma end of every conditional node.params.length === 1 type inside every angle bracket. But some of those usages are not compatible with typescript, such as this one.
// IN
// *****************************************************
const useState = <T>(x: T): T => x;
const foo = <T>(a: T) => {
const x = useState<number>(2);
};
// OUT
// *****************************************************
const useState = <T,>(x: T): T => x;
const foo = <T,>(a: T) => {
const x = useState<number,>(2); // now this becomes incompatible due to <number,>
};Or am I missing something?
There was a problem hiding this comment.
Oh true. TSTypeParameterInstantiation is called with an additional argument, parent: t.Node: you can check if parent.type === "ArrowFunctionExpression".
There was a problem hiding this comment.
We also need to find the type of FunctionDeclaration right?
function foo<T>(a: T) {} // AST says this is ArrowFunctionExpression (init type)
const foo= <T>(x: T): T => x; // and says this is FunctionDeclaration (type)They both should be generated with at least one comma inside their angle brackets.
There was a problem hiding this comment.
I don't think the first one needs a trailing comma, because it's not ambiguous with JSX.
There was a problem hiding this comment.
If you are interested, I have another bug for you which can be fixed after that this PR is merged (we first need a second review)!
We currently parse <T>() => {} without problems even when both the JSX and TypeScript plugins are enabled. If there is a single type parameter and there isn't a trailing comma, we should report a parser error. It can probably be reported in
this.raise (see how it's used in other parts of that file). OVERWRITE=true yarn jest parser will then probably update some existing (wrong) tests: I would be surprised if we need new tests for this.
(Note: that function is probably one of the most complex parser functions 😬)
There was a problem hiding this comment.
Absolutely, I want to work on this case. please assign it to me.
When do you think other reviewer can review this PR?
There was a problem hiding this comment.
Probably @JLHwung or @existentialism can review this tomorrow or on Monday.
I haven't created an issue for that bug; feel free to consider it assigned to you and just link my comment in the new PR 🙂
Btw, you don't need to wait for this to be merged because it's a parser bug, but you will then need to rebase your new PR after that this is merged to check if any related @babel/generator tests need to be merged.
There was a problem hiding this comment.
Absolutely, will start to work on it ASAP. Thanks @nicolo-ribaudo
There was a problem hiding this comment.
If you are interested, I have another bug for you which can be fixed after that this PR is merged (we first need a second review)!
We currently parse
<T>() => {}without problems even when both the JSX and TypeScript plugins are enabled. If there is a single type parameter and there isn't a trailing comma, we should report a parser error. It can probably be reported in, using
this.raise(see how it's used in other parts of that file).OVERWRITE=true yarn jest parserwill then probably update some existing (wrong) tests: I would be surprised if we need new tests for this.
(Note: that function is probably one of the most complex parser functions 😬)
Hi @nicolo-ribaudo
I start to work on this bug today, will quote reply once I create a PR
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
Thanks!
While reviewing this PR I found a TS bug: microsoft/TypeScript#47355
From now on, any project which uses typescript plugin, will unconditionally print the last comma inside type parameter angle brackets.
Here is a demo code