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AS,SAAS,CAAE,KAAa;IAC/B,IAAI,KAAK,GAAG,CAAC,CAAA;IAEb,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,IAAI,IAAI,KAAK,IAAI,EAAE;YACjB,KAAK,EAAE,CAAA;SACR;KACF;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,cAAc,GAAgB,IAAI,GAAG,CAAC;IAC1C,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI;IACJ,IAAI,CAAC,oFAAoF;CAC1F,CAAC,CAAA;AAEF;;GAEG;AACH,SAAS,aAAa,CAAE,KAAc;IACpC,OAAO,KAAK,CAAC,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;AACtE,CAAC","sourcesContent":["import { diffLines } from 'diff'\nimport { homedir } from 'os'\nimport { join } from 'path'\nimport { Recoverable, start } from 'repl'\nimport { Script } from 'vm'\nimport { Service, CreateOptions, TSError } from './index'\nimport { readFileSync, statSync } from 'fs'\nimport { Console } from 'console'\nimport * as tty from 'tty'\n\n/**\n * Eval filename for REPL/debug.\n * @internal\n */\nexport const EVAL_FILENAME = `[eval].ts`\n\nexport interface ReplService {\n readonly state: EvalState\n /**\n * Bind this REPL to a ts-node compiler service. A compiler service must be bound before `eval`-ing code or starting the REPL\n */\n setService (service: Service): void\n evalCode (code: string): void\n /**\n * `eval` implementation compatible with node's REPL API\n */\n nodeEval (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any): void\n evalAwarePartialHost: EvalAwarePartialHost\n /** Start a node REPL */\n start (code?: string): void\n /** @internal */\n readonly stdin: NodeJS.ReadableStream\n /** @internal */\n readonly stdout: NodeJS.WritableStream\n /** @internal */\n readonly stderr: NodeJS.WritableStream\n /** @internal */\n readonly console: Console\n}\n\nexport interface CreateReplOptions {\n service?: Service\n state?: EvalState\n stdin?: NodeJS.ReadableStream\n stdout?: NodeJS.WritableStream\n stderr?: NodeJS.WritableStream\n}\n\nexport function createRepl (options: CreateReplOptions = {}) {\n let service = options.service\n const state = options.state ?? new EvalState(join(process.cwd(), EVAL_FILENAME))\n const evalAwarePartialHost = createEvalAwarePartialHost(state)\n const stdin = options.stdin ?? process.stdin\n const stdout = options.stdout ?? process.stdout\n const stderr = options.stderr ?? process.stderr\n const _console = stdout === process.stdout && stderr === process.stderr ? console : new Console(stdout, stderr)\n\n const replService: ReplService = {\n state: options.state ?? new EvalState(join(process.cwd(), EVAL_FILENAME)),\n setService,\n evalCode,\n nodeEval,\n evalAwarePartialHost,\n start,\n stdin,\n stdout,\n stderr,\n console: _console\n }\n return replService\n\n function setService (_service: Service) {\n service = _service\n }\n\n function evalCode (code: string) {\n return _eval(service!, state, code)\n }\n\n function nodeEval (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any) {\n let err: Error | null = null\n let result: any\n\n // TODO: Figure out how to handle completion here.\n if (code === '.scope') {\n callback(err)\n return\n }\n\n try {\n result = evalCode(code)\n } catch (error) {\n if (error instanceof TSError) {\n // Support recoverable compilations using >= node 6.\n if (Recoverable && isRecoverable(error)) {\n err = new Recoverable(error)\n } else {\n console.error(error)\n }\n } else {\n err = error\n }\n }\n\n return callback(err, result)\n }\n\n function start (code?: string) {\n // TODO assert that service is set; remove all ! postfixes\n return startRepl(replService, service!, state, code)\n }\n}\n\n/**\n * Eval state management. Stores virtual `[eval].ts` file\n */\nexport class EvalState {\n /** @internal */\n input = ''\n /** @internal */\n output = ''\n /** @internal */\n version = 0\n /** @internal */\n lines = 0\n\n // tslint:disable-next-line:variable-name\n __tsNodeEvalStateBrand: unknown\n\n constructor (public path: string) { }\n}\n\n/**\n * Filesystem host functions which are aware of the \"virtual\" [eval].ts file used to compile REPL inputs.\n * Must be passed to `create()` to create a ts-node compiler service which can compile REPL inputs.\n */\nexport type EvalAwarePartialHost = Pick\n\nexport function createEvalAwarePartialHost (state: EvalState): EvalAwarePartialHost {\n function readFile (path: string) {\n if (path === state.path) return state.input\n\n try {\n return readFileSync(path, 'utf8')\n } catch (err) {/* Ignore. */}\n }\n function fileExists (path: string) {\n if (path === state.path) return true\n\n try {\n const stats = statSync(path)\n return stats.isFile() || stats.isFIFO()\n } catch (err) {\n return false\n }\n }\n return { readFile, fileExists }\n}\n\n/**\n * Evaluate the code snippet.\n */\nfunction _eval (service: Service, state: EvalState, input: string) {\n const lines = state.lines\n const isCompletion = !/\\n$/.test(input)\n const undo = appendEval(state, input)\n let output: string\n\n try {\n output = service.compile(state.input, state.path, -lines)\n } catch (err) {\n undo()\n throw err\n }\n\n // Use `diff` to check for new JavaScript to execute.\n const changes = diffLines(state.output, output)\n\n if (isCompletion) {\n undo()\n } else {\n state.output = output\n }\n\n return changes.reduce((result, change) => {\n return change.added ? exec(change.value, state.path) : result\n }, undefined)\n}\n\n/**\n * Execute some code.\n */\nfunction exec (code: string, filename: string) {\n const script = new Script(code, { filename: filename })\n\n return script.runInThisContext()\n}\n\n/**\n * Start a CLI REPL.\n */\nfunction startRepl (replService: ReplService, service: Service, state: EvalState, code?: string) {\n // Eval incoming code before the REPL starts.\n if (code) {\n try {\n replService.evalCode(`${code}\\n`)\n } catch (err) {\n replService.console.error(err)\n process.exit(1)\n }\n }\n\n const repl = start({\n prompt: '> ',\n input: replService.stdin,\n output: replService.stdout,\n // Mimicking node's REPL implementation: https://github.com/nodejs/node/blob/168b22ba073ee1cbf8d0bcb4ded7ff3099335d04/lib/internal/repl.js#L28-L30\n terminal: (replService.stdout as tty.WriteStream).isTTY && !parseInt(process.env.NODE_NO_READLINE!, 10),\n eval: replService.nodeEval,\n useGlobal: true\n })\n\n // Bookmark the point where we should reset the REPL state.\n const resetEval = appendEval(state, '')\n\n function reset () {\n resetEval()\n\n // Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.\n exec('exports = module.exports', state.path)\n }\n\n reset()\n repl.on('reset', reset)\n\n repl.defineCommand('type', {\n help: 'Check the type of a TypeScript identifier',\n action: function (identifier: string) {\n if (!identifier) {\n repl.displayPrompt()\n return\n }\n\n const undo = appendEval(state, identifier)\n const { name, comment } = service.getTypeInfo(state.input, state.path, state.input.length)\n\n undo()\n\n if (name) repl.outputStream.write(`${name}\\n`)\n if (comment) repl.outputStream.write(`${comment}\\n`)\n repl.displayPrompt()\n }\n })\n\n // Set up REPL history when available natively via node.js >= 11.\n if (repl.setupHistory) {\n const historyPath = process.env.TS_NODE_HISTORY || join(homedir(), '.ts_node_repl_history')\n\n repl.setupHistory(historyPath, err => {\n if (!err) return\n\n replService.console.error(err)\n process.exit(1)\n })\n }\n}\n\n/**\n * Append to the eval instance and return an undo function.\n */\nfunction appendEval (state: EvalState, input: string) {\n const undoInput = state.input\n const undoVersion = state.version\n const undoOutput = state.output\n const undoLines = state.lines\n\n // Handle ASI issues with TypeScript re-evaluation.\n if (undoInput.charAt(undoInput.length - 1) === '\\n' && /^\\s*[\\/\\[(`-]/.test(input) && !/;\\s*$/.test(undoInput)) {\n state.input = `${state.input.slice(0, -1)};\\n`\n }\n\n state.input += input\n state.lines += lineCount(input)\n state.version++\n\n return function () {\n state.input = undoInput\n state.output = undoOutput\n state.version = undoVersion\n state.lines = undoLines\n }\n}\n\n/**\n * Count the number of lines.\n */\nfunction lineCount (value: string) {\n let count = 0\n\n for (const char of value) {\n if (char === '\\n') {\n count++\n }\n }\n\n return count\n}\n\nconst RECOVERY_CODES: Set = new Set([\n 1003, // \"Identifier expected.\"\n 1005, // \"')' expected.\"\n 1109, // \"Expression expected.\"\n 1126, // \"Unexpected end of text.\"\n 1160, // \"Unterminated template literal.\"\n 1161, // \"Unterminated regular expression literal.\"\n 2355 // \"A function whose declared type is neither 'void' nor 'any' must return a value.\"\n])\n\n/**\n * Check if a function can recover gracefully.\n */\nfunction isRecoverable (error: TSError) {\n return error.diagnosticCodes.every(code => RECOVERY_CODES.has(code))\n}\n"]}