Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Add support for optional chaining operator to TypeScript lexer (rouge…
Browse files Browse the repository at this point in the history
…-ruby#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.
  • Loading branch information
pyrmont authored and mattt committed May 21, 2020
1 parent bf871c6 commit b2848b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rouge/lexers/typescript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/visual/samples/typescript
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

0 comments on commit b2848b3

Please sign in to comment.