Repository: w3c/webidl2.js
Pull request #206 (2018-06-14 04:45:51)

Add trivia for callbacks
Countdown, 5, 4, 😁

Always thanks for your reviews!


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index d2301b6..e287317 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -662,21 +662,30 @@
 
     function callback() {
       let ret;
-      if (!consume("callback")) return;
+      const callbackToken = consume("callback");
+      if (!callbackToken) return;
       const tok = consume("interface");
       if (tok) {
         ret = interface_rest({ typeName: "callback interface" });
+        ret.trivia.callback = callbackToken.trivia;
         ret.trivia.base = tok.trivia;
         return ret;
       }
+      const trivia = { base: callbackToken.trivia };
       const name = consume(ID) || error("No name for callback");
+      trivia.name = name.trivia;
       ret = current = { type: "callback", name: sanitize_name(name.value, "callback") };
-      consume("=") || error("No assignment in callback");
+      const assign = consume("=") || error("No assignment in callback");
+      trivia.assign = assign.trivia;
       ret.idlType = return_type() || error("Missing return type");
-      consume("(") || error("No arguments in callback");
+      const open = consume("(") || error("No arguments in callback");
+      trivia.open = open.trivia;
       ret.arguments = argument_list();
-      consume(")") || error("Unterminated callback");
-      consume(";") || error("Unterminated callback");
+      const close = consume(")") || error("Unterminated callback");
+      trivia.close = close.trivia;
+      const termination = consume(";") || error("Unterminated callback");
+      trivia.termination = termination.trivia;
+      ret.trivia = trivia;
       return ret;
     }
 
diff --git a/lib/writer.js b/lib/writer.js
index f7daa8b..f9e65e4 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -150,9 +150,11 @@
       return `${ret}${it.target} includes ${it.includes};`;
     }
     function callback(it) {
-      const ret = extended_attributes(it.extAttrs);
+      const ext = extended_attributes(it.extAttrs);
+      const head = `${it.trivia.base}callback${it.trivia.name}${it.name}`;
       const args = it.arguments.map(argument).join("");
-      return `${ret}callback ${it.name} =${type(it.idlType)}(${args});`;
+      const signature = `${type(it.idlType)}${it.trivia.open}(${args}${it.trivia.close})`;
+      return `${ext}${head}${it.trivia.assign}=${signature}${it.trivia.termination};`;
     }
     function enum_(it) {
       const ext = extended_attributes(it.extAttrs);
@@ -165,7 +167,7 @@
       return `${readonly}${it.trivia.type}${it.type}${bracket}${it.trivia.termination};`;
     }
     function callbackInterface(it) {
-      return `callback${interface_(it)}`;
+      return `${it.trivia.callback}callback${interface_(it)}`;
     }
 
     const table = {


Test patch:
diff --git a/test/syntax/json/callback.json b/test/syntax/json/callback.json
index 879dd9e..0986de7 100644
--- a/test/syntax/json/callback.json
+++ b/test/syntax/json/callback.json
@@ -46,6 +46,14 @@
                 "separator": null
             }
         ],
+        "trivia": {
+            "base": "",
+            "name": " ",
+            "assign": " ",
+            "open": " ",
+            "close": "",
+            "termination": ""
+        },
         "extAttrs": null
     },
     {
@@ -126,7 +134,8 @@
             "name": " ",
             "open": " ",
             "close": "\n",
-            "termination": ""
+            "termination": "",
+            "callback": "\n\n"
         },
         "inheritance": null,
         "extAttrs": null
@@ -208,6 +217,14 @@
                 "separator": null
             }
         ],
+        "trivia": {
+            "base": "\n\n",
+            "name": " ",
+            "assign": " ",
+            "open": " ",
+            "close": "",
+            "termination": ""
+        },
         "extAttrs": null
     }
 ]