How to comment out the function in the input? #15327
Unanswered
MubashirWaheed
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Why does the node is not removed when |
Beta Was this translation helpful? Give feedback.
0 replies
-
Attaching comments to a removed node is sort of undefined behaviour. Can you try replacing the function declaration into an empty statement with source comment attached? {
visitor: {
FunctionDeclaration(path) {
const value = path.getSource();
const replaced = path.replaceWith(t.emptyStatement());
t.addComment(replaced.node, "leading", `${value} `);
},
},
}; |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to write a custom babel plugin that comments out the function in the input.
Here is the first example
If I log the output I get the following and that's what I intended to get
But the issue comes when I slightly change the input to
const code = ` +console.log("Test") function greet(name) { return 'Hello ' + name; } console.log(greet('Bob'));` `
If I run the above babel code then the function comment is not added and I get
console.log("Test");\nconsole.log(greet('Bob'));
But the goal is to get
How this can be achieved?
I tried
path.insertAfter(t.addComment(path.node, 'leading',
${value}));
but the issue is original function is not removed for some reasonoutput
Beta Was this translation helpful? Give feedback.
All reactions