From 5337d8da14872ff694cc2732d83e9a27ed7236e8 Mon Sep 17 00:00:00 2001 From: Jeanine Adkisson Date: Tue, 13 Oct 2020 14:20:52 -0400 Subject: [PATCH] Improve optional chaining in JavaScript lexer (#1594) Previously, the example `${obj?.prop}` would result in a runaway string literal, since the `?` would start a ternary expression, and Rouge would lose the stack state causing the second `` ` `` to terminate the literal. This commit fixes that issue. --- lib/rouge/lexers/javascript.rb | 4 ++++ spec/visual/samples/javascript | 2 ++ 2 files changed, 6 insertions(+) diff --git a/lib/rouge/lexers/javascript.rb b/lib/rouge/lexers/javascript.rb index bc45af6c28..9ac323aba9 100644 --- a/lib/rouge/lexers/javascript.rb +++ b/lib/rouge/lexers/javascript.rb @@ -162,6 +162,10 @@ def self.id_regex push :template_string end + # special case for the safe navigation operator ?. + # so that we don't start detecting a ternary expr + rule %r/[?][.]/, Punctuation + rule %r/[?]/ do token Punctuation push :ternary diff --git a/spec/visual/samples/javascript b/spec/visual/samples/javascript index ccdb5e3776..e6396ed0b3 100644 --- a/spec/visual/samples/javascript +++ b/spec/visual/samples/javascript @@ -229,6 +229,8 @@ function* range(from, to) 1 + 2 }`; +var notATernary = `${obj?.prop}`; + @(function(thing) { return thing; }) class Person { @deprecate