-
Notifications
You must be signed in to change notification settings - Fork 656
fix(rome_js_parser): TsPropertySignatureClassMember with initializer #3003
fix(rome_js_parser): TsPropertySignatureClassMember with initializer #3003
Conversation
@MichaReiser , It looks that |
xtask/codegen/js.ungram
Outdated
@@ -719,6 +719,7 @@ TsPropertySignatureClassMember = | |||
modifiers: TsPropertySignatureModifierList | |||
name: JsAnyClassMemberName | |||
property_annotation: TsAnyPropertySignatureAnnotation? | |||
value: JsInitializerClause? |
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.
Can you please list valid and invalid cases so that we can decide on where the initializer should be placed.
For example, the initializer isn't valid in combination with the definite assignment operator but this AST would now allow for a property having an initializer and a definite assignment operator.
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.
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.
Do we really need to check this for now? If we do, we could only check for string or numeric literal
, we can't check for literal enum reference
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.
What about adding some todos? And finish this after we have the ability to type checking?
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 don't think we have to rush this change as I would argue, that it's probably not a too common case to have an initializer in an ambient context.
Also, changing AST structures is rather involved which is why I would prefer not to rush it. What's important for now is to fully understand valid and invalid patterns and we can then start thinking about how we want to change the AST structure.
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.
But maybe it's better to open an issue in TypeScript repo.
format
Also, this is a known issue of our parser, see #2310 (comment). |
Thanks @magic-akari for the nice research! Valid cases in ambient context:
class T {
declare readonly a = 3;
}
class T1 {
declare readonly a = 'test';
}
class T3 {
declare readonly a = `test`;
}
Invalid case in ambient context
class T2 {
declare readonly a = `test${20}`;
} any other cases are considered to be invalid. |
A few solutions I could give for now:
|
Thanks for this listing. How does the syntax work in combination with the optional and definite operators? Is it only allowed if the readonly modifier is present? |
Yes, |
See the Typescript implementation https://github.com/microsoft/TypeScript/pull/26313/files#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8R29425-R29429 |
I think checking phase could left to #3000 |
I'm conflicted on how to best add this to our AST so that the API encodes as many constraints as possible.
What's your preference @IWANABETHATGUY and for what reasons? I guess, I overall dislike TypeScript design decision behind adding support of this syntax rather than using `readonly prop: "value" but that's obviously out of my control :D |
This PR is stale because it has been open 14 days with no activity. |
Any update? |
@MichaReiser @IWANABETHATGUY what's the status of this PR? Is it ready for merging/approved? Or should we put it on hold? |
Last week I am working on the |
@IWANABETHATGUY what's your opinion on this? |
I prefer the third one. |
Could you explain why? |
Making our Ast shape more compatible with Typescript, we could validate the error either in the parser phase or analyzer phase which is more flexible. |
This new syntax is even more subtle:
That's why I'm currently leaning towards introducing its own member type
I'm undecided if we want a specific initializer only allowing @IWANABETHATGUY what do you think of a new member type that we only add to classes? |
I think it is alright to introduce a new type which only used for |
We already have a code that checks this: crates\rome_js_parser\src\syntax\class.rs:944 else if modifiers.has(ModifierKind::Declare) || p.state.in_ambient_context() {
// test_err ts ts_property_initializer_ambient_context
// declare class A { prop = "test" }
// class B { declare prop = "test" }
p.error(
p.err_builder("Initializers are not allowed in ambient contexts.")
.primary(initializer.range(p), ""),
);
} We can "fix" the example just doing:
So my suggestion is actually to use |
✅ Deploy Preview for docs-rometools ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Summary
TsPropertySignatureClassMember
with initializer is allowed in Typescript, see https://www.typescriptlang.org/play?#code/MYGwhgzhAEAq0G8BQ1oBMCmowCcPTzDQHsA7EAT2gBcMJroBeaAZgG4kBfIATest Plan