Repository: w3c/webidl2.js
Pull request #200 (2018-06-10 13:53:08)

Add trivia for extended attribute identifiers
The list now contains objects instead of strings.


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index 0499b97..a7b64df 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -768,16 +768,14 @@
 
     function identifiers() {
       const arr = [];
-      const id = consume(ID);
-      if (id) {
-        arr.push(id.value);
-      }
-      else error("Expected identifiers but not found");
-      while (true) {
-        if (consume(",")) {
-          const name = consume(ID) || error("Trailing comma in identifiers list");
-          arr.push(name.value);
-        } else break;
+      const id = untyped_consume(ID) || error("Expected identifiers but none found");
+      id.separator = untyped_consume(",") || null;
+      arr.push(id);
+      while (id.separator) {
+        const id = untyped_consume(ID) || error("Trailing comma in identifiers list");
+        id.separator = untyped_consume(",") || null;
+        arr.push(id);
+        if (!id.separator) break;
       }
       return arr;
     }
diff --git a/lib/writer.js b/lib/writer.js
index 3a7462b..e6caef0 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -43,10 +43,15 @@
       if (arg.separator) ret += `${arg.separator.trivia}${arg.separator.value}`;
       return ret;
     };
+    function identifier(id) {
+      let ret = id.trivia + id.value;
+      if (id.separator) ret += id.separator.trivia + id.separator.value;
+      return ret;
+    }
     function make_ext_at(it) {
       let ret = it.name;
       if (it.rhs) {
-        if (it.rhs.type === "identifier-list") ret += `=(${it.rhs.value.join(",")})`;
+        if (it.rhs.type === "identifier-list") ret += `=(${it.rhs.value.map(identifier).join("")})`;
         else ret += `=${it.rhs.value}`;
       }
       if (it.arguments) ret += `(${it.arguments.map(argument).join("")})`;


Test patch:
diff --git a/test/invalid/json/extattr-empty-ids.json b/test/invalid/json/extattr-empty-ids.json
index 4337f1e..2a2b1f7 100644
--- a/test/invalid/json/extattr-empty-ids.json
+++ b/test/invalid/json/extattr-empty-ids.json
@@ -1,4 +1,4 @@
 {
-    "message": "Got an error before parsing any named definition: Expected identifiers but not found",
+    "message": "Got an error before parsing any named definition: Expected identifiers but none found",
     "line": 1
 }
diff --git a/test/syntax/json/extended-attributes.json b/test/syntax/json/extended-attributes.json
index 78c51d4..6416068 100644
--- a/test/syntax/json/extended-attributes.json
+++ b/test/syntax/json/extended-attributes.json
@@ -26,8 +26,19 @@
                 "rhs": {
                     "type": "identifier-list",
                     "value": [
-                        "Worker",
-                        "ServiceWorker"
+                        {
+                            "value": "Worker",
+                            "trivia": "",
+                            "separator": {
+                                "value": ",",
+                                "trivia": ""
+                            }
+                        },
+                        {
+                            "value": "ServiceWorker",
+                            "trivia": "",
+                            "separator": null
+                        }
                     ]
                 }
             },