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
add class-method-parameter-decorators #303
add class-method-parameter-decorators #303
Conversation
Slightly off-topic, but I think it was the other way around - typescript-eslint-parser existed before babel's support and I think babel drew inspiration from the ESTree AST it produced. I'm not sure I follow why defining an optional property on I agree that turning back time having a |
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.
I like the idea of Parameter
. To me, this is an outstanding bug in ESTree that we can actually address. We already add new expected node types into the params
array of functions: we started with just Identifier
and then kept adding stuff as syntax for parameters changed. I think adding Parameter
now also gives us some cover for an additional parameter types that might be introduced in the future.
} | ||
``` | ||
|
||
If `params` contains a `Parameter` node, its parent must be a `MethodDefinition` under a `ClassBody` node. |
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.
I'm not sure we need this restriction. Could we just say that Parameter
is always valid in a Function#params
array? Maybe we could say if decorators
is not empty, then the parent must be a MethodDefinition
?
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.
If `params` contains a `Parameter` node, its parent must be a `MethodDefinition` under a `ClassBody` node. | |
If `params` contains a `Parameter` node, its parent must be a `MethodDefinition`. |
The proposal explicitly disallows decorated parameter in general function expression. Previously I overlooked the spec: I thought MethodDefinition
is also shared in ObjectExpression
.
The intent of this PR is to use Parameter
only when decorators
are not empty, otherwise we will have two representations for non-decorated parameters. Though I lean towards using the Parameter
for other elements as well.
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.
Gotcha. I suppose we can always revisit later.
Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
Thanks for correcting me. I am not very informed about the history of the babylon parser.
AFAIK the ESTree spec does not have optional property. Most properties can be initialized as For reference here is a discussion thread on Babel about making the identifier node atomic: babel/babel#9545 |
You can definitely have nullable properties in the AST - for example. A return statement may or may not have an argument - so it's Couldn't we have the same sort of spec for I personally just don't like the idea of updating the AST to wrap the parameter if and only if it has a decorator. It's just creating an inconsistent state that downstream consumers have to handle. For example - what's to stop a parser from always producing The state is just inconsistent and that means downstream consumers have to expliclty support both states. I'd personally prefer just fully breaking change things and going all the way to "estree compatible parsers must always produce a |
I like I don't find And as I mentioned elsewhere, we have a long history of adding new node types in the |
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.
LGTM
Off-topic: That is exactly my initial thought! Then I changed to |
View Rendered Text
This PR adds Decorators for Class Method and Constructor Parameters support, currently Stage 1.
Currently Babel has two models for the TS decorated parameter:
The second approach, adding decorators to all
Pattern
AST, seems to be overkill for me because of the concern on memory and precision: the identifier nodes are virtually everywhere, but only a few of them, as function parameters, can be decorated. So I propose to add a newParameter
node as a wrapper aroundPattern
that also offers decorators storage.If we agree on the proposed AST structure and the tc39 proposal were adopted without dramatic changes, then Babel has to migrate the current TypeScript AST from
Identifier { decorators: [ Decorator ]
toParameter
when it becomes a JS-thing.@bradzacher Since the typescript-estree seems to follow Babel's design here for
TSParameterProperty
, I would like to know your opinion.