Skip to content

Split declaration initialization / Fix TS Annotation Duplication#244

Merged
nicoespeon merged 4 commits intonicoespeon:masterfrom
iulspop:split-declaration-initialization/fix-ts-annotation-duplication
Feb 9, 2021
Merged

Split declaration initialization / Fix TS Annotation Duplication#244
nicoespeon merged 4 commits intonicoespeon:masterfrom
iulspop:split-declaration-initialization/fix-ts-annotation-duplication

Conversation

@iulspop
Copy link
Contributor

@iulspop iulspop commented Feb 5, 2021

Closes #232

Adds a test to cover split declaration with TS annotation and fixes the duplication issue.

split-declaration-initialization-ts

I'm not sure if my implementation is readable. I felt the need to add a comment to clarify why I create a new Identifier.
It's necessary, because if I reassign the typeAnnotation property to null, it mutates the Identifier node in the variableDeclaration too. I want the annotation to remain in the declaration, so I need to create a new Identifier and modify that.

Feel free to improve the implementation!

Does not work because Identifier node is mutated by setting the typeAnnotation property to null and the variableDeclaration needs a type annotation.
Copy link
Owner

@nicoespeon nicoespeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the fix!

I'll update the Changelog & get that release this week with the other changes ❤️
I'll try to get some improvements I've been working on done too, then I'll release.

lastName = "Doe";`
},
{
description: "declarations with type annotations",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for adding a test to illustrate

t.expressionStatement(t.assignmentExpression("=", id, init))
)
.map(function ({ id, init }) {
if (id.type == "Identifier" && "typeAnnotation" in id) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can write:

if (t.isIdentifier(id)) {

And that should work. It will be less dependent of the implementation details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you merged into master without adding your suggestion. Did you change your mind?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iulspop nope, I did it faster. It wasn't blocking so simpler to get this merged then refactor on master 👍

9b6624b

@nicoespeon
Copy link
Owner

@allcontributors please add iulspop for bug

@allcontributors
Copy link
Contributor

@nicoespeon

I've put up a pull request to add @iulspop! 🎉

@nicoespeon nicoespeon merged commit 59249cb into nicoespeon:master Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split declaration and initialization duplicates type in ts declarations

2 participants