Repository: w3c/webidl2.js
Pull request #284 (2019-03-15 12:26:24)

fix(lib/webidl2): disallow null on constants
Following https://github.com/heycam/webidl/pull/686.


Patch:
diff --git a/lib/webidl2.js b/lib/webidl2.js
index dc8c10b..327c4f5 100644
--- a/lib/webidl2.js
+++ b/lib/webidl2.js
@@ -436,7 +436,7 @@
     }
 
     function const_value() {
-      return consume("true", "false", "null", "Infinity", "-Infinity", "NaN", FLOAT, INT);
+      return consume("true", "false", "Infinity", "-Infinity", "NaN", FLOAT, INT);
     }
 
     function const_data(token) {
@@ -753,7 +753,7 @@
         if (!assign) {
           return null;
         }
-        const def = const_value() || consume(STR, "[")  || error("No value for default");
+        const def = const_value() || consume(STR, "null", "[") || error("No value for default");
         const expression = [def];
         if (def.type === "[") {
           const close = consume("]") || error("Default sequence value must be empty");
@@ -804,7 +804,9 @@
           };
         }
         idlType = Object.assign({ type: "const-type" }, EMPTY_IDLTYPE, idlType);
-        type_suffix(idlType);
+        if (probe("?")) {
+          error("Unexpected nullable constant type");
+        }
         tokens.name = consume(ID) || error("No name for const");
         tokens.assign = consume("=") || error("No value assignment for const");
         tokens.value = const_value() || error("No value for const");


Test patch:
diff --git a/test/invalid/baseline/const-null.txt b/test/invalid/baseline/const-null.txt
new file mode 100644
index 0000000..3ea4d4e
--- /dev/null
+++ b/test/invalid/baseline/const-null.txt
@@ -0,0 +1,3 @@
+Syntax error at line 2, since `interface MyConstants`:
+  const MyPrimitive MY_NULL = null;
+                              ^ No value for const
diff --git a/test/invalid/baseline/const-nullable.txt b/test/invalid/baseline/const-nullable.txt
new file mode 100644
index 0000000..1453c52
--- /dev/null
+++ b/test/invalid/baseline/const-nullable.txt
@@ -0,0 +1,3 @@
+Syntax error at line 3, since `interface MyConstants`:
+  const boolean? ARE_WE_THERE_YET = false;
+               ^ Unexpected nullable constant type
diff --git a/test/invalid/idl/const-null.widl b/test/invalid/idl/const-null.widl
new file mode 100644
index 0000000..1125c5d
--- /dev/null
+++ b/test/invalid/idl/const-null.widl
@@ -0,0 +1,3 @@
+interface MyConstants {
+  const MyPrimitive MY_NULL = null;
+};
diff --git a/test/invalid/idl/const-nullable.widl b/test/invalid/idl/const-nullable.widl
new file mode 100644
index 0000000..47f68f6
--- /dev/null
+++ b/test/invalid/idl/const-nullable.widl
@@ -0,0 +1,4 @@
+// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06
+interface MyConstants {
+  const boolean? ARE_WE_THERE_YET = false;
+};
diff --git a/test/syntax/baseline/nullable.json b/test/syntax/baseline/nullable.json
index ae70eaa..9eefe9d 100644
--- a/test/syntax/baseline/nullable.json
+++ b/test/syntax/baseline/nullable.json
@@ -1,88 +1,4 @@
 [
-    {
-        "type": "interface",
-        "name": "MyConstants",
-        "escapedName": "MyConstants",
-        "inheritance": null,
-        "members": [
-            {
-                "type": "const",
-                "name": "ARE_WE_THERE_YET",
-                "idlType": {
-                    "type": "const-type",
-                    "generic": null,
-                    "nullable": {
-                        "trivia": ""
-                    },
-                    "union": false,
-                    "idlType": "boolean",
-                    "baseName": "boolean",
-                    "escapedBaseName": "boolean",
-                    "prefix": null,
-                    "postfix": null,
-                    "separator": null,
-                    "extAttrs": null,
-                    "trivia": {
-                        "base": " "
-                    }
-                },
-                "extAttrs": null,
-                "value": {
-                    "type": "boolean",
-                    "value": false
-                },
-                "trivia": {
-                    "base": "\n  ",
-                    "name": " ",
-                    "assign": " ",
-                    "value": " ",
-                    "termination": ""
-                }
-            },
-            {
-                "type": "const",
-                "name": "MY_NULL",
-                "idlType": {
-                    "type": "const-type",
-                    "generic": null,
-                    "nullable": {
-                        "trivia": ""
-                    },
-                    "union": false,
-                    "idlType": "MyPrimitive",
-                    "baseName": "MyPrimitive",
-                    "escapedBaseName": "MyPrimitive",
-                    "prefix": null,
-                    "postfix": null,
-                    "separator": null,
-                    "extAttrs": null,
-                    "trivia": {
-                        "base": " "
-                    }
-                },
-                "extAttrs": null,
-                "value": {
-                    "type": "null"
-                },
-                "trivia": {
-                    "base": "\n  ",
-                    "name": " ",
-                    "assign": " ",
-                    "value": " ",
-                    "termination": ""
-                }
-            }
-        ],
-        "extAttrs": null,
-        "partial": null,
-        "trivia": {
-            "base": "// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06\n",
-            "name": " ",
-            "open": " ",
-            "close": "\n",
-            "termination": ""
-        }
-    },
     {
         "type": "interface",
         "name": "Node",
@@ -126,7 +42,7 @@
         "extAttrs": null,
         "partial": null,
         "trivia": {
-            "base": "\n\n",
+            "base": "",
             "name": " ",
             "open": " ",
             "close": "\n  // ...\n",
diff --git a/test/syntax/idl/nullable.widl b/test/syntax/idl/nullable.widl
index eeceb3f..feb529d 100644
--- a/test/syntax/idl/nullable.widl
+++ b/test/syntax/idl/nullable.widl
@@ -1,9 +1,3 @@
-// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06
-interface MyConstants {
-  const boolean? ARE_WE_THERE_YET = false;
-  const MyPrimitive? MY_NULL = null;
-};
-
 interface Node {
   readonly attribute DOMString? namespaceURI;
   // ...