path常用属性和方法介绍

发布时间 2023-11-19 10:29:52作者: 守护式等待

1 path.toString()  当前路径所对应的源代码 

const visitor = {
    VariableDeclaration(path) {
        console.log(path.toString());   // 当前路径所对应的源代码
    },
}

2.path.isXXX()  判断path是什么type

if (path.isStringLiteral()) {
    //do something;
}

3.path.parentPath  获取path的上一级路径

let parent = path.parentPath;

4.path.get() 获取path的子路径

path.get('id');

5.path.remove() 删除path

path.remove()  

6.path.replaceWith()  替换path

const type = require("@babel/types")
path.replaceWith(type.NumericLiteral(3));

7.path.node  node,其实是path的一个属性

const node = path.node;