shell
npm install --save-dev @babel/helper-plugin-utilities
script
import { assertStringLiteral, addComment, isBlockScoped, inherits, replacementLiteral } from '@babel/helper-plugin-utilities';
export default function customPlugin() {
return {
visitor: {
Identifier(path, state) {
assertStringLiteral(path.node, 'name');
addComment(path.node, 'leading', 'This is a comment');
if (isBlockScoped(path.node)) {
console.log('This identifier is block scoped.');
}
const newNode = replacementLiteral('New Value');
path.replaceWith(newNode);
},
},
};
}