typescript-to-lua
Version:
A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!
155 lines (154 loc) • 8.83 kB
TypeScript
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { OneToManyVisitorResult } from "../utils/lua-ast";
import { TransformationContext } from "./context";
interface NodesBySyntaxKind {
[]: ts.NumericLiteral;
[]: ts.BigIntLiteral;
[]: ts.StringLiteral;
[]: ts.JsxText;
[]: ts.RegularExpressionLiteral;
[]: ts.NoSubstitutionTemplateLiteral;
[]: ts.TemplateHead;
[]: ts.TemplateMiddle;
[]: ts.TemplateTail;
[]: ts.Identifier;
[]: ts.QualifiedName;
[]: ts.ComputedPropertyName;
[]: ts.TypeParameterDeclaration;
[]: ts.ParameterDeclaration;
[]: ts.Decorator;
[]: ts.PropertySignature;
[]: ts.PropertyDeclaration;
[]: ts.MethodSignature;
[]: ts.MethodDeclaration;
[]: ts.ConstructorDeclaration;
[]: ts.GetAccessorDeclaration;
[]: ts.SetAccessorDeclaration;
[]: ts.CallSignatureDeclaration;
[]: ts.ConstructSignatureDeclaration;
[]: ts.IndexSignatureDeclaration;
[]: ts.ObjectBindingPattern;
[]: ts.ArrayBindingPattern;
[]: ts.BindingElement;
[]: ts.ArrayLiteralExpression;
[]: ts.ObjectLiteralExpression;
[]: ts.PropertyAccessExpression;
[]: ts.ElementAccessExpression;
[]: ts.CallExpression;
[]: ts.NewExpression;
[]: ts.TaggedTemplateExpression;
[]: ts.TypeAssertion;
[]: ts.ParenthesizedExpression;
[]: ts.FunctionExpression;
[]: ts.ArrowFunction;
[]: ts.DeleteExpression;
[]: ts.TypeOfExpression;
[]: ts.VoidExpression;
[]: ts.AwaitExpression;
[]: ts.PrefixUnaryExpression;
[]: ts.PostfixUnaryExpression;
[]: ts.BinaryExpression;
[]: ts.ConditionalExpression;
[]: ts.TemplateExpression;
[]: ts.YieldExpression;
[]: ts.SpreadElement;
[]: ts.ClassExpression;
[]: ts.OmittedExpression;
[]: ts.ExpressionWithTypeArguments;
[]: ts.AsExpression;
[]: ts.NonNullExpression;
[]: ts.MetaProperty;
[]: ts.SatisfiesExpression;
[]: ts.TemplateSpan;
[]: ts.SemicolonClassElement;
[]: ts.Block;
[]: ts.VariableStatement;
[]: ts.EmptyStatement;
[]: ts.ExpressionStatement;
[]: ts.IfStatement;
[]: ts.DoStatement;
[]: ts.WhileStatement;
[]: ts.ForStatement;
[]: ts.ForInStatement;
[]: ts.ForOfStatement;
[]: ts.ContinueStatement;
[]: ts.BreakStatement;
[]: ts.ReturnStatement;
[]: ts.WithStatement;
[]: ts.SwitchStatement;
[]: ts.LabeledStatement;
[]: ts.ThrowStatement;
[]: ts.TryStatement;
[]: ts.DebuggerStatement;
[]: ts.VariableDeclaration;
[]: ts.VariableDeclarationList;
[]: ts.FunctionDeclaration;
[]: ts.ClassDeclaration;
[]: ts.InterfaceDeclaration;
[]: ts.TypeAliasDeclaration;
[]: ts.EnumDeclaration;
[]: ts.ModuleDeclaration;
[]: ts.ModuleBlock;
[]: ts.CaseBlock;
[]: ts.NamespaceExportDeclaration;
[]: ts.ImportEqualsDeclaration;
[]: ts.ImportDeclaration;
[]: ts.ImportClause;
[]: ts.NamespaceImport;
[]: ts.NamedImports;
[]: ts.ImportSpecifier;
[]: ts.ExportAssignment;
[]: ts.ExportDeclaration;
[]: ts.NamedExports;
[]: ts.ExportSpecifier;
[]: ts.MissingDeclaration;
[]: ts.ExternalModuleReference;
[]: ts.JsxElement;
[]: ts.JsxSelfClosingElement;
[]: ts.JsxOpeningElement;
[]: ts.JsxClosingElement;
[]: ts.JsxFragment;
[]: ts.JsxOpeningFragment;
[]: ts.JsxClosingFragment;
[]: ts.JsxAttribute;
[]: ts.JsxAttributes;
[]: ts.JsxSpreadAttribute;
[]: ts.JsxExpression;
[]: ts.CaseClause;
[]: ts.DefaultClause;
[]: ts.HeritageClause;
[]: ts.CatchClause;
[]: ts.PropertyAssignment;
[]: ts.ShorthandPropertyAssignment;
[]: ts.SpreadAssignment;
[]: ts.EnumMember;
[]: ts.SourceFile;
[]: ts.BooleanLiteral;
[]: ts.BooleanLiteral;
[]: ts.NullLiteral;
[]: ts.SuperExpression;
[]: ts.ThisExpression;
[]: ts.NotEmittedStatement;
}
export type ExpressionLikeNode = ts.Expression | ts.QualifiedName | ts.ExternalModuleReference;
export type StatementLikeNode = ts.Statement;
export type VisitorResult<T extends ts.Node> = T extends ExpressionLikeNode ? lua.Expression : T extends StatementLikeNode ? OneToManyVisitorResult<lua.Statement> : T extends ts.SourceFile ? lua.File : OneToManyVisitorResult<lua.Node>;
export type Visitor<T extends ts.Node> = FunctionVisitor<T> | ObjectVisitor<T>;
export type FunctionVisitor<T extends ts.Node> = (node: T, context: TransformationContext) => VisitorResult<T>;
export interface ObjectVisitor<T extends ts.Node> {
transform: FunctionVisitor<T>;
/**
* Visitors with higher priority are called first.
*
* Higher-priority visitors can call lower ones with `context.superTransformNode`.
*
* Standard visitors have the lowest (`-Infinity`) priority.
*/
priority?: number;
}
export type Visitors = {
[]?: Visitor<NodesBySyntaxKind[P]>;
};
export type VisitorMap = Map<ts.SyntaxKind, Array<FunctionVisitor<ts.Node>>>;
export {};