Skip to content

Latest commit

 

History

History
20 lines (15 loc) · 861 Bytes

ast-tip-syntaxkind.md

File metadata and controls

20 lines (15 loc) · 861 Bytes

TIP: SyntaxKind enum

SyntaxKind is defined as a const enum, here is a sample:

export const enum SyntaxKind {
    Unknown,
    EndOfFileToken,
    SingleLineCommentTrivia,
    // ... LOTS more

It's a const enum (a concept we covered previously) so that it gets inlined (e.g. ts.SyntaxKind.EndOfFileToken becomes 1) and we don't get a dereferencing cost when working with the AST. However, the compiler is compiled with --preserveConstEnums compiler flag so that the enum is still available at runtime. So in JavaScript you can use ts.SyntaxKind.EndOfFileToken if you want. Additionally you can convert these enum members to display strings using the following function:

export function syntaxKindToName(kind: ts.SyntaxKind) {
    return (<any>ts).SyntaxKind[kind];
}