This document specifies the extensions to the core ESTree AST types to support the ES2020 grammar.
extend interface Literal <: Expression {
type: "Literal";
value: string | boolean | null | number | RegExp | bigint;
}
value
property can be aBigInt
value to representBigInt
literals such as100n
.
interface BigIntLiteral <: Literal {
bigint: string;
}
bigint
property is the string representation of theBigInt
value. It doesn't include the suffixn
.- In environments that don't support
BigInt
values,value
property will benull
as theBigInt
value can't be represented natively.
interface ChainExpression <: Expression {
type: "ChainExpression";
expression: ChainElement;
}
interface ChainElement <: Node {
optional: boolean;
}
extend interface CallExpression <: ChainElement {}
extend interface MemberExpression <: ChainElement {}
- The
ChainExpression
node is the root of optional chaining. - The
ChainExpression
node contains one or moreChainElement
nodes that areoptional:true
. On the other hand,ChainElement
nodes that areoptional:true
belong to aChainExpression
node. - For backward compatibility, if all
ChainElement
nodes of a chain areoptional:false
, theChainExpression
node isn't inserted as the root of the chain. - Evaluation:
- The
ChainExpression
node is evaluated to the result of theexpression
property's node. - If the
callee|object
property is evaluated to nullish and theoptional
property istrue
, then the node and ancestor nodes are skipped until the closestChainExpression
node, and the result of theChainExpression
node becomesundefined
.
- The
For Examples:
// obj.aaa.bbb
{
"type": "MemberExpression",
"optional": false,
"object": {
"type": "MemberExpression",
"optional": false,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
// obj.aaa?.bbb
{
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": {
"type": "MemberExpression",
"optional": false,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
}
// obj?.aaa.bbb
{
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": false,
"object": {
"type": "MemberExpression",
"optional": true,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
}
// obj?.aaa?.bbb
{
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": {
"type": "MemberExpression",
"optional": true,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
}
// (obj.aaa).bbb
{
"type": "MemberExpression",
"optional": false,
"object": {
"type": "MemberExpression",
"optional": false,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
// (obj.aaa)?.bbb
{
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": {
"type": "MemberExpression",
"optional": false,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
},
"property": { "type": "Identifier", "name": "bbb" }
}
}
// (obj?.aaa).bbb
{
"type": "MemberExpression",
"optional": false,
"object": {
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
}
},
"property": { "type": "Identifier", "name": "bbb" }
}
// (obj?.aaa)?.bbb
{
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": {
"type": "ChainExpression",
"expression": {
"type": "MemberExpression",
"optional": true,
"object": { "type": "Identifier", "name": "obj" },
"property": { "type": "Identifier", "name": "aaa" }
}
},
"property": { "type": "Identifier", "name": "bbb" }
}
}
interface ImportExpression <: Expression {
type: "ImportExpression";
source: Expression;
}
ImportExpression
node represents Dynamic Imports such asimport(source)
. Thesource
property is the importing source as similar to ImportDeclaration node, but it can be an arbitrary expression node.
extend enum LogicalOperator {
"||" | "&&" | "??"
}
- The
operator
property of theLogicalExpression
node can be"??"
to represent Nullish Coalescing syntax.
Existing MetaProperty node represents import.meta
meta property as well.
extend interface ExportAllDeclaration {
exported: Identifier | null;
}
The exported
property contains an Identifier
when a different exported name is specified using as
, e.g., export * as foo from "mod";
.