Closed Itach1Uchixa closed 2 months ago
Attributes and directives are both VAttribute, but you can distinguish them by looking at the directive
property on the VAttribute object to see if it has v-
or not. Note that the key
property has a different type between the two as well - one is a VDirectiveKey and the other is VIdentifier
const test: CodemodPlugin = {
type: 'codemod',
name: 'test',
transform({ sfcAST, utils: { traverseTemplateAST } }) {
if (sfcAST) {
traverseTemplateAST(sfcAST, {
enterNode(node) {
if (node.type === 'VAttribute'
&& node.directive
&& node.key.name.name === 'slot') {
// v-slot directive
}
if (node.type === 'VAttribute'
&& !node.directive
&& node.key.name === 'slot') {
// slot attribute
}
},
});
}
},
};
I would lean away from trying to work directly with source code in a codemod. vue-metamorph runs through all codemod plugins before rendering the changes. It doesn't re-parse the code for every plugin.
It would be awkward to add source code to the plugin context because any nodes added by a plugin wouldn't be available to look at in the source code by later plugins.
I would be great if source code string would be available in CodemodPluginContext. I came across the problem when I can't distinguish v-slot and old deprecated slot attribute both of them giving same VAttribute result. If I hand access to source code string such cases would be solved easily