UNPKG

tstosc

Version:

A transpiler that convert TypeScript to SuperCollider's SCLang.

178 lines (175 loc) 8.89 kB
import ts from 'typescript'; import { Result } from '../../../util/Result.js'; import { GeneratorContext } from '../context.js'; import '../../../cli/args.js'; /** * Constant array to store all supported `SyntaxKind`. * `class` is not considered a literal here, since it need special handling. * * Notice that all recorded syntax kind is supported **does not means that** * only the member listed here is supported. * For example, `undefined` acts like a constant, but it is defined as an `ts.Identifier`. */ declare const supported_literal_syntax_kind: readonly [ts.SyntaxKind.NumericLiteral, ts.SyntaxKind.BigIntLiteral, ts.SyntaxKind.StringLiteral, ts.SyntaxKind.RegularExpressionLiteral, ts.SyntaxKind.NoSubstitutionTemplateLiteral, ts.SyntaxKind.TemplateExpression, ts.SyntaxKind.ArrayLiteralExpression, ts.SyntaxKind.ObjectLiteralExpression, ts.SyntaxKind.FunctionExpression, ts.SyntaxKind.ArrowFunction, ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.NullKeyword]; /** * Convert TypeScript's literal into SuperCollider's. * * ### Caution * * Instead of using `null` and `undefined`, when writing programs for SuperCollider, please use `nil`. * * ### Conversion Table * * | TS | SC | Example | Convert Result | * | ------------------------ | -------------------- | :----------- | :--------------------- | * | `number` | `Integer` or `Float` | `42` | `42` | * | `boolean` | `Boolean` | `true` | `true` | * | `string` | `String` | `"a"` | `"a"` | * | `string` (template) | `String` | `${a}0${b}` | `"" ++ a ++ "0" ++ b` | * | `array` | `Array` | `[0, 1]` | `[0, 1]` | * | `object` | `Dictionary` | `{a:0}` | `Dictionary["a" -> 0]` | * | `function` | `Function` | `a => a + 1` | `{ arg a; a + 1 }` | * | `null` (deprecated) | `Nil` | `null` | `nil` | * | `undefined` (deprecated) | `Nil` | `undefined` | `nil` | * * ### Generate Context Relationship * * Notice that at most situation, `convertTSExpressionToSC` does not indent result. * See `convertTSStatementToSC` for indentation rule. */ declare function convertTSLiteralToSC(literal: TSLiteral, generator_context?: GeneratorContext): string; declare function isNullOrUndefined(node: ts.Node): boolean; type TSNumberType = { numericLiteralFlags: ts.TokenFlags; } & (ts.NumericLiteral | ts.BigIntLiteral); /** * ### Logic * * First, check if number inside range. If safe, go to the next stage. * * If literal is integer outside of `i32` range, convert to sclang `float` (which is actually `f64`) * **without** lost of precision. * * If literal is integer outside of `i64` range, convert to `float` **with** lost of precision, * giving warning. * * ### Warning * * SCLang only support integer literal up to `i32`, but float literal to `f64`. * * TypeScript does **not** support `float` to have radix-prefix, but SCLang **does**. */ declare function convertTSNumberToSC(literal: TSNumberType, generator_context?: GeneratorContext): string; declare enum OutOfSCIntRange { out_of_i32_inside_i64 = 0, out_of_i64 = 1 } declare function assertSafeIntegerInSC(literal: TSNumberType): Result<null, OutOfSCIntRange>; /** * This function use the prefix to infer the radix. * Notice that this function **does not** check the correctness of literal. * * @param literal A correct TypeScript numeric literal. */ declare function getRadix(literal: TSNumberType): ts.TokenFlags.None | ts.TokenFlags.HexSpecifier | ts.TokenFlags.BinarySpecifier | ts.TokenFlags.OctalSpecifier; declare function convertTSTemplateStringToSC(literal: ts.TemplateLiteral, generator_context?: GeneratorContext): string; declare function convertTSArrayToSC(literal: ts.ArrayLiteralExpression, generator_context?: GeneratorContext): string; declare function convertTSObjectToSC(literal: ts.ObjectLiteralExpression, generator_context?: GeneratorContext): string; /** * Contains a leading space, and a `;` if necessay. */ declare function getArgLine(trivial_params: TSTrivialParameter[], arg_omittable_params: TSArgOmittableParameter[], destruct_params: TSDestructingParameter[], collecting_param: TSCollectingParameter | null, generator_context: GeneratorContext): string; declare function getDestructingParamSolvingPart(destruct_params: TSDestructingParameter[], generator_context: GeneratorContext): string; declare function convertTSFunctionToSCWithParam(literal: TSFunctionEssential, trivial_params: TSTrivialParameter[], arg_omittable_params: TSArgOmittableParameter[], destruct_params: TSDestructingParameter[], collecting_param: TSCollectingParameter | null, generator_context?: GeneratorContext): string; declare function convertTSFunctionToSC(literal: TSFunctionEssential, generator_context?: GeneratorContext): string; declare function convertToSCSymbol(node: ts.Identifier): string; declare function extractParameters(f: Pick<TSFunctionEssential, "parameters">): { /** Which could be simply convert alike `a`. */ trivial_params: TSTrivialParameter[]; /** Which could be simply convert alike `a = nil` or `a = 0`. */ arg_omittable_params: TSArgOmittableParameter[]; /** Need to generate a dummy parameter, and handled inside function body. */ destruct_params: TSDestructingParameter[]; /** The rest param collecting parameter. */ collecting_param: TSCollectingParameter | null; }; /** * Parameters that are positional, non-default-initialised. * * Example: `a` in `function (a) { }`. */ type TSTrivialParameter = Pick<ts.ParameterDeclaration, "kind"> & { name: ts.Identifier; questionToken: undefined; dotDotDotToken: undefined; initializer: undefined; }; /** * Parameters that could be omitted when passing argument. * * Example: `a` and `b` in `function (a?, b = 1) { }`. */ type TSArgOmittableParameter = TSUndefinedableParameter | TSDefaultValuedParameter; /** * Parameters that could be `undefined` when passing argument. * * Example: `a` in `function (a?) { }`. */ type TSUndefinedableParameter = ts.ParameterDeclaration & { name: ts.Identifier; questionToken: ts.QuestionToken; dotDotDotToken: undefined; initializer: ts.Expression; }; /** * Parameters with a default value. * * Example: `a` in `function (a = 1) { }`. */ type TSDefaultValuedParameter = ts.ParameterDeclaration & { name: ts.Identifier; questionToken: undefined; dotDotDotToken: undefined; initializer: ts.Expression; }; /** * Parameters with a default value. * * Example: `a` in `function (a = 1) { }`. */ type TSCollectingParameter = ts.ParameterDeclaration & { name: ts.Identifier; questionToken: undefined; dotDotDotToken: undefined; initializer: ts.Expression; }; /** * Parameters that is a destructing pattern. * * Example: `[a, b]` and `{c, d}` in `function ([a, b], {c, d}) { }`. */ type TSDestructingParameter = ts.ParameterDeclaration & { name: ts.BindingPattern; }; /** * Due to the special design of TypeScript compiler, * this project will extends the definition of `ts.LiteralExpression` * in order to make program easy-to-understand. */ type TSLiteral = ts.LiteralExpression | ts.BooleanLiteral | ts.NullLiteral | ts.ArrayLiteralExpression | ts.ObjectLiteralExpression | TSFunctionLiteral; /** * Due to the special design of TypeScript compiler, * this project will extends the definition of `ts.FunctionExpression` * in order to make program easy-to-understand. */ type TSFunctionLiteral = ts.FunctionExpression | ts.ArrowFunction; type TSFunctionEssential = ts.Node & Pick<ts.FunctionExpression, "body" | "parameters">; /** * Check if the node is literal, or literal-like expression * (including `ArrayLiteralExpression`, `ObjectLiteralExpression`, * `FunctionExpression` and `ArrowFunction`). * * ### Warning * * To check the literal that defined by ts, use `ts.isLiteral` instead. */ declare function isTSLiteral(node: ts.Node): node is TSLiteral; export { type TSArgOmittableParameter, type TSCollectingParameter, type TSDefaultValuedParameter, type TSDestructingParameter, type TSFunctionEssential, type TSFunctionLiteral, type TSNumberType, type TSTrivialParameter, type TSUndefinedableParameter, assertSafeIntegerInSC, convertTSArrayToSC, convertTSFunctionToSC, convertTSFunctionToSCWithParam, convertTSLiteralToSC, convertTSNumberToSC, convertTSObjectToSC, convertTSTemplateStringToSC, convertToSCSymbol, extractParameters, getArgLine, getDestructingParamSolvingPart, getRadix, isNullOrUndefined, isTSLiteral, supported_literal_syntax_kind };