Repository: w3c/webidl2.js
Pull request #209 (2018-06-14 14:01:53)

Add trivia for implements/includes
Sad that we cannot remove `implements` yet...


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index 0a38852..16771e8 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -1097,14 +1097,20 @@
       const start_position = consume_position;
       const target = consume(ID);
       if (!target) return;
-      if (consume("implements")) {
+      const trivia = { target: target.trivia };
+      const implementsToken = consume("implements");
+      if (implementsToken) {
+        trivia.implements = implementsToken.trivia;
         const ret = {
           type: "implements",
           target: target.value
         };
         const imp = consume(ID) || error("Incomplete implements statement");
+        trivia.mixin = imp.trivia;
         ret.implements = imp.value;
-        consume(";") || error("No terminating ; for implements statement");
+        ret.trivia = trivia;
+        const termination = consume(";") || error("No terminating ; for implements statement");
+        trivia.termination = termination.trivia;
         return ret;
       } else {
         // rollback
@@ -1116,14 +1122,20 @@
       const start_position = consume_position;
       const target = consume(ID);
       if (!target) return;
-      if (consume("includes")) {
+      const trivia = { target: target.trivia };
+      const includesToken = consume("includes");
+      if (includesToken) {
+        trivia.includes = includesToken.trivia;
         const ret = {
           type: "includes",
           target: target.value
         };
         const imp = consume(ID) || error("Incomplete includes statement");
+        trivia.mixin = imp.trivia;
         ret.includes = imp.value;
-        consume(";") || error("No terminating ; for includes statement");
+        ret.trivia = trivia;
+        const termination = consume(";") || error("No terminating ; for includes statement");
+        trivia.termination = termination.trivia;
         return ret;
       } else {
         // rollback
diff --git a/lib/writer.js b/lib/writer.js
index a2132cf..c66c54e 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -143,11 +143,11 @@
     }
     function implements_(it) {
       const ret = extended_attributes(it.extAttrs);
-      return `${ret}${it.target} implements ${it.implements};`;
+      return `${ret}${it.trivia.target}${it.target}${it.trivia.implements}implements${it.trivia.mixin}${it.implements}${it.trivia.termination};`;
     }
     function includes(it) {
       const ret = extended_attributes(it.extAttrs);
-      return `${ret}${it.target} includes ${it.includes};`;
+      return `${ret}${it.trivia.target}${it.target}${it.trivia.includes}includes${it.trivia.mixin}${it.includes}${it.trivia.termination};`;
     }
     function callback(it) {
       const ext = extended_attributes(it.extAttrs);


Test patch:
diff --git a/test/syntax/json/implements.json b/test/syntax/json/implements.json
index 9c61367..8485130 100644
--- a/test/syntax/json/implements.json
+++ b/test/syntax/json/implements.json
@@ -197,6 +197,12 @@
         "type": "implements",
         "target": "Node",
         "implements": "EventTarget",
+        "trivia": {
+            "target": "\n\n  ",
+            "implements": " ",
+            "mixin": " ",
+            "termination": ""
+        },
         "extAttrs": null
     }
 ]
diff --git a/test/syntax/json/mixin.json b/test/syntax/json/mixin.json
index 39dd8b6..d1e9725 100644
--- a/test/syntax/json/mixin.json
+++ b/test/syntax/json/mixin.json
@@ -51,12 +51,24 @@
         "type": "includes",
         "target": "Window",
         "includes": "GlobalCrypto",
+        "trivia": {
+            "target": "\n\n",
+            "includes": " ",
+            "mixin": " ",
+            "termination": ""
+        },
         "extAttrs": null
     },
     {
         "type": "includes",
         "target": "WorkerGlobalScope",
         "includes": "GlobalCrypto",
+        "trivia": {
+            "target": "\n",
+            "includes": " ",
+            "mixin": " ",
+            "termination": ""
+        },
         "extAttrs": null
     },
     {