From cccbde6d5311cf0c85fc43303d6bccbd5cabbe39 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 15 Mar 2019 21:25:45 +0900 Subject: [PATCH 1/2] fix(lib/webidl2): disallow null on constants --- lib/webidl2.js | 5 +- test/invalid/baseline/const-null.txt | 3 + test/invalid/baseline/const-nullable.txt | 3 + test/invalid/idl/const-null.widl | 3 + test/invalid/idl/const-nullable.widl | 4 ++ test/syntax/baseline/nullable.json | 86 +----------------------- test/syntax/idl/nullable.widl | 6 -- 7 files changed, 16 insertions(+), 94 deletions(-) create mode 100644 test/invalid/baseline/const-null.txt create mode 100644 test/invalid/baseline/const-nullable.txt create mode 100644 test/invalid/idl/const-null.widl create mode 100644 test/invalid/idl/const-nullable.widl diff --git a/lib/webidl2.js b/lib/webidl2.js index 49c13425..875904a5 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) { @@ -721,7 +721,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"); @@ -772,7 +772,6 @@ }; } idlType = Object.assign({ type: "const-type" }, EMPTY_IDLTYPE, idlType); - type_suffix(idlType); 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"); diff --git a/test/invalid/baseline/const-null.txt b/test/invalid/baseline/const-null.txt new file mode 100644 index 00000000..3ea4d4eb --- /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 00000000..60f59c69 --- /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; + ^ No name for const diff --git a/test/invalid/idl/const-null.widl b/test/invalid/idl/const-null.widl new file mode 100644 index 00000000..1125c5d5 --- /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 00000000..47f68f6b --- /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 ae70eaac..9eefe9d9 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 eeceb3f7..feb529d6 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; // ... From e4757cf50d6a97e467a7d02a7dcb725ca6131a2b Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 15 Mar 2019 21:55:18 +0900 Subject: [PATCH 2/2] proper error message --- lib/webidl2.js | 3 +++ test/invalid/baseline/const-nullable.txt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/webidl2.js b/lib/webidl2.js index 875904a5..f3fccca5 100644 --- a/lib/webidl2.js +++ b/lib/webidl2.js @@ -772,6 +772,9 @@ }; } idlType = Object.assign({ type: "const-type" }, EMPTY_IDLTYPE, 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"); diff --git a/test/invalid/baseline/const-nullable.txt b/test/invalid/baseline/const-nullable.txt index 60f59c69..1453c529 100644 --- a/test/invalid/baseline/const-nullable.txt +++ b/test/invalid/baseline/const-nullable.txt @@ -1,3 +1,3 @@ Syntax error at line 3, since `interface MyConstants`: const boolean? ARE_WE_THERE_YET = false; - ^ No name for const + ^ Unexpected nullable constant type