[gtksourceview/gtksourceview-4-6] javascript.lang: Fix default value assignment in object destructuring



commit d43d01dd27017c94586c0a95f6ebe43b384a0909
Author: Jeffery To <jeffery to gmail com>
Date:   Thu Mar 26 17:24:55 2020 +0800

    javascript.lang: Fix default value assignment in object destructuring
    
    The object-literal context is used for cases of destructuring assignment
    but it was lacking the initializer (default value assignment), e.g. in
    this case:
    
    ({ a = 1, b = 2 } = { a: 3 })
    
    the 1 and 2 would not be highlighted correctly.
    
    This adds default value assignment for each object literal property.
    
    Fixes #116.

 data/language-specs/javascript-literals.lang | 52 ++++++++++++----------------
 tests/syntax-highlighting/file.j             | 40 ++++++++++++++++-----
 tests/syntax-highlighting/file.js            | 40 ++++++++++++++++-----
 tests/syntax-highlighting/file.jsx           | 40 ++++++++++++++++-----
 tests/syntax-highlighting/file.ts            | 40 ++++++++++++++++-----
 tests/syntax-highlighting/file.tsx           | 40 ++++++++++++++++-----
 6 files changed, 183 insertions(+), 69 deletions(-)
---
diff --git a/data/language-specs/javascript-literals.lang b/data/language-specs/javascript-literals.lang
index 5d046cb8..d3c4d51b 100644
--- a/data/language-specs/javascript-literals.lang
+++ b/data/language-specs/javascript-literals.lang
@@ -351,42 +351,36 @@
          }
     -->
 
-    <context id="_object-literal-property-content">
+    <context id="_object-literal-property-value" once-only="true">
+      <start>:</start>
+      <end>\%{js:before-next-token}</end>
       <include>
+        <context ref="js:embedded-lang-hooks"/>
+        <context ref="js:comments"/>
 
-        <context id="_object-literal-property-name" once-only="true">
-          <start>\%{js:before-next-token}</start>
-          <end>\%{js:before-next-token}</end>
+        <context id="_object-literal-property-value-content">
           <include>
-            <context ref="js:embedded-lang-hooks"/>
-            <context ref="js:comments"/>
-
-            <context id="_object-literal-property-name-content">
-              <include>
-                <context ref="js:ordered-spread-syntax"/> <!-- ES2018 -->
-                <context ref="js-fn:ordered-method-definition"/> <!-- includes property-name -->
-              </include>
-            </context> <!-- /_object-literal-property-name-content -->
-
+            <context ref="js-expr:expression-without-comma"/>
           </include>
-        </context> <!-- /_object-literal-property-name -->
-
-        <context id="_object-literal-property-value" once-only="true">
-          <start>:</start>
-          <end>\%{js:before-next-token}</end>
-          <include>
-            <context ref="js:embedded-lang-hooks"/>
-            <context ref="js:comments"/>
+        </context> <!-- /_object-literal-property-value-content -->
 
-            <context id="_object-literal-property-value-content">
-              <include>
-                <context ref="js-expr:expression-without-comma"/>
-              </include>
-            </context> <!-- /_object-literal-property-value-content -->
+      </include>
+    </context> <!-- /_object-literal-property-value -->
 
-          </include>
-        </context> <!-- /_object-literal-property-value -->
+    <context id="_ordered-object-literal-property-value" once-only="true">
+      <start>\%{js:before-next-token}</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context ref="_object-literal-property-value"/>
+      </include>
+    </context> <!-- /_ordered-object-literal-property-value -->
 
+    <context id="_object-literal-property-content">
+      <include>
+        <context ref="js:ordered-spread-syntax"/> <!-- ES2018 -->
+        <context ref="js-fn:ordered-method-definition"/> <!-- includes property-name -->
+        <context ref="_ordered-object-literal-property-value"/>
+        <context ref="js:ordered-default-value-assignment"/> <!-- for destructuring assignment -->
       </include>
     </context> <!-- /_object-literal-property-content -->
 
diff --git a/tests/syntax-highlighting/file.j b/tests/syntax-highlighting/file.j
index 1c639485..0cef88ab 100644
--- a/tests/syntax-highlighting/file.j
+++ b/tests/syntax-highlighting/file.j
@@ -603,17 +603,29 @@ a = function fn(x = 1, y) {};
 
 // Array destructuring
 a = function fn([x]) {};
