[gtksourceview: 1/8] typescript.lang: Add support for optional chaining



commit 713b88a775a4fbba1ab543253630344662a1f61a
Author: Jeffery To <jeffery to gmail com>
Date:   Fri Nov 15 03:22:19 2019 +0800

    typescript.lang: Add support for optional chaining
    
    Added in TypeScript 3.7.
    
    Based on the stage 3 proposal:
    https://github.com/tc39/proposal-optional-chaining/

 data/language-specs/typescript-js-expressions.lang | 22 ++++++++++++++++++++++
 data/language-specs/typescript.lang                |  1 +
 tests/syntax-highlighting/file.ts                  |  6 ++++++
 tests/syntax-highlighting/file.tsx                 |  6 ++++++
 4 files changed, 35 insertions(+)
---
diff --git a/data/language-specs/typescript-js-expressions.lang 
b/data/language-specs/typescript-js-expressions.lang
index 7c6cbd0e..c4a629bd 100644
--- a/data/language-specs/typescript-js-expressions.lang
+++ b/data/language-specs/typescript-js-expressions.lang
@@ -137,6 +137,27 @@
     </context> <!-- /_function-call-type-arguments-lists -->
 
 
+    <!-- # Optional chaining: stage 3 proposal
+
+         obj?.prop
+         obj?.[expr]
+         func?.(...args)
+    -->
+
+    <!-- we only add the dot-property accessor case
+         other cases will still be matched (the context will end early)
+    -->
+    <context id="_optional-dot-property-accessors">
+      <start>\?\.(?![0-9])</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="typescript:optional-chaining-operator"/>
+        <context ref="js:comments"/>
+        <context ref="js-expr:_dot-property-accessor-content"/>
+      </include>
+    </context> <!-- /_optional-dot-property-accessors -->
+
+
     <!-- # Post-primary expression -->
 
     <!-- replaces js-expr:_post-primary-expression -->
@@ -144,6 +165,7 @@
       <include>
         <context ref="_non-null-assertion-operators"/>
         <context ref="_function-call-type-arguments-lists"/>
+        <context ref="_optional-dot-property-accessors"/>
         <context ref="js-expr:_post-primary-expression" original="true"/>
       </include>
     </context> <!-- /post-primary-expression -->
diff --git a/data/language-specs/typescript.lang b/data/language-specs/typescript.lang
index 3b958e74..37434333 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -76,6 +76,7 @@
 
     <!-- JavaScript expressions -->
     <style id="non-null-assertion-operator"                   name="Non-null assertion operator"/>
+    <style id="optional-chaining-operator"                    name="Optional chaining operator"/>
 
     <!-- JavaScript statements -->
     <style id="global-augmentation"                           name="Global augmentation"/>
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index f805a009..1fbe09cd 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -471,6 +471,12 @@ a = import . /* comment
 a = import // comment
 .meta.__dirname; // incorrectly highlighted
 
+// Optional chaining (stage 3 proposal)
+obj?.prop;
+obj?.[expr];
+func?.(...args);
+foo?.3:0; // correctly highlighted as the ternary operator, not optional chaining
+
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index 31a09b6d..caabf38e 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -574,6 +574,12 @@ a = import . /* comment
 a = import // comment
 .meta.__dirname; // incorrectly highlighted
 
+// Optional chaining (stage 3 proposal)
+obj?.prop;
+obj?.[expr];
+func?.(...args);
+foo?.3:0; // correctly highlighted as the ternary operator, not optional chaining
+
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]