estree-to-babel
Version:
convert estree ast to babel
46 lines (33 loc) • 1.07 kB
JavaScript
;
module.exports.convertNodeComments = (node) => {
const {comments} = node;
if (!comments)
return;
delete node.comments;
node.leadingComments = undefined;
node.trailingComments = undefined;
node.innerComments = undefined;
for (const comment of comments) {
const group = getCommentGroup(comment);
if (!node[group])
node[group] = [];
delete comment.leading;
delete comment.trailing;
comment.type = getCommentType(comment);
node[group].push(comment);
}
};
module.exports.convertProgramComments = (comments) => {
for (const comment of comments) {
comment.type = getCommentType(comment);
}
return comments;
};
const getCommentType = ({type}) => `Comment${type}`;
function getCommentGroup({trailing, leading}) {
if (trailing)
return 'trailingComments';
if (leading)
return 'leadingComments';
return 'innerComments'; // Dangling comments, such as `[/* a */]`.
}