[英]Babel plugin addComment doesn't work when ran with codemod
我創建了一個 babel 插件:
module.exports = function (babel) {
const { types: t } = babel;
return {
name: 'addComment',
visitor: {
Program(path, state) {
path.addComment('leading', '@@@ My precious @@@');
path.unshiftContainer('body', t.noop());
}
}
};
}
我希望它應該在模塊頂部添加一個注釋行// @@@ My precious @@@
並在注釋后添加一個空行。
我用@codemod/cli 運行了這個插件:
./node_modules/.bin/codemod --plugin ./babel-plugin.js ./transform-me.js
而且我在源文件中只插入了一個空行,沒有注釋行。 如果我在 astexplorer.net 中嘗試相同的代碼,它工作正常。
我嘗試使用 "comments": true 選項添加.babelrc 文件並使用 --find-babel-config 參數運行 codemod。 同樣的結果。
我做錯了什么?
我找到了一個決定。 如果我直接操作評論數組,則插入評論:
function addComment(path, comment) {
const rootNode = path.node.body[0];
if (!rootNode.comments) {
rootNode.comments = [];
}
rootNode.comments.push({
leading: true,
trailing: false,
value: comment,
type: 'CommentLine'
});
}
path.addComment('leading', 'my comment') -> addComment(path, 'my comment')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.