[gtksourceview: 2/5] javascript.lang: Support optional chaining



commit 0132084b0504b8e2b8bd2b24b12184adf4121fc5
Author: Jeffery To <jeffery to gmail com>
Date:   Sat Jan 18 01:18:27 2020 +0800

    javascript.lang: Support optional chaining
    
    The proposal reached stage 4 in December 2019, will be in ES2020.
    
    This also removes TypeScript-specific handling for optional chaining.
    
    Reference:
    https://github.com/tc39/proposal-optional-chaining

 data/language-specs/javascript-expressions.lang    | 25 ++++++++++++++++++++++
 data/language-specs/javascript.lang                |  1 +
 data/language-specs/typescript-js-expressions.lang | 22 -------------------
 data/language-specs/typescript.lang                |  1 -
 tests/syntax-highlighting/file.j                   | 11 ++++++++++
 tests/syntax-highlighting/file.js                  | 11 ++++++++++
 tests/syntax-highlighting/file.jsx                 | 11 ++++++++++
 tests/syntax-highlighting/file.ts                  | 17 +++++++++------
 tests/syntax-highlighting/file.tsx                 | 17 +++++++++------
 9 files changed, 81 insertions(+), 35 deletions(-)
---
diff --git a/data/language-specs/javascript-expressions.lang b/data/language-specs/javascript-expressions.lang
index c34fea33..95026a7b 100644
--- a/data/language-specs/javascript-expressions.lang
+++ b/data/language-specs/javascript-expressions.lang
@@ -283,6 +283,9 @@
          obj['property']
          fn()
          tag`template`
+         obj?.property
+         obj?.[expr]
+         func?.()
     -->
 
     <context id="_post-primary-expression">
@@ -324,6 +327,28 @@
           </include>
         </context> <!-- /_bracket-property-accessors -->
 
+        <!-- ES2020 -->
+        <!-- <OptionalChain> (part of) -->
+        <context id="_optional-chain">
+          <start>\?\.(?![0-9])</start>
+          <end>\%{js:before-next-token}</end>
+          <include>
+            <context ref="js:embedded-lang-hooks"/>
+            <context ref="js:comments"/>
+
+            <context id="_optional-chain-content">
+              <include>
+                <!-- only match dot-property accessor content here
+                     this context will end early for other cases
+                -->
+                <context ref="js-val:properties-methods"/>
+                <context ref="js:identifier"/>
+              </include>
+            </context> <!-- /_optional-chain-content -->
+
+          </include>
+        </context> <!-- /_optional-chain -->
+
         <context ref="_function-arguments-lists"/>
         <context ref="js-lit:template-literals"/>
       </include>
diff --git a/data/language-specs/javascript.lang b/data/language-specs/javascript.lang
index 42ec61c8..4ae8ab11 100644
--- a/data/language-specs/javascript.lang
+++ b/data/language-specs/javascript.lang
@@ -139,6 +139,7 @@
          * BigInt
          * Promise.allSettled
          * globalThis
+         * Optional Chaining
 
          Features from Annex B of the spec are not highlighted to
          discourage their use, except:
diff --git a/data/language-specs/typescript-js-expressions.lang 
b/data/language-specs/typescript-js-expressions.lang
index 59007417..3e7416bc 100644
--- a/data/language-specs/typescript-js-expressions.lang
+++ b/data/language-specs/typescript-js-expressions.lang
@@ -138,27 +138,6 @@
     </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 -->
@@ -166,7 +145,6 @@
       <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 fc19676e..c5568153 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -76,7 +76,6 @@
 
     <!-- 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.j b/tests/syntax-highlighting/file.j
index 62f01920..4591ce33 100644
--- a/tests/syntax-highlighting/file.j
+++ b/tests/syntax-highlighting/file.j
@@ -811,6 +811,17 @@ fn(x, y,);
 myTag`That ${ person } is ${ age }`;
 
 
+/*
+ * Optional chaining (ES2020)
+ */
+
+obj?.prototype;
+obj?.['constructor'];
+func?.(1, 2, ...args);
+
+foo?.3:0; // ternary operator, not optional chaining
+
+
 /*
  * Statements and declarations
  */
diff --git a/tests/syntax-highlighting/file.js b/tests/syntax-highlighting/file.js
index ac0e1f3b..fa7e0680 100644
--- a/tests/syntax-highlighting/file.js
+++ b/tests/syntax-highlighting/file.js
@@ -510,6 +510,17 @@ fn(x, y,);
 myTag`That ${ person } is ${ age }`;
 
 
+/*
+ * Optional chaining (ES2020)
+ */
+
+obj?.prototype;
+obj?.['constructor'];
+func?.(1, 2, ...args);
+
+foo?.3:0; // ternary operator, not optional chaining
+
+
 /*
  * Statements and declarations
  */
diff --git a/tests/syntax-highlighting/file.jsx b/tests/syntax-highlighting/file.jsx
index d7f18777..cfb23f1d 100644
--- a/tests/syntax-highlighting/file.jsx
+++ b/tests/syntax-highlighting/file.jsx
@@ -567,6 +567,17 @@ fn(x, y,);
 myTag`That ${ person } is ${ age }`;
 
 
+/*
+ * Optional chaining (ES2020)
+ */
+
+obj?.prototype;
+obj?.['constructor'];
+func?.(1, 2, ...args);
+
+foo?.3:0; // ternary operator, not optional chaining
+
+
 /*
  * Statements and declarations
  */
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index f51d1190..f902fa14 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -495,12 +495,6 @@ 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
-
 // Nullish coalescing (stage 3 proposal)
 ( obj ?? 'default value' );
 a = foo ?? 1, bar ?? 2;
@@ -1114,6 +1108,17 @@ fn(x, y,);
 myTag`That ${ person } is ${ age }`;
 
 
+/*
+ * Optional chaining (ES2020)
+ */
+
+obj?.prototype;
+obj?.['constructor'];
+func?.(1, 2, ...args);
+
+foo?.3:0; // ternary operator, not optional chaining
+
+
 /*
  * Statements and declarations
  */
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index c713e5cd..cd515d92 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -601,12 +601,6 @@ 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
-
 // Nullish coalescing (stage 3 proposal)
 ( obj ?? 'default value' );
 a = foo ?? 1, bar ?? 2;
@@ -1222,6 +1216,17 @@ fn(x, y,);
 myTag`That ${ person } is ${ age }`;
 
 
+/*
+ * Optional chaining (ES2020)
+ */
+
+obj?.prototype;
+obj?.['constructor'];
+func?.(1, 2, ...args);
+
+foo?.3:0; // ternary operator, not optional chaining
+
+
 /*
  * Statements and declarations
  */


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