ts-node
ts-node copied to clipboard
Add syntax highlighting to REPL input
Desired Behavior
ts-node currently supports output highlighting, but not input:
Additional context
The next REPL of nodejs - node-prototype-repl - already supports syntax highlighting for js. It uses emphasize. If we change the following line:
https://github.com/nodejs/repl/blob/db95cfb1a177b056b8bc257dfd62c7b0e50134df/src/highlight.js#L44 to:
emphasize.highlight('ts', s, sheet).value;
We can get typescript syntax highlighting for node REPL:
For additional details see https://github.com/TypeStrong/ts-node/issues/1144.
Labelled "help wanted" to indicate we will wait for a pull request.
@cspotcode @sinapis I can help. I will create a PR with the suggested change
@sinapis what is needed to be done for this to be implemented?
I stumbled upon some TS compiler API functionality that's relevant here:
const classifier = ts.createClassifier();
const classifications = classifier.getEncodedLexicalClassifications('const a = 123;', ts.EndOfLineState.None);
for(let i = 0; i < classifications.spans.length; i += 3) {
const [start, length, classification] = classifications.spans.slice(i, i + 3);
console.log(start, length, ts.ClassificationType[classification]);
}
0 5 keyword
6 1 identifier
8 1 operator
10 3 numericLiteral
13 1 punctuation