From b2848b37332fe7214ecd9c8177eda8eec8f21065 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Sat, 4 Apr 2020 05:29:11 +0900 Subject: [PATCH] Add support for optional chaining operator to TypeScript lexer (#1475) TypeScript has added support for the optional chaining operator `?.` as of version 3.7. This commit adds support by prepending a rule to the `:root` state of the TypeScript lexer. Eventually, when JavaScript adopts the operator, the rule should be moved into the `:root` state there and removed from this lexer. --- lib/rouge/lexers/typescript.rb | 4 ++++ spec/visual/samples/typescript | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/rouge/lexers/typescript.rb b/lib/rouge/lexers/typescript.rb index 52f95e7695..5a4283fd59 100644 --- a/lib/rouge/lexers/typescript.rb +++ b/lib/rouge/lexers/typescript.rb @@ -19,6 +19,10 @@ class Typescript < Javascript mimetypes 'text/typescript' + prepend :root do + rule %r/[?][.]/, Punctuation + end + prepend :statement do rule %r/(#{Javascript.id_regex})(\??)(\s*)(:)/ do groups Name::Label, Punctuation, Text, Punctuation diff --git a/spec/visual/samples/typescript b/spec/visual/samples/typescript index 7379879409..be3897b743 100644 --- a/spec/visual/samples/typescript +++ b/spec/visual/samples/typescript @@ -198,3 +198,8 @@ declare module "foo" { [K in keyof IFoo]?: IFoo[K] } } + +// Template strings +const x = `Hello world ${a.b}`; +const y = `Hello world ${a?.b}`; +const z = `Hello world`;