UNPKG

tstosc

Version:

A transpiler that convert TypeScript to SuperCollider's SCLang.

94 lines (91 loc) 6.12 kB
import ts from 'typescript'; import { GeneratorContext } from '../context.js'; import '../../../cli/args.js'; declare const supported_expression_syntax_kind: readonly [ts.SyntaxKind.ArrayLiteralExpression, ts.SyntaxKind.ObjectLiteralExpression, ts.SyntaxKind.PropertyAccessExpression, ts.SyntaxKind.ElementAccessExpression, ts.SyntaxKind.CallExpression, ts.SyntaxKind.NewExpression, ts.SyntaxKind.TaggedTemplateExpression, ts.SyntaxKind.TypeAssertionExpression, ts.SyntaxKind.ParenthesizedExpression, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.PrefixUnaryExpression, ts.SyntaxKind.PostfixUnaryExpression, ts.SyntaxKind.BinaryExpression, ts.SyntaxKind.ConditionalExpression, ts.SyntaxKind.TemplateExpression, ts.SyntaxKind.SpreadElement, ts.SyntaxKind.ClassExpression, ts.SyntaxKind.OmittedExpression, ts.SyntaxKind.ExpressionWithTypeArguments, ts.SyntaxKind.AsExpression, ts.SyntaxKind.NonNullExpression, ts.SyntaxKind.MetaProperty, ts.SyntaxKind.SatisfiesExpression]; /** * ### Generate Context Relationship * * `is_generating_constructor`: Controls how `this` and `super` are transpiled. * * ### Warning * * * No `;` is attached. * * Notice that at most situation, `convertTSExpressionToSC` does not indent result. * See `convertTSStatementToSC` for indentation rule. * * If converting an expression with self-indecr operator, this function generates multiple lines of statement, * with the last statements being a global variable refering to the value of original expression. */ declare function convertTSExpressionToSC(expr: ts.Expression, generator_context?: GeneratorContext): string; type SelfIndecrExpression = (ts.PrefixUnaryExpression | ts.PostfixUnaryExpression) & { operator: ts.SyntaxKind.PlusPlusToken | ts.SyntaxKind.MinusMinusToken; }; type SelfIndecrTarget = ts.Identifier | ts.PropertyAccessExpression | ts.ElementAccessExpression; declare function isSelfIndecrExpression(e: ts.Node): e is SelfIndecrExpression; declare function hasSelfIndecrExpression(e: ts.Node): boolean; /** * Find all identifier that uses self-indecr operator. */ declare function tryFindSelfIndecrOperator(e: ts.Node): SelfIndecrTarget[]; declare function isStoringObjectLiteral(node: ts.Node, generator_context: GeneratorContext): boolean; /** * ### Processing Logic * * * If left side of expression is an object literal, it will be translated to `TSTOSC_ObjectLiteral` in SCLang. * So, using `[]` to index. * * Otherwise, just use `.`. */ declare function convertTSPropertyAccessExpressionToSC(e: ts.PropertyAccessExpression, generator_context?: GeneratorContext): string; declare function convertTSElementAccessExpressionToSC(e: ts.ElementAccessExpression, generator_context?: GeneratorContext): string; declare function isMethod(node: ts.Node, generator_context: GeneratorContext): boolean; declare function convertTSCallExpressionToSC(e: ts.CallExpression, generator_context?: GeneratorContext): string; declare function convertTSNewExpressionToSC(e: ts.NewExpression, generator_context?: GeneratorContext): string; declare function convertTSTaggedTemplateExpressionToSC(e: ts.TaggedTemplateExpression, generator_context?: GeneratorContext): string; /** * Handle things like `+a`, `!a`, or `++a`. * * If exist unary self increment/decrement expression, * global variables such as `~tstosc__pre_incr` will be generated beforehand. */ declare function translateTSPrefixUnaryExpressionToSC(e: ts.PrefixUnaryExpression, generator_context?: GeneratorContext): string; /** * Handle things like `a++` and `a--`. * * If exist unary self increment/decrement expression, * global variables such as `~tstosc__pre_incr` will be generated beforehand. */ declare function translateTSPostfixUnaryExpressionToSC(e: ts.PostfixUnaryExpression, generator_context?: GeneratorContext): string; declare function convertTSConditionalExpressionToSC(e: ts.ConditionalExpression, generator_context?: GeneratorContext): string; declare function convertTSBinaryExpressionToSC(e: ts.BinaryExpression, generator_context?: GeneratorContext): string; /** * Test if an identifier (including variable, function, or class) will be considered legal in SuperCollider. * * * Check if first character is a English letter. * * Check if remain characters (if exist) are word-like (`\w` in regex). */ declare function isLegalSCIdentifier(name: string): boolean; /** * Test if an variable will be considered legal in SuperCollider. * * * Check if first character is a English letter, and it is lower-case. * * Check if remain characters (if exist) are word-like (`\w` in regex). */ declare function isLegalSCVar(name: string): boolean; /** * Test if an class will be considered legal in SuperCollider. * * * Check if first character is a English letter, and it is Upper-Case. * * Check if remain characters (if exist) are word-like (`\w` in regex). */ declare function isLegalSCClass(name: string): boolean; /** * Escape the inputed variable's name, if it is not legal in SCLang. */ declare function escapeForSCVarIfNeeded(name: string, esc_seq?: string): string; /** * Escape the inputed function/method's name, if it is not legal in SCLang. */ declare function escapeForSCFunctionIfNeeded(name: string, esc_seq?: string): string; /** * Escape the inputed class's name, if it is not legal in SCLang. */ declare function escapeForSCClassIfNeeded(name: string, esc_seq?: string): string; export { type SelfIndecrExpression, type SelfIndecrTarget, convertTSBinaryExpressionToSC, convertTSCallExpressionToSC, convertTSConditionalExpressionToSC, convertTSElementAccessExpressionToSC, convertTSExpressionToSC, convertTSNewExpressionToSC, convertTSPropertyAccessExpressionToSC, convertTSTaggedTemplateExpressionToSC, escapeForSCClassIfNeeded, escapeForSCFunctionIfNeeded, escapeForSCVarIfNeeded, hasSelfIndecrExpression, isLegalSCClass, isLegalSCIdentifier, isLegalSCVar, isMethod, isSelfIndecrExpression, isStoringObjectLiteral, supported_expression_syntax_kind, translateTSPostfixUnaryExpressionToSC, translateTSPrefixUnaryExpressionToSC, tryFindSelfIndecrOperator };