Skip to content
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

fix flow-comments - class type paramters and implements #9897

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions packages/babel-plugin-transform-flow-comments/src/index.js
Expand Up @@ -135,14 +135,31 @@ export default declare(api => {

Class(path) {
const { node } = path;
if (node.typeParameters) {
const typeParameters = path.get("typeParameters");
if (node.typeParameters || node.implements) {
const comments = [];
if (node.typeParameters) {
const typeParameters = path.get("typeParameters");
comments.push(
generateComment(typeParameters, typeParameters.node).replace(
/^:: /,
"",
),
);
typeParameters.remove();
}
if (node.implements) {
const impls = path.get("implements");
comments.push(
"implements " +
impls
.map(impl => generateComment(impl).replace(/^:: /, ""))
.join(", "),
);
delete node["implements"];
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't impls.remove() work? It's better not to mutate the ast directly, since path methods sometimes do some clean up when mutating nodes.

Copy link
Member Author

Choose a reason for hiding this comment

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

impls is an array, there's no .remove()
should I clone the node, modify and path.replaceWith(...) with the clone?

Copy link
Member

Choose a reason for hiding this comment

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

Oh you can impls.forEach(p => p.remove())

Copy link
Member Author

Choose a reason for hiding this comment

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

I remembered I tried it before, i ended up with class Foo implements {

Copy link
Member

Choose a reason for hiding this comment

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

It's ok like this then.

}

const block = path.get("body");
block.addComment(
"leading",
generateComment(typeParameters, typeParameters.node),
);
typeParameters.remove();
block.addComment("leading", ":: " + comments.join(" "));
}
},
},
Expand Down
@@ -0,0 +1 @@
class Foo implements Bar, Baz {}
@@ -0,0 +1,3 @@
class Foo
/*:: implements Bar, Baz*/
{}
@@ -0,0 +1 @@
class Foo<S, T> implements Bar, Baz {}
@@ -0,0 +1,3 @@
class Foo
/*:: <S, T> implements Bar, Baz*/
{}
@@ -0,0 +1 @@
class Foo<T> {}
@@ -0,0 +1,3 @@
class Foo
/*:: <T>*/
{}