You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repl.d.ts 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. declare module "repl" {
  2. import { Interface, Completer, AsyncCompleter } from "readline";
  3. import { Context } from "vm";
  4. import { InspectOptions } from "util";
  5. interface ReplOptions {
  6. /**
  7. * The input prompt to display.
  8. * Default: `"> "`
  9. */
  10. prompt?: string;
  11. /**
  12. * The `Readable` stream from which REPL input will be read.
  13. * Default: `process.stdin`
  14. */
  15. input?: NodeJS.ReadableStream;
  16. /**
  17. * The `Writable` stream to which REPL output will be written.
  18. * Default: `process.stdout`
  19. */
  20. output?: NodeJS.WritableStream;
  21. /**
  22. * If `true`, specifies that the output should be treated as a TTY terminal, and have
  23. * ANSI/VT100 escape codes written to it.
  24. * Default: checking the value of the `isTTY` property on the output stream upon
  25. * instantiation.
  26. */
  27. terminal?: boolean;
  28. /**
  29. * The function to be used when evaluating each given line of input.
  30. * Default: an async wrapper for the JavaScript `eval()` function. An `eval` function can
  31. * error with `repl.Recoverable` to indicate the input was incomplete and prompt for
  32. * additional lines.
  33. *
  34. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_default_evaluation
  35. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_custom_evaluation_functions
  36. */
  37. eval?: REPLEval;
  38. /**
  39. * If `true`, specifies that the default `writer` function should include ANSI color
  40. * styling to REPL output. If a custom `writer` function is provided then this has no
  41. * effect.
  42. * Default: the REPL instance's `terminal` value.
  43. */
  44. useColors?: boolean;
  45. /**
  46. * If `true`, specifies that the default evaluation function will use the JavaScript
  47. * `global` as the context as opposed to creating a new separate context for the REPL
  48. * instance. The node CLI REPL sets this value to `true`.
  49. * Default: `false`.
  50. */
  51. useGlobal?: boolean;
  52. /**
  53. * If `true`, specifies that the default writer will not output the return value of a
  54. * command if it evaluates to `undefined`.
  55. * Default: `false`.
  56. */
  57. ignoreUndefined?: boolean;
  58. /**
  59. * The function to invoke to format the output of each command before writing to `output`.
  60. * Default: a wrapper for `util.inspect`.
  61. *
  62. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_customizing_repl_output
  63. */
  64. writer?: REPLWriter;
  65. /**
  66. * An optional function used for custom Tab auto completion.
  67. *
  68. * @see https://nodejs.org/dist/latest-v11.x/docs/api/readline.html#readline_use_of_the_completer_function
  69. */
  70. completer?: Completer | AsyncCompleter;
  71. /**
  72. * A flag that specifies whether the default evaluator executes all JavaScript commands in
  73. * strict mode or default (sloppy) mode.
  74. * Accepted values are:
  75. * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
  76. * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
  77. * prefacing every repl statement with `'use strict'`.
  78. */
  79. replMode?: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
  80. /**
  81. * Stop evaluating the current piece of code when `SIGINT` is received, i.e. `Ctrl+C` is
  82. * pressed. This cannot be used together with a custom `eval` function.
  83. * Default: `false`.
  84. */
  85. breakEvalOnSigint?: boolean;
  86. }
  87. type REPLEval = (this: REPLServer, evalCmd: string, context: Context, file: string, cb: (err: Error | null, result: any) => void) => void;
  88. type REPLWriter = (this: REPLServer, obj: any) => string;
  89. /**
  90. * This is the default "writer" value, if none is passed in the REPL options,
  91. * and it can be overridden by custom print functions.
  92. */
  93. const writer: REPLWriter & { options: InspectOptions };
  94. type REPLCommandAction = (this: REPLServer, text: string) => void;
  95. interface REPLCommand {
  96. /**
  97. * Help text to be displayed when `.help` is entered.
  98. */
  99. help?: string;
  100. /**
  101. * The function to execute, optionally accepting a single string argument.
  102. */
  103. action: REPLCommandAction;
  104. }
  105. /**
  106. * Provides a customizable Read-Eval-Print-Loop (REPL).
  107. *
  108. * Instances of `repl.REPLServer` will accept individual lines of user input, evaluate those
  109. * according to a user-defined evaluation function, then output the result. Input and output
  110. * may be from `stdin` and `stdout`, respectively, or may be connected to any Node.js `stream`.
  111. *
  112. * Instances of `repl.REPLServer` support automatic completion of inputs, simplistic Emacs-style
  113. * line editing, multi-line inputs, ANSI-styled output, saving and restoring current REPL session
  114. * state, error recovery, and customizable evaluation functions.
  115. *
  116. * Instances of `repl.REPLServer` are created using the `repl.start()` method and _should not_
  117. * be created directly using the JavaScript `new` keyword.
  118. *
  119. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_repl
  120. */
  121. class REPLServer extends Interface {
  122. /**
  123. * The `vm.Context` provided to the `eval` function to be used for JavaScript
  124. * evaluation.
  125. */
  126. readonly context: Context;
  127. /**
  128. * The `Readable` stream from which REPL input will be read.
  129. */
  130. readonly inputStream: NodeJS.ReadableStream;
  131. /**
  132. * The `Writable` stream to which REPL output will be written.
  133. */
  134. readonly outputStream: NodeJS.WritableStream;
  135. /**
  136. * The commands registered via `replServer.defineCommand()`.
  137. */
  138. readonly commands: { readonly [name: string]: REPLCommand | undefined };
  139. /**
  140. * A value indicating whether the REPL is currently in "editor mode".
  141. *
  142. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_commands_and_special_keys
  143. */
  144. readonly editorMode: boolean;
  145. /**
  146. * A value indicating whether the `_` variable has been assigned.
  147. *
  148. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
  149. */
  150. readonly underscoreAssigned: boolean;
  151. /**
  152. * The last evaluation result from the REPL (assigned to the `_` variable inside of the REPL).
  153. *
  154. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
  155. */
  156. readonly last: any;
  157. /**
  158. * A value indicating whether the `_error` variable has been assigned.
  159. *
  160. * @since v9.8.0
  161. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
  162. */
  163. readonly underscoreErrAssigned: boolean;
  164. /**
  165. * The last error raised inside the REPL (assigned to the `_error` variable inside of the REPL).
  166. *
  167. * @since v9.8.0
  168. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
  169. */
  170. readonly lastError: any;
  171. /**
  172. * Specified in the REPL options, this is the function to be used when evaluating each
  173. * given line of input. If not specified in the REPL options, this is an async wrapper
  174. * for the JavaScript `eval()` function.
  175. */
  176. readonly eval: REPLEval;
  177. /**
  178. * Specified in the REPL options, this is a value indicating whether the default
  179. * `writer` function should include ANSI color styling to REPL output.
  180. */
  181. readonly useColors: boolean;
  182. /**
  183. * Specified in the REPL options, this is a value indicating whether the default `eval`
  184. * function will use the JavaScript `global` as the context as opposed to creating a new
  185. * separate context for the REPL instance.
  186. */
  187. readonly useGlobal: boolean;
  188. /**
  189. * Specified in the REPL options, this is a value indicating whether the default `writer`
  190. * function should output the result of a command if it evaluates to `undefined`.
  191. */
  192. readonly ignoreUndefined: boolean;
  193. /**
  194. * Specified in the REPL options, this is the function to invoke to format the output of
  195. * each command before writing to `outputStream`. If not specified in the REPL options,
  196. * this will be a wrapper for `util.inspect`.
  197. */
  198. readonly writer: REPLWriter;
  199. /**
  200. * Specified in the REPL options, this is the function to use for custom Tab auto-completion.
  201. */
  202. readonly completer: Completer | AsyncCompleter;
  203. /**
  204. * Specified in the REPL options, this is a flag that specifies whether the default `eval`
  205. * function should execute all JavaScript commands in strict mode or default (sloppy) mode.
  206. * Possible values are:
  207. * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
  208. * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
  209. * prefacing every repl statement with `'use strict'`.
  210. */
  211. readonly replMode: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
  212. /**
  213. * NOTE: According to the documentation:
  214. *
  215. * > Instances of `repl.REPLServer` are created using the `repl.start()` method and
  216. * > _should not_ be created directly using the JavaScript `new` keyword.
  217. *
  218. * `REPLServer` cannot be subclassed due to implementation specifics in NodeJS.
  219. *
  220. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_class_replserver
  221. */
  222. private constructor();
  223. /**
  224. * Used to add new `.`-prefixed commands to the REPL instance. Such commands are invoked
  225. * by typing a `.` followed by the `keyword`.
  226. *
  227. * @param keyword The command keyword (_without_ a leading `.` character).
  228. * @param cmd The function to invoke when the command is processed.
  229. *
  230. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_replserver_definecommand_keyword_cmd
  231. */
  232. defineCommand(keyword: string, cmd: REPLCommandAction | REPLCommand): void;
  233. /**
  234. * Readies the REPL instance for input from the user, printing the configured `prompt` to a
  235. * new line in the `output` and resuming the `input` to accept new input.
  236. *
  237. * When multi-line input is being entered, an ellipsis is printed rather than the 'prompt'.
  238. *
  239. * This method is primarily intended to be called from within the action function for
  240. * commands registered using the `replServer.defineCommand()` method.
  241. *
  242. * @param preserveCursor When `true`, the cursor placement will not be reset to `0`.
  243. */
  244. displayPrompt(preserveCursor?: boolean): void;
  245. /**
  246. * Clears any command that has been buffered but not yet executed.
  247. *
  248. * This method is primarily intended to be called from within the action function for
  249. * commands registered using the `replServer.defineCommand()` method.
  250. *
  251. * @since v9.0.0
  252. */
  253. clearBufferedCommand(): void;
  254. /**
  255. * Initializes a history log file for the REPL instance. When executing the
  256. * Node.js binary and using the command line REPL, a history file is initialized
  257. * by default. However, this is not the case when creating a REPL
  258. * programmatically. Use this method to initialize a history log file when working
  259. * with REPL instances programmatically.
  260. * @param path The path to the history file
  261. */
  262. setupHistory(path: string, cb: (err: Error | null, repl: this) => void): void;
  263. /**
  264. * events.EventEmitter
  265. * 1. close - inherited from `readline.Interface`
  266. * 2. line - inherited from `readline.Interface`
  267. * 3. pause - inherited from `readline.Interface`
  268. * 4. resume - inherited from `readline.Interface`
  269. * 5. SIGCONT - inherited from `readline.Interface`
  270. * 6. SIGINT - inherited from `readline.Interface`
  271. * 7. SIGTSTP - inherited from `readline.Interface`
  272. * 8. exit
  273. * 9. reset
  274. */
  275. addListener(event: string, listener: (...args: any[]) => void): this;
  276. addListener(event: "close", listener: () => void): this;
  277. addListener(event: "line", listener: (input: string) => void): this;
  278. addListener(event: "pause", listener: () => void): this;
  279. addListener(event: "resume", listener: () => void): this;
  280. addListener(event: "SIGCONT", listener: () => void): this;
  281. addListener(event: "SIGINT", listener: () => void): this;
  282. addListener(event: "SIGTSTP", listener: () => void): this;
  283. addListener(event: "exit", listener: () => void): this;
  284. addListener(event: "reset", listener: (context: Context) => void): this;
  285. emit(event: string | symbol, ...args: any[]): boolean;
  286. emit(event: "close"): boolean;
  287. emit(event: "line", input: string): boolean;
  288. emit(event: "pause"): boolean;
  289. emit(event: "resume"): boolean;
  290. emit(event: "SIGCONT"): boolean;
  291. emit(event: "SIGINT"): boolean;
  292. emit(event: "SIGTSTP"): boolean;
  293. emit(event: "exit"): boolean;
  294. emit(event: "reset", context: Context): boolean;
  295. on(event: string, listener: (...args: any[]) => void): this;
  296. on(event: "close", listener: () => void): this;
  297. on(event: "line", listener: (input: string) => void): this;
  298. on(event: "pause", listener: () => void): this;
  299. on(event: "resume", listener: () => void): this;
  300. on(event: "SIGCONT", listener: () => void): this;
  301. on(event: "SIGINT", listener: () => void): this;
  302. on(event: "SIGTSTP", listener: () => void): this;
  303. on(event: "exit", listener: () => void): this;
  304. on(event: "reset", listener: (context: Context) => void): this;
  305. once(event: string, listener: (...args: any[]) => void): this;
  306. once(event: "close", listener: () => void): this;
  307. once(event: "line", listener: (input: string) => void): this;
  308. once(event: "pause", listener: () => void): this;
  309. once(event: "resume", listener: () => void): this;
  310. once(event: "SIGCONT", listener: () => void): this;
  311. once(event: "SIGINT", listener: () => void): this;
  312. once(event: "SIGTSTP", listener: () => void): this;
  313. once(event: "exit", listener: () => void): this;
  314. once(event: "reset", listener: (context: Context) => void): this;
  315. prependListener(event: string, listener: (...args: any[]) => void): this;
  316. prependListener(event: "close", listener: () => void): this;
  317. prependListener(event: "line", listener: (input: string) => void): this;
  318. prependListener(event: "pause", listener: () => void): this;
  319. prependListener(event: "resume", listener: () => void): this;
  320. prependListener(event: "SIGCONT", listener: () => void): this;
  321. prependListener(event: "SIGINT", listener: () => void): this;
  322. prependListener(event: "SIGTSTP", listener: () => void): this;
  323. prependListener(event: "exit", listener: () => void): this;
  324. prependListener(event: "reset", listener: (context: Context) => void): this;
  325. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  326. prependOnceListener(event: "close", listener: () => void): this;
  327. prependOnceListener(event: "line", listener: (input: string) => void): this;
  328. prependOnceListener(event: "pause", listener: () => void): this;
  329. prependOnceListener(event: "resume", listener: () => void): this;
  330. prependOnceListener(event: "SIGCONT", listener: () => void): this;
  331. prependOnceListener(event: "SIGINT", listener: () => void): this;
  332. prependOnceListener(event: "SIGTSTP", listener: () => void): this;
  333. prependOnceListener(event: "exit", listener: () => void): this;
  334. prependOnceListener(event: "reset", listener: (context: Context) => void): this;
  335. }
  336. /**
  337. * A flag passed in the REPL options. Evaluates expressions in sloppy mode.
  338. */
  339. export const REPL_MODE_SLOPPY: symbol; // TODO: unique symbol
  340. /**
  341. * A flag passed in the REPL options. Evaluates expressions in strict mode.
  342. * This is equivalent to prefacing every repl statement with `'use strict'`.
  343. */
  344. export const REPL_MODE_STRICT: symbol; // TODO: unique symbol
  345. /**
  346. * Creates and starts a `repl.REPLServer` instance.
  347. *
  348. * @param options The options for the `REPLServer`. If `options` is a string, then it specifies
  349. * the input prompt.
  350. */
  351. function start(options?: string | ReplOptions): REPLServer;
  352. /**
  353. * Indicates a recoverable error that a `REPLServer` can use to support multi-line input.
  354. *
  355. * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_recoverable_errors
  356. */
  357. class Recoverable extends SyntaxError {
  358. err: Error;
  359. constructor(err: Error);
  360. }
  361. }