7、何为traverse 的 path、scope、visitor

50 阅读1分钟

源码 parse 成 AST 之后,需要进行 AST 的遍历和增删改(transform)。那么 transform 的流程是什么样的?

babel 会递归遍历 AST,遍历过程中处理到不同的 AST 会调用不同的 visitor 函数来实现 transform。这其实是一种设计模式,叫做访问者模式:

visitor 模式

image.png

babel traverse

image.png

path

image.png

作用域 path.scope

image.png

image.png

scope.bindings、scope.references(重要)

image.png

state

image.png

AST 的别名

image.png