Skip to content

Latest commit

 

History

History
429 lines (331 loc) · 10.1 KB

es2015.md

File metadata and controls

429 lines (331 loc) · 10.1 KB

This document specifies the extensions to the core ESTree AST types to support the ES2015 grammar.

Programs

extend interface Program {
    sourceType: "script" | "module";
    body: [ Statement | ImportOrExportDeclaration ];
}

Parsers must specify sourceType as "module" if the source has been parsed as an ES6 module. Otherwise, sourceType must be "script".

Functions

extend interface Function {
    generator: boolean;
}

Statements

ForOfStatement

interface ForOfStatement <: ForInStatement {
    type: "ForOfStatement";
}

Declarations

VariableDeclaration

extend interface VariableDeclaration {
    kind: "var" | "let" | "const";
}

Expressions

interface Super <: Node {
    type: "Super";
}

extend interface CallExpression {
    callee: Expression | Super;
    arguments: [ Expression | SpreadElement ];
}

extend interface MemberExpression {
    object: Expression | Super;
}

A super pseudo-expression.

interface SpreadElement <: Node {
    type: "SpreadElement";
    argument: Expression;
}

extend interface ArrayExpression {
    elements: [ Expression | SpreadElement | null ];
}

extend interface NewExpression {
    arguments: [ Expression | SpreadElement ];
}

Spread expression, e.g., [head, ...iter, tail], f(head, ...iter, ...tail).

FIXME: This describes the Esprima and Acorn behaviors, which is not currently aligned with the SpiderMonkey behavior.

extend interface AssignmentExpression {
    left: Pattern;
}

Note that pre-ES6 code was allowed to pass references around and so left was much more liberal; an implementation might choose to continue using old definition if it needs to support such legacy code.

extend interface Property {
    key: Expression;
    method: boolean;
    shorthand: boolean;
    computed: boolean;
}

ArrowFunctionExpression

interface ArrowFunctionExpression <: Function, Expression {
    type: "ArrowFunctionExpression";
    body: FunctionBody | Expression;
    expression: boolean;
    generator: false;
}

A fat arrow function expression, e.g., let foo = (bar) => { /* body */ }.

YieldExpression

interface YieldExpression <: Expression {
    type: "YieldExpression";
    argument: Expression | null;
    delegate: boolean;
}

A yield expression.

Template Literals

TemplateLiteral

interface TemplateLiteral <: Expression {
    type: "TemplateLiteral";
    quasis: [ TemplateElement ];
    expressions: [ Expression ];
}

TaggedTemplateExpression

interface TaggedTemplateExpression <: Expression {
    type: "TaggedTemplateExpression";
    tag: Expression;
    quasi: TemplateLiteral;
}

TemplateElement

interface TemplateElement <: Node {
    type: "TemplateElement";
    tail: boolean;
    value: {
        cooked: string;
        raw: string;
    };
}

Patterns

ObjectPattern

interface AssignmentProperty <: Property {
    type: "Property"; // inherited
    value: Pattern;
    kind: "init";
    method: false;
}

interface ObjectPattern <: Pattern {
    type: "ObjectPattern";
    properties: [ AssignmentProperty ];
}

ArrayPattern

interface ArrayPattern <: Pattern {
    type: "ArrayPattern";
    elements: [ Pattern | null ];
}

RestElement

interface RestElement <: Pattern {
    type: "RestElement";
    argument: Pattern;
}

AssignmentPattern

interface AssignmentPattern <: Pattern {
    type: "AssignmentPattern";
    left: Pattern;
    right: Expression;
}

Classes

interface Class <: Node {
    id: Identifier | null;
    superClass: Expression | null;
    body: ClassBody;
}

ClassBody

interface ClassBody <: Node {
    type: "ClassBody";
    body: [ MethodDefinition ];
}

MethodDefinition

interface MethodDefinition <: Node {
    type: "MethodDefinition";
    key: Expression;
    value: FunctionExpression;
    kind: "constructor" | "method" | "get" | "set";
    computed: boolean;
    static: boolean;
}

