[英]Babel Plugin: How to insert a sibling node
It seems that path.insertAfter(), path.insertBefore(), path.unshiftContainer(), and path.pushContainer() only work with Statements.似乎 path.insertAfter()、path.insertBefore()、path.unshiftContainer() 和 path.pushContainer() 只适用于 Statements。 How do you insert nodes that are not Statements?
如何插入不是 Statements 的节点?
In my case I am writing a babel jsx plugin and I am trying to insert a sibling node which is a JSXExpressionContainer.就我而言,我正在编写一个 babel jsx 插件,并且我正在尝试插入一个兄弟节点,它是一个 JSXExpressionContainer。 When I do this I get the following error:
当我这样做时,我收到以下错误:
TypeError: Property body[0] of BlockStatement expected node to be of a type ["Statement"] but instead got "JSXExpressionContainer"
How do I fix this?我该如何解决?
From the documentation here :从这里的文档:
FunctionDeclaration(path) {
path.insertBefore(t.expressionStatement(t.stringLiteral("Because I'm easy come, easy go.")));
path.insertAfter(t.expressionStatement(t.stringLiteral("A little high, little low.")));
}
Looking at your error, make sure you do your types correctly.查看您的错误,确保您正确输入。 Just check babel types here and see what parameters are needed by each type.
只需在这里检查 babel 类型,看看每种类型需要什么参数。
I found a workaround by just pushing the code as plaintext.我找到了一种解决方法,只需将代码作为纯文本推送即可。 React seems to work just fine.
React 似乎工作得很好。
path.node.children.push('<div>Click me</div>')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.