UNPKG

tstosc

Version:

A transpiler that convert TypeScript to SuperCollider's SCLang.

110 lines (107 loc) 3.92 kB
import ts from 'typescript'; class TStoSCError extends Error { } class UnsupportedError extends TStoSCError { causing_file; causing_offset_start; causing_offset_end; tryGetErrorOrigin() { let result = ""; if (this.causing_file != void 0) { if (this.causing_offset_start != void 0 && this.causing_offset_end != void 0) { let real_start = this.causing_offset_start; let real_end = this.causing_offset_end; while (/\s/.test(this.causing_file.text[real_start])) { real_start++; real_end++; } const { line: pos_line, character: pos_col } = this.causing_file.getLineAndCharacterOfPosition(real_start); let line_start = 0; const line_start__offset = this.causing_file.getPositionOfLineAndCharacter(pos_line, 0); while (/\s/.test(this.causing_file.text[line_start__offset + line_start])) { line_start++; } let line_end = line_start; while (this.causing_file.text[line_start__offset + line_end] != "\n" && this.causing_file.text[line_start__offset + line_end] != "\r" && this.causing_file.end >= line_start__offset + line_end) { line_end++; } if (this.causing_file.getLineAndCharacterOfPosition(real_end).line > pos_line) { real_end = line_start__offset + line_end; } const example_line = this.causing_file.text.slice(line_start__offset + line_start, line_start__offset + line_end); result += `At file "${this.causing_file.fileName}:${pos_line + 1}:${pos_col + 1}". | ${pos_line + 1} ` + example_line + "\n| " + " ".repeat((pos_line + 1).toString().length + pos_col - line_start) + "^".repeat(real_end - real_start); } else { result += `At file "${this.causing_file.fileName}".`; } } return result; } constructor(message, causing_file, causing_start, causing_end) { super(message); this.causing_file = causing_file; this.causing_offset_start = causing_start; this.causing_offset_end = causing_end; } } class UnsupportedTypeError extends UnsupportedError { /** * Generate error message (by default, `description` is `"node"`): * * ```ts * `The ${description} with syntax kind "${ts.SyntaxKind[n.kind]}" is not supported.` * ``` */ static forNodeWithSyntaxKind(n, description = "node") { return new UnsupportedTypeError( `The ${description} with syntax kind "${ts.SyntaxKind[n.kind]}" is not supported.`, n.getSourceFile(), n.pos, n.end ); } /** * Generate error message (by default, `description` is `"provided syntax"`): * * ```ts * `The ${description} of syntax kind "${ts.SyntaxKind[k]}" is not supported.` * ``` */ static ofSyntaxKind(k, description = "provided syntax", n) { return new UnsupportedTypeError( `The ${description} of syntax kind "${ts.SyntaxKind[k]}" is not supported.`, n?.getSourceFile(), n?.pos, n?.end ); } constructor(message, causing_file, causing_start, causing_end) { super(message, causing_file, causing_start, causing_end); } } class UnsupportedSyntaxError extends UnsupportedError { } class SourceFileNotFoundError extends TStoSCError { static forFile(name_or_path_as_hint) { return new SourceFileNotFoundError( `The source file "${name_or_path_as_hint}" cannot be found.` ); } } class RuntimeError extends TStoSCError { } class UserAbortionError extends RuntimeError { /** * Generate error message (by default, `description` is `"user choose not to proceed"`): * * ```ts * `Action aborted: ${reason}.` * ``` */ static for(reason = "user choose not to proceed") { return new UserAbortionError( `Action aborted: ${reason}.` ); } } export { RuntimeError, SourceFileNotFoundError, TStoSCError, UnsupportedSyntaxError, UnsupportedTypeError, UserAbortionError };