-a = function fn([x, y]) {};
+a = function fn([x = 5, y = 7]) {}; // default values
+a = function fn([x, , y]) {}; // ignoring some returned values (elision)
+a = function fn([x, ...y]) {}; // rest syntax
 ([x]) => x;
-([x, y]) => x + y;
+([x = 5, y = 7]) => x + y; // default values
+([x, , y]) => x + y; // ignoring some returned values (elision)
+([x, ...y]) => y; // rest syntax
 
 // Object destructuring
 a = function fn({ x }) {};
-a = function fn({ x, b: y }) {};
+a = function fn({ a: x, b: y }) {}; // assigning to new variable names
+a = function fn({ x = 5, y = 7 }) {}; // default values
+a = function fn({ a: x = 5, b: y = 7 }) {}; // assigning to new variable names and default values
+a = function fn({ ['a']: x, ['b']: y }) {}; // computed property names
+a = function fn({ x, y, ...rest }) {}; // rest properties (ES2018)
 ({ x }) => x;
-({ x, b: y }) => x + y;
+({ a: x, b: y }) => x + y; // assigning to new variable names
+({ x = 5, y = 7 }) => x + y; // default values
+({ a: x = 5, b: y = 7 }) => x + y; // assigning to new variable names and default values
+({ ['a']: x, ['b']: y }) => x + y; // computed property names
+({ x, y, ...rest }) => x; // rest properties (ES2018)
 
-// Destructuring and default value
+// Destructuring and default parameters
 a = function f([x, y] = [1, 2], {c: z} = {c: 3}) {};
 ([x, y] = [1, 2], {c: z} = {c: x + y}) => x + y + z;
 
