Repository: w3c/webidl2.js
Pull request #188 (2018-06-05 02:40:03)

Generics always as an array
A simpler logic.

saschanaz: @marcoscaceres I pushed some changes requested in #185. That was quick! 👍😆

Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index 4217816..10c210b 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -321,11 +321,11 @@
       switch (name.type) {
         case "Promise":
           if (probe("[")) error("Promise type cannot have extended attribute");
-          ret.idlType = return_type(typeName);
+          ret.idlType = [return_type(typeName)];
           break;
         case "sequence":
         case "FrozenArray":
-          ret.idlType = type_with_extended_attributes(typeName);
+          ret.idlType = [type_with_extended_attributes(typeName)];
           break;
         case "record":
           if (probe("[")) error("Record key cannot have extended attribute");
diff --git a/lib/writer.js b/lib/writer.js
index ac4778a..aeebe5b 100644
--- a/lib/writer.js
+++ b/lib/writer.js
@@ -1,27 +1,13 @@
 "use strict";
 
 (() => {
-  function write(ast, opt = {}) {
-    const noop = str => str;
-    const optNames = "type".split(" ");
-    const context = [];
-    for (const o of optNames) {
-      if (!opt[o]) opt[o] = noop;
-    }
-
-    function literal(it) {
-      return it.value;
-    };
+  function write(ast) {
     function type(it) {
-      if (typeof it === "string") return opt.type(it); // XXX should maintain some context
+      if (typeof it === "string") return it;
       let ret = extended_attributes(it.extAttrs);
       if (it.union) ret += `(${it.idlType.map(type).join(" or ")})`;
-      else {
-        if (it.generic) ret += `${it.generic}<`;
-        if (Array.isArray(it.idlType)) ret += it.idlType.map(type).join(", ");
-        else ret += type(it.idlType);
-        if (it.generic) ret += ">";
-      }
+      else if (it.generic) ret += `${it.generic}<${it.idlType.map(type).join(", ")}>`;
+      else ret += type(it.idlType);
       if (it.nullable) ret += "?";
 
       return ret;
@@ -46,14 +32,12 @@
       return ret;
     };
     function make_ext_at(it) {
-      context.unshift(it);
       let ret = it.name;
       if (it.rhs) {
         if (it.rhs.type === "identifier-list") ret += `=(${it.rhs.value.join(",")})`;
         else ret += `=${it.rhs.value}`;
       }
       if (it.arguments) ret += `(${it.arguments.length ? it.arguments.map(argument).join(",") : ""})`;
-      context.shift(); // XXX need to add more contexts, but not more than needed for ReSpec
       return ret;
     };
     function extended_attributes(eats) {
@@ -61,11 +45,10 @@
       return `[${eats.map(make_ext_at).join(", ")}]`;
     };
 
-    const modifiers = "getter setter deleter stringifier static".split(" ");
     function operation(it) {
       let ret = extended_attributes(it.extAttrs);
       if (it.stringifier && !it.idlType) return "stringifier;";
-      for (const mod of modifiers) {
+      for (const mod of ["getter",  "setter", "deleter", "stringifier", "static"]) {
         if (it[mod]) ret += mod + " ";
       }
       ret += type(it.idlType) + " ";
@@ -155,10 +138,10 @@
       return ret + "};";
     };
     function iterable(it) {
-      return `iterable<${Array.isArray(it.idlType) ? it.idlType.map(type).join(", ") : type(it.idlType)}>;`;
+      return `iterable<${it.idlType.map(type).join(", ")}>;`;
     };
     function legacyiterable(it) {
-      return `legacyiterable<${Array.isArray(it.idlType) ? it.idlType.map(type).join(", ") : type(it.idlType)}>;`;
+      return `legacyiterable<${it.idlType.map(type).join(", ")}>;`;
     };
     function maplike(it) {
       return `${it.readonly ? "readonly " : ""}maplike<${it.idlType.map(type).join(", ")}>;`;


Test patch:
diff --git a/test/syntax/json/dictionary.json b/test/syntax/json/dictionary.json
index 8bbc6b9..b7f28d2 100644
--- a/test/syntax/json/dictionary.json
+++ b/test/syntax/json/dictionary.json
@@ -67,14 +67,16 @@
                     "generic": "sequence",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "dictionary-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "long",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "dictionary-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "long",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "extAttrs": [],
diff --git a/test/syntax/json/generic.json b/test/syntax/json/generic.json
index 2933144..e817028 100644
--- a/test/syntax/json/generic.json
+++ b/test/syntax/json/generic.json
@@ -16,28 +16,34 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": "Promise",
-                        "nullable": false,
-                        "union": false,
-                        "idlType": {
+                    "idlType": [
+                        {
                             "type": "return-type",
-                            "generic": "sequence",
+                            "generic": "Promise",
                             "nullable": false,
                             "union": false,
-                            "idlType": {
-                                "type": "return-type",
-                                "generic": null,
-                                "nullable": true,
-                                "union": false,
-                                "idlType": "DOMString",
-                                "extAttrs": []
-                            },
+                            "idlType": [
+                                {
+                                    "type": "return-type",
+                                    "generic": "sequence",
+                                    "nullable": false,
+                                    "union": false,
+                                    "idlType": [
+                                        {
+                                            "type": "return-type",
+                                            "generic": null,
+                                            "nullable": true,
+                                            "union": false,
+                                            "idlType": "DOMString",
+                                            "extAttrs": []
+                                        }
+                                    ],
+                                    "extAttrs": []
+                                }
+                            ],
                             "extAttrs": []
-                        },
-                        "extAttrs": []
-                    },
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "bar",
@@ -56,14 +62,16 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "attribute-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "DOMString",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "attribute-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "DOMString",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "baz",
@@ -98,14 +106,16 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": null,
-                        "nullable": true,
-                        "union": false,
-                        "idlType": "Client",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "return-type",
+                            "generic": null,
+                            "nullable": true,
+                            "union": false,
+                            "idlType": "Client",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "getServiced",
@@ -125,14 +135,16 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "any",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "return-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "any",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "reloadAll",
@@ -168,14 +180,16 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "any",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "return-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "any",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "default",
diff --git a/test/syntax/json/promise-void.json b/test/syntax/json/promise-void.json
index 6340f8d..e071f22 100644
--- a/test/syntax/json/promise-void.json
+++ b/test/syntax/json/promise-void.json
@@ -15,14 +15,16 @@
                     "generic": "Promise",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "void",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "return-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "void",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "meow",
diff --git a/test/syntax/json/record.json b/test/syntax/json/record.json
index 1a8aa1b..9657aaf 100644
--- a/test/syntax/json/record.json
+++ b/test/syntax/json/record.json
@@ -32,31 +32,33 @@
                             "generic": "sequence",
                             "nullable": false,
                             "union": false,
-                            "idlType": {
-                                "type": "argument-type",
-                                "generic": "record",
-                                "nullable": false,
-                                "union": false,
-                                "idlType": [
-                                    {
-                                        "type": "argument-type",
-                                        "generic": null,
-                                        "nullable": false,
-                                        "union": false,
-                                        "idlType": "ByteString",
-                                        "extAttrs": []
-                                    },
-                                    {
-                                        "type": "argument-type",
-                                        "generic": null,
-                                        "nullable": false,
-                                        "union": false,
-                                        "idlType": "any",
-                                        "extAttrs": []
-                                    }
-                                ],
-                                "extAttrs": []
-                            },
+                            "idlType": [
+                                {
+                                    "type": "argument-type",
+                                    "generic": "record",
+                                    "nullable": false,
+                                    "union": false,
+                                    "idlType": [
+                                        {
+                                            "type": "argument-type",
+                                            "generic": null,
+                                            "nullable": false,
+                                            "union": false,
+                                            "idlType": "ByteString",
+                                            "extAttrs": []
+                                        },
+                                        {
+                                            "type": "argument-type",
+                                            "generic": null,
+                                            "nullable": false,
+                                            "union": false,
+                                            "idlType": "any",
+                                            "extAttrs": []
+                                        }
+                                    ],
+                                    "extAttrs": []
+                                }
+                            ],
                             "extAttrs": []
                         },
                         "name": "param",
diff --git a/test/syntax/json/sequence.json b/test/syntax/json/sequence.json
index 20f74a3..cf8333b 100644
--- a/test/syntax/json/sequence.json
+++ b/test/syntax/json/sequence.json
@@ -32,14 +32,16 @@
                             "generic": "sequence",
                             "nullable": false,
                             "union": false,
-                            "idlType": {
-                                "type": "argument-type",
-                                "generic": null,
-                                "nullable": false,
-                                "union": false,
-                                "idlType": "float",
-                                "extAttrs": []
-                            },
+                            "idlType": [
+                                {
+                                    "type": "argument-type",
+                                    "generic": null,
+                                    "nullable": false,
+                                    "union": false,
+                                    "idlType": "float",
+                                    "extAttrs": []
+                                }
+                            ],
                             "extAttrs": []
                         },
                         "name": "coordinates",
@@ -60,14 +62,16 @@
                     "generic": "sequence",
                     "nullable": false,
                     "union": false,
-                    "idlType": {
-                        "type": "return-type",
-                        "generic": null,
-                        "nullable": false,
-                        "union": false,
-                        "idlType": "float",
-                        "extAttrs": []
-                    },
+                    "idlType": [
+                        {
+                            "type": "return-type",
+                            "generic": null,
+                            "nullable": false,
+                            "union": false,
+                            "idlType": "float",
+                            "extAttrs": []
+                        }
+                    ],
                     "extAttrs": []
                 },
                 "name": "getInflectionPoints",
@@ -119,21 +123,23 @@
                             "generic": "sequence",
                             "nullable": false,
                             "union": false,
-                            "idlType": {
-                                "type": "argument-type",
-                                "generic": null,
-                                "nullable": false,
-                                "union": false,
-                                "idlType": "long",
-                                "extAttrs": [
-                                    {
-                                        "name": "XAttr",
-                                        "arguments": null,
-                                        "type": "extended-attribute",
-                                        "rhs": null
-                                    }
-                                ]
-                            },
+                            "idlType": [
+                                {
+                                    "type": "argument-type",
+                                    "generic": null,
+                                    "nullable": false,
+                                    "union": false,
+                                    "idlType": "long",
+                                    "extAttrs": [
+                                        {
+                                            "name": "XAttr",
+                                            "arguments": null,
+                                            "type": "extended-attribute",
+                                            "rhs": null
+                                        }
+                                    ]
+                                }
+                            ],
                             "extAttrs": []
                         },
                         "name": "arg",
diff --git a/test/syntax/json/typedef.json b/test/syntax/json/typedef.json
index 2fc815d..e6ecf3a 100644
--- a/test/syntax/json/typedef.json
+++ b/test/syntax/json/typedef.json
@@ -58,14 +58,16 @@
             "generic": "sequence",
             "nullable": false,
             "union": false,
-            "idlType": {
-                "type": "typedef-type",
-                "generic": null,
-                "nullable": false,
-                "union": false,
-                "idlType": "Point",
-                "extAttrs": []
-            },
+            "idlType": [
+                {
+                    "type": "typedef-type",
+                    "generic": null,
+                    "nullable": false,
+                    "union": false,
+                    "idlType": "Point",
+                    "extAttrs": []
+                }
+            ],
             "extAttrs": []
         },
         "name": "PointSequence",
diff --git a/test/syntax/json/typesuffixes.json b/test/syntax/json/typesuffixes.json
index 47d2a98..c5fb932 100644
--- a/test/syntax/json/typesuffixes.json
+++ b/test/syntax/json/typesuffixes.json
@@ -32,14 +32,16 @@
                             "generic": "sequence",
                             "nullable": true,
                             "union": false,
-                            "idlType": {
-                                "type": "argument-type",
-                                "generic": null,
-                                "nullable": true,
-                                "union": false,
-                                "idlType": "DOMString",
-                                "extAttrs": []
-                            },
+                            "idlType": [
+                                {
+                                    "type": "argument-type",
+                                    "generic": null,
+                                    "nullable": true,
+                                    "union": false,
+                                    "idlType": "DOMString",
+                                    "extAttrs": []
+                                }
+                            ],
                             "extAttrs": []
                         },
                         "name": "foo",