Repository: w3c/webidl2.js
Pull request #203 (2018-06-12 14:54:31)

Add trivia for mixins and namespaces
Almost there 😎


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index 3bbbc49..960ee4f 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -897,6 +897,7 @@
         mixin: mixin.trivia
       };
       const name = consume(ID) || error("No name for interface mixin");
+      trivia.name = name.trivia;
       const mems = [];
       const ret = current = {
         type: "interface mixin",
@@ -905,10 +906,14 @@
         members: mems,
         trivia
       };
-      consume("{") || error("Bodyless interface mixin");
+      const open = consume("{") || error("Bodyless interface mixin");
+      trivia.open = open.trivia;
       while (true) {
-        if (consume("}")) {
-          consume(";") || error("Missing semicolon after interface mixin");
+        const close = consume("}");
+        if (close) {
+          trivia.close = close.trivia;
+          const termination = consume(";") || error("Missing semicolon after interface mixin");
+          trivia.termination = termination.trivia;
           return ret;
         }
         const ea = extended_attrs();
@@ -933,19 +938,27 @@
     }
 
     function namespace(isPartial) {
-      if (!consume("namespace")) return;
+      const base = consume("namespace");
+      if (!base) return;
+      const trivia = { base: base.trivia };
       const name = consume(ID) || error("No name for namespace");
+      trivia.name = name.trivia;
       const mems = [];
       const ret = current = {
         type: "namespace",
         name: isPartial ? name.value : sanitize_name(name.value, "namespace"),
         partial: isPartial,
-        members: mems
+        members: mems,
+        trivia
       };
-      consume("{") || error("Bodyless namespace");
+      const open = consume("{") || error("Bodyless namespace");
+      trivia.open = open.trivia;
       while (true) {
-        if (consume("}")) {
-          consume(";") || error("Missing semicolon after namespace");
+        const close = consume("}");
+        if (close) {
+          trivia.close = close.trivia;
+          const termination = consume(";") || error("Missing semicolon after namespace");
+          trivia.termination = termination.trivia;
           return ret;
         }
         const ea = extended_attrs();
diff --git a/lib/writer.js b/lib/writer.js
index 5b9d51c..f7ef2c2 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -104,16 +104,16 @@
     function interface_mixin(it) {
       let ret = extended_attributes(it.extAttrs);
       if (it.partial) ret += "partial";
-      ret += `${it.trivia.base}interface${it.trivia.mixin}mixin ${it.name} `;
-      ret += `{${iterate(it.members)}};`;
+      ret += `${it.trivia.base}interface${it.trivia.mixin}mixin${it.trivia.name}${it.name}`;
+      ret += `${it.trivia.open}{${iterate(it.members)}${it.trivia.close}}${it.trivia.termination};`;
       return ret;
     }
 
     function namespace(it) {
       let ret = extended_attributes(it.extAttrs);
-      if (it.partial) ret += "partial ";
-      ret += `namespace ${it.name} `;
-      ret += `{${iterate(it.members)}};`;
+      if (it.partial) ret += "partial";
+      ret += `${it.trivia.base}namespace${it.trivia.name}${it.name}`;
+      ret += `${it.trivia.open}{${iterate(it.members)}${it.trivia.close}}${it.trivia.termination};`;
       return ret;
     }
 


Test patch:
diff --git a/test/syntax/json/mixin.json b/test/syntax/json/mixin.json
index c05aa35..901cb92 100644
--- a/test/syntax/json/mixin.json
+++ b/test/syntax/json/mixin.json
@@ -39,7 +39,11 @@
         ],
         "trivia": {
             "base": "// Extracted from https://heycam.github.io/webidl/#using-mixins-and-partials on 2017-11-02\n\n",
-            "mixin": " "
+            "mixin": " ",
+            "name": " ",
+            "open": " ",
+            "close": "\n",
+            "termination": ""
         },
         "extAttrs": null
     },
@@ -95,7 +99,11 @@
         ],
         "trivia": {
             "base": " ",
-            "mixin": " "
+            "mixin": " ",
+            "name": " ",
+            "open": " ",
+            "close": "\n",
+            "termination": ""
         },
         "extAttrs": null
     }
diff --git a/test/syntax/json/namespace.json b/test/syntax/json/namespace.json
index 4cd5459..f1e72e8 100644
--- a/test/syntax/json/namespace.json
+++ b/test/syntax/json/namespace.json
@@ -231,6 +231,13 @@
                 "extAttrs": null
             }
         ],
+        "trivia": {
+            "base": "// Extracted from Web IDL editors draft March 27 2017\n",
+            "name": " ",
+            "open": " ",
+            "close": "\n",
+            "termination": ""
+        },
         "extAttrs": null
     },
     {
@@ -238,6 +245,13 @@
         "name": "SomeNamespace",
         "partial": true,
         "members": [],
+        "trivia": {
+            "base": " ",
+            "name": " ",
+            "open": " ",
+            "close": "\n  /* namespace_members... */\n",
+            "termination": ""
+        },
         "extAttrs": null
     }
 ]