@@ -758,8 +770,20 @@ a = class extends Bar {
 ( a &= 1 );
 ( a |= 1 );
 ( a ^= 1 );
-( [a, b] = [1, 2] ); // array destructuring
-( {a, b} = { a: 1, b: 2} ); // object destructuring
+
+// Array destructuring
+( [a, b] = [1, 2] );
+( [a = 5, b = 7] = [1] ); // default values
+( [a, , b] = f() ); // ignoring some returned values (elision)
+( [a, ...b] = [1, 2, 3] ); // rest syntax
+
+// Object destructuring
+( {a, b} = { a: 1, b: 2} );
+( { a: foo, b: bar } = { a: 1, b: 2 } ); // assigning to new variable names
+( { a = 5, b = 7 } = { a: 1 } ); // default values
+( { a: foo = 5, b: bar = 7 } = { a: 1 } ); // assigning to new variable names and default values
+( { ['a']: foo, ['b']: bar } = { a: 1, b: 2 } ); // computed property names
+( { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 } ); // rest properties (ES2018)
 
 // Comma
 1, 2 ;
@@ -1103,7 +1127,7 @@ var [
     ]
     ;
 var [a = 5, b = 7] = [1]; // default values
-var [a, , b] = f(); // ignoring some returned values
+var [a, , b] = f(); // ignoring some returned values (elision)
 var [a, ...b] = [1, 2, 3]; // rest syntax
 
 // Object destructuring
diff --git a/tests/syntax-highlighting/file.js b/tests/syntax-highlighting/file.js
index 36657c62..039c1dad 100644
--- a/tests/syntax-highlighting/file.js
+++ b/tests/syntax-highlighting/file.js
@@ -302,17 +302,29 @@ a = function fn(x = 1, y) {};
 
 // Array destructuring
 a = function fn([x]) {};
-a = function fn([x, y]) {};
+a = function fn([x = 5, y = 7]) {}; // default values
+a = function fn([x, , y]) {}; // ignoring some returned values (elision)
+a = function fn([x, ...y]) {}; // rest syntax
 ([x]) => x;
-([x, y]) => x + y;
+([x = 5, y = 7]) => x + y; // default values
+([x, , y]) => x + y; // ignoring some returned values (elision)
+([x, ...y]) => y; // rest syntax
 
 // Object destructuring
 a = function fn({ x }) {};
-a = function fn({ x, b: y }) {};
+a = function fn({ a: x, b: y }) {}; // assigning to new variable names
+a = function fn({ x = 5, y = 7 }) {}; // default values
+a = function fn({ a: x = 5, b: y = 7 }) {}; // assigning to new variable names and default values
+a = function fn({ ['a']: x, ['b']: y }) {}; // computed property names
+a = function fn({ x, y, ...rest }) {}; // rest properties (ES2018)
 ({ x }) => x;
-({ x, b: y }) => x + y;
+({ a: x, b: y }) => x + y; // assigning to new variable names
+({ x = 5, y = 7 }) => x + y; // default values
+({ a: x = 5, b: y = 7 }) => x + y; // assigning to new variable names and default values
+({ ['a']: x, ['b']: y }) => x + y; // computed property names
+({ x, y, ...rest }) => x; // rest properties (ES2018)
 
-// Destructuring and default value
+// Destructuring and default parameters
 a = function f([x, y] = [1, 2], {c: z} = {c: 3}) {};
 ([x, y] = [1, 2], {c: z} = {c: x + y}) => x + y + z;
 
@@ -457,8 +469,20 @@ a = class extends Bar {
 ( a &= 1 );
 ( a |= 1 );
 ( a ^= 1 );
-( [a, b] = [1, 2] ); // array destructuring
-( {a, b} = { a: 1, b: 2} ); // object destructuring
+
+// Array destructuring
+( [a, b] = [1, 2] );
+( [a = 5, b = 7] = [1] ); // default values
+( [a, , b] = f() ); // ignoring some returned values (elision)
+( [a, ...b] = [1, 2, 3] ); // rest syntax
+
+// Object destructuring
+( {a, b} = { a: 1, b: 2} );
+( { a: foo, b: bar } = { a: 1, b: 2 } ); // assigning to new variable names
+( { a = 5, b = 7 } = { a: 1 } ); // default values
+( { a: foo = 5, b: bar = 7 } = { a: 1 } ); // assigning to new variable names and default values
+( { ['a']: foo, ['b']: bar } = { a: 1, b: 2 } ); // computed property names
+( { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 } ); // rest properties (ES2018)
 
 // Comma
 1, 2 ;
@@ -802,7 +826,7 @@ var [
     ]
     ;
 var [a = 5, b = 7] = [1]; // default values
-var [a, , b] = f(); // ignoring some returned values
+var [a, , b] = f(); // ignoring some returned values (elision)
 var [a, ...b] = [1, 2, 3]; // rest syntax
 
 // Object destructuring
diff --git a/tests/syntax-highlighting/file.jsx b/tests/syntax-highlighting/file.jsx
index 6da543f5..595f7969 100644
--- a/tests/syntax-highlighting/file.jsx
+++ b/tests/syntax-highlighting/file.jsx
@@ -359,17 +359,29 @@ a = function fn(x = 1, y) {};
 
 // Array destructuring
 a = function fn([x]) {};
-a = function fn([x, y]) {};
+a = function fn([x = 5, y = 7]) {}; // default values
+a = function fn([x, , y]) {}; // ignoring some returned values (elision)
+a = function fn([x, ...y]) {}; // rest syntax
 ([x]) => x;
-([x, y]) => x + y;
+([x = 5, y = 7]) => x + y; // default values
+([x, , y]) => x + y; // ignoring some returned values (elision)
+([x, ...y]) => y; // rest syntax
 
 // Object destructuring
 a = function fn({ x }) {};
-a = function fn({ x, b: y }) {};
+a = function fn({ a: x, b: y }) {}; // assigning to new variable names
+a = function fn({ x = 5, y = 7 }) {}; // default values
+a = function fn({ a: x = 5, b: y = 7 }) {}; // assigning to new variable names and default values
+a = function fn({ ['a']: x, ['b']: y }) {}; // computed property names
+a = function fn({ x, y, ...rest }) {}; // rest properties (ES2018)
 ({ x }) => x;
-({ x, b: y }) => x + y;
+({ a: x, b: y }) => x + y; // assigning to new variable names
+({ x = 5, y = 7 }) => x + y; // default values
+({ a: x = 5, b: y = 7 }) => x + y; // assigning to new variable names and default values
+({ ['a']: x, ['b']: y }) => x + y; // computed property names
+({ x, y, ...rest }) => x; // rest properties (ES2018)
 
-// Destructuring and default value
+// Destructuring and default parameters
 a = function f([x, y] = [1, 2], {c: z} = {c: 3}) {};
 ([x, y] = [1, 2], {c: z} = {c: x + y}) => x + y + z;
 
@@ -514,8 +526,20 @@ a = class extends Bar {
 ( a &= 1 );
 ( a |= 1 );
 ( a ^= 1 );
-( [a, b] = [1, 2] ); // array destructuring
-( {a, b} = { a: 1, b: 2} ); // object destructuring
+
+// Array destructuring
+( [a, b] = [1, 2] );
+( [a = 5, b = 7] = [1] ); // default values
+( [a, , b] = f() ); // ignoring some returned values (elision)
+( [a, ...b] = [1, 2, 3] ); // rest syntax
+
+// Object destructuring
+( {a, b} = { a: 1, b: 2} );
+( { a: foo, b: bar } = { a: 1, b: 2 } ); // assigning to new variable names
+( { a = 5, b = 7 } = { a: 1 } ); // default values
+( { a: foo = 5, b: bar = 7 } = { a: 1 } ); // assigning to new variable names and default values
+( { ['a']: foo, ['b']: bar } = { a: 1, b: 2 } ); // computed property names
+( { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 } ); // rest properties (ES2018)
 
 // Comma
 1, 2 ;
@@ -859,7 +883,7 @@ var [
     ]
     ;
 var [a = 5, b = 7] = [1]; // default values
-var [a, , b] = f(); // ignoring some returned values
+var [a, , b] = f(); // ignoring some returned values (elision)
 var [a, ...b] = [1, 2, 3]; // rest syntax
 
 // Object destructuring
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index b2bf3d52..157d1f5c 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -896,17 +896,29 @@ a = function fn(x = 1, y) {};
 
 // Array destructuring
 a = function fn([x]) {};
-a = function fn([x, y]) {};
+a = function fn([x = 5, y = 7]) {}; // default values
+a = function fn([x, , y]) {}; // ignoring some returned values (elision)
+a = function fn([x, ...y]) {}; // rest syntax
 ([x]) => x;
-([x, y]) => x + y;
+([x = 5, y = 7]) => x + y; // default values
+([x, , y]) => x + y; // ignoring some returned values (elision)
+([x, ...y]) => y; // rest syntax
 
 // Object destructuring
 a = function fn({ x }) {};
-a = function fn({ x, b: y }) {};
+a = function fn({ a: x, b: y }) {}; // assigning to new variable names
+a = function fn({ x = 5, y = 7 }) {}; // default values
+a = function fn({ a: x = 5, b: y = 7 }) {}; // assigning to new variable names and default values
+a = function fn({ ['a']: x, ['b']: y }) {}; // computed property names
+a = function fn({ x, y, ...rest }) {}; // rest properties (ES2018)
 ({ x }) => x;
-({ x, b: y }) => x + y;
+({ a: x, b: y }) => x + y; // assigning to new variable names
+({ x = 5, y = 7 }) => x + y; // default values
+({ a: x = 5, b: y = 7 }) => x + y; // assigning to new variable names and default values
+({ ['a']: x, ['b']: y }) => x + y; // computed property names
+({ x, y, ...rest }) => x; // rest properties (ES2018)
 
-// Destructuring and default value
+// Destructuring and default parameters
 a = function f([x, y] = [1, 2], {c: z} = {c: 3}) {};
 ([x, y] = [1, 2], {c: z} = {c: x + y}) => x + y + z;
 
@@ -1051,8 +1063,20 @@ a = class extends Bar {
 ( a &= 1 );
 ( a |= 1 );
 ( a ^= 1 );
-( [a, b] = [1, 2] ); // array destructuring
-( {a, b} = { a: 1, b: 2} ); // object destructuring
+
+// Array destructuring
+( [a, b] = [1, 2] );
+( [a = 5, b = 7] = [1] ); // default values
+( [a, , b] = f() ); // ignoring some returned values (elision)
+( [a, ...b] = [1, 2, 3] ); // rest syntax
+
+// Object destructuring
+( {a, b} = { a: 1, b: 2} );
+( { a: foo, b: bar } = { a: 1, b: 2 } ); // assigning to new variable names
+( { a = 5, b = 7 } = { a: 1 } ); // default values
+( { a: foo = 5, b: bar = 7 } = { a: 1 } ); // assigning to new variable names and default values
+( { ['a']: foo, ['b']: bar } = { a: 1, b: 2 } ); // computed property names
+( { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 } ); // rest properties (ES2018)
 
 // Comma
 1, 2 ;
@@ -1396,7 +1420,7 @@ var [
     ]
     ;
 var [a = 5, b = 7] = [1]; // default values
-var [a, , b] = f(); // ignoring some returned values
+var [a, , b] = f(); // ignoring some returned values (elision)
 var [a, ...b] = [1, 2, 3]; // rest syntax
 
 // Object destructuring
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index 2ed8a485..9507ea1f 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -1004,17 +1004,29 @@ a = function fn(x = 1, y) {};
 
 // Array destructuring
 a = function fn([x]) {};
-a = function fn([x, y]) {};
+a = function fn([x = 5, y = 7]) {}; // default values
+a = function fn([x, , y]) {}; // ignoring some returned values (elision)
+a = function fn([x, ...y]) {}; // rest syntax
 ([x]) => x;
-([x, y]) => x + y;
+([x = 5, y = 7]) => x + y; // default values
+([x, , y]) => x + y; // ignoring some returned values (elision)
+([x, ...y]) => y; // rest syntax
 
 // Object destructuring
 a = function fn({ x }) {};
-a = function fn({ x, b: y }) {};
+a = function fn({ a: x, b: y }) {}; // assigning to new variable names
+a = function fn({ x = 5, y = 7 }) {}; // default values
+a = function fn({ a: x = 5, b: y = 7 }) {}; // assigning to new variable names and default values
+a = function fn({ ['a']: x, ['b']: y }) {}; // computed property names
+a = function fn({ x, y, ...rest }) {}; // rest properties (ES2018)
 ({ x }) => x;
-({ x, b: y }) => x + y;
+({ a: x, b: y }) => x + y; // assigning to new variable names
+({ x = 5, y = 7 }) => x + y; // default values
+({ a: x = 5, b: y = 7 }) => x + y; // assigning to new variable names and default values
+({ ['a']: x, ['b']: y }) => x + y; // computed property names
+({ x, y, ...rest }) => x; // rest properties (ES2018)
 
-// Destructuring and default value
+// Destructuring and default parameters
 a = function f([x, y] = [1, 2], {c: z} = {c: 3}) {};
 ([x, y] = [1, 2], {c: z} = {c: x + y}) => x + y + z;
 
@@ -1159,8 +1171,20 @@ a = class extends Bar {
 ( a &= 1 );
 ( a |= 1 );
 ( a ^= 1 );
-( [a, b] = [1, 2] ); // array destructuring
-( {a, b} = { a: 1, b: 2} ); // object destructuring
+
+// Array destructuring
+( [a, b] = [1, 2] );
+( [a = 5, b = 7] = [1] ); // default values
+( [a, , b] = f() ); // ignoring some returned values (elision)
+( [a, ...b] = [1, 2, 3] ); // rest syntax
+
+// Object destructuring
+( {a, b} = { a: 1, b: 2} );
+( { a: foo, b: bar } = { a: 1, b: 2 } ); // assigning to new variable names
+( { a = 5, b = 7 } = { a: 1 } ); // default values
+( { a: foo = 5, b: bar = 7 } = { a: 1 } ); // assigning to new variable names and default values
+( { ['a']: foo, ['b']: bar } = { a: 1, b: 2 } ); // computed property names
+( { a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 } ); // rest properties (ES2018)
 
 // Comma
 1, 2 ;
@@ -1504,7 +1528,7 @@ var [
     ]
     ;
 var [a = 5, b = 7] = [1]; // default values
-var [a, , b] = f(); // ignoring some returned values
+var [a, , b] = f(); // ignoring some returned values (elision)
 var [a, ...b] = [1, 2, 3]; // rest syntax
 
 // Object destructuring


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