UNPKG

tstosc

Version:

A transpiler that convert TypeScript to SuperCollider's SCLang.

126 lines (120 loc) 4.54 kB
'use strict'; var ts = require('typescript'); var Result_cjs = require('../../util/Result.cjs'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var ts__default = /*#__PURE__*/_interopDefault(ts); class Analyser { static default_compiler_option = { noEmit: true, lib: ["lib.esnext.full.d.ts"], target: ts__default.default.ScriptTarget.ESNext, module: ts__default.default.ModuleKind.ESNext, esModuleInterop: true, moduleResolution: ts__default.default.ModuleResolutionKind.Bundler }; static default_compiler_host = ts__default.default.createCompilerHost( Analyser.default_compiler_option, true // Not forget to let it set parent node. ); static virtual_file_name = "virtual_file__do_not_really_exist.ts"; static default_constructor_args = { root_files_path: [] }; program; constructor(args = Analyser.default_constructor_args) { const root_files_path = "root_files_path" in args ? args.root_files_path : args.files.map((f) => f.input_path); this.program = ts__default.default.createProgram({ rootNames: root_files_path, options: Analyser.default_compiler_option, host: Analyser.default_compiler_host }); } /** * The most important feature of `Analyser`. * * This function returns the configured `ts.Program` which handles all inputed files. */ getCompilerProgram() { return this.program; } /** * Get the Abstract Syntax Tree (AST, with type `ts.SourceFile`) of a TypeScript file. * * @param path If relative, the `.` stands for the working directory when `tstosc` starts. * Be careful that the file you want to get AST from, must be **in** the `root_files_path`, * or **being referenced** by the files in the `root_files_path`. */ getASTOfFile(file_path) { return this.program.getSourceFile(file_path); } static buildASTFromCode(source_code) { const ts_source_file = ts__default.default.createSourceFile( Analyser.virtual_file_name, source_code, ts__default.default.ScriptTarget.Latest, true // From ChatGPT: Set to `true` to enable syntax tree preservation ); { const possible_errors = Analyser.preCheckErrorsBeforeBuildAST({ ts_source_file }); if (possible_errors.length > 0) { return Result_cjs.Result.createErr({ error_type: "input_is_not_valid_ts" /* input_is_not_valid_ts */, details: possible_errors }); } } return Result_cjs.Result.createOk(ts_source_file); } static preCheckErrorsBeforeBuildAST({ ts_source_file }) { const host = { ...Analyser.default_compiler_host, getSourceFile: (file_name, language_version) => { if (file_name == ts_source_file.fileName) { return ts_source_file; } else { return Analyser.default_compiler_host.getSourceFile(file_name, language_version); } }, getDefaultLibFileName: () => ts__default.default.getDefaultLibFileName(Analyser.default_compiler_option) }; return ts__default.default.getPreEmitDiagnostics( ts__default.default.createProgram({ rootNames: [ts_source_file.fileName], options: Analyser.default_compiler_option, host }) ).map( function(d) { const error_message = ts__default.default.flattenDiagnosticMessageText(d.messageText, "\n"); if (d.file != void 0 && d.start != void 0) { const { line, character } = ts__default.default.getLineAndCharacterOfPosition(d.file, d.start); return { is_compiler_error: false, position_line: line + 1, position_column: character + 1, message: error_message }; } else { return { is_compiler_error: true, message: error_message }; } } ); } checkIfExistsUnsupportSyntax() { } traverseTSNode(ts_node, action) { action(ts_node); ts__default.default.forEachChild(ts_node, action); } } var BuildASTFailErrorType = /* @__PURE__ */ ((BuildASTFailErrorType2) => { BuildASTFailErrorType2["input_is_not_valid_ts"] = "input_is_not_valid_ts"; BuildASTFailErrorType2["input_contains_unsupported_syntax"] = "input_contains_unsupported_syntax"; return BuildASTFailErrorType2; })(BuildASTFailErrorType || {}); function getASTFromCode(source_code) { return Analyser.buildASTFromCode(source_code); } exports.Analyser = Analyser; exports.BuildASTFailErrorType = BuildASTFailErrorType; exports.getASTFromCode = getASTFromCode;