From f74ff7be546c5f2057889b273889df4f793cac2f Mon Sep 17 00:00:00 2001 From: hemanth Date: Sun, 19 Jul 2026 15:31:01 -0700 Subject: [PATCH] repl: add inline syntax highlighting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the Acorn tokenizer to colorize JavaScript input in real-time. Keywords are magenta, strings green, numbers yellow, regexps red, and comments gray. Highlighting is display-only — repl.line stays plain text. Can be disabled via `syntaxHighlighting: false` in repl.start() or TERM=dumb. Refs: https://github.com/nodejs/node/issues/48164 Signed-off-by: hemanth --- doc/api/repl.md | 29 +- lib/internal/repl/utils.js | 92 ++++++ lib/repl.js | 6 + .../parallel/test-repl-syntax-highlighting.js | 292 ++++++++++++++++++ 4 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-syntax-highlighting.js diff --git a/doc/api/repl.md b/doc/api/repl.md index c384ebf1391303..9f54bbc32052ed 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -27,7 +27,8 @@ result. Input and output may be from `stdin` and `stdout`, respectively, or may be connected to any Node.js [stream][]. Instances of [`repl.REPLServer`][] support automatic completion of inputs, -completion preview, simplistic Emacs-style line editing, multi-line inputs, +completion preview, inline syntax highlighting, simplistic Emacs-style line +editing, multi-line inputs, [ZSH][]-like reverse-i-search, [ZSH][]-like substring-based history search, ANSI-styled output, saving and restoring current REPL session state, error recovery, and customizable evaluation functions. Terminals that do not support @@ -232,6 +233,27 @@ undefined undefined ``` +### Syntax highlighting + + + +When `useColors` is `true`, the REPL highlights +JavaScript input using the following color scheme: + +* Keywords (`const`, `let`, `function`, `if`, `return`, etc.): Magenta +* Strings (single-quoted, double-quoted, and template literals): Green +* Numbers: Yellow +* Boolean literals (`true`, `false`), `null`, `undefined`, `NaN`, + `Infinity`: Yellow +* Regular expressions: Red +* Comments (line and block): Gray + +Syntax highlighting is applied only to the display; the internal `line` +property remains plain text. Highlighting can be disabled by passing +`syntaxHighlighting: false` to [`repl.start()`][]. + ### Reverse-i-search