ClassDeclaration

interface ClassDeclaration <: Class, Declaration {
    type: "ClassDeclaration";
    id: Identifier;
}

ClassExpression

interface ClassExpression <: Class, Expression {
    type: "ClassExpression";
}

MetaProperty

interface MetaProperty <: Expression {
    type: "MetaProperty";
    meta: Identifier;
    property: Identifier;
}
  • MetaProperty node represents new.target meta property in ES2015. In the future, it will represent other meta properties as well.

Modules

ImportOrExportDeclaration

interface ImportOrExportDeclaration <: Node { }

An import or export declaration.

ModuleSpecifier

interface ModuleSpecifier <: Node {
    local: Identifier;
}

A specifier in an import or export declaration.

Imports

ImportDeclaration

interface ImportDeclaration <: ImportOrExportDeclaration {
    type: "ImportDeclaration";
    specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
    source: Literal;
}

An import declaration, e.g., import foo from "mod";.

ImportSpecifier

interface ImportSpecifier <: ModuleSpecifier {
    type: "ImportSpecifier";
    imported: Identifier;
}

An imported variable binding, e.g., {foo} in import {foo} from "mod" or {foo as bar} in import {foo as bar} from "mod". The imported field refers to the name of the export imported from the module. The local field refers to the binding imported into the local module scope. If it is a basic named import, such as in import {foo} from "mod", both imported and local are equivalent Identifier nodes; in this case an Identifier node representing foo. If it is an aliased import, such as in import {foo as bar} from "mod", the imported field is an Identifier node representing foo, and the local field is an Identifier node representing bar.

ImportDefaultSpecifier

interface ImportDefaultSpecifier <: ModuleSpecifier {
    type: "ImportDefaultSpecifier";
}

A default import specifier, e.g., foo in import foo from "mod.js".

ImportNamespaceSpecifier

interface ImportNamespaceSpecifier <: ModuleSpecifier {
    type: "ImportNamespaceSpecifier";
}

A namespace import specifier, e.g., * as foo in import * as foo from "mod.js".

Exports

ExportNamedDeclaration

interface ExportNamedDeclaration <: ImportOrExportDeclaration {
    type: "ExportNamedDeclaration";
    declaration: Declaration | null;
    specifiers: [ ExportSpecifier ];
    source: Literal | null;
}

An export named declaration, e.g., export {foo, bar};, export {foo} from "mod"; or export var foo = 1;.

Note: Having declaration populated with non-empty specifiers or non-null source results in an invalid state.

ExportSpecifier

interface ExportSpecifier <: ModuleSpecifier {
    type: "ExportSpecifier";
    exported: Identifier;
}

An exported variable binding, e.g., {foo} in export {foo} or {bar as foo} in export {bar as foo}. The exported field refers to the name exported in the module. The local field refers to the binding into the local module scope. If it is a basic named export, such as in export {foo}, both exported and local are equivalent Identifier nodes; in this case an Identifier node representing foo. If it is an aliased export, such as in export {bar as foo}, the exported field is an Identifier node representing foo, and the local field is an Identifier node representing bar.

ExportDefaultDeclaration

interface AnonymousDefaultExportedFunctionDeclaration <: Function {
    type: "FunctionDeclaration";
    id: null;
}

interface AnonymousDefaultExportedClassDeclaration <: Class {
    type: "ClassDeclaration";
    id: null;
}

interface ExportDefaultDeclaration <: ImportOrExportDeclaration {
    type: "ExportDefaultDeclaration";
    declaration: AnonymousDefaultExportedFunctionDeclaration | FunctionDeclaration | AnonymousDefaultExportedClassDeclaration | ClassDeclaration | Expression;
}

An export default declaration, e.g., export default function () {}; or export default 1;.

ExportAllDeclaration

interface ExportAllDeclaration <: ImportOrExportDeclaration {
    type: "ExportAllDeclaration";
    source: Literal;
}

An export batch declaration, e.g., export * from "mod";.