Skip to content

Commit

Permalink
Support nullish coalescing operator in TypeScript lexer (#1529)
Browse files Browse the repository at this point in the history
TypeScript has added support for the nullish coalescing operator `??` as
of version 3.7. This commit adds support by prepending a rule to the
`:root` state of lexers that extend the TypeScript common module.
When JavaScript adopts the operator, the rule should be moved into the
`:root` state of the JavaScript lexer and removed from the TypeScript
common module.
  • Loading branch information
pyrmont authored Jun 2, 2020
1 parent 30fc93f commit ce26fd9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rouge/lexers/typescript/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def builtins
def self.extended(base)
base.prepend :root do
rule %r/[?][.]/, base::Punctuation
rule %r/[?]{2}/, base::Operator
end

base.prepend :statement do
Expand Down
4 changes: 4 additions & 0 deletions spec/visual/samples/typescript
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ declare module "foo" {
const x = `Hello world ${a.b}`;
const y = `Hello world ${a?.b}`;
const z = `Hello world`;

// Nullish coalescing in template strings
const content = `Value is ${value ?? 'not set'}`;
const description = "Highlighting is not broken now";

0 comments on commit ce26fd9

Please sign in to comment.