Skip to content

Commit

Permalink
Improve optional chaining in JavaScript lexer (#1594)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jneen authored Oct 13, 2020
1 parent 9c8c092 commit 5337d8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rouge/lexers/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/visual/samples/javascript
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ function* range(from, to)
1 + 2
}`;

var notATernary = `${obj?.prop}`;

@(function(thing) { return thing; })
class Person {
@deprecate
Expand Down

0 comments on commit 5337d8d

Please sign in to comment.