[gtksourceview: 12/14] jsx.lang: Highlight invalid characters as errors



commit bf6c6576d41bb5411a5182d8138da3585c84f3ec
Author: Jeffery To <jeffery to gmail com>
Date:   Thu Jun 11 04:51:58 2020 +0800

    jsx.lang: Highlight invalid characters as errors
    
    This highlights } and > in text positions as errors. The JSX spec
    already disallows this (along with { and <) but TypeScript 3.9 and Babel
    are now enforcing this rule to be more comformant.
    
    This also restructures the contexts so that /> outside of tags (i.e. in
    text positions) will not end an element prematurely.

 data/language-specs/jsx.lang       | 91 ++++++++++++++++++++++++--------------
 tests/syntax-highlighting/file.jsx |  4 ++
 tests/syntax-highlighting/file.tsx |  4 ++
 3 files changed, 65 insertions(+), 34 deletions(-)
---
diff --git a/data/language-specs/jsx.lang b/data/language-specs/jsx.lang
index 32d3984f..94c041c0 100644
--- a/data/language-specs/jsx.lang
+++ b/data/language-specs/jsx.lang
@@ -251,38 +251,10 @@
 
     <!-- # Element / fragment -->
 
-    <context id="_element-content">
+    <context id="_start-tag-tail-non-empty-element-content">
       <include>
 
-        <!-- ## Start tag -->
-
-        <!-- <JSXOpeningElement> / <JSXSelfClosingElement> -->
-        <context id="_start-tag-head" style-ref="xml:tag" once-only="true" class="no-spell-check">
-          <start>\%{_start-tag-start}</start>
-          <end>(?=/?&gt;)</end>
-          <include>
-            <context sub-pattern="0" where="start" style-ref="xml:element-name"/>
-            <context ref="js:comments"/>
-
-            <context id="_start-tag-head-content">
-              <include>
-                <context ref="_ordered-element-name"/>
-                <context ref="_spread-attributes"/>
-                <context ref="_attribute-names"/>
-                <context ref="_attribute-initializers"/>
-              </include>
-            </context> <!-- /_start-tag-head-content -->
-
-          </include>
-        </context> <!-- /_start-tag-head -->
-
-        <context id="_start-tag-tail-empty-element-end-parent" style-ref="xml:tag" end-parent="true" 
class="no-spell-check">
-          <start>(?=/&gt;)</start>
-          <end>/&gt;</end>
-          <include>
-            <context sub-pattern="0" where="end" style-ref="xml:element-name"/>
-          </include>
-        </context> <!-- /_start-tag-tail-empty-element-end-parent -->
+        <!-- ## Start tag tail -->
 
         <context id="_start-tag-tail" style-ref="xml:tag" once-only="true" class="no-spell-check">
           <match>&gt;</match>
@@ -316,8 +288,7 @@
         <context id="_elements" style-ref="element" class-disabled="no-spell-check">
           <start>(?=\%{_start-tag-start})</start>
           <include>
-            <!-- no comments here - comments appear to be parsed as regular
-                 text -->
+            <!-- no comments here - comments parsed as regular text -->
             <context ref="_element-content"/>
           </include>
         </context> <!-- /_elements -->
@@ -348,6 +319,59 @@
              appear to be parsed -->
         <context ref="xml:entity"/>
         <context ref="xml:character-reference"/>
+
+        <!-- ## Invalid characters -->
+
+        <!-- the spec does not allow these characters: { < > }
+             but we will only highlight > and } as errors -->
+        <context id="_invalid-characters" style-ref="xml:error">
+          <match>[&gt;}]</match>
+        </context> <!-- /_invalid-characters -->
+
+      </include>
+    </context> <!-- /_start-tag-tail-non-empty-element-content -->
+
+    <context id="_element-content">
+      <include>
+
+        <!-- ## Start tag -->
+
+        <!-- <JSXOpeningElement> / <JSXSelfClosingElement> -->
+        <context id="_start-tag-head" style-ref="xml:tag" once-only="true" class="no-spell-check">
+          <start>\%{_start-tag-start}</start>
+          <end>(?=/?&gt;)</end>
+          <include>
+            <context sub-pattern="0" where="start" style-ref="xml:element-name"/>
+            <context ref="js:comments"/>
+
+            <context id="_start-tag-head-content">
+              <include>
+                <context ref="_ordered-element-name"/>
+                <context ref="_spread-attributes"/>
+                <context ref="_attribute-names"/>
+                <context ref="_attribute-initializers"/>
+              </include>
+            </context> <!-- /_start-tag-head-content -->
+
+          </include>
+        </context> <!-- /_start-tag-head -->
+
+        <context id="_choice-start-tag-tail-empty-element" style-ref="xml:tag" end-parent="true" 
class="no-spell-check">
+          <start>(?=/&gt;)</start>
+          <end>/&gt;</end>
+          <include>
+            <context sub-pattern="0" where="end" style-ref="xml:element-name"/>
+          </include>
+        </context> <!-- /_choice-start-tag-tail-empty-element -->
+
+        <context id="_choice-start-tag-tail-non-empty-element" end-parent="true">
+          <start>(?=&gt;)</start>
+          <include>
+            <!-- no comments here - comments parsed as regular text -->
+            <context ref="_start-tag-tail-non-empty-element-content"/>
+          </include>
+        </context> <!-- /_choice-start-tag-tail-non-empty-element -->
+
       </include>
     </context> <!-- /_element-content -->
 
@@ -355,8 +379,7 @@
     <context id="choice-element" style-ref="element" end-parent="true" class-disabled="no-spell-check">
       <start>(?=\%{_start-tag-start})</start>
       <include>
-        <!-- no comments here - comments appear to be parsed as regular
-             text -->
+        <!-- no comments here - comments parsed as regular text -->
         <context ref="_element-content"/>
       </include>
     </context> <!-- /choice-element -->
diff --git a/tests/syntax-highlighting/file.jsx b/tests/syntax-highlighting/file.jsx
index 2c92b33c..f4364bda 100644
--- a/tests/syntax-highlighting/file.jsx
+++ b/tests/syntax-highlighting/file.jsx
@@ -40,6 +40,10 @@
 // XML character entity / numeric character references
 ( <div>&gt;&#47;</div> );
 
+// Invalid characters
+( <div>greater than > right brace }</div> );
+( <div>valid alternatives: &gt; {">"} &rbrace; {'}'}</div> );
+
 // Fragment
 (
     <>
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index 73ae26a1..8783a8a1 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -94,6 +94,10 @@ a = <GenericComponent<string> a={10} b="hi"/>;
 // XML character entity / numeric character references
 ( <div>&gt;&#47;</div> );
 
+// Invalid characters
+( <div>greater than > right brace }</div> );
+( <div>valid alternatives: &gt; {">"} &rbrace; {'}'}</div> );
+
 // Fragment
 (
     <>


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