-
-
Notifications
You must be signed in to change notification settings - Fork 75
Fix: Only set optional property on certain node property (fixes #289) #292
Conversation
LGTM |
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 think this is still broken in the case where the method or property is computed. E.g. adapting your test case:
class X {
private ['foo']? = undefined;
}
...optional is missing from the AST as I mention here https://github.com/JamesHenry/tsep-babylon-test/issues/13#issuecomment-301612343
LGTM |
@JamesHenry Sorry about the delay. For computed properties there is no |
@soda0289 I don't think we should allow that. As per JamesHenry/tsep-babylon-test#13, I think we should be consistent with our placement of optional. Is there anything I am missing? |
@JamesHenry Are you saying the optional flag should always be on the parent node? In this case the |
Hmm yeah upon further digging the TypeScript AST (with its top level I think we should confirm this decision either way with Andy on JamesHenry/tsep-babylon-test#13 - it's not going to be a perfect either way, we just need to align on it I think. However, given that my take a while, let's get this change in as-is and loop back to it if we have to make another breaking change. |
Sounds good. I'll ask for a release today after this is merged in. |
After this commit, interface I {
[Symbol.iterator]?: { x };
} no longer has |
@vjeux I can look into it |
I'm really excited, the list of remaining issues for typescript in prettier is getting real close to 0, keep up the awesome work! |
@JamesHenry @vjeux Currently |
Okay, so for interface I {
[Symbol.iterator]?: { x };
} there's now a interface Base {
x: {
- a?: string,
+ a??: string,
b: string
};
} -var v: { e?(): number };
+var v: { e??(): number }; |
If I remove all the handling of -var x: { [A in keyof B]?: any };
+var x: { [A in keyof B]: any }; I'm not really sure if that's intended |
QuestionToken should not a appear that is a left over from typescript ast. I can change it so that |
Right now we have code that handles |
@vjeux Can you give this branch a try: fix-computed-property It's a breaking change. We use the property |
Let me try! |
It seems to fix all the issues I outlined above. I'm still working through all the fatals it introduces, give me a few more minutes :) |
With the branch, it seems like interface I {
public [a: string]: number;
} { type: 'TSInterfaceBody',
body:
[ { type: 'TSIndexSignature',
range: [Object],
loc: [Object],
index: [Object],
typeAnnotation: [Object] } ],
range: [ 12, 45 ],
loc: { start: { line: 1, column: 12 }, end: { line: 3, column: 1 } } } { type: 'TSIndexSignature',
range: [ 16, 43 ],
loc: { start: { line: 2, column: 2 }, end: { line: 2, column: 29 } },
index:
{ type: 'Identifier',
range: [ 24, 25 ],
loc: { start: [Object], end: [Object] },
name: 'a',
typeAnnotation:
{ type: 'TypeAnnotation',
loc: [Object],
range: [Object],
typeAnnotation: [Object] } },
typeAnnotation:
{ type: 'TypeAnnotation',
loc: { start: [Object], end: [Object] },
range: [ 36, 42 ],
typeAnnotation: { type: 'TSNumberKeyword', range: [Object], loc: [Object] } } } |
Same for readonly interface I {
readonly [a: string]: number;
} |
It's also missing initializers interface Foo {
bar: number = 5;
} { type: 'TSInterfaceBody',
body:
[ { type: 'TSPropertySignature',
range: [Object],
loc: [Object],
optional: false,
computed: false,
key: [Object],
typeAnnotation: [Object] } ],
range: [ 14, 36 ],
loc: { start: { line: 1, column: 14 }, end: { line: 3, column: 1 } } } { type: 'TSPropertySignature',
range: [ 18, 34 ],
loc: { start: { line: 2, column: 2 }, end: { line: 2, column: 18 } },
optional: false,
computed: false,
key:
{ type: 'Identifier',
range: [ 18, 21 ],
loc: { start: [Object], end: [Object] },
name: 'bar' },
typeAnnotation:
{ type: 'TypeAnnotation',
loc: { start: [Object], end: [Object] },
range: [ 23, 29 ],
typeAnnotation: { type: 'TSNumberKeyword', range: [Object], loc: [Object] } } } |
Thanks, will update the branch |
Okay, those are the only things that are breaking :) I was able to tweak the rendering output of prettier to make everything else pass again! |
Just push new commit to branch. It adds the properties Let me know if I'm missing anything or if AST doesn't make sense, still need to add tests for these new properties |
Everything passes with 2d09fb1 ! |
Okay, so once that PR lands and #265, we'll be all green from prettier perspective! Thanks for driving all this :) |
Once we get a minor release out we can start merging in the breaking changes. |
I moved the optional property logic to each parent node instead of trying to figure it out in the Identifier node.