Repository: w3c/webidl2.js
Pull request #195 (2018-06-10 05:20:12)

Add trivia for inheritance
This changes type of `inheritance` field from string to object with `name` and `trivia` field.


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index f13f820..b69cb36 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -572,9 +572,10 @@
     }
 
     function inheritance() {
-      if (consume(":")) {
+      const colon = consume(":");
+      if (colon) {
         const inh = consume(ID) || error("No type in inheritance");
-        return inh.value;
+        return { name: inh.value, trivia: { colon: colon.trivia, name: inh.trivia } };
       }
     }
 
diff --git a/lib/writer.js b/lib/writer.js
index 058dc4f..0d66c8b 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -72,11 +72,15 @@
       return ret;
     };
 
+    function inheritance(inh) {
+      return `${inh.trivia.colon}:${inh.trivia.name}${inh.name}`;
+    }
+
     function interface_(it) {
       let ret = extended_attributes(it.extAttrs);
       if (it.partial) ret += "partial";
       ret += `${it.trivia.base}interface${it.trivia.name}${it.name}`;
-      if (it.inheritance) ret += `: ${it.inheritance}`;
+      if (it.inheritance) ret += inheritance(it.inheritance);
       ret += `${it.trivia.open}{${iterate(it.members)}${it.trivia.close}}${it.trivia.termination};`;
       return ret;
     };
@@ -100,8 +104,8 @@
     function dictionary(it) {
       let ret = extended_attributes(it.extAttrs);
       if (it.partial) ret += "partial ";
-      ret += `dictionary ${it.name} `;
-      if (it.inheritance) ret += `: ${it.inheritance} `;
+      ret += `dictionary ${it.name}`;
+      if (it.inheritance) ret += inheritance(it.inheritance);
       ret += `{${iterate(it.members)}};`;
       return ret;
     };


Test patch:
diff --git a/test/syntax/json/dictionary-inherits.json b/test/syntax/json/dictionary-inherits.json
index c1085fc..553977a 100644
--- a/test/syntax/json/dictionary-inherits.json
+++ b/test/syntax/json/dictionary-inherits.json
@@ -115,7 +115,13 @@
                 "default": null
             }
         ],
-        "inheritance": "PaintOptions",
+        "inheritance": {
+            "name": "PaintOptions",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     }
 ]
diff --git a/test/syntax/json/extended-attributes.json b/test/syntax/json/extended-attributes.json
index a1fa575..e7a8e45 100644
--- a/test/syntax/json/extended-attributes.json
+++ b/test/syntax/json/extended-attributes.json
@@ -11,7 +11,13 @@
             "close": "\n\n",
             "termination": ""
         },
-        "inheritance": "WorkerGlobalScope",
+        "inheritance": {
+            "name": "WorkerGlobalScope",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": [
             {
                 "name": "Global",
diff --git a/test/syntax/json/generic.json b/test/syntax/json/generic.json
index 254792f..e2a8f71 100644
--- a/test/syntax/json/generic.json
+++ b/test/syntax/json/generic.json
@@ -342,7 +342,13 @@
             "close": "\n",
             "termination": ""
         },
-        "inheritance": "Event",
+        "inheritance": {
+            "name": "Event",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     }
 ]
diff --git a/test/syntax/json/inherits-getter.json b/test/syntax/json/inherits-getter.json
index 08eb23b..35db629 100644
--- a/test/syntax/json/inherits-getter.json
+++ b/test/syntax/json/inherits-getter.json
@@ -127,7 +127,13 @@
             "close": "\n",
             "termination": ""
         },
-        "inheritance": "Animal",
+        "inheritance": {
+            "name": "Animal",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     },
     {
@@ -177,7 +183,13 @@
             "close": "\n",
             "termination": ""
         },
-        "inheritance": "Person",
+        "inheritance": {
+            "name": "Person",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     }
 ]
diff --git a/test/syntax/json/interface-inherits.json b/test/syntax/json/interface-inherits.json
index 36a4bf5..c91ca02 100644
--- a/test/syntax/json/interface-inherits.json
+++ b/test/syntax/json/interface-inherits.json
@@ -88,7 +88,13 @@
             "close": "\n",
             "termination": ""
         },
-        "inheritance": "Animal",
+        "inheritance": {
+            "name": "Animal",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     },
     {
@@ -134,7 +140,13 @@
             "close": "\n",
             "termination": ""
         },
-        "inheritance": "Animal",
+        "inheritance": {
+            "name": "Animal",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": []
     }
 ]
diff --git a/test/syntax/json/namedconstructor.json b/test/syntax/json/namedconstructor.json
index 93d2214..ce9eba1 100644
--- a/test/syntax/json/namedconstructor.json
+++ b/test/syntax/json/namedconstructor.json
@@ -11,7 +11,13 @@
             "close": "\n  // ...\n",
             "termination": ""
         },
-        "inheritance": "HTMLMediaElement",
+        "inheritance": {
+            "name": "HTMLMediaElement",
+            "trivia": {
+                "colon": " ",
+                "name": " "
+            }
+        },
         "extAttrs": [
             {
                 "name": "NamedConstructor",