为什么 const/let VariableDeclarations 在 AST 树中而不是像 ECMAScript 中的 LexicalDeclarations?

Why are const/let VariableDeclarations in AST trees and not LexicalDeclarations like in ECMAScript?

在 ECMAScript 中只有 var 是 'VariableDeclaration' 并且 const/let 是 'LexicalDeclaration' 类型。

const/let

var

但是,我一直在学习 AST 树并注意到 estree 和 babel 解析器都在 'Variable Declarations' 下定义了 const/let。

estree

babel-parser

节点类型不应该对应于 ECMAScript 节点类型,还是我误解了 AST 树的构建方式?

Shouldn't the node types correspond to ECMAScript node types

AST 格式的规范完全独立于语言语法定义。往往两者最终会产生相关性,但并不是它们匹配的总体目标。

在 ESTree 的上下文中,您将使用 .kind 来区分各种类型,而不是使用不同的节点类型,尽管这肯定是一种替代方法。