-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix #498: Optimize rest arguments #19330
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
Conversation
3e5efd1
to
d65b369
Compare
@mhegazy I left three failing baselines, because I'm not sure about them. They are: parser509668, varArgConstructorMemberParameter and restParamModifier2. Should the rest parameter be emitted when there is a failed attempt to use it as a parameter property? I'm thinking it's fine for the optimization to occur (i.e. not emit the rest parameter), but I just want to make sure. Edit 1 There is also accessorWithRestParam, which @Kovensky pointed out. There are probably more error cases (which do not compile) that I've missed, so the generalized question is: can I still apply the optimization if the code doesn't compile? |
v[_i] = arguments[_i]; | ||
} | ||
}, | ||
set: function () { }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong.
This test file is wrong.
class C { set X(...v) {} }
, which this file is testing, is invalid syntax. set
methods, on either classes or objects, only accept exactly one valid FormalParameter
, which does not allow for FunctionRestParameter
.
See https://tc39.github.io/ecma262/#prod-PropertySetParameterList
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, thanks.
Yet TypeScript does not allow set
to have a rest parameter anyway (i.e. the test code won't compile).
So I guess I'll revert this baseline and add it to the list above, where I ask @mhegazy about whether or not to apply the optimization in error cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the transform looks correct -- the test file having a valid-looking output at all is wrong. Invalid syntax should either throw or stay as invalid syntax 😄
This is probably out of scope of this PR, though, but the change to that file helped notice it.
d65b369
to
4d61e29
Compare
…not the rest parameter should be emitted.
"NodeCheckFlags.EmitRestParam" flag is not set.
… node, in the "checkIdentifier" function, if the parameter is a rest parameter.
4d61e29
to
4097e97
Compare
@Andy-MS If you get a chance and this applies to you, some input would be much appreciated. |
This can be done without adding a new check flag. return !!node
&& !!node.dotDotDotToken
&& node.name.kind === SyntaxKind.Identifier
&& !inConstructorWithSynthesizedSuper
&& resolver.isRestParameterUsed(getOriginalNode(node, isParameter)); In isRestParameterUsed(node: ts.ParameterDeclaration): boolean; In @rbuckton Maybe |
Is this feature actually worth adding though? What's a real-world use for having a lot of unused rest parameters? |
Thanks for the feedback @Andy-MS.
|
As for whether this is necessary, pinging @rbuckton.
|
Fixes #498.
Inspiration of the implementation is drawn from this.