[gtksourceview: 3/14] javascript.lang: Add highlighting for import.meta



commit 566794d11a01ee83ef63d25e15a8504dd10605e8
Author: Jeffery To <jeffery to gmail com>
Date:   Tue Jun 2 20:03:59 2020 +0800

    javascript.lang: Add highlighting for import.meta
    
    This moves the highlighting for import.meta (added for ES2020) from
    typescript.lang into javascript.lang.
    
    This also fixes highlighting for dynamic imports (import()) that span
    multiple lines.

 data/language-specs/javascript-expressions.lang | 73 +++++++++++++++++++++++++
 data/language-specs/javascript-modules.lang     |  4 +-
 data/language-specs/javascript-values.lang      | 13 -----
 data/language-specs/javascript.lang             | 16 ------
 data/language-specs/typescript-js-modules.lang  | 38 +++++--------
 data/language-specs/typescript-js-values.lang   | 68 -----------------------
 data/language-specs/typescript.lang             | 15 -----
 tests/syntax-highlighting/file.j                | 31 +++++++----
 tests/syntax-highlighting/file.js               | 31 +++++++----
 tests/syntax-highlighting/file.jsx              | 31 +++++++----
 tests/syntax-highlighting/file.ts               | 43 +++++++--------
 tests/syntax-highlighting/file.tsx              | 43 +++++++--------
 12 files changed, 190 insertions(+), 216 deletions(-)
---
diff --git a/data/language-specs/javascript-expressions.lang b/data/language-specs/javascript-expressions.lang
index 13175eb1..772b2d61 100644
--- a/data/language-specs/javascript-expressions.lang
+++ b/data/language-specs/javascript-expressions.lang
@@ -51,6 +51,78 @@
     -->
 
 
+    <!-- # "import" expression
+
+         import("module")
+         import.meta
+    -->
+
+    <!-- a consequence of this is that "import" by itself (when used as
+         a primary expression) will not be highlighted as an error -->
+
+    <!-- ES2020 -->
+    <!-- <ImportCall> (part of) -->
+    <context id="_choice-import-call-expression" end-parent="true">
+      <start>\(</start>
+      <end>\)</end>
+      <include>
+        <context ref="js:embedded-lang-hooks"/>
+        <context ref="js:comments"/>
+
+        <context id="_import-call-expression-content">
+          <include>
+            <context ref="expression-without-comma"/>
+          </include>
+        </context> <!-- /_import-call-expression-content -->
+      </include>
+    </context> <!-- /_choice-import-call-expression -->
+
+    <!-- ES2020 -->
+    <!-- <ImportMeta> (part of) -->
+    <context id="_choice-import-meta-expression" end-parent="true">
+      <start>\.</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="js:keyword"/>
+        <context ref="js:embedded-lang-hooks"/>
+        <context ref="js:comments"/>
+
+        <context id="_import-meta-expression-content">
+          <include>
+
+            <context id="_import-meta-expression-keyword" style-ref="js:keyword" once-only="true">
+              <match extended="true">
+                \%{js:keyword-start} meta \%{js:keyword-end}
+              </match>
+            </context> <!-- /_import-meta-expression-keyword -->
+
+          </include>
+        </context> <!-- /_import-meta-expression-content -->
+
+      </include>
+    </context> <!-- /_choice-import-meta-expression -->
+
+    <context id="_choice-import-expression" end-parent="true">
+      <start extended="true">
+        \%{js:keyword-start} import \%{js:keyword-end}
+      </start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="js:keyword"/>
+        <context ref="js:embedded-lang-hooks"/>
+        <context ref="js:comments"/>
+
+        <context id="_import-expression-content">
+          <include>
+            <context ref="_choice-import-call-expression"/>
+            <context ref="_choice-import-meta-expression"/>
+          </include>
+        </context> <!-- /_import-expression-content -->
+
+      </include>
+    </context> <!-- /_choice-import-expression -->
+
+
     <!-- # "new" expression
 
          new Array()
@@ -203,6 +275,7 @@
             <context ref="js-lit:choice-template-literal"/>
             <context ref="js-fn:choice-function-expression"/>
             <context ref="js-fn:choice-class-expression"/>
+            <context ref="_choice-import-expression"/>
             <context ref="_choice-new-expression"/>
             <context ref="_choice-grouping"/>
             <context ref="js-lit:choice-number"/>
diff --git a/data/language-specs/javascript-modules.lang b/data/language-specs/javascript-modules.lang
index cac548fa..89c93cb9 100644
--- a/data/language-specs/javascript-modules.lang
+++ b/data/language-specs/javascript-modules.lang
@@ -404,10 +404,10 @@
     -->
 
     <!-- <ImportDeclaration> -->
+    <!-- also matches import() and import.meta -->
     <context id="import-declarations" style-ref="js:export-import-declaration">
       <start extended="true">
         \%{js:keyword-start} import \%{js:keyword-end}
-        (?! \%{js:import-function-keyword-suffix} )
       </start>
       <end>\%{js:before-next-token}</end>
       <include>
@@ -417,6 +417,8 @@
 
         <context id="_import-declaration-content">
           <include>
+            <context ref="js-expr:_choice-import-call-expression"/>
+            <context ref="js-expr:_choice-import-meta-expression"/>
             <context ref="_choice-import-named"/>
             <context ref="_choice-all-as-namespace"/>
             <context ref="_choice-import-default"/>
diff --git a/data/language-specs/javascript-values.lang b/data/language-specs/javascript-values.lang
index 8dd510f0..e5078860 100644
--- a/data/language-specs/javascript-values.lang
+++ b/data/language-specs/javascript-values.lang
@@ -130,19 +130,6 @@
           <keyword>this</keyword>
         </context> <!-- /_object-keywords -->
 
-        <context id="_import-function-keywords">
-          <start extended="true">
-            \%{js:keyword-start} import \%{js:keyword-end}
-            (?= \%{js:import-function-keyword-suffix} )
-          </start>
-          <end>(?=\()</end>
-          <include>
-            <context sub-pattern="0" where="start" style-ref="js:keyword"/>
-            <!-- no embedded-lang-hooks here -->
-            <context ref="js:comments"/>
-          </include>
-        </context> <!-- /_import-function-keywords -->
-
       </include>
     </context> <!-- /global-values -->
 
diff --git a/data/language-specs/javascript.lang b/data/language-specs/javascript.lang
index 089bba6a..c06522ff 100644
--- a/data/language-specs/javascript.lang
+++ b/data/language-specs/javascript.lang
@@ -287,10 +287,6 @@
           so we move the definitions of these regexes here
     -->
 
-    <define-regex id="import-function-keyword-suffix" extended="true">
-      \%{optional-whitespace-or-comments} \(
-    </define-regex> <!-- /import-function-keyword-suffix -->
-
     <!-- async function (ES2017)
          no line terminator allowed between "async" and "function" -->
     <define-regex id="function-expression-keyword" extended="true">
@@ -594,18 +590,6 @@
 
     <!-- ## Misc syntax -->
 
-    <context id="_keyword-dot" style-ref="keyword" once-only="true">
-      <match>\.</match>
-    </context> <!-- /_keyword-dot -->
-
-    <context id="ordered-keyword-dot" once-only="true">
-      <start>\%{before-next-token}</start>
-      <end>\%{before-next-token}</end>
-      <include>
-        <context ref="_keyword-dot"/>
-      </include>
-    </context> <!-- /ordered-keyword-dot -->
-
     <context id="_rest-syntax" style-ref="rest-syntax" once-only="true">
       <match>\.\.\.</match>
     </context> <!-- /_rest-syntax -->
diff --git a/data/language-specs/typescript-js-modules.lang b/data/language-specs/typescript-js-modules.lang
index 12163768..63a693dd 100644
--- a/data/language-specs/typescript-js-modules.lang
+++ b/data/language-specs/typescript-js-modules.lang
@@ -102,7 +102,7 @@
         <context ref="typescript-js-st:choice-ambient-declaration"/>
         <context ref="typescript-js-st:choice-const-declaration"/>
         <context ref="typescript-js-st:choice-enum-declaration"/>
-        <context ref="_choice-import-declaration"/> <!-- for import alias declaration -->
+        <context ref="_choice-import-default-declaration"/> <!-- for import alias declaration -->
         <context ref="typescript-js-st:choice-interface-declaration"/>
         <context ref="typescript-js-st:choice-namespace-declaration"/>
         <context ref="typescript-js-st:choice-type-alias-declaration"/>
@@ -199,35 +199,25 @@
       </include>
     </context> <!-- /import-default-content -->
 
-    <!-- ## Import declaration -->
-
-    <define-regex id="_import-declaration-keyword" extended="true">
-      \%{js:keyword-start} import \%{js:keyword-end}
-      (?! \%{js:import-function-keyword-suffix} )
-      (?! \%{typescript:import-meta-object-keyword-suffix} )
-    </define-regex>
-
-    <!-- <ImportDeclaration> -->
-    <context id="_choice-import-declaration" style-ref="js:export-import-declaration" end-parent="true">
-      <start>\%{_import-declaration-keyword}</start>
+    <!-- this is like js-mod:import-declarations but only matches import
+         default (for import alias) -->
+    <context id="_choice-import-default-declaration" style-ref="js:export-import-declaration" 
end-parent="true">
+      <start extended="true">
+        \%{js:keyword-start} import \%{js:keyword-end}
+      </start>
       <end>\%{js:before-next-token}</end>
       <include>
         <context sub-pattern="0" where="start" style-ref="js:keyword"/>
         <context ref="js:comments"/>
-        <context ref="js-mod:_import-declaration-content"/>
-      </include>
-    </context> <!-- /_choice-import-declaration -->
 
-    <!-- replaces js-mod:import-declarations -->
-    <context id="import-declarations" style-ref="js:export-import-declaration">
-      <start>\%{_import-declaration-keyword}</start>
-      <end>\%{js:before-next-token}</end>
-      <include>
-        <context sub-pattern="0" where="start" style-ref="js:keyword"/>
-        <context ref="js:comments"/>
-        <context ref="js-mod:_import-declaration-content"/>
+        <context id="_import-default-declaration-content">
+          <include>
+            <context ref="js-mod:_choice-import-default"/>
+          </include>
+        </context> <!-- /_import-default-declaration-content -->
+
       </include>
-    </context> <!-- /import-declarations -->
+    </context> <!-- /_choice-import-default-declaration -->
 
   </definitions>
 </language>
diff --git a/data/language-specs/typescript.lang b/data/language-specs/typescript.lang
index 6ca558b8..0adfa707 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -111,13 +111,6 @@
           so we move the definitions of these regexes here
     -->
 
-    <define-regex id="import-meta-object-keyword-suffix" extended="true">
-      \%{js:optional-whitespace-or-comments}
-      \.
-      \%{js:optional-whitespace-or-comments}
-      meta \%{js:keyword-end}
-    </define-regex> <!-- /import-meta-object-keyword-suffix -->
-
     <!-- no line terminator allowed between "abstract" and "class"
          technically, there can be abstract class declarations but not
          abstract class expressions -->
@@ -697,7 +690,6 @@
     <context id="unknown-context-error-workaround">
       <include>
         <context ref="typescript-js-lit:choice-number"/>
-        <context ref="typescript-js-val:global-values"/>
         <context ref="typescript-js-fn:function-parameters-content"/>
         <context ref="typescript-js-expr:grouping-item-content"/>
         <context ref="typescript-js-st:class-declarations"/>
@@ -733,11 +725,6 @@
     <replace id="js-lit:choice-number" ref="typescript-js-lit:choice-number"/>
 
 
-    <!-- ## Values -->
-
-    <replace id="js-val:global-values" ref="typescript-js-val:global-values"/>
-
-
     <!-- ## Functions and classes -->
 
     <!-- ### Function expression -->
@@ -812,8 +799,6 @@
 
     <replace id="js-mod:_import-default-content" ref="typescript-js-mod:import-default-content"/>
 
-    <replace id="js-mod:import-declarations" ref="typescript-js-mod:import-declarations"/>
-
 
     <!-- # Main context -->
 
diff --git a/tests/syntax-highlighting/file.j b/tests/syntax-highlighting/file.j
index d46990f7..e4bdf23d 100644
--- a/tests/syntax-highlighting/file.j
+++ b/tests/syntax-highlighting/file.j
@@ -529,23 +529,32 @@ globalThis; // ES2020
 super;
 this;
 
+// dynamic import (ES2020)
+import("module").then();
+import /* comment */ ("module").then();
+import // comment
+("module").then();
+a = await import("module");
+a = await import /* comment */ ("module");
+a = await import // comment
+("module");
+
+// import.meta (ES2020)
+import.meta;
+import . /* comment */ meta;
+import // comment
+.meta;
+a = import.meta;
+a = import . /* comment */ meta;
+a = import // comment
+.meta;
+
 // new.target
 new.target;
 new . /* comment */ target;
 new // comment
 .target;
 
-// function keywords
-import(); // ES2020
-import /* comment */ (); // ES2020
-// incorrectly highlighted (though it may appear correct)
-import
-();
-import /* comment
-*/ ();
-import // comment
-();
-
 // properties (subset)
 array.length;
 Math.PI;
diff --git a/tests/syntax-highlighting/file.js b/tests/syntax-highlighting/file.js
index 9a5dd5dc..4d43c5dc 100644
--- a/tests/syntax-highlighting/file.js
+++ b/tests/syntax-highlighting/file.js
@@ -228,23 +228,32 @@ globalThis; // ES2020
 super;
 this;
 
+// dynamic import (ES2020)
+import("module").then();
+import /* comment */ ("module").then();
+import // comment
+("module").then();
+a = await import("module");
+a = await import /* comment */ ("module");
+a = await import // comment
+("module");
+
+// import.meta (ES2020)
+import.meta;
+import . /* comment */ meta;
+import // comment
+.meta;
+a = import.meta;
+a = import . /* comment */ meta;
+a = import // comment
+.meta;
+
 // new.target
 new.target;
 new . /* comment */ target;
 new // comment
 .target;
 
-// function keywords
-import(); // ES2020
-import /* comment */ (); // ES2020
-// incorrectly highlighted (though it may appear correct)
-import
-();
-import /* comment
-*/ ();
-import // comment
-();
-
 // properties (subset)
 array.length;
 Math.PI;
diff --git a/tests/syntax-highlighting/file.jsx b/tests/syntax-highlighting/file.jsx
index d7c32319..9b43a6a2 100644
--- a/tests/syntax-highlighting/file.jsx
+++ b/tests/syntax-highlighting/file.jsx
@@ -285,23 +285,32 @@ globalThis; // ES2020
 super;
 this;
 
+// dynamic import (ES2020)
+import("module").then();
+import /* comment */ ("module").then();
+import // comment
+("module").then();
+a = await import("module");
+a = await import /* comment */ ("module");
+a = await import // comment
+("module");
+
+// import.meta (ES2020)
+import.meta;
+import . /* comment */ meta;
+import // comment
+.meta;
+a = import.meta;
+a = import . /* comment */ meta;
+a = import // comment
+.meta;
+
 // new.target
 new.target;
 new . /* comment */ target;
 new // comment
 .target;
 
-// function keywords
-import(); // ES2020
-import /* comment */ (); // ES2020
-// incorrectly highlighted (though it may appear correct)
-import
-();
-import /* comment
-*/ ();
-import // comment
-();
-
 // properties (subset)
 array.length;
 Math.PI;
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index b80e7d57..81c0fe1d 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -483,18 +483,6 @@ a = class {
 
 /* Expression */
 
-// Dynamic import expression (ES2020)
-a = import('module');
-import("module").then(module => {});
-
-// import.meta (stage 3 proposal)
-a = import.meta.__dirname;
-a = import . /* comment */ meta.__dirname;
-a = import . /* comment
-*/ meta.__dirname; // incorrectly highlighted
-a = import // comment
-.meta.__dirname; // incorrectly highlighted
-
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();
@@ -825,23 +813,32 @@ globalThis; // ES2020
 super;
 this;
 
+// dynamic import (ES2020)
+import("module").then();
+import /* comment */ ("module").then();
+import // comment
+("module").then();
+a = await import("module");
+a = await import /* comment */ ("module");
+a = await import // comment
+("module");
+
+// import.meta (ES2020)
+import.meta;
+import . /* comment */ meta;
+import // comment
+.meta;
+a = import.meta;
+a = import . /* comment */ meta;
+a = import // comment
+.meta;
+
 // new.target
 new.target;
 new . /* comment */ target;
 new // comment
 .target;
 
-// function keywords
-import(); // ES2020
-import /* comment */ (); // ES2020
-// incorrectly highlighted (though it may appear correct)
-import
-();
-import /* comment
-*/ ();
-import // comment
-();
-
 // properties (subset)
 array.length;
 Math.PI;
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index a681d28b..ee08e480 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -589,18 +589,6 @@ a = class {
 
 /* Expression */
 
-// Dynamic import expression (ES2020)
-a = import('module');
-import("module").then(module => {});
-
-// import.meta (stage 3 proposal)
-a = import.meta.__dirname;
-a = import . /* comment */ meta.__dirname;
-a = import . /* comment
-*/ meta.__dirname; // incorrectly highlighted
-a = import // comment
-.meta.__dirname; // incorrectly highlighted
-
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();
@@ -933,23 +921,32 @@ globalThis; // ES2020
 super;
 this;
 
+// dynamic import (ES2020)
+import("module").then();
+import /* comment */ ("module").then();
+import // comment
+("module").then();
+a = await import("module");
+a = await import /* comment */ ("module");
+a = await import // comment
+("module");
+
+// import.meta (ES2020)
+import.meta;
+import . /* comment */ meta;
+import // comment
+.meta;
+a = import.meta;
+a = import . /* comment */ meta;
+a = import // comment
+.meta;
+
 // new.target
 new.target;
 new . /* comment */ target;
 new // comment
 .target;
 
-// function keywords
-import(); // ES2020
-import /* comment */ (); // ES2020
-// incorrectly highlighted (though it may appear correct)
-import
-();
-import /* comment
-*/ ();
-import // comment
-();
-
 // properties (subset)
 array.length;
 Math.PI